Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA question create a class Shop that maintains information about the inventory

ID: 3868999 • Letter: J

Question

JAVA question

create a class Shop that maintains information about the inventory of a shop.


Create an instance method, void addItem(String name, int stock, int price)
which is used to add to an internal data structure an item, how many of that
item are in the shop, and how much each costs, in cents.
Create another instance method, int totalStockValue(), which uses the internal
data structure to return the sum of the value of all stock, as an int, in cents.
Implement another instance method void addItem(String name, int stock, int price,
int rate, int reorderdays) which allows you to store not only the item name, stock
level and price, but also the rate (in items per day) that the item is usually sold,
and the number of days it takes to reorder the stock.
Implement another instance method HashMap reorder() which returns a hash map
listing the name (key) and number (value) of all of the items that need to be reordered
today in order to ensure that the expected number of stock is always at least one
(ie sufficient to last until the order arrives, assuming average sales). Whenever an
item is ordered, seven days worth of average sales minus the current stock of that
item is always ordered.

Explanation / Answer


import java.util.ArrayList;
import java.util.HashMap;

public class Shop {
    ArrayList<StoreItem> itemsInStock = new ArrayList<>();
    int total;

    public void addItem(String name, int stock, int price) {
        itemsInStock.add(new StoreItem(name, stock, price));
    }

    public int totalStockValue() {
        for (StoreItem storeItem : itemsInStock) {
                total = total + storeItem.getPrice() * storeItem.getStock();
        }
        return total;
    }

    public void addItem(String name, int stock, int price, int rate, int reOrderDays) {
        itemsInStock.add(new StoreItem(name, stock, price, rate, reOrderDays));

    }

    public HashMap reorder() {
        HashMap<String, Integer> reOrderHashMap = new HashMap<>();
        for (StoreItem storeItem : itemsInStock) {
            if (storeItem.getReOrderDate() > 2) {
                int reOrder = storeItem.getRate() * 7 - storeItem.getStock();
                if (reOrder > 2) {
                    reOrderHashMap.put(storeItem.getName(), reOrder);
                }
            }
        }
        return reOrderHashMap;
    }
}

class StoreItem {
    private String name;
    private int stock;
    private int price;
    private int rate;
    private int reOrderDate;

    public StoreItem(String name, int stock, int price) {
        this.name = name;
        this.stock = stock;
        this.price = price;
    }

    public StoreItem(String name, int stock, int price, int rate, int reOrderDate) {
        this.name = name;
        this.stock = stock;
        this.price = price;
        this.rate = rate;
        this.reOrderDate = reOrderDate;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public int getStock() {
        return stock;
    }

    public void setStock(int stock) {
        this.stock = stock;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getRate() {
        return rate;
    }

    public void setRate(int rate) {
        this.rate = rate;
    }

    public int getReOrderDate() {
        return reOrderDate;
    }

    public void setReOrderDate(int reOrderDate) {
        this.reOrderDate = reOrderDate;
    }
}