1.5 A retail shop have functionality to sell. The sell function must take item name, and size as a
parameter. Removes the item from the stock and put that Item in the sold list. The sell function also
displays the sold item details, name, size, and days before expiry date. [2]
If item is not available in the stock a message must be displayed on the screen “no stock”;
1.6 An Items class need to be created in order to implement a complete Retail Shop. All items MUST/
FORCED to have the getPrice() function returns price of an item , setPrice(double price), returns
nothing, but set a price of an Item. [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