public class OnlineShopper
1: /** OnlineShopper.java
2: A class that maintains a shopping cart for an online store.
3: @author Frank M. Carrano
4: @version 4.0
5: */
6: public class OnlineShopper
7: {
8: public static void main(String[] args)
9: {
10: Item[] items =
11: {
12: new Item("Bird feeder", 2050),
13: new Item("Squirrel guard", 1547),
14: new Item("Bird bath", 4499),
15: new Item("Sunflower seeds", 1295)
16: };
17: //Exactly one of the following three lines must be activated:
18: //BagInterface<Item> shoppingCart = new FixedSizeArrayBag<>();
19: //BagInterface<Item> shoppingCart = new ResizableArrayBag<>();
20: //
21: BagInterface<Item> shoppingCart = new LinkedBag<>();
23: int totalCost = 0;
24: //Statements that add selected items to the shopping cart:
25: for (int index = 0; index < items.length; index++)
26: {
27: Item nextItem = items[index]; //Simulate getting item from shopper
28: shoppingCart.add(nextItem);
29: totalCost = totalCost + nextItem.getPrice();
30: }
31: //Simulate checkout
32: while (!shoppingCart.isEmpty())
33: System.out.println(shoppingCart.remove());
34: System.out.println
35: (
36: "Total cost: " + "\t$"
37: + totalCost / 100 + "." + totalCost % 100
38: );
39: }
40: }
41: /* Output:
42: Sunflower seeds $12.95
43: Bird bath $44.99
44: Squirrel guard $15.47
45: Bird feeder $20.50
46: Total cost: $93.91
47: */