Vending machine

This program receives three commands from keyboard input which represents the necessary actions to buy a item. They are:

  • insertCoin - insert $0.25 into the machine. Each item has a cost of $0.50.
  • returnCoin - return the inserted money to the user.
  • vendItem <num> - ask the machine to deliver an specified item <num>.

To finish the program press "CTRL+D".

<source lang="java"> package vending;

import java.io.*; import java.util.StringTokenizer;

public class VendingMachine { final private int COIN = 25; private int totValue; private int currValue; private Dispenser d;

public VendingMachine() { totValue = 0; currValue = 0; d = new Dispenser(); }

/** * This method simulates that a new coin of 25 was inserted. It returns the * current value in cents in the machine. * * @param - * no parameter required. * @return - the current value in cents. */ public int insertCoin() { currValue += COIN; return currValue; }

/** * This method simulates the return of the coins. * * @param - * no parameter required. * @return - the current value in cents, or zero when no coin was inserted. */ public int returnCoin() { int value = currValue;

if (currValue != 0) currValue = 0;

return value; }

/** * This method simulates the request for a given item. * * @param the * number of a given item. Valid items are numbered from 1 to 20. * * @return the current value remaining in cents after on success. On fail, * one of the exceptions below is thrown. The priorities of the * exceptions are: *

*

    *
  1. NoCoinsException; *
  2. InvalidItemException; *
  3. UnavailableItemException; and *
  4. NotEnoughtCreditException. *

* * @throws NoCoinsException * when no coin has been inserted. * @throws InvalidItemException * when the selected item is invalid. * @throws UnavailableItemException * when the selected item is valid but is currently unavailable. * @throws NotEnoughtCreditException * when the selected item is invalid but the credit is not * enought to buy it. */ public int vendItem(int selection) { int expense;

expense = d.dispense(currValue, selection); totValue += expense; currValue -= expense;

return currValue; }

/** * These method simulates the behaviour of the vending machine. Actions like * insert a coin, ask to return the coins and ask to buy a item are * performed by calling the operations:

*

    *
  • insertCoin; *
  • returnCoin; and *
  • vendItem <item_number>. <\UL> *

    * The operations can be entered by keyboard or by text file in any order. * * After executing each one of these operations, a message is displayed to * the user, indicanting whether it was executed successfully or not. */ static public void main(String args[]) throws Exception { BufferedReader drvInput; String tcLine = new String(); String methodName = new String(); VendingMachine machine = new VendingMachine(); if (args.length < 1) // reading operations from keyboard drvInput = new BufferedReader(new InputStreamReader(System.in)); else // reading operations from a text file drvInput = new BufferedReader(new FileReader(args[0])); System.out.println("VendingMachine ON"); // for keyboard input - press CTRL+C to stop the simulation while ((tcLine = drvInput.readLine()) != null) { StringTokenizer tcTokens = new StringTokenizer(tcLine); if (tcTokens.hasMoreTokens()) methodName = tcTokens.nextToken(); int value = 0; // A coin is inserted if (methodName.equals("insertCoin")) { value = machine.insertCoin(); System.out.println("Current value = " + value); } // Coins are returned (if any) else if (methodName.equals("returnCoin")) { value = machine.returnCoin(); if (value == 0) System.err.println("No coins to return"); else System.out.println("Take your coins"); } // The sold item is delivered to the user (if the item is // available and the amount is enought to buy it) else if (methodName.equals("vendItem")) { Integer selection = new Integer(tcTokens.nextToken()); try { value = machine.vendItem(selection.intValue()); System.out.println("Take your item!!!"); System.out.println("Current value = " + value); } catch (NoCoinsException nce) { System.out.println("No coins inserted!!!"); } catch (InvalidItemException ite) { System.out.println("The requested item (" + selection + ") is invalid!!!"); } catch (UnavailableItemException uie) { System.out.println("The requested item (" + selection + ") is unavailable!!!"); } catch (NotEnoughtCreditException nece) { System.out.println("Current value not enought to buy item " + selection + "."); } } else { System.out.println("Invalid operation!!!"); } } System.out.println("VendingMachine OFF"); } } // class VendingMachine </source> <source lang="java"> package vending; public class Dispenser { final private int MINSEL = 1; // First valid item final private int MAXSEL = 20; // Last valid item final private int VAL = 50; // The current valid and available items private int[] availSelectionVals = { 1, 2, 3, 4,/*5,*/ 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, /*18,*/19 /*,20*/}; /** * Simulates the behaviour of the dispenser component of a vending machine. * A given item is dispensed when there is enought credit to buy it and the * item is valid and available. * * @param credit - * the current value in cents in the coin compartment. A value of * 50 is necessary to buy any item. * * @param sel - * the selected item. A valid item is a integer number x, such * that 1 <= x <= 20. * * @return the value of the item (expense). Currently is always 50. * * @throws NoCoinsException * when no coin has been inserted. * @throws InvalidItemException * when the selected item is invalid. * @throws UnavailableItemException * when the selected item is valid but is currently unavailable. * @throws NotEnoughtCreditException * when the selected item is invalid but the credit is not * enought to buy it. */ public int dispense(int credit, int sel) { int val = 0; if (credit == 0) // no coins inserted throw new NoCoinsException(); else if ((sel < MINSEL) || (sel > MAXSEL)) // selected item out of range throw new InvalidItemException(); else if (!available(sel)) // valid but unavailable item selected throw new UnavailableItemException(); else { val = VAL; if (credit < val) { // valid and available item selected but not enought credit int value = val - credit; val = 0; throw new NotEnoughtCreditException("More " + value + " is required"); } else { // sale performed successfully return val; } } } private boolean available(int sel) { for (int i = 0; i < availSelectionVals.length; i++) if (availSelectionVals[i] == sel) return true; return false; } } </source> <source lang="java"> package vending; /** * Exception class for Dispenser class when * no coins has been inserted to perform the sale. */ public class InvalidItemException extends RuntimeException { /** * Construct this exception object. */ public InvalidItemException() { super(); } /** * Construct this exception object. * @param message the error message. */ public InvalidItemException(String message) { super(message); } } </source> <source lang="java"> package vending; /** * Exception class for Dispenser class when * no coins has been inserted to perform the sale. */ public class NoCoinsException extends RuntimeException { /** * Construct this exception object. */ public NoCoinsException() { super(); } /** * Construct this exception object. * @param message the error message. */ public NoCoinsException(String message) { super(message); } } </source> <source lang="java"> package vending; /** * Exception class for Dispenser class when * no coins has been inserted to perform the sale. */ public class NotEnoughtCreditException extends RuntimeException { /** * Construct this exception object. */ public NotEnoughtCreditException() { super(); } /** * Construct this exception object. * @param message the error message. */ public NotEnoughtCreditException(String message) { super(message); } } </source> <source lang="java"> package vending; /** * Exception class for Dispenser class when * no coins has been inserted to perform the sale. */ public class UnavailableItemException extends RuntimeException { /** * Construct this exception object. */ public UnavailableItemException() { super(); } /** * Construct this exception object. * @param message the error message. */ public UnavailableItemException(String message) { super(message); } } </source>