-
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:
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:
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:
Code:
163.00000000000009
Dollars 0
Expected: 1
Quarters -4
Expected: 2
Dimes -100000
Expected: 1
Nickels -200000
Expected: 0
Pennies -100
Expected: 3
-
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.
-
Re: Cash Register Class
Thanks Norm, I did test using System.out.println of methods and found discrepancies, now I have correct code:
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