Question: Using Java, Solve the following problem (a) Create an exception class called ProductException, whose constructor accepts a String for a message to be displayed. The

Using Java, Solve the following problem

(a) Create an exception class called ProductException, whose constructor accepts a String for a message to be displayed. The ProductException class is designed to be thrown when a product number or a price do not satisfy certain conditions. The ProductException class should display an appropriate message when it is thrown.

(b) Create a class called Product which has two instance variables, productNum as a String and price as a double. The Product constructor accepts values for initializing the productNum and price. The constructor uses two methods for setting the instance variables: setProductNum() and setPrice(). The setProductNum() method throws a ProductException when product number does not consist of three digits; and the setPrice() method throws a ProductException if the price is less than $ 0.01 or if the price is greater than $1.00. Add a toString() method to display a Product object information.

(c) Write a client class that builds a list of Product objects, using an array of maximum size of 10. The client allows a user to enter values for a product number and its price. The client class catches and handles the exception if it is thrown due to creating unacceptable Product object that does not satisfy the product conditions. The exception is handled by printing an appropriate message. Display an appropriate message when a Product object is created successfully. At the end display the list of accepted product items.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!