File 1: RetailProduct.java
Create a file named Retail product.java that contains fields for the: product name, price, quantity.
This file should contain a constructor that initializes all fields. In addition, it should have getter and setter functions for all fields. Finally you should also have a toString method that display the values of all fields.
Ensure that the setPrice and setQuantity methods thow a NumberFormatException if the price is a negative number and if the quantity is a negative number or the quantity is above 10000 units. Create your own message for the exception
File 2: testRetailProduct.java
Ask the user to provide a product name, a price and quantity for the object. Utilize a try and catch block to ensure that the user provided the correct type of input for price and quantity (capturing InputMismatchException).
Once the user provides data of the correct type, create a second try and catch block to that ensures a new RetailProduct is created correctly, if an exception is caught, display the error message by utilizing the getMessage() method.
At the end of the program, utilize the toString method to display the information about the RetailProduct that was created.
Program flow:
- Request product information from user – Use try and catch to ensure correct data types
- Create new RetailProduct – Use another try and catch to ensure Number Format is valid
- Display RetailProduct fields utilizing toString