In Java Create a class called RetailItemDemo class - Create a RetailItem Object
ID: 3766693 • Letter: I
Question
In Java
Create a class called RetailItemDemo class
- Create a RetailItem Object
- Get the item description from the keyboard
-Get the item units from the keyboard.
-If the number of units is negative when entered at the keyboard, then have the program catch the NegativeUnitsException and output "Error: Negative number given for units:(print the negative value)
- Get the item price from the keyboard.
-If the price is negative when entered at the keyboard, then have the program catch the NegativePriceException and output " Error: Negative number given for the price: (print the negative value)
Display the output for the item.
Input should be:
Item description: Shirt
Units on hand: -15 (obvious error)
Units on hand: 17
Item price: -12.50 (obvious error)
Item price: 12.50
Then print out all of the information on the item.
- Create a RetailItem class with the following methods: Default constructor, overloaded constructor, setDescription, setUnitsOnHand, setPrice, getDescription, getUnitsOnHand, getPrice
- Create a NegativePriceException class with a default constructor and an overloaded constructor that reports the price that caused the exception to be thrown.
- Create a NegativeUnitsException class with a default constructor and an overloaded constructor that reports the units value that caused the exception to be thrown
Explanation / Answer
Answer :
class RetailItem
{
private String description;
private int unitsOnHand;
private double price;
public RetailItem(double price, int unitsOnHand, String description)
{
this.price = price;
this.unitsOnHand = unitsOnHand;
this.description = description;
}
public String getDescription()
{
return description;
}
public double getPrice()
{
return price;
}
public int getUnitsOnHand()
{
return unitsOnHand;
}
}
public class RetailItemDemo
{
public static void main(String[] Args)
{
RetailItem a1 = new RetailItem(59.95, 12, "Jacket");
RetailItem a2 = new RetailItem(34.95, 40, "Shirt");
RetailItem a3 = new RetailItem(39.95, 22, "Jeans");
RetailItem a4 = new RetailItem(1337.95, 1337, "TrollFace");
System.out.println("Units Price Item ------------------");
System.out.println(a1.getUnitsOnHand() + " $" + a1.getPrice() + " " + a1.getDescription());
System.out.println(a2.getUnitsOnHand() + " $" + a2.getPrice() + " " + a2.getDescription());
System.out.println(a3.getUnitsOnHand() + " $" + a3.getPrice() + " " + a3.getDescription());
System.out.println(a4.getUnitsOnHand() + " $" + a4.getPrice() + " " + a4.getDescription());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.