INHERITANCE AND POLYMORPHISM

Create a class Product which models customer’s purchased products. This class has:

– two private instance variables name (String) and unitPrice (double).

– A constructor to initialize the instance variables through parameters.

– A default constructor to initialize name to “unkown”, and unitPrice to 0. Use this(…)

– A method getPrice that returns the unitPrice.

– A getName method that returns the name.

– A toString method to return the name of the product followed by @ symbol, then the unitPrice.

 

Create two subclasses WeighedProduct and CountedProduct.  WeighedProduct has an additional instance variable weight (double) in Kg while CountedProduct has an additional variable quantity (int) both private.

– Write an appropriate constructor for each of the classes making use of the constructor of the superclass.

– Override getPrice method that returns the price of the Product based on its unit price and weight (WeighedProduct), or quantity (CountedProduct).  Make use of getPrice of the superclass

– Override also toString method for each class making use of the toString method of the superclass in defining those of the subclasses.

– toString should return something that can be printed on the receipt. For example

o Apple Fuji                  @ 5.00   1.5Kg     7.5 AED (in case of WeighedProduct class)

o Notebook Al-Bayan @ 6.5   10 units   65 AED (in case of CountedProduct class)

 

Write an application class with main method where you create an ArrayList<Product > object. Create several objects from the two subclasses and add them to the array list. Display the contents of the array list, one item per line. For each item, the display should contain Product name, unit price, weight (in Kgs) or quantity, and amount. You may use the toString method. The last line should show the total amount of all items purchased.

THIS PAPER HAS BEEN COMPLETED BY OUR WRITERS

Order your custom paper