

/** CashRegister: a class that "knows" what a cash register "knows"
 *  and can carry out the actions performed by a cash register:
 *  - record new items on a sales slip or "tape" for the customer;
 *  - calculate tax and print a total at the end.
 */

public class CashRegister {
   // fields used by the class.
   
   // Notice that these fields are all private: the main program
   // used them only by calling methods of this class.
   // Also notice that three of the fields are "final" -- they
   // cannot be changed by the methods using them, and are simply
   // names for constants that would otherwise be "magic numbers".
   
   protected long total;
   private final int maxPrintedNameLength = 12;
   protected final int priceLength = 8;
   private final double taxRate = 15;

   /**
    * CashRegister: a constructor that simply initializes the total.
    */
   public CashRegister ( ) {
      total = 0;
   }

   /**
    * ringUpItem: add an item to the sales slip
    * @param itemName java.lang.String - the name of the item
    * @param itemCost long             - the price of the item
    */
   public void ringUpItem (String itemName, long itemCost) {
      
      // accumulate item's price into total
      total += itemCost; // remember: "x += a" is the same as "x = x + a"
      
      // print a line on the sales slip for this item
      // first, the name, followed by a suitable number of blanks
      if (itemName.length() <= maxPrintedNameLength) {
         System.out.print (itemName);
         for (int i = 0; i < maxPrintedNameLength - itemName.length(); i++) {
            System.out.print (" ");
         }
      }   
      else {
         System.out.print (itemName.substring (0, maxPrintedNameLength));
      }      
      
      // second, the price
      ShowMoney.print (itemCost, priceLength);
      
      // end the line
      System.out.println ("");
   }

   /**
    * summarize: compute and print the tax and the total.
    */
   public void summarize ( ) {
      final String taxLabel = "tax";
      final String totalLabel = "TOTAL";
      
      // Notice the cast, "(long)". We need it because taxRate is a
      // double, so the result of the calculation is double.
      // (And why is taxRate double?)
      long tax = (long) (total * taxRate/100);
      
      // Leave a blank line.
      System.out.println("");
      
      // Print the tax -- which we can treat as an "item".
      ringUpItem (taxLabel, tax);
      
      // Another blank line, and then the total.
      System.out.println ("");
      // (Why can't we use ringUpItem for the total?)
      if (totalLabel.length() < maxPrintedNameLength) {
         System.out.print (totalLabel);
         for (int i = 0; i < maxPrintedNameLength - totalLabel.length(); i++)
            System.out.print(" ");
      }
      else {
         System.out.print (totalLabel.substring (0, maxPrintedNameLength));
      }
      ShowMoney.print (total, priceLength);
      
      //All done now.
      System.out.println ("\nThank you for shopping at Phil's.");
      System.out.println ("-----------------------------------");
			  
   }
}

