In: Computer Science
a) Design a Java Class which represents a Retail Item. It is to have the following fields
Item Name
Item Serial No
Item Unit Price
Item Stock Level
Item Reorder Level
It is to have at least the following methods an Observer, Mutator and a Display method for each field. It is also to have a buy and a restock method and a method to issue a warning if the stock level goes below the re-order level.
b) Extend the Retail Item class of part a) above to represent a Perishable Item.
This subclass will have extra fields to represent expiry date, storage instructions and a consume deadline i,e the number of days in which the item must be consumed once opened.
Again have at least the following methods for these new fields, namely an Observer, Mutator and a Display method. Also have a method which displays the number of days until the expiry date.
c) Write a control program which creates five instances of standard retail items and five instances of perishable items.
1. Buy 2 Retail Items and 3 perishable items
2. Display the Stock levels of any two items
3. Calculate the total value of Items in Stock including perishable items Note Do not include any perishable items which are past their sell-by date
4. Re-order Stock for any three Items
Screenshot of the code:
RetailItem.java:
PerishableItems.java:
Test.java:
Output:
Code to copy:
RetailItem.java:
//Define the class.
public class RetailItem
{
//Define the variables.
private String itemName;
private String serialNumber;
private int unitPrice;
private int stockLevel;
private int reorderLevel;
//Define the constructor.
public RetailItem(String itemName,
String serialNumber, int unitPrice,
int stockLevel, int reorderLevel)
{
//Initialize the data members.
this.itemName = itemName;
this.serialNumber = serialNumber;
this.unitPrice = unitPrice;
this.stockLevel = stockLevel;
this.reorderLevel = reorderLevel;
}
//Define the method to return
//item name.
public String getterItemName()
{
return itemName;
}
//Define the method to set the item name.
public void setterItemName(String itemName)
{
this.itemName = itemName;
}
//Define the method to return
//serial number.
public String getterSerialNumber()
{
return serialNumber;
}
//Define the method to set serial number.
public void setterSerialNumber(String serialNumber)
{
this.serialNumber = serialNumber;
}
//Define the method to return
//unit price.
public int getterUnitPrice()
{
return unitPrice;
}
//Define the method to set the unit price.
public void setterUnitPrice(int unitPrice)
{
this.unitPrice = unitPrice;
}
//Define the method to return
//the stockLevel.
public int getterStockLevel()
{
return stockLevel;
}
//Define the method to set the stock level
public void setterStockLevel(int stockLevel)
{
this.stockLevel = stockLevel;
}
//Define the method to return
//the reorder levels.
public int getterReorderLevel()
{
return reorderLevel;
}
//Define the method to set the reorder level.
public void setterReorderLevel(int reorderLevel)
{
this.reorderLevel = reorderLevel;
}
//Define the method to update the
//Stock level after buying the stock.
public void buy(int b)
{
setterStockLevel(getterStockLevel() + b);
}
//Define the method to set the re-stock
//levels.
public void restock(int stock)
{
setterStockLevel(stock);
}
//Define the method to display the warning.
public void warning()
{
//Compare the stock level.
//Display error if stock is insufficient.
if(getterStockLevel()
System.out.println("WARNING: Stock "
+ "level is below the
reorder level!");
else
System.out.println("Stock level "
+
"looks good!"); } //Define the method to
return the value //of data members of the
class. @Override public
String toString() {
return "RetailItem [itemName=" + itemName +
", serialNumber=" +
serialNumber + ","
+ "
unitPrice=" + unitPrice +
",
stockLevel=" + stockLevel + ","
+ " reorderLevel=" +
reorderLevel + "]"; } } PerishableItems.java: //Import for input. import java.util.*; //Import for time. import java.util.concurrent.TimeUnit; public class PerishableItems
extends RetailItem { //Define the variables. private Date expiryDate; private String
storageInstructions; private int
consumeDeadline; //Define the constructor. public PerishableItems(String
itemName, String
serialNumber, int unitPrice,
int stockLevel, int
reorderLevel, Date
expiryDate, String storageInstructions,
int consumeDeadline) { //Use super for base
class.
super(itemName, serialNumber,
unitPrice, stockLevel, reorderLevel); //Set the values of data
members.
this.expiryDate = expiryDate;
this.storageInstructions =
storageInstructions;
this.consumeDeadline = consumeDeadline; } //Define the method to return the expiry date. public Date getterExpiryDate() { return
expiryDate; } //Define the method to set the expiry date. public void
setterExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate; } //Define the method to return the //storage instruction. public String
getterStorageInstructions() { return
storageInstructions; } //Define the method to set the storage //Instruction. public void
setterStorageInstructions(String
storageInstructions) {
this.storageInstructions =
storageInstructions; } //Define the method to return the deadlines. public int
getterConsumeDeadline() { return
consumeDeadline; } //Define the method to set the deadlines. public void
setterConsumeDeadline(int consumeDeadline) {
this.consumeDeadline = consumeDeadline; } //Define the method to return the expiry date. public int
daysExpiry() { int diff =
(int) (new Date().getTime() -
getterExpiryDate().getTime());
return (int)
TimeUnit.DAYS.convert(diff,
TimeUnit.MILLISECONDS); } //Define the method to return the value of //data members. @Override public String toString() { return
"PerishableItems [expiryDate=" + expiryDate +
", storageInstructions=" +
storageInstructions
+ ", consumeDeadline=" + consumeDeadline +
", getItemName()=" + getterItemName() +
", getSerialNumber()="+ getterSerialNumber() +
", getUnitPrice()=" + getterUnitPrice() +
", getStockLevel()=" + getterStockLevel()+ ",
getReorderLevel()=" + getterReorderLevel()+ "]"; } } Test.java: //Import for date. import java.util.Date; //Define the class test. public class Test { //Define the main method. public static
void main(String[] args) { //Create 2 objects of
retailitem class. RetailItem item1 =
new RetailItem ("Iphone7", "XYZ001",
70000, 100, 50); RetailItem item2 =
new RetailItem ("MacBook", "XYZ002",
90000, 120, 70); //Create 3 items of
perishableitem
class. PerishableItems item3 =
new PerishableItems ("Peanuts", "Pe01", 40,
700, 200, new Date(),
"Use by
time 30 days", 2); PerishableItems item4 =
new PerishableItems ("Bread", "B001", 200,
9000, 1100, new Date(),
"Use
by time 2 days", 5); PerishableItems item5 =
new PerishableItems ("Butter", "B002", 90, 800,
200, new Date(),
"Store
in refrigerator", 7); //Display the stock level
of 2 items.
System.out.println("Stock level for item
1= "
+item1.getterStockLevel());
System.out.println("Stock level for item
2= "
+item2.getterStockLevel()); //Calculate the total value
of items in stock. int tValue
= item1.getterUnitPrice() +
item2.getterUnitPrice()
+
item3.getterUnitPrice()
+
item4.getterUnitPrice()
+
item5.getterUnitPrice(); //Display the total
value.
System.out.println("Total Stock values =
" +tValue); //Re-stock for 3 items. item1.restock(1000); item2.restock(2000); item3.restock(3000); //Display the record after
re-stocking.
System.out.println("After
restocking:\n");
System.out.println(item1);
System.out.println(item2);
System.out.println(item3); } }