Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

RetailItem Exceptions Programming Challenge 4 of Chapter 6 required you to write

ID: 3821273 • Letter: R

Question

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. Modify the RetailItem class so that it throws the appropriate exception when either of these errors occurs.Demonstrate the modified Retail Item class and the exception classes in a program.

retailitem.java


import java.util.InputMismatchException;

public class retailitem
{
    private String description;
    private int units;
    private double price;
  
    public retailitem()
    {
        description = null;
        units = 0;
        price = 0.0;
    }
  
    public retailitem(String description, int units, double price)
    {
        this.description = description;
        if(units < 0)
        {
            throw new InputMismatchException("Units can not be a negative number");
        }
        else
        {
            this.units = units;
        }
        if(price < 0.0)
        {
            throw new InputMismatchException("Price can not be a negative number");
        }
        else
        {
            this.price = price;
        }
    }
  
     public String getDescription()
     {
         return description;
     }
   
     public int getUnits()
     {
         return units;
     }
   
     public double getPrice()
     {
         return price;
     }
   
     public retailitem copy()
     {
         retailitem copy = new retailitem(description,units,price);
         return copy;
     }
   
     public String toString()
     {
         return "Item is" + description + "There are"
+ units + " " + "left in the store" +
"It currently is selling for" + " " + price;
     }
    
}

Explanation / Answer

public class RetailItem

{

private String description;

private int unitsOnHand;    

private double price;        

public RetailItem()

{

description = "";

unitsOnHand = 0;

price = 0.0;

}

public RetailItem(String d, int u, double p)

{

description = d;

unitsOnHand = u;

price = p;

}

public void setDescription(String d)

{

description = d;

}

public void setUnitsOnHand(int u)

{

unitsOnHand = u;

}

public void setPrice(double p)

{

price = p;

}

public String getDescription()

{

return description;

}

public int getUnitsOnHand()

{

return unitsOnHand;

}

public double getPrice()

{

return price;

}

}

public class RetailItemDemo

{

public static void main(String[] args)

{

RetailItem item1 = new RetailItem();

item1.setDescription("Jacket");

item1.setUnitsOnHand(12);

item1.setPrice(59.95);

RetailItem item2 =new RetailItem("Designer Jeans", 40, 34.95);

RetailItem item3 = new RetailItem();

item3.setDescription("Shirt");

item3.setUnitsOnHand(20);

item3.setPrice(24.95);

System.out.println("Item #1");

System.out.println("Description: " + item1.getDescription());

System.out.println("Units on hand: " + item1.getUnitsOnHand());

System.out.println("Price: " + item1.getPrice());

System.out.println(" Item #2");

System.out.println("Description: " + item2.getDescription());

System.out.println("Units on hand: " + item2.getUnitsOnHand());

System.out.println("Price: " + item2.getPrice());

System.out.println(" Item #3");

System.out.println("Description: " + item3.getDescription());

System.out.println("Units on hand: " + item3.getUnitsOnHand());

System.out.println("Price: " + item3.getPrice());

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote