Write a CashRegister class that can be used with the RetailItem class that you w
ID: 3557184 • Letter: W
Question
Write a CashRegister class that can be used with the RetailItem class that you wrote in Part 1. 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 sales subtotal, amount of sales tax and total.
And here is my RetailItem Class I wrote....
import java.io.*;
import java.util.Scanner; //Needed for Scanner Class
public class RetailItem {
private String description; // Name of item
private int units; // Amount of units
private double price; // Price of each unit
public RetailItem()
{
}
// Counstructor with arguements
public RetailItem(String des, int u, double p)
{
description = des;
units = u;
price = p;
}
/**
*This setDescription Method sets the item description
*@param des The String stored in the description field.
*/
public void setDescription (String des)
{
description=des;
}
/**
*This setPrice Method sets the price of the item.
*@param p The value stored in the price field.
*/
public void setPrice (double p)
{
price=p;
}
/**
*This setUnits Method is the number of units of a
*specific item.
*@param u The value stored in the units field.
*/
void setUnits(int u)
{
units = u;
}
/**
*The getUnits method returns the number of units.
*@return The Value in the Units field.
*/
public int getUnits()
{
return units;
}
/**
*The getDescription method returns the description of
*the item.
*@return The despription in the description field.
*/
public String getDescription()
{
return description;
}
/**
*The getPrice method returns the items price.
*@return The value in the price field.
*/
public double getPrice()
{
return price;
}
}
/**
*This program demostrates the RetailItem Class
*/
class RetailTest
{
public static void main (String [] args)
{
String str ="Shirt";
//Creates new RetailItem objects.
RetailItem r1=new RetailItem ("Jacket", 12, 59.95);
RetailItem r2=new RetailItem ("Designer Jeans", 40, 34.95);
RetailItem r3 =new RetailItem ();
//function call to set values
r3.setDescription(str);
r3.setUnits(20);
r3.setPrice(24.95);
//Output of data
System.out.println(" Description Units on Hand Price");
System.out.println("Item#1 "+r1.getDescription()+ " "+r1.getUnits()
+ " " + r1.getPrice());
System.out.println("Item#2 "+r2.getDescription()+ " "+r2.getUnits()
+ " " + r2.getPrice());
System.out.println("Item#3 "+r3.getDescription()+ " "+r3.getUnits() +
" " + r3.getPrice());
//Exit program
System.exit(0);
}
}
Explanation / Answer
import java.util.Scanner;
import java.text.DecimalFormat;
public class CashRegister
{
private RetailItem item;
private int quantity;
private double price;
private double subTotal;
private double salesTax;
private double total;
/**
Constructor
@param RetailItem The item to purchase.
@param quantity The number of items.
@param price The price of item.
*/
public CashRegister(RetailItem i, int q)
{
//Create a copy of the object referenced by
//Retail Item.
this.item = new RetailItem(i);
quantity = q;
}
/**
getItem method
@return A copy of the RetailItem object
for the item being purchased.
*/
public RetailItem getRetailItem()
{
//Retun a copy of the object referenced by item
RetailItem myRetailItem = new RetailItem("Designer Jeans", 40, 34.95);
CashRegister myCashRegister = new CashRegister(myRetailItem, 1);
return new RetailItem();
}
/**
The getPrice method rerurns the item's price.
@return The item's price.
*/
public double getPrice()
{
return price;
}
/**
getUnit method
@return The number of items being puchased.
*/
public int getQuantity()
{
return quantity;
}
/**
getsubtotal method
@return The subtotal of the item puchased
*/
public double getSubTotal()
{
subTotal = item.getPrice() * quantity;
return subTotal;
}
/**
getSalesTax method
@return The sales tax of the items puchased
*/
public double getTax()
{
salesTax = this.getSubTotal() * .06;
return salesTax;
}
/**
getTotal method
@return The total of the items puchased
*/
public double getTotal()
{
total = this.getSubTotal() + this.getTax();
return total;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.