Answer to Question #308902 in Java | JSP | JSF for Unknown363256

Question #308902

1.7 All Items have item name, item size, item price, expiryDate (use LocalDate class for this) . Item fall into the following categories: Drink, Diary, and Detergent.

Drink items have an additional variable a string that shows what type of drink : “fizzy drink” and “fruit drink”.

Dairy has storage Temperature, this is a recommended storage temperature.

Detergent have a string indicating “liquid” or “powder” or “bar”. [5] 1.8 Item has function to view number of days left before expiry. viewExpiry( )

returns an int number of days left before expiry. [2] If this number is less than or equal to 1, print in capital letters that ITEM EXPIRED


1
Expert's answer
2022-03-10T10:18:49-0500
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;
    }
}

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