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
public class RetailItem {
private String itemName;
private String serialNumber;
private int unitPrice;
private int stockLevel;
private int reorderLevel;
public RetailItem(String itemName, String
serialNumber, int unitPrice, int stockLevel, int reorderLevel)
{
this.itemName = itemName;
this.serialNumber =
serialNumber;
this.unitPrice = unitPrice;
this.stockLevel = stockLevel;
this.reorderLevel =
reorderLevel;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getSerialNumber() {
return serialNumber;
}
public void setSerialNumber(String serialNumber)
{
this.serialNumber =
serialNumber;
}
public int getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(int unitPrice) {
this.unitPrice = unitPrice;
}
public int getStockLevel() {
return stockLevel;
}
public void setStockLevel(int stockLevel) {
this.stockLevel = stockLevel;
}
public int getReorderLevel() {
return reorderLevel;
}
public void setReorderLevel(int reorderLevel) {
this.reorderLevel =
reorderLevel;
}
public void buy(int b) {//Buy stocks of value b
setStockLevel(getStockLevel() +
b);
}
public void restock(int stock) {
setStockLevel(stock);
}
public void warning() {
if(getStockLevel()
else {
System.out.println("Stock level looks good!");
}
}
@Override
public String toString() {
return "RetailItem [itemName=" +
itemName + ", serialNumber=" + serialNumber + ", unitPrice=" +
unitPrice
+ ", stockLevel=" + stockLevel + ",
reorderLevel=" + reorderLevel + "]";
}
}
import java.util.Date;
import java.util.concurrent.TimeUnit;
public class PerishableItems extends RetailItem{
private Date expiryDate;
private String storageInstructions;
private int consumeDeadline;
public PerishableItems(String itemName, String
serialNumber, int unitPrice, int stockLevel, int
reorderLevel,
Date expiryDate,
String storageInstructions, int consumeDeadline) {
super(itemName, serialNumber,
unitPrice, stockLevel, reorderLevel);
this.expiryDate = expiryDate;
this.storageInstructions =
storageInstructions;
this.consumeDeadline =
consumeDeadline;
}
public Date getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(Date expiryDate) {
this.expiryDate = expiryDate;
}
public String getStorageInstructions() {
return storageInstructions;
}
public void setStorageInstructions(String
storageInstructions) {
this.storageInstructions =
storageInstructions;
}
public int getConsumeDeadline() {
return consumeDeadline;
}
public void setConsumeDeadline(int consumeDeadline)
{
this.consumeDeadline =
consumeDeadline;
}
public int daysExpiry() {
int diff = (int) (new
Date().getTime() - getExpiryDate().getTime());
return (int)
TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
@Override
public String toString() {
return "PerishableItems
[expiryDate=" + expiryDate + ", storageInstructions=" +
storageInstructions
+ ", consumeDeadline=" + consumeDeadline + ",
getItemName()=" + getItemName() + ", getSerialNumber()="
+ getSerialNumber() + ", getUnitPrice()=" +
getUnitPrice() + ", getStockLevel()=" + getStockLevel()
+ ", getReorderLevel()=" + getReorderLevel() +
"]";
}
}
import java.util.Date;
public class Test {
public static void main(String[] args) {
RetailItem item1 = new
RetailItem("Laptop", "ABC001", 40000, 1000, 500);
RetailItem item2 = new
RetailItem("Mobile", "ABC002", 30000, 1200, 700);
PerishableItems item3 = new
PerishableItems("Milk", "P001", 40, 500, 100, new Date(), "Store in
cool place", 2);
PerishableItems item4 = new
PerishableItems("Bread", "P002", 400, 5000, 1000, new Date(), "Do
not heat it", 5);
PerishableItems item5 = new
PerishableItems("Noodles", "P003", 50, 400, 100, new Date(), "Store
in dry place", 7);
System.out.println("Stock level for
item 1= "+item1.getStockLevel());
System.out.println("Stock level for
item 2= "+item2.getStockLevel());
int totalValue =
item1.getUnitPrice() + item2.getUnitPrice() + item3.getUnitPrice()
+item4.getUnitPrice() +item5.getUnitPrice();
System.out.println("Stock values =
"+totalValue);
item1.restock(1000);
item2.restock(2000);
item3.restock(3000);
System.out.println("After
restocking:\n");
System.out.println(item1);
System.out.println(item2);
System.out.println(item3);
}
}
Sample output:
Stock level for item 1= 1000
Stock level for item 2= 1200
Stock values = 70490
After restocking:
RetailItem [itemName=Laptop, serialNumber=ABC001,
unitPrice=40000, stockLevel=1000, reorderLevel=500]
RetailItem [itemName=Mobile, serialNumber=ABC002, unitPrice=30000,
stockLevel=2000, reorderLevel=700]
PerishableItems [expiryDate=Wed Oct 18 19:50:25 IST 2017,
storageInstructions=Store in cool place, consumeDeadline=2,
getItemName()=Milk, getSerialNumber()=P001, getUnitPrice()=40,
getStockLevel()=3000, getReorderLevel()=100]