Source of OnlineShopper.java


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