JAVA RetailItem Exceptions Programming Challenge 4 of Chapte
JAVA
RetailItem Exceptions
Programming Challenge 4 of Chapter 6 required you to write a RetailItem class that holds
data pertaining to a retail item. Write an exception class that can be instantiated and thrown
when a negative number is given for the price. Write another exception class that can be
instantiated and thrown when a negative number is given for the units on hand. Demonstrate
the exception classes in a program.
Chapter 11 Challenge 3 (you need 4 classes: Negative price and Negative unit exceptions, Retail item and demo classes)
-------------------------------------------------------------------------------------------------------------------------------------------------
Hello I need help with the question above I have also included the previous code from Programming Challenge 4 of Chapter 6 :
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is question number 6 just incase you need it:
6. CashRegister Class
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.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
And the output is suppose to look like this:
Output
Enter the item description: candy bar
Enter the units on hand: -5
Error: Negative number given for units: -5.0
Enter the units on hand: 5
Enter the item price: -10
Error: Negative number given for price: -10.0
Enter the item price: 10
You entered:
Description: candy bar
Units on hand: 5
Price: 10.0
Solution
solution
public class RetailItem{
private String itemName;
private int unitsOnHand;
private double price;
//----------------------------------------------------------------------------------------------------------------------
public RetailItem(String itemName, int unitsOnHand, double price) {
this.itemName = itemName;
this.unitsOnHand = unitsOnHand;
this.price = price;
}
/**
* It will return the itemName
*/
public String getItemName() {
return itemName;
}
/**
* It will set the ItemName
*/
public void setItemName(String itemName) {
this.itemName = itemName;
}
/**
* It will return the UnitsOnHand
*/
public int getUnitsOnHand() {
return unitsOnHand;
}
/**
* It will set the unitsOnHand
*/
public void setUnitsOnHand(int unitsOnHand) {
this.unitsOnHand = unitsOnHand;
}
/**
* It will return the price
*/
public double getPrice() {
return price;
}
/**
* It will set the price
*/
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return \"description :\" + itemName + \"\ unitsOnHand :\" + unitsOnHand
+ \"\ price :\" + price ;
}
}
package com.sp.resources;
public class CashRegister {
private RetailItem item;
private int noOfUnitsBuying;
CashRegister(RetailItem item, int noOfUnitsBuying) {
this.item = item;
this.noOfUnitsBuying = noOfUnitsBuying;
}
/**
* it will return the item
*/
public RetailItem getItem() {
return item;
}
/**
* it will set the item
*/
public void setItem(RetailItem item) {
this.item = item;
}
/**
* it will get the amount of units being purchase
*/
public int getNoOfUnitsBuying() {
return noOfUnitsBuying;
}
/**
* it will set the amount of units being purchase
*/
public void setNoOfUnitsBuying(int noOfUnitsBuying) {
this.noOfUnitsBuying = noOfUnitsBuying;
}
/*
According to the textbook you need to get: 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 RetailItem object that was passed as an
argument to the constructor.
*/
public double getSubtotal() {
return item.getPrice() * noOfUnitsBuying;
}
/*
According to the textbook you need to get: The getTax method should return the amount of sales tax on the purchase.
The sales tax rate is 6 percent of a retail sale.
*/
public double getTax() {
return getSubtotal() * 0.06;
}
/*
According to the textbook you need to get: The getTotal method should return the total of the sale, which is the
subtotal plus the sales tax.
*/
public double getTotal() {
return getSubtotal() + getTax();
}
}
package com.sp.resources;
import java.util.Scanner;
public class CashRegisterDemo {
public static void main(String[] args) {
Scanner scanner = null;
try {
String itemName;
int unitsOnHand;
double price;
RetailItem item;
int noOfUnitsBuying;
scanner = new Scanner(System.in);
/*
It will ask the user: Item being purchased? Units on hand? Price? and how many candy bars is the user buying?
*/
System.out.print(\"enter the item description:\");
itemName = scanner.nextLine();
System.out.print(\"Units on hand: \");
unitsOnHand = scanner.nextInt();
if(unitsOnHand<0)
{
try {
throw new invalidStockDetailsException(\"Error: Negative number given for units:\" +unitsOnHand);
} catch (invalidStockDetailsException e) {
String exception=new invalidStockDetailsException(\"Error: Negative number given for units:\" +unitsOnHand).getString();
System.out.println(exception);
System.out.print(\"Units on hand: \");
unitsOnHand = scanner.nextInt();
}
}
System.out.print(\"Price: $\");
price = scanner.nextDouble();
if(price<0)
{
try {
throw new invalidPriceDetailsException(\"Error: Negative number given for price:\" +price);
} catch (invalidPriceDetailsException e) {
String exception=new invalidPriceDetailsException(\"Error: Negative number given for price:\" +price).getString();
System.out.println(exception);
System.out.print(\"Price: $\");
price= scanner.nextInt();
}
/*System.out.println(\"Error: Negative number given for price:\" +price);
System.out.print(\"Price: $\");
price = scanner.nextDouble();*/
}
/*
item will contain the itemName, unitsOnHand, and the price.
*/
item = new RetailItem(itemName, unitsOnHand, price);
System.out.println(item.toString());
System.out.print(\"How many\" +itemName+\" are you buying? \");
noOfUnitsBuying = scanner.nextInt();
if(noOfUnitsBuying<0)
{
try {
throw new invalidBuyingDetailsException(\"Error: Negative number given for units:\" +noOfUnitsBuying);
} catch (invalidBuyingDetailsException e) {
String exception=new invalidStockDetailsException(\"Error: Negative number given for units:\" +noOfUnitsBuying).getString();
System.out.println(exception);
System.out.print(\"enter How many candy bars are you buying? \");
noOfUnitsBuying = scanner.nextInt();
}
}
/*
if the units that the customer is buying is less or equal to what we have in hand the program will display
the subtotal, sales tax, total, and the units on hand.
*/
if (noOfUnitsBuying <= item.getUnitsOnHand()) {
CashRegister cashRegister = new CashRegister(item, noOfUnitsBuying);
System.out.println(\"Subtotal: $\" + cashRegister.getSubtotal());
System.out.println(\"Sales tax: $\" + cashRegister.getTax());
System.out.println(\"Total: $\" + cashRegister.getTotal());
item.setUnitsOnHand(unitsOnHand - noOfUnitsBuying);
System.out.println(\"Units on hand: \" + item.getUnitsOnHand());
/*
otherwise, if the user inputs more than what we have in hand the program will display a message saying:
\"Sorry, we only have (the amount we have on hand)\" and the program will end
*/
} else {
System.out.println(\"Sorry, we only have \"
+ item.getUnitsOnHand() + \" in stock.\");
}
} catch (Exception e) {
}
}
}
output
enter the item description:candy bar
Units on hand: -5
Error: Negative number given for units:-5
Units on hand: 5
Price: $-10
Error: Negative number given for price:-10.0
Price: $10
description :candy bar
unitsOnHand :5
price :10.0
How manycandy bar are you buying? 2
Subtotal: $20.0
Sales tax: $1.2
Total: $21.2
Units on hand: 3





