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)

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

Hello I need help with this program this is what I got so far but I dont get the output can you please tell me what I am doing wrong or what extra code do I need to add in order to show this output. Thank you in advance.

================================================================

=========================================================

===================================================================

Solution

Please find the code and comments for description :

CODE :

import java.util.Scanner; //Needed for Scanner class

//InvalidPrice Exception class
class InvalidPrice extends Exception {

    public InvalidPrice(double n) {
        super(\"Error: Negative number given for price : \" + n);
    }
}//end class

//Invalid Price Exception class
class InvalidUnits extends Exception {

    public InvalidUnits(double n) {
        super(\"Error: Negative number given for units : \" + n);
    }
}//end class

class RetailItem {

    private String description;
    private int units;
    private double price;

    public RetailItem() {

    }

    //Constructor with arguments
    public RetailItem(String des, int u, double p) throws InvalidPrice, InvalidUnits {
        description = des;
        if (u < 0) {
            throw new InvalidUnits(u);
        } else {
            units = u;
        }
        units = u;
        if (p < 0) {
            throw new InvalidPrice(p);
        } else {
            price = p;
        }
    }

    //Mutator function for description
    public void setDescription(String des) {
        description = des;
    }

    //Mutator function for price
    public void setPrice(double p) throws InvalidPrice {
        if (p < 0) {
            throw new InvalidPrice(p);
        } else {
            price = p;
        }
    }

    //Mutator function for units on hand
    void setUnits(int u) throws InvalidUnits {
        if (u < 0) {
            throw new InvalidUnits(u);
        } else {
            units = u;
        }
    }

    //Accessor function for units on hand
    public int getUnits() {
        return units;
    }

    //Accessor function for description
    public String getDescription() {
        return description;
    }

    //Accessor function for price
    public double getPrice() {
        return price;
    }
}

public class RetailItemDemo {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        RetailItem r1;
        String itemDesc;
        int units;
        double price;
        //RetailItem with three objects

        System.out.println(\"Enter the item description : \");
        itemDesc = sc.nextLine();
        System.out.println(\"Enter the units on hand : \");
        units = sc.nextInt();
        while (units < 0) {
            try {
                if (units < 0) {
                    throw new InvalidUnits(units);
                }
            } catch (InvalidUnits e) {
                System.out.println(e.getMessage());
            }
            System.out.println(\"Enter the units on hand : \");
            units = sc.nextInt();
        }

        System.out.println(\"Enter the item price : \");
        price = sc.nextDouble();

        while (price < 0) {
            try {
                if (price < 0) {
                    throw new InvalidPrice(price);
                }
            } catch (InvalidPrice e) {
                System.out.println(e.getMessage());
            }
            System.out.println(\"Enter the item price : \");
            price = sc.nextDouble();
        }
      
        r1 = new RetailItem(itemDesc, units, price);

        //function call to set values
        System.out.println(\"       \");
        System.out.println(\"\\tDescription\\tUnits on Hand\\tPrice\");
        System.out.println(\"       \");
        //Accessor function calls
        System.out.println(\"Item #1\\t\" + r1.getDescription() + \"\\t\\t\" + r1.getUnits() + \"\\t\\t\" + r1.getPrice());

        //pause system for a while
        System.exit(0);
    }
}

OUTPUT :

Enter the item description :
Shirts
Enter the units on hand :
-5
Error: Negative number given for units : -5.0
Enter the units on hand :
-10
Error: Negative number given for units : -10.0
Enter the units on hand :
5
Enter the item price :
-99
Error: Negative number given for price : -99.0
Enter the item price :
-78
Error: Negative number given for price : -78.0
Enter the item price :
54
     
   Description   Units on Hand   Price
     
Item #1   Shirts       5       54.0

Hope this is helpful.

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 e
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 e
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 e
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 e

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site