Question
The gym at the university has purchased some simple vending machines and wants an equally simple system to manage them. Each vending machine holds only
The gym at the university has purchased some simple vending machines and wants an equally simple system to manage them. Each vending machine holds only one kind of item: drinks, crisps, or cookies. Every item held by the machine is identical and has a fixed price. The maximum number of items held by one machine is ten. Vending machines are delivered empty. A vending machine has a power state: it can either be ON or OFF. If a vending machine is currently in the ON state then the following operations are possible: View what kind of item the machine holds, the cost of one item, and how many items are currently in stock. Add a specified quantity of items to the vending machine. Buy an item from the machine, reducing the quantity of items in the machine by 1. If a vending machine is currently in the OFF state, then the only operation that can be performed is to toggle the power to ON.
Task 2.1 Your first task is to model a VendingMachine class in Java. Please carefully read the above description to identify the class attributes and methods. You need to pay careful consideration to the types you use to represent data, what parameters are used in your constructor and methods, along with exactly what setters, getters and other methods you make available. If a method is called which is not available in the current power state (e.g., an attempt to view the number of items and their cost for the particular vending machine when the vending machine is off) then you should print out an error message to the screen and do nothing further. The state of the vending machine will remain the same as before the operation was executed.
Task 2.2 Create a Test class and implement a main method to test your VendingMachine class implementation. You may reuse some code from Lab 1 if you wish. In your Main method create an array that can hold 4 Vending Machine instances. Create all 4 VendingMachine objects. Place these into the array. Use the methods of the VendingMachine class to set the attributes of the machines so they match the following table:
Item-Name | Quantity | Cost
|
Chips | 9 | 5 |
Drinks | 7 | 3 |
Cookies | 2 | 1 |
Drinks | 5 | 2 |
Print the attributes of the vending machines in a nicely-formatted table (it does not need to look identical to the above).
Task 2.3 Another task is to extend the VendingMachine class so that each Vending Machine that is created has a unique automatically generated serial number (e.g., an integer). The first available serial number is 1000 and subsequent serial numbers should be sequential. Serial numbers should be readable externally but not writable. Hint: static is very useful to implement this efficiently. Back in your Test class, test your serial number implementation by verifying that the four machines have the serial numbers 1000, 1001, 1002, and 1003. Check that your printing of the vending machines now includes the serial number, as in:
VM-No | Item-Name | Quantity | Cost |
1000 | Chips | 9 | 5 |
1001 | Drinks | 7 | 3 |
1002 | Cookies | 2 | 1 |
1003 | Drinks | 5 | 2 |
Task 2.4 Recall that no vending machine may hold more than 10 itemsthere is an item quantity limit. Suppose now that larger machines have been bought which may have higher item quantity limits.
Modify your VendingMachine class so that it has two constructors. One constructor should allow the setting of an item quantity limit; a default constructor should set the item quantity limit for the machine to 10. However, there are still limits on the size of the machine: the upper bound for item quantity limits is 50. Once set for a particular machine, an item quantity limit should not be modifiable (think about an appropriate modifier for the attribute).
The use of item quantity limits needs to be handled in your program. If a user tries to create a machine with an item quantity limit higher than 50, or tries to put so many items into the machine that its limit is reached, there is an error. Your program should throw an IllegalArgumentException instead of processing the users attempt. This is a standard exception which indicates that a method or constructor has been passed an illegal or inappropriate argument.
If you dont know exceptions, you should look it up in the Java API. Do a search for java throw exceptions and use the official Oracle page. Also, look up the IllegalArgumentException.
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