smeacham

CashRegister.java

Feb 11th, 2015
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. /**
  2.  * A cash register totals up sales and computes change due.
  3.  */
  4. public class CashRegister {
  5.     public static final double QUARTER_VALUE = 0.25;
  6.     public static final double DIME_VALUE = 0.10;
  7.     public static final double NICKLE_VALUE = 0.05;
  8.     public static final double PENNY_VALUE = 0.01;
  9.  
  10.     private double purchase;
  11.     private double payment;
  12.    
  13.     /**
  14.      * Constructs a cash register with no money in it.
  15.      */
  16.     public CashRegister() {
  17.         purchase = 0;
  18.         payment = 0;
  19.     }
  20.    
  21.     /**
  22.      * Records the purchase price of an item.
  23.      * @param amount the price of the purchased item
  24.      */
  25.     public void recordPurchase(double amount) {
  26.         purchase = purchase + amount;
  27.     }
  28.    
  29.     /**
  30.      * Processes the payment received from the customer.
  31.      * @param dollars the number of dollars in the payment
  32.      * @param quarters the number of quarters in the payment
  33.      * @param dimes the number of dimes in the payment
  34.      * @param nickles the number of nickles in the payment
  35.      * @param pennies the number of pennies in the payment
  36.      */
  37.     public void receivePayment(int dollars, int quarters, int dimes, int nickles, int pennies) {
  38.         payment =
  39.                 dollars +
  40.                 quarters * QUARTER_VALUE +
  41.                 dimes * DIME_VALUE +
  42.                 nickles * NICKLE_VALUE +
  43.                 pennies * PENNY_VALUE;
  44.     }
  45.    
  46.     /**
  47.      * Computes the change due and resets the machine for the next customer.
  48.      * @return the change due to the customer
  49.      */
  50.     public double giveChange() {
  51.         double change = payment - purchase;
  52.         purchase = 0;
  53.         payment = 0;
  54.         return change;
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment