Results 1 to 3 of 3

Thread: purchase count

  1. #1
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

    Post purchase count

    Hi All!

    Was implementing class that keeps track of the total number of items in a sale:
    CashRegisterTwo 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;
    	
    }
    and CashRegisterTwoTester class:
    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");
    		
    
    	}
    
    }
    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 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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: purchase count

    You'll need an instance field that is incremented by 1 each time you record a purchase.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    FOX427 is offline Member
    Join Date
    Jun 2011
    Posts
    52
    Rep Power
    0

Similar Threads

  1. Count down
    By tj_wolf2 in forum New To Java
    Replies: 8
    Last Post: 03-14-2011, 05:07 PM
  2. Getting row count
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-11-2008, 08:49 AM
  3. Cart of Purchase
    By Marcus in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-06-2007, 05:16 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •