6. |
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 inventory to arrive.
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.
Create a program to run this simulation. Implement customers and the store manager as threads. Make sure all appropriate methods are synchronized. 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 . . .
Answer:
|