Description:Consider a vending machine, which vends soft drinks, that accept coins only. The vending machine allows customers to insert coins, purchase a product and reject inserted coins. In this assignment, you need to build a system to simulate the operations of vending soft drinks in a vending machine. Please read this assignment sheet carefully before starting your work.Typical vending machine consist of these components: A Coin Slot allows customers to insert coins into the machine. It also serves as temporality storage for inserted coins and accumulates the amount of face value. A button to reject all inserted coins. A Coin Changer stores coins for giving change. Change is the money that is returned to the customer who has paid for the product that costs less than the amount that the customer has given. A Soft Drink Slot holds the same products of soft drinks in a column. When a transaction is made, the slot will drop a can of drink into the dispenser.Standard workflow for vending a soft drink in a vending machine is as follow:1. The customer inserts coins into the Coin Slot.2. The customer selects a product.3. If the customer has inserted enough amount of money for the product, the vending machine firstly drops the product and return change (if necessary). Then, all coins in the Coin Slot are moved to the Coin Changer.
Task: Implement this system. Below shows a simple run-down.A partial completed VendingMachine class is provided as followsimport java.util.ArrayList;
public class VendingMachine {private ArrayList coinChanger; private ArrayList coinSlot;private ArrayList softDrinkSlots;
public VendingMachine() { coinChanger = new ArrayList(); coinSlot = new ArrayList();softDrinkSlots = new ArrayList();}
public void addCoinToCoinChanger(Integer c) { coinChanger.add(c);}
public void addSoftDrinkSlot(SoftDrinkSlot s) { softDrinkSlots.add(s);}/* You may add other non-static properties and methods */}
You must not modify the parameter lists of the constructor, addCoinToCoinChanger() and addSoftDrinkSlot() as we will use these methods to initiate the vending machine in test cases evaluation.The SoftDrinkSlot class is provided as followpublic class SoftDrinkSlot {
private String name; private int price;private int quantity;
public SoftDrinkSlot(String name, int price, int quantity) {this.name = name; this.price = price; this.quantity = quantity;}/* You may add other non-static properties and methods */}
You must not modify the parameter lists of the constructor as we will use it in test cases evaluation.The system should be operated via commands (except initiating the vending machine), which is, every time we want to operate the vending machine, an instance of Command object should be created. The Command object will be the interface for the main program to perform actions on the Vending Machine. Different types of commands are handled by different specific classes that inherit the Command class. The Command class is defined as followspublic class Command { public String execute(VendingMachine v, String cmdPart){String result = null;return result;}}
You need to implement 3 commands for the system, namely CmdInsertCoin, CmdRejectCoins and CmdPurchase. These commands should inherit the Command class. Each Command class perform specific actions to the vending machine in the execute() method. The method takes the VendingMachine object and a command content in String as the input parameters. After performing actions, the method should return the result of the command in String.1. CmdInsertCoin Insert a coin2. CmdRejectCoins Reject all coins from Coin Slot3. CmdPurchase Vend a can of soft drink to the customer. When giving a change to the customer, the machine should find the minimum number of coins in the Coin Changer to make the change.For example, CmdInsertCoin inherit Command class and perform actions in the execute() method:public class CmdInsertCoin extends Command {
@Overridepublic String execute(VendingMachine v, String cmdPart) {Integer coin = Integer.valueOf(cmdPart);// Add the coin to Coin Slotreturn /**/;}}
You also need to add handling for the following special cases, but you can assume no more than one special case would happen simultaneously in our test cases. Insufficient amount of money to buy the drink. The drink is out of stock Not enough coins in Coin Changer for giving a particular amount of change to the customer.Notes:1. The listing of coins should be sorted by the face value ascendingly.2. All coins are in dollar unit ($1, $2, $5, $10), no need to deal with cents.3. When preparing coins for a change, the vending machine looks for coins in the Coin Changer only.4. The system should find the minimum number of coins for preparing the change. For example, if one $5 coin is available, then the $5 coin should be selected instead of selecting $1, $2 and $2 coins.5. You are free to add other classes that help you to implement the system. You can upload up to 50 files to Moodle.6. You must NOT declare any static variable in your program as a static variable will disrupt our evaluation system.7. If you failed to pass a test case, Moodle only shows you the last line of expected output and program output.
Reviews
There are no reviews yet.