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).
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 managers 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.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started