Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You have to run all of this as one program and run it. and also give explanation for this code. The goal of this lab

image text in transcribedimage text in transcribed

You have to run all of this as one program and run it. and also give explanation for this code.

The goal of this lab is to prepare you to model classes after reading and analysing a version of a customer brief who are looking to hire you to implement their system. Carefully read the following brief and think about how you would implement it in Java. This is a simple prototype system and you are not required to implement any more than is specifically asked for. Do not get misled by your own beliefs about how the system should work! 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 imple- mentation. 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: Cost 5 Item-Name Chips Drinks Cookies Drinks Quantity 9 7 2 5 3 1 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 1000 Chips 1001 Drinks 1002 Cookies 1003 Drinks 9 7 2 5 Cost 5 3 1 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 cre- ate 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 user's attempt. This is a standard excep- tion which indicates that a method or constructor has been passed an illegal or inappropriate argument. If you don't know erceptions, 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago