001: /**
002: A cash register totals up sales and computes change due.
003: */
004: public class CashRegister
005: {
006:    /**
007:    Constructs a cash register with no money in it.
008:    */
009:    public CashRegister()
010:    {
011:       purchase = 0;
012:       change = 0;
013:    }
014:    /**
015:       Records the purchase price of an item.
016:       @param amount the price of the purchased item
017:    */
018:    public void recordPurchase(double amount)
019:    {
020:       purchase = purchase + (int) Math.round(purchase * PENNIES_PER_DOLLAR);
021:    }
022:    
023:    /**
024:       Enters the payment received from the customer.
025:       @param dollars the number of dollars in the payment
026:       @param quarters the number of quarters in the payment
027:       @param dimes the number of dimes in the payment
028:       @param nickels the number of nickels in the payment
029:       @param pennies the number of pennies in the payment
030:    */
031:    public void enterPayment(int dollars, int quarters, 
032:       int dimes, int nickels, int pennies)
033:    {
034:       int payment = dollars * PENNIES_PER_DOLLAR 
035:          + quarters * PENNIES_PER_QUARTER + dimes * PENNIES_PER_DIME
036:          + nickels * PENNIES_PER_NICKEL + pennies;
037:       change = payment - purchase;            
038:       purchase = 0;
039:    }
040:    
041:    /**
042:       Computes the number of dollars due to the customer.
043:       @return the number of dollars in the change due
044:    */
045:    public int giveDollars()
046:    {
047:       int dollars = change / PENNIES_PER_DOLLAR; 
048:       change = change - dollars * PENNIES_PER_DOLLAR;
049:       return dollars;
050:    }
051: 
052:    /**
053:       Computes the number of quarters due to the customer.
054:       @return the number of quarters in the change due
055:    */
056:    public int giveQuarters()
057:    {
058:       int quarters = change / PENNIES_PER_QUARTER; 
059:       change = change - quarters * PENNIES_PER_QUARTER;
060:       return quarters;
061:    }
062: 
063:    /**
064:       Computes the number of dimes due to the customer.
065:       @return the number of dimes in the change due
066:    */
067:    public int giveDimes()
068:    {
069:       int dimes = change / PENNIES_PER_DIME; 
070:       change = change - dimes * PENNIES_PER_DIME;
071:       return dimes;
072:    }
073: 
074:    /**
075:       Computes the number of nickels due to the customer.
076:       @return the number of nickels in the change due
077:    */
078:    public int giveNickels()
079:    {
080:       int nickels = change / PENNIES_PER_NICKEL; 
081:       change = change - nickels * PENNIES_PER_NICKEL;
082:       return nickels;
083:    }
084: 
085:    /**
086:       Computes the number of pennies due to the customer.
087:       @return the number of pennies in the change due
088:    */
089:    public int givePennies()
090:    {
091:       int pennies = change; 
092:       change = 0;
093:       return pennies;
094:    }
095: 
096:    private static final int PENNIES_PER_DOLLAR = 100;
097:    private static final int PENNIES_PER_QUARTER = 25;
098:    private static final int PENNIES_PER_DIME = 10;
099:    private static final int PENNIES_PER_NICKEL = 5;
100: 
101:    private int purchase;
102:    private int change;
103: }