Question: Java Code: One of the original goals of our program was performing an inventory check, to find out what items need to be reordered. Right
Java Code:
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:
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).
Next, modify the toString() method to include this value in its results.
Then, add a new instance method to the Item class: mustReorder() 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.
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 mustReorder() 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. Please find the Hardware store Program below.
(public class Activity1A ,public class Activity1B, public class Activity1C).
public class Activity1A { public static void main(String[] args) { Item bolt = new Item("machine bolt", 50, "M50-H", 120);
System.out.println(bolt); bolt.sell(50); bolt.order(20); System.out.println(bolt);
System.out.println(" End of processing."); } }
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 void sell(int amount) { quantity -= amount; }
public void order(int amount) { quantity += amount; }
public String toString() { return "Item '" + name + "' size=" + size + "mm code='" + code + "' quantity=" + quantity; } }
public class Activity1B {
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);
inventory[0].order(90);
inventory[1].order(1);
inventory[2].order(220);
inventory[3].order(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 printInventory(Item[] inventory) {
System.out.println("Inventory:");
for (int i = 0; i < inventory.length; i++) {
System.out.println(inventory[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 int sell(int amount) {
int result = amount;
if (amount > quantity) {
result = quantity;
}
quantity -= amount;
return result;
}
public void order(int amount) {
quantity += amount;
}
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;
}
}
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 < inventory.length; i++) {
System.out.println(inventory[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 < 0) {
result = 0;
} else {
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;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
