Source of OnlineShopper.java


  1: /** A class that maintains a shopping cart for an online store.
  2:     @author Frank M. Carrano, Timothy M. Henry
  3:     @version 5.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 Bag<>();
 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

 25:       // Simulate checkout
 26:       while (!shoppingCart.isEmpty())
 27:          System.out.println(shoppingCart.remove());
 28:       
 29:                 System.out.println("Total cost: " + "\t$" + totalCost / 100 + "." +
 30:                          totalCost % 100);
 31:         } // end main
 32: } // end OnlineShopper

 34: /*
 35: Sunflower seeds $12.95
 36: Bird bath            $44.99
 37: Squirrel guard         $15.47
 38: Bird feeder            $20.50
 39: Total cost:          $93.91
 40: */