# purchase count

• 04-29-2012, 08:57 AM
FOX427
purchase count
Hi All!

Was implementing class that keeps track of the total number of items in a sale:
CashRegisterTwo class:
Code:

```public class CashRegisterTwo {         /**         * Constructs a cash register with no money in it.         */         public CashRegisterTwo()         {                 purchase = 0;                 payment = 0;         }                 /**         * Records the purchase price of an item.         * @param amount the rice of the purchased item         */         public void recordPurchase(double amount)         {                 purchase = purchase + amount;                         }         /**         * Enters the payment received from the customer.         * @param dollars the number of dollars in the payment         * @param quarters the number of quarters in the payment         * @param dimes the number of dimes in the payment         * @param nickels the number of nickels in the payment         * @param pennies the number of pennies in the payment         */         public void enterDollars(int dollars)         {                 payment=payment + dollars;         }         public void enterQuarters(int quarters)         {                 payment=payment + quarters*QUARTER_VALUE;         }         public void enterDimes(int dimes)         {                 payment= payment + dimes*DIME_VALUE;         }         public void enterNickels(int nickels)         {                 payment=payment + nickels*NICKEL_VALUE;         }         public void enterPennies(int pennies)         {                 payment = payment + pennies*PENNY_VALUE;         }                         /**         * Computes the change due and resets the machine for the next  customer.         * @return the change due to the customer         */         public double giveChange()         {                 double change = payment - purchase;                 purchase = 0;                 payment = 0;                 return change;         }         public static final double QUARTER_VALUE = 0.25;         public static final double DIME_VALUE = 0.1;         public static final double NICKEL_VALUE = 0.05;         public static final double PENNY_VALUE = 0.01;                 private double purchase;         private double payment;         }```
and CashRegisterTwoTester class:
Code:

``` public class CashRegisterTwoTester {         /**         * @param args         */         public static void main(String[] args) {                 // TODO Auto-generated method stub                 CashRegisterTwo register= new CashRegisterTwo();                 register.recordPurchase(20.50);                 register.recordPurchase(5.34);                 register.recordPurchase(1.16);                 register.enterDollars(27);                 register.enterQuarters(2);                 System.out.println("Change: " + register.giveChange());                 System.out.println("Expected: 0.5");                         } }```
so right now I need to supply method "int getItemCount()" to the CashRegisterTwo class, the problem is I dont really know how to link it to the recordPurchase method as it is related(in my opinion) to it. Probably it will look similar to this:
Code:

```public int getItemCount() {     int itemCount=(int) (purchase/purchase);     itemCount=itemCount+1;     return itemCount; }```
as you may have guessed it is not correct, I am missing smth here, also I need to set itemCount to 0 at the end. can you guys advice me what I should use and where? Thanks.
• 04-29-2012, 10:03 AM
DarrylBurke
Re: purchase count
You'll need an instance field that is incremented by 1 each time you record a purchase.

db
• 04-29-2012, 06:55 PM
FOX427
Re: purchase count
Thanks, now it works:)