Question: At this point, students should create new classes ProductPart5 and ProductTesterPart5 that will add onto the functionality of the project. (copy and paste part 4
At this point, students should create new classes ProductPart5 and ProductTesterPart5 that will add onto the functionality of the project. (copy and paste part 4 code into new part 5 classes) Topic(s): Adding a subclass, using extends, using super(), overriding methods from a superclass.
1. Create a subclass of the ProductPart5 class that has two additional variables. (For example, a DVD subclass could use movie title and length).
2. In the subclass, override the method to calculate the value of the inventory of a product with the same name as that method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
3. Override the toString() method from the ProductPart5 class so that all information about new subclass objects can be printed to the output.
4. Modify ProductTesterPart5 so that an array of objects of the new subclass can be created from user input. Display the subclass products using a for loop.
Here is the code:
package javaInventory;
public class ProductPart5 {
private int itemnumber; //unique value for identification
private String name; //name of the product
private int qtyinstock; //quantity in stock
private double price; //price of the product
//default constructor which initializes instance variables
//numeric values are 0 (zero) and String values are "" (null)
public ProductPart5()
{
itemnumber = 0;
name = "";
qtyinstock = 0;
price = 0;
}
//overload the constructor to allow setting values for Products
public ProductPart5(int i, String n, int q, double p)
{
itemnumber = i;
name = n;
qtyinstock = q;
price = p;
}
//item number setter
public void setItemNumber(int i)
{
itemnumber = i;
}
//name setter
public void setName(String n)
{
name = n;
}
//quantity in stock setter
public void setQtyInStock(int q)
{
qtyinstock = q;
}
//price setter
public void setPrice(int p)
{
price = p;
}
//add a quantity to qtyinstock
//receiving a shipment
public void addToInventory(int q)
{
qtyinstock += q;
}
//subtract a quantity from qtyinstock
//sales
public void deductFromInventory(int q)
{
qtyinstock -= q;
}
//itemnumber getter
public int getItemNumber()
{
return itemnumber;
}
//name getter
public String getName()
{
return name;
}
//quantityinstock getter
public int getQtyInStock()
{
return qtyinstock;
}
//price getter
public double getPrice()
{
return price;
}
//get total value of inventory for this Product
public double getInventoryValue()
{
return price * qtyinstock;
}
//override toString() Method from the Object class
//to allow display of each object to the console
public String toString()
{
return " Item Number: " + getItemNumber() + " Name: " + getName() +
" Inventory: " + getQtyInStock() + " Price: " +
getPrice() + " Inventory Value for this product: " + getInventoryValue();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
