Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: Cash Register Class

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

    Post Cash Register Class

    Hi All!
    Having difficulties with exercise, it asks to implement class that directs cashier how to give a change. The cash register computes the amount to be returned to the customer, in pennies. I coded smth, but the result is really bad :( Can someone give advice what I should change? Thanks in advance!


    CashRegisterThree Class:
    Java Code:
    /**
     * A cash register totals up sales and computes change due.
     * @author FOX
     *
     */
    public class CashRegisterThree {
    	/**
    	 * Constructs a cash register with no money in it.
    	 */
    	public CashRegisterThree()
    	{
    		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 enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
    	{
    		payment = dollars + quarters*QUARTER_VALUE + dimes*DIME_VALUE +
    				nickels*NICKEL_VALUE + 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)*100;
    		purchase = 0;
    		payment = 0;
    		return change;
    	}
    	public int giveDollars()
    	{
    		changeInDollars=(int) (change/100);
    		change=change-100;
    		return changeInDollars;
    	}
    	public int giveQuarters()
    	{
    		changeInQuarters=(int) (change/(QUARTER_VALUE*100));
    		change = (int) (change - (quarters*QUARTER_VALUE*100));
    		return changeInQuarters;
    	}
    	public int giveDimes()
    	{
    		changeInDimes = (int) (change/DIME_VALUE*100);
    		change=(int) (change-(dimes*DIME_VALUE*100));
    		return changeInDimes;
    	}
    	public int giveNickels()
    	{
    		changeInNickels = (int) (change/NICKEL_VALUE*100);
    		change = (int) (change - (nickels*NICKEL_VALUE*100));
    		return (int) changeInNickels;
    	}
    	public int givePennies()
    	{
    		changeInPennies=(int) change;
    		return (int) changeInPennies;
    	}
    	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;
    	
    	private int dollars;
    	private int quarters;
    	private int dimes;
    	private int nickels;
    	private int pennies;
    	private int change;
    	
    	public int changeInPennies;
    	public int changeInQuarters;
    	public int changeInDollars;
    	public int changeInDimes;
    	public int changeInNickels;
    	
    }
    CashRegisterThreeTester Class:
    Java Code:
    /**
     * This class tests the CashRegisterOne class
     * @author FOX
     *
     */
    public class CashRegisterThreeTester {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		CashRegisterThree register = new CashRegisterThree();
    		
    		register.recordPurchase(8.37);
    		register.enterPayment(10, 0, 0, 0, 0);
    		System.out.println(register.giveChange());
    		System.out.println("Dollars " + register.giveDollars());
    		System.out.println("Expected: 1");
    		System.out.println("Quarters " + register.giveQuarters());
    		System.out.println("Expected: 2");
    		System.out.println("Dimes " + register.giveDimes());
    		System.out.println("Expected: 1");
    		System.out.println("Nickels " + register.giveNickels());
    		System.out.println("Expected: 0");
    		System.out.println("Pennies " + register.givePennies());
    		System.out.println("Expected: 3");
    		
    	
    
    	}
    
    }
    result is:
    Java Code:
    163.00000000000009
    Dollars 0
    Expected: 1
    Quarters -4
    Expected: 2
    Dimes -100000
    Expected: 1
    Nickels -200000
    Expected: 0
    Pennies -100
    Expected: 3

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Cash Register Class

    Try debugging the code by adding printlns to the methods that give the wrong results. Print out the values of all the variables and expressions used to compute the results. The print out will show you where the program is going wrong.

    One problem could be that the code is using double vs int values. Work in pennies vs dollars.
    FOX427 likes this.
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Cash Register Class

    Thanks Norm, I did test using System.out.println of methods and found discrepancies, now I have correct code:
    Java Code:
    /**
     * A cash register totals up sales and computes change due.
     * @author FOX
     *
     */
    public class CashRegisterThree {
    	/**
    	 * Constructs a cash register with no money in it.
    	 */
    	public CashRegisterThree()
    	{
    		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;
    		System.out.println("Purchase is " + purchase);
    	}
    	/**
    	 * 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 enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies)
    	{
    		payment = dollars + quarters*QUARTER_VALUE + dimes*DIME_VALUE +
    				nickels*NICKEL_VALUE + pennies*PENNY_VALUE;
    		System.out.println("Payment is "+ payment);
    		
    	}
    	/**
    	 * Computes the change due and resets the machine for the next  customer.
    	 * @return the change due to the customer
    	 */
    	public double giveChange()
    	{
    		System.out.println("Giving a change");
    		double change = payment - purchase;
    		
    		return change;
    	}
    	public int giveDollars()
    	{
    		change=payment-purchase;
    		System.out.println(change);
    		int changeInDollars=(int) (change);
    		change=change-changeInDollars;
    		return changeInDollars;
    	}
    	public int giveQuarters()
    	{
    		int changeInQuarters=(int) (change/QUARTER_VALUE);
    		change=change-(changeInQuarters*QUARTER_VALUE);
    		return changeInQuarters;
    	}
    	public int giveDimes()
    	{
    		int changeInDimes = (int) (change/DIME_VALUE);
    		change=change-(changeInDimes*DIME_VALUE);
    		return changeInDimes;
    	}
    	public int giveNickels()
    	{
    		int changeInNickels = (int) (change/NICKEL_VALUE);
    		change=change-(changeInNickels*NICKEL_VALUE);
    		return changeInNickels;
    	}
    	public int givePennies()
    	{
    		int changeInPennies=(int) (change/PENNY_VALUE);
    		purchase=0;
    		payment=0;
    		return changeInPennies;
    	}
    	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;
    	private double change;
    
    	
    	
    	
    	
    }
    Result:
    Purchase is 8.37
    Payment is 10.0
    Giving a change
    1.6300000000000008
    1.6300000000000008
    Dollars 1
    Expected: 1
    Quarters 2
    Expected: 2
    Dimes 1
    Expected: 1
    Nickels 0
    Expected: 0
    Pennies 3
    Expected: 3

Similar Threads

  1. POS Machine Using JAVA - How To Open Cash Drawer
    By relixus in forum New To Java
    Replies: 0
    Last Post: 11-23-2011, 04:11 PM
  2. please help! (array-register)
    By stegano in forum New To Java
    Replies: 13
    Last Post: 03-16-2011, 12:18 AM
  3. Cash Prize Challenge
    By Laura 123 in forum New To Java
    Replies: 19
    Last Post: 04-01-2009, 04:56 AM
  4. Register a callback function in other class
    By barts2108 in forum New To Java
    Replies: 2
    Last Post: 11-10-2008, 05:24 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
  •