Enhance the CashRegister class so that it keeps track of the total number of ite
ID: 3638670 • Letter: E
Question
Enhance the CashRegister class so that it keeps track of the total number of items in a sale. Count all recorded purchases and supply a method
int getItemCount()
that returns the number of items of the current purchase. Remember to reset the count at the end of the purchase.
Your tester class should be called CashRegisterTester.
Use the following class in your solution:
/**
A cash register totals up sales and computes change due. Also keeps track
of the number of items in sale.
*/
public class CashRegister
{
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
payment = 0;
}
/**
Records the purchase price of an item.
@param amount the price of the purchased item
*/
public void recordPurchase(double amount)
{
double newTotal = purchase + amount;
purchase = newTotal;
}
/**
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;
purchase = 0;
payment = 0;
return change;
}
public int getItemCount()
{
. . .
}
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;
}
Explanation / Answer
import java.io.*; class CashRegister { private double purchase; private double payment; 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; public CashRegister() { purchase = 0; payment = 0; } public void recordPurchase(double amount) { double newTotal = purchase + amount; purchase = newTotal; } public void enterPayment(int dollars, int quarters, int dimes, int nickels, int pennies) { payment = payment + dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE + nickels * NICKEL_VALUE + pennies * PENNY_VALUE; } public int getItemCount(int count) { count = count + 1; return count; } public double giveChange() { double change = payment - purchase; purchase = 0; payment = 0; return change; } } public class CashRegisterTester { public static void main(String args[]) { int dollars = 0, quarters = 0, dimes = 0, nickels = 0, pennies = 0; double amount; boolean choice = true; int count = 0; // number of purchased items. CashRegister cr = new CashRegister(); try { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); while(choice) { System.out.print("The price of the item which is you want to buy(salesman use only): $ "); String price = br.readLine(); amount = Double.parseDouble(price); System.out.println("Enter payment for the item"); System.out.print("Enter number of dollars: "); String doll = br.readLine(); dollars = Integer.parseInt(doll); System.out.print("Enter number of quarters: "); String quar = br.readLine(); quarters = Integer.parseInt(quar); System.out.print("Enter number of dimes: "); String dim = br.readLine(); dimes = Integer.parseInt(dim); System.out.print("Enter number of nickels: "); String nick = br.readLine(); nickels = Integer.parseInt(nick); System.out.print("Enter number of pennies: "); String penn = br.readLine(); pennies = Integer.parseInt(penn); cr.recordPurchase(amount); // records the total price of purchased items. cr.enterPayment(dollars, quarters, dimes, nickels, pennies); // records the total payment by you int counter = cr.getItemCount(count); // count the number of purchased items. System.out.print("Do you want to buy another item?(y/n): "); String s = br.readLine(); if(!s.equalsIgnoreCase("y")) { double change = cr.giveChange(); // calculate the change. System.out.println("Change is: "+change); System.out.println("Number of purchaged items: "+counter); System.out.println("Thank you"); choice = false; } else { count = count + 1; } } } catch(Exception e) { System.out.println("Exception occurs"); } } } OUTPUT: Enter number of quarters: 700 Enter number of dimes: 600 Enter number of nickels: 500 Enter number of pennies: 400 Do you want to buy another item?(y/n): n Change is: 164.0 Number of purchaged items: 1 Thank you
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.