Given the following Java CashRegister class and driver: public class CashRegiste
ID: 3799297 • Letter: G
Question
Given the following Java CashRegister class and driver:
public class CashRegister
{
private int customerCount;
private int itemCount;
private double payment;
private double purchases;
private double salesTaxRate;
private double salesTaxTotal;
public CashRegister(double rate)
{
customerCount = 0;
itemCount = 0;
payment = 0;
purchases = 0;
salesTaxRate = rate;
salesTaxTotal = 0;
}
public void recordPurchase(double amount) /** Calculate sales of tax free items. */
{
purchases += amount;
itemCount++;
}
public void recordTaxablePurchase(double amount) /** Calculate sales of taxable items. */
{
recordPurchase(amount);
double salesTax = amount * salesTaxRate / 100.0;
salesTaxTotal += salesTax;
}
public void receivePayment(double amount)
{ payment += amount; }
public int getCustomerCount()
{ return customerCount; }
public int getItemCount()
{ return itemCount; }
public double giveChange()
{
customerCount++;
double change = payment - purchases - salesTaxTotal;
payment = 0;
purchases = 0;
salesTaxTotal = 0;
return change;
}
public double salesTotal()
{
/** Accumulate sales totals for customers. */
}
public double totalSalesTax()
{
/** Accumulate tax totals for customers. */
}
public String getReport()
{
System.out.println("******[Register Accounting Information]******");
String result = " Number of customers: " + getCustomerCount();
result = result + " " + "Number of items sold: " + getItemCount();
result = result + " " + "Sales total: $" + // accumulated sales total rounded to two decimals.
result = result + " " + "Sales tax total: $" + // accumulated sales tax rounded to two decimals.
return result;
}
public void reset()
{
customerCount = 0;
itemCount = 0;
}
}
// driver for CashRegister
public class CashRegisterTester
{
public static void main(String[] args)
{
CashRegister register = new CashRegister(6.75); // 6.75 percent sales tax
// customer 1
register.recordPurchase(2.59); // Not taxable
register.recordTaxablePurchase(34.45); // Taxable
register.receivePayment(50.00);
double change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (50 - 2.59 - 1.0675 * 34.45));
System.out.println();
// customer 2
register.recordPurchase(2.97); // Not taxable
register.recordTaxablePurchase(12.95); // Taxable
register.receivePayment(20.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (20 - 2.97 - 1.0675 * 12.95));
System.out.println();
// print report of total number of customers and items sold
System.out.println();
System.out.println(register.getReport());
System.out.println();
// customer 1
register.recordPurchase(5.47); // Not taxable
register.recordTaxablePurchase(9.19); // Taxable
register.receivePayment(20.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (20 - 5.47 - 1.0675 * 9.19));
System.out.println();
// customer 2
//register.recordPurchase(2.97); // Not taxable
register.recordTaxablePurchase(58.93); // Taxable
register.receivePayment(65.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (65.00 - 0 - 1.0675 * 58.93));
System.out.println();
// customer 3
register.recordPurchase(4.48); // Not taxable
register.recordTaxablePurchase(12.90); // Taxable
register.receivePayment(20.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (20.00 - 4.48 - 1.0675 * 12.90));
System.out.println();
// print report of total number of customers and items sold
System.out.println();
System.out.println(register.getReport());
}
}
Modify the CashRegister class and driver to account for the following missing methods (Sales total and Sales tax total). Display the following outcome:
******[Register Accounting Information]******
Number of customers: 2
Number of items sold: 5
Sales total: $52.96 // total rounded to two decimal places.
Sales tax total: $3.19 // sales tax total rounded to two decimal places.
*****************************************************************************************************
******[Register Accounting Information]******
Number of customers: 3
Number of items sold: 12
Sales total: $90.97 // total rounded to two decimal places.
Sales tax total: $ 5.47 // total rounded to two decimal places.
************************************************************************************************
Explanation / Answer
I have modified the code and removed resetting from change function. Instead of keeping at there, you will have a new reset method which will reset the items.
package test;
public class CashRegister
{
private int customerCount;
private int itemCount;
private double payment;
private double purchases;
private double salesTaxRate;
private double salesTaxTotal;
public CashRegister(double rate)
{
customerCount = 0;
itemCount = 0;
payment = 0;
purchases = 0;
salesTaxRate = rate;
salesTaxTotal = 0;
}
public void recordPurchase(double amount) /** Calculate sales of tax free items. */
{
purchases += amount;
itemCount++;
}
public void recordTaxablePurchase(double amount) /** Calculate sales of taxable items. */
{
recordPurchase(amount);
double salesTax = amount * salesTaxRate / 100.0;
salesTaxTotal += salesTax;
}
public void receivePayment(double amount)
{
payment += amount;
}
public int getCustomerCount()
{
return customerCount;
}
public int getItemCount()
{
return itemCount;
}
public double giveChange()
{
customerCount++;
double change = payment - purchases - salesTaxTotal;
return change;
}
public double salesTotal()
{
return purchases + salesTaxTotal;
}
public double totalSalesTax()
{
return salesTaxTotal;
}
public String getReport()
{
System.out.println("******[Register Accounting Information]******");
String result = " Number of customers: " + getCustomerCount();
result = result + " " + "Number of items sold: " + getItemCount();
result = result + " " + "Sales total: $" + salesTotal(); // accumulated sales total rounded to two decimals.
result = result + " " + "Sales tax total: $" + totalSalesTax(); // accumulated sales tax rounded to two decimals.
return result;
}
public void reset()
{
customerCount = 0;
itemCount = 0;
payment = 0;
purchases = 0;
salesTaxTotal = 0;
}
}
package test;
public class CashRegisterTester
{
public static void main(String[] args)
{
CashRegister register = new CashRegister(6.75); // 6.75 percent sales tax
// customer 1
register.recordPurchase(2.59); // Not taxable
register.recordTaxablePurchase(34.45); // Taxable
register.receivePayment(50.00);
double change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (50 - 2.59 - 1.0675 * 34.45));
System.out.println();
// customer 2
register.recordPurchase(2.97); // Not taxable
register.recordTaxablePurchase(12.95); // Taxable
register.receivePayment(20.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (20 - 2.97 - 1.0675 * 12.95));
System.out.println();
// print report of total number of customers and items sold
System.out.println();
System.out.println(register.getReport());
System.out.println();
register.reset();
// customer 1
register.recordPurchase(5.47); // Not taxable
register.recordTaxablePurchase(9.19); // Taxable
register.receivePayment(20.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (20 - 5.47 - 1.0675 * 9.19));
System.out.println();
// customer 2
//register.recordPurchase(2.97); // Not taxable
register.recordTaxablePurchase(58.93); // Taxable
register.receivePayment(65.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (65.00 - 0 - 1.0675 * 58.93));
System.out.println();
// customer 3
register.recordPurchase(4.48); // Not taxable
register.recordTaxablePurchase(12.90); // Taxable
register.receivePayment(20.00);
change = register.giveChange();
System.out.println(change);
System.out.println("Expected: $" + (20.00 - 4.48 - 1.0675 * 12.90));
System.out.println();
// print report of total number of customers and items sold
System.out.println();
System.out.println(register.getReport());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.