You will create a program that simulates a store\'s inventory. A store carries v
ID: 3919685 • Letter: Y
Question
You will create a program that simulates a store's inventory. A store carries varying quantities of five different items (your choice of items and amounts). Five customers repeatedly generate random requests for multiple items and ask the store to fill the requests. If the store has enough items in inventory, it will fill the request, and subtract the quantities of the request from the inventory. If the store does not have enough items in inventory, the customer must wait for more inventories to arrive. A customer should sleep for 50 milliseconds before making a new request. A store manager is in charge of ordering more items. The store manager will check to see if any of the items in inventory are depleted. If they are, he will order more items. However, the store manager has other things to do, so he doesn't constantly check the amounts in the inventory. He sleeps in between checks of the inventory. Sample output from the program may look like:
. . Store inventory
---------------
Apples: 44, Oranges: 29, Pineapples: 9, Persimmons: 43, Kumquats: 27
---------------
Customer 4 requests, 1 Apple, 3 Oranges, 9 Pineapples, 0 Persimmons, 5 Kumquats Request fulfilled
---------------
Store inventory
---------------
Apples: 43, Oranges: 26, Pineapples: 0, Persimmons: 43, Kumquats: 22
---------------
Customer 0 requests, 4 Apples, 0 Oranges, 2 Pineapples, 1 Persimmon, 3 Kumquats
Customer 0 waiting . . .
Customer 1 requests, 2 Apples, 9 Oranges, 5 Pineapples, 7 Persimmons, 3 Kumquats Customer 1 waiting . . .
Customer 2 requests, 2 Apples, 9 Oranges, 5 Pineapples, 7 Persimmons, 3 Kumquats Customer 2 waiting . . .
Customer 3 requests, 2 Apples, 9 Oranges, 5 Pineapples, 7 Persimmons, 3 Kumquats Customer 3 waiting . . .
** RESTOCKING **
---------------
Store inventory
---------------
Apples: 50, Oranges: 50, Pineapples: 50, Persimmons: 50, Kumquats: 50
---------------
Customer 0 requests, 4 Apples, 0 Oranges, 2 Pineapples, 1 Persimmon, 3 Kumquats
Request fulfilled
. . .
You will create a program to run this simulation. Make sure all appropriate methods are synchronized. An Item class has been provided for your convenience:
public class Item {
private String name;
private int quantity;
public Item(String aName) {
this(aName, 0);
}
public Item(String aName, int aQuantity) {
name = aName;
quantity = aQuantity;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int newQuantity) {
quantity = newQuantity;
}
public boolean equals(Object otherObject) {
if (otherObject.getClass() != this.getClass())
return false;
Item other = (Item) otherObject;
if (other.getName().equals(name))
return true;
return false;
}
}
Start by creating the class that simulates customer requests called CustomerRunnable. This class is going to have to interact with the store class that you have not yet created yet. Because of the size of this program, and the relatively complicated way that the objects interact, you should consider modeling your program before heading directly to the code.
2.2. Now create the class that models the manager’s behavior. Remember, a store manager is in charge of ordering more items. The store manager will check to see if any of the items in inventory are depleted. If they are, he will order more items. However, the store manager has other things to do, so he doesn't constantly check the amounts in the inventory. He sleeps in between checks of the inventory for 50ms.
2.3. Now implement the class that models the store and generates the output shown in 2.1. This is the most technical code that you will need to write for this question, think carefully about what data needs to be protected, and which pieces of code need to be synchronized.
2.4 Now create the simulator class with five customers. Once you are convinced that none of the store data is being corrupted, save and submit your work.
Explanation / Answer
public class Inventory
{
private double ItemNumber; //to store Item number
private String ItemName; //to store Item name
private double UnitsInStock; //to store number of units in stock
private double UnitPrice; //to store Unit price
//private double InventoryValue; //to store Inventory value
//private double TotalInventory; //to store Total Inventory value
public Inventory() //constructor with no initial variables
{
this ("", 0.0, 0.0, 0.0);
}
public Inventory (String name, double number, double stock, double price)
{
ItemName = name; //set Item name
ItemNumber = number; //set Item number
UnitsInStock = stock; //set Units in stock
UnitPrice = price; //set Unit price
}
//method to set the Item name
public void setItemName( String name )
{
ItemName = name; //store the Item name
} //end method setItemName
//method to get the Item name
public String getItemName()
{
return ItemName;
} //end method getItemName
//method to set the Item number
public void setItemNumber( int number )
{
ItemNumber = number; //store the Item number
} //end method setItemNumber
//method to get the Item number
public double getItemNumber()
{
return ItemNumber;
} //end method getItemNumber
//method to set the UnitsInStock
public void setUnitsInStock( double stock )
{
UnitsInStock = stock; //store the Units in stock
} //end method setUnitsInStock
//method to get the UnitsInStock
public double getUnitsInStock()
{
return UnitsInStock;
} //end getUnitsInStock
//method to set the UntiPrice
public void setUnitPrice( double price )
{
UnitPrice = price; //store the Unit price
} //end method setUnitPrice
//method to get the UnitPrice
public double getUnitPrice()
{
return UnitPrice;
} //end getUnitPrice
//{
//} //end method setInventoryValue
//public double getInventoryValue()
//{
//} //end method to getInventoryValue
public double getInventoryValue()
{
return UnitPrice*UnitsInStock;
} //end method to getInventoryValue
//method to set TotalInventory
public void setTotalInventory(double value)
{
TotalInventory = total;
}
public double getTotalInventory()
{
return TotalInventory;
} //end method to getTotalInventory
} //end class Inventory
//Inventory Test program part 1
//InventoryTest class
import java.util.Arrays;
public class InventoryTest1
{
public static void main( String args[] )
{
Inventory myInventory = new Inventory();
//displays welcome message
System.out.println ("Store Inventory");
System.out.println();//skips a line
Inventory[] Fruits = new Inventory[5];
Fruits[0] = new Inventory("Apples", 112.1, 7, 12.99);
Fruits[1] = new Inventory("Oranges", 114.1, 25, 14.99);
Fruits[2] = new Inventory("Pineapples", 116.1, 15, 10.99);
Fruits[3] = new Inventory("Persimmons", 102.1, 10, 0.99);
Fruits[4] = new Inventory("Kumquats", 104.1, 5, 9.99);
//For each array element, output value
for (int count = 0; count < Fruits.length; count++ );
System.out.printf("Product Name: %s ", Fruits[count].getItemName());
System.out.printf("Item Number: %4.2f ", Fruits[count].getItemNumber());
System.out.printf("Units In Stock: %.2f ", Fruits[count].getUnitsInStock());
System.out.printf("Unit Price: $%4.2f ", Fruits[count].getUnitPrice());
System.out.printf("Inventory Value: $%4.2f ", Fruits[count].getInventoryValue());
System.out.println(); //blank line to seperate products
}
}//end class InventoryTest1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.