Objectives

The objectives of this lab are to implement an inheritance hierarchy into our Shop project.

ShopV7.0 (creating the project)

Download ShopV6.0 Project

Creating ShopV7.0 Project

  • In Eclipse, create a new project called ShopV7.0.

  • Right-click on the src folder and select New, followed by Package. Enter “models” as the package name:

Creating models package

  • Create two more packages: “controllers” and “utils”.

  • Copy the ShopV6.0 files into the ShopV7.0 project to the locations specified in the screen shot below. The project should be error free:

ShopV7.0 project structure

Testing the app

  • Run the app to make sure that all works ok.

ShopV7.0 (inheritance hierarchy)

  • In this step, you will continue working on ShopV7.0 and add an inheritance hierarchy to represent different types of Product.

Inheritance Hierarchy

  • In Eclipse, open ShopV7.0 (if it isn't open already).

  • In the models package, create a new class called DairyProduct that extents Product. This class should have:

    • a field called bestBeforeDate. Its type should be Date.
    • a constructor that takes in the date field along with the super class fields.
    • an accessor and mutator for the bestBeforeDate field.
    • a toString method that returns the bestBeforeDate and the four super class fields (note: use super.toString() here).
  • In the models package, create a new class called Milk that extents DairyProduct. This class should have:

    • a field called cartonSize. Its type should be int.
    • a field called fatContent. Its type should be String.
    • a constructor that takes in the two instance fields along with the super class fields.
    • an accessor and mutator for each instance field.
    • a toString method that returns the two instance fields and all superclass fields. The toString should also indicate that the product type is milk.
  • In the models package, create a new class called Butter that extents DairyProduct. This class should have:

    • a field called packetGrams. Its type should be int.
    • a field called isSalted. Its type should be boolean.
    • a constructor that takes in the two instance fields along with the super class fields.
    • an accessor and mutator for each instance field.
    • a toString method that returns the two instance fields and all superclass fields. The toString should also indicate that the product type is butter.

ShopV7.0 (using the inheritance hierarchy)

  • In this step, you will continue working on ShopV7.0 and use the new inheritance hierarchy in the menu controller class.

MenuController Changes

  • In Eclipse, open ShopV7.0 (if it isn't open already).

  • Open the class, MenuController and locate the readProductDetails() method.

  • We need to make the following changes to this method so that we can input either Milk or Butter details:

    • Ask the user to enter a best before date (you will need to do some research here yourself to figure this bit out).
    • Ask the user to state whether they are entering butter or milk.
    • If they are entering a milk product, ask for the cartonSize and the fatContent. Add a Milk object to the products array in Store.
    • If they are entering a butter product, ask for the packetGrams and whether the product is salted or not. Add a Butter object to the products array in Store.
  • Now locate the updateProductDetails() method. Do you need to make any changes here?

Testing the app

  • Run the app to make sure that all works ok.

Solution