We are going to add transactions to HW5 using a class Composite. We illustrate with two separate
Question:
We are going to add transactions to HW5 using a class Composite. We illustrate with two separate examples (A,B) below. Given: final Video v1 = Data.newVideo("K", 2003, "S"); final Video v2 = Data.newVideo("S", 2002, "K"); (A) c is a transaction with TWO add commands below. ie. the two adds are treated as a unit. final Inventory inventory = Data.newInventory(); UndoableCommand c1 = Data.newAddCmd(inventory, v1, 2); UndoableCommand c2 = Data.newAddCmd(inventory, v2, 3); Composite c = new Composite(); c.add(c1); c.add(c2); Assert.assertEquals( 0, inventory.size() ); c.run(); Assert.assertEquals( 2, inventory.size() ); c.undo(); Assert.assertEquals( 0, inventory.size() ); c.redo(); Assert.assertEquals( 2, inventory.size() ); (B) Transactions can also combine different kinds of commands. eg. final Inventory inventory = Data.newInventory(); UndoableCommand c1 = Data.newAddCmd(inventory, v1, 2); UndoableCommand c2 = Data.newAddCmd(inventory, v2, 3); UndoableCommand c3 = Data.newOutCmd(inventory, v1). Composite d = new Composite(); d.add(c3); d.add(c2); Assert.assertEquals( 0, inventory.size() ); c1.run(); Assert.assertEquals( 1, inventory.size() ); d.run(); Assert.assertEquals( 2, inventory.size() ); Assert.assertEquals( "K (2003) : S [2,1,1]", inventory.get(v1).toString() ); d.undo(); Assert.assertEquals( 1, inventory.size() ); Assert.assertEquals( "K (2003) : S [2,0,0]", inventory.get(v1).toString() ); Write code for a class class Composite implements UndoableCommand{ // implement add, run, undo,redo methods