Results 1 to 3 of 3
Thread: purchase count
- 04-29-2012, 07:57 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
purchase count
Hi All!
Was implementing class that keeps track of the total number of items in a sale:
CashRegisterTwo class:
and CashRegisterTwoTester class:Java 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; }
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:Java 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"); } }
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.Java Code:public int getItemCount() { int itemCount=(int) (purchase/purchase); itemCount=itemCount+1; return itemCount; }
- 04-29-2012, 09:03 AM #2
Re: purchase count
You'll need an instance field that is incremented by 1 each time you record a purchase.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-29-2012, 05:55 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Count down
By tj_wolf2 in forum New To JavaReplies: 8Last Post: 03-14-2011, 05:07 PM -
Getting row count
By Java Tip in forum Java TipReplies: 0Last Post: 02-11-2008, 08:49 AM -
Cart of Purchase
By Marcus in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 07-06-2007, 05:16 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks