Question: ---------------- public class Activity1C { public static void main(String[] args) { Item[] inventory = new Item[4]; inventory[0] = new Item(machine bolt, 50, M50-H, 120); inventory[1]
![---------------- public class Activity1C { public static void main(String[] args) {](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f4609d376e0_54866f4609ccebae.jpg)
----------------
public class Activity1C { public static void main(String[] args) { Item[] inventory = new Item[4];
inventory[0] = new Item("machine bolt", 50, "M50-H", 120); inventory[1] = new Item("phillips screw", 25, "P25-16", 750); inventory[2] = new Item("lock washer", 12, "W12-L", 400); inventory[3] = new Item("drywall screw", 38, "P38-DW", 2400);
printInventory(inventory);
sell(inventory[0], 90); sell(inventory[1], 750); sell(inventory[2], 600); sell(inventory[3], 2500);
printInventory(inventory);
order(inventory[0], 90); order(inventory[1], 1); order(inventory[2], 220); order(inventory[3], 50); order(inventory[3], -50);
printInventory(inventory);
System.out.println(" End of processing."); }
public static void sell(Item item, int amount) { int inStock;
inStock = item.sell(amount);
System.out.print("Selling " + amount + " of item: "); if (inStock == amount) { System.out.println("all are in stock."); } else { System.out.println(inStock + " in stock, " + (amount - inStock) + " on backorder."); } } public static void order(Item item, int amount) { String reorder;
reorder = item.order(amount);
if (reorder == null) { System.out.println("Unable to order item: invalid amount " + amount); } else { System.out.println(reorder); } }
public static void printInventory(Item[] inventory) { System.out.println("Inventory:"); for (int i = 0; i
class Item { private String name; private int size; private String code; private int quantity;
public Item(String name, int size, String code, int quantity) { this.name = name; this.size = size; this.code = code; this.quantity = quantity; }
public String order(int amount) { String result = null;
if (amount > 0) { result = "Re-order item: " + code + " Amount ordered: " + amount; quantity += amount; }
return result; }
public int sell(int amount) { int result = amount;
if (amount quantity) { result = quantity; } quantity -= amount; }
return result; }
public String toString() { String result;
result = "Item '" + name + "' size=" + size + "mm code='" + code; if (quantity >= 0) { result += " quantity=" + quantity; } else { result += " quantity=0 (" + -quantity + " backordered)"; }
return result; } }
EXERCISE One of the original goals of our program was performing an inventory check, to find out what items need to be reordered. Right now, there is no way to do so. To complete the program, we will add an additional method that can find if the stock of an item is too low. We can define "too low" as being "less than 25% of the original quantity when the item's object was created" Download the complete Hardware Store program and add the following features to it: 1. First, add an additional instance variable reorderQuantity (type int). In the constructor, initialize it to the quantity divided by 4 (using integer division, do not change the constructor's parameters) 2. Next, modify the toString) method to include this value in its results 3. Then, add a new instance method to the Item class: mustReorderO which returns a boolean value, true if the quantity of the item is less than the reorderQuantity, or false if there is at least that amount of the item still available 4. Finally, test your new code by writing a static method checkInventory in the main class. Pass it the entire array containing the inventory, and call mustReorderO for every item. If the method returns true, call order (the static method) to order 200 more of that item Test your new code by calling checkInventory after the initial round of sales, and printing the inventory afterwards. Add a few more sales to the program (cause some items to reach low inventory), and call it again Submit your updated Item class and the checkInventory method (only) by the due date specified in the course schedule
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
