*For this lab we are NOT using copy constructors** RetailItem Class Instance var
ID: 3715369 • Letter: #
Question
*For this lab we are NOT using copy constructors**
RetailItem Class
Instance variables:
String description; // Item description
int unitsOnHand; // Number of units on hand
double price; // Unit price
Constructor
Initializes item's description with an empty string, units on hand to 0, and price to 0.0.
Constructor
Initializes the item's description, units on hand, and price with values passed as arguments.
setDescription method sets the item's description.
The setUnitsOnHand method sets the item's number of units on hand.
The setPrice method sets the item's price.
The getDescription method returns the item's description.
The getUnitsOnHand method returns the item's number of units on hand.
The getPrice method rerurns the item's price.
Create a toString method
CashRegister Class
Instance variables:
double TAX_RATE = 0.06; // Sales tax rate
double retail; // Retail price
int quantity; // Quantity purchased
Constructor
Will accept a RetailItem object and a quantity
getSubtotal method calculates and returns the subtotal of the sale using the following formula retail*quantity
getTax method calculates the sales tax for the sale. Call the getSubtotal method and multiply by the tax rate
getTotal method calculates the total of the sale. To do this you must call the getSubtotal method and add it to the getTax method
Create a toString method
Main Class
Create a variable for units on hand and initialize to 100
Create a variable for item price and initialize it to .75
Create a variable for product and initialize it to "Candy Bar"
Create a variable for quantity and initialize it to 50
Create a RetailItem object using the data required
Print out the item being purchased, the units on hand and the price of the item
Prompt the user for ("How many candy bars are you buying? ");
Create an if statement that tests to see if the quantity the user wants is more than the quantity you have. If true then print out a message informing the user that you only have x amount of items on hand. If false create a cashRegister object, print out the subtotal, print out the tax and print out the total. Update the units on hand by calling the get method for units on hand and subtracting the quantity the user requested. (be sure to test both scenarios when running your program
Print out the units on hand
Explanation / Answer
Program with comments:
RetailItem class:
package com.chegg.constructors;
//RetailItem class
public class RetailItem {
String description; // Item description
int unitsOnHand; // Number of units on hand
double price; // Unit price
//Constructor that initializes description with an empty string, units on hand to 0, and price to 0.0
RetailItem(){
this.description = "";
this.unitsOnHand = 0;
this.price = 0.0;
}
//Constructor that initializes the description, units on hand, and price with values passed as arguments.
RetailItem(String description,int units,double price){
this.description = description;
this.unitsOnHand = units;
this.price = price;
}
//Method setDescription which sets the item's description
public void setDescription(String description) {
this.description = description;
}
//Method setUnitsOnHand which sets the item's number of units on hand
public void setUnitsOnHand(int units) {
this.unitsOnHand = units;
}
//Method setPrice which sets the item's price
public void setPrice(double price) {
this.price = price;
}
//Method getDescription which returns the description
public String getDescription() {
return this.description;
}
//Method getUnitsOnHand which returns the units on hand
public int getUnitsOnHand() {
return this.unitsOnHand;
}
//Method getPrice which returns the price
public double getPrice() {
return this.price;
}
//Method toString which displays the RetailItem object variables information
@Override
public String toString() {
// TODO Auto-generated method stub
return "The Item being purchased: "+description+" ,Units on hand: "+unitsOnHand+
" ,Price of item:"+price;
}
}
CashRegister class:
package com.chegg.constructors;
//CashRegister class
public class CashRegister {
double TAX_RATE = 0.06; // Sales tax rate
double retail; // Retail price
int quantity; // Quantity purchased
//Constructor which accepts a RetailItem object and a quantity
public CashRegister(RetailItem obj,int qty) {
this.retail = obj.getPrice();
this.quantity = qty;
}
//Method getSubtotal to return the subtotal of the sale
public double getSubtotal() {
return retail*quantity;
}
//Method getTax which returns the sales tax applicable
public double getTax() {
return TAX_RATE*getSubtotal();
}
//Method getTotal which returns the total of the sale
public double getTotal() {
return getSubtotal()+getTax();
}
//Method toString which displays the Subtotal, Tax Applicable and Total price of the sale
@Override
public String toString() {
// TODO Auto-generated method stub
return "Subtotal: "+getSubtotal() +
" Tax applicable: "+getTax()+
" Total price: "+getTotal();
}
}
Main class:
package com.chegg.constructors;
public class Main {
public static void main(String[] args) {
//Initializing unitsOnHand, itemPrice and product variable values
int unitsOnHand = 100;
double itemPrice = 0.75;
String product = "Candy Bar";
//Constructing Retailitem object and initializing the item description, unitsOnHand and item price values by passing as arguments
RetailItem item = new RetailItem(product,unitsOnHand,itemPrice);
//Triggering the toString method of the RetailItem object
System.out.println(item);
//Prompt to the user
System.out.println("How many candy bars are you buying?");
//Quantity set as in the requirement mentioned
int quantity = 50;
//Checking if the quantity is greater than the units on hand and displaying the current units on hand
if(quantity>item.getUnitsOnHand()) {
System.out.println("We have only "+item.getUnitsOnHand()+" number of items on hand");
}
else {
//Creating the CashRegister object and passing the RetailItem and quantity values
CashRegister obj = new CashRegister(item, quantity);
//Triggering the toString method of the CashRegister object
System.out.println(obj);
//Subtracting the quantity from the units on hand
item.setUnitsOnHand(item.getUnitsOnHand()-quantity);
//Displaying the updated units on hand
System.out.println("The Current Units on hand: "+item.getUnitsOnHand());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.