Answer to Question #310105 in Java | JSP | JSF for mnghema

Question #310105

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]


1
Expert's answer
2022-03-12T18:05:23-0500
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*;

public class RetailShop {
    private int sumOfSoldPrice = 0;
    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);
            sumOfSoldPrice += itemInStock.getPrice() * itemSize;
            
            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");
        }
    }

    public int getSumOfSoldPrice() {
        return sumOfSoldPrice;
    }
}


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;
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS