Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a CashRegister class that can be used with the RetailItem class that you w

ID: 3598448 • Letter: W

Question

Write a CashRegister class that can be used with the RetailItem class that you wrote in Chapter 6’s Programming Challenge 4. The CashRegister class should simulate the sale of a retail item. It should have a constructor that accepts a RetailItem object as an argument. The constructor should also accept an integer that represents the quantity of items being purchased. In addition, the class should have the following methods: - The getSubtotal method should return the subtotal of the sale, which is the quantity multiplied by the price. This method must get the price from the RetailItem object that was passes as an arugument to the constructor. - The getTax method should return the amount of sales tax on the purchase. The sales tax rate is 6 percent of a retail sale. - The getTotal method should return the total of the sale, which is the subtotal plus the sales tax.
Demonstrate the class in a program that asks the user for the quantity of items being purchased, and then displays the sale’s subtotal, amount of sales tax, and total.

Explanation / Answer

CashRegister.java

public class CashRegister {

private double TAX_RATE = 0.06;

private static double retailPrice;

private static int quantity;

public CashRegister(RetailItem item) {

retailPrice+=item.getPrice();

quantity+=item.getUnitsOnHand();

}

public double getSubtotal() {

return retailPrice;

}

public double getTax() {

return retailPrice * TAX_RATE;

}

public double getTotal() {

return getSubtotal()+getTax();

}

}

RetailItem.java

public class RetailItem {

private String description;

private int unitsOnPurchase;

private double price;

public RetailItem(String description, double price, int unitsOnPurchase ){

this.description = description;

this.unitsOnPurchase = unitsOnPurchase;

this.price = price;

}

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public int getUnitsOnHand() {

return unitsOnPurchase;

}

public void setUnitsOnHand(int unitsOnHand) {

this.unitsOnPurchase = unitsOnHand;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

}

CashRegisterTest.java

import java.text.DecimalFormat;

public class CashRegisterTest {

public static void main(String[] args) {

RetailItem items[] = new RetailItem[10];

items[0] = new RetailItem("gallon of Milk", 3.29, 1);

items[1] = new RetailItem("dozens of eggs", 3.58, 2);

items[2] = new RetailItem("box of cereal", 10.47, 3);

items[3] = new RetailItem("Del Turkey", 13.98, 4);

items[4] = new RetailItem("Rolls", 7.92, 5);

items[5] = new RetailItem("half gallon Ice Cream", 8.98, 2);

items[6] = new RetailItem("box of cookies", 10.47, 5);

items[7] = new RetailItem("zip storage bags", 14.45, 6);

items[8] = new RetailItem("apples", 2.97, 1);

items[9] = new RetailItem("toilet paper rolls", 8.90, 2);

System.out.println(" Sales Receipt");

DecimalFormat df = new DecimalFormat("0.00");

System.out.println("------------------------------------------");

CashRegister c = null;

for(int i=0;i<items.length;i++) {

c = new CashRegister(items[i]);

System.out.println((i+1)+" "+items[i].getDescription()+" "+items[i].getPrice());

}

System.out.println("==========================================");

System.out.println(" Tax = "+df.format(c.getTax()));

System.out.println(" Total Due = "+df.format(c.getTotal()));

}

}

Output:

Sales Receipt

------------------------------------------

1 gallon of Milk 3.29

2 dozens of eggs 3.58

3 box of cereal 10.47

4 Del Turkey 13.98

5 Rolls 7.92

6 half gallon Ice Cream 8.98

7 box of cookies 10.47

8 zip storage bags 14.45

9 apples 2.97

10 toilet paper rolls 8.9

==========================================

Tax = 5.10

Total Due = 90.11

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote