Write a CashRegister class that can be used with the RetailItem class that you w
ID: 3625567 • 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 passed as an argument 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
import java.io.*;
import java.util.Scanner; // Needed for Scanner class
class RetailItem
{
private String description;
private int units;
private double price;
public RetailItem()
{
}
public RetailItem( RetailItem obj)
{
description=obj.description;
units=obj.units;
price=obj.price;
}
//Constructor with arguments
public RetailItem(String des,int u,double p)
{
description=des;
units=u;
price=p;
}
//Mutator function for description
public void setDescription(String des)
{
description=des;
}
//Mutator function for price
public void setPrice(double p)
{
price=p;
}
//Mutator function for units on hand
void setUnits(int u)
{
units=u;
}
//Accessor function for units on hand
public int getUnits()
{
return units;
}
//Accessor function for description
public String getDescription()
{
return description;
}
//Accessor function for price
public double getPrice()
{
return price;
}
}
class CashRegister
{
private RetailItem Ritem;
private int quantity;
public CashRegister(RetailItem RItem,int items)
{
Ritem=new RetailItem(RItem);
quantity=items;
}
public double getSubtotal()
{
return quantity* Ritem.getPrice();
}
public double getTax()
{
return (quantity* Ritem.getPrice())*0.06;
}
public double getTotal()
{
return (getSubtotal()+getTax());
}
}
class CashRegisterTest
{
public static void main(String [] args)
{
RetailItem item1=new RetailItem("Designer Jeans",40,34.95);
CashRegister reg1=new CashRegister(item1,5);
System.out.println("SubTotal:"+reg1.getSubtotal());
System.out.println("Sales Tax:"+reg1.getTax());
System.out.println("Total: $"+reg1.getTotal());
//exit program
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.