Organising Item storage in the computer can assist in ensuring that the Items that were stored in the
stock item list first are the first to be taken out. This will ensure that older stock is sold first. Conduct
some research on what data structure you can use in the storage of Item in the retail shop instead of
using a list.
2.1 Change the storage List of the Items in the Retail Shop so that the item stored first are the items
removed first when items are sold
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;
public class RetailShop {
public Map<String, Item> stock = new HashMap<>();
public Map<String, Integer> sold = new HashMap<>();
public void sell(String itemName, int itemSize) {
Item itemInStock = stock.get(itemName);
if (itemInStock != null && itemInStock.getAmount() >= itemSize) {
itemInStock.setAmount(itemInStock.getAmount() - itemSize);
sold.merge(itemName, itemSize, Integer::sum);
long daysBeforeExpiry = Duration.between(LocalDateTime.now(), itemInStock.getExpiryDate()).toDays();
System.out.println("Name: " + itemName);
System.out.println("Size: " + itemSize);
System.out.println("Days before expiry date: " + daysBeforeExpiry);
} else {
System.out.println("no stock");
}
}
}
import java.time.LocalDateTime;
public class Item {
private String name;
private double price;
private int amount;
private LocalDateTime expiryDate;
public Item(String name, double price, int amount, LocalDateTime expiryDate) {
this.name = name;
this.price = price;
this.amount = amount;
this.expiryDate = expiryDate;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public LocalDateTime getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(LocalDateTime expiryDate) {
this.expiryDate = expiryDate;
}
}
Comments
Leave a comment