A Retail has name, a List containing Items available in the stock and another List containing sold
items. [2]
1.2 A Retail have functionality to view available stock, that prints Class name of item, name of Item,
size of item and total number of items that have same name, and size. [5]
EXAMPLE
ClassName Item name Size Total
Drink coke 1 10; // this show that in the list there are 10 coke that are size of 1
Drink coke 2 5; // this show that in the list there are 5 coke that are size 2
Sizes in drinks means litres.
Hint: Items are the same if their class name, item name and size are the same.
1.3. A retail Shop has a functionality to add item into the stock list. The add item takes a parameter
the Item to be added on stock. [2]
1.4 A retail shop also has a functionality to calculate total sales that returns double sum of money
made from all sold items. [2]
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