/* Simulate a soft-drink vending machine. */

import chapman.io.*;

public class VendingMachine {
  // The total amount received in cents
  static int amount_received = 0;
  // For getting input
  static StdIn in = new StdIn();

  public static void main(String[] args) {
    for (;;) {
      System.out.println("$" + amount_received/100.0 + " in machine.");
      char c = getInput();
      process(c);
    }
  }

  public static char getInput() {
    for (;;) {
      System.out.print("[C]oin insert, [B]utton press or [M]oney return? ");
      char ch = in.readChar();
      ch = Character.toLowerCase(ch);
      if (ch == 'c' || ch == 'b' || ch == 'm')
        return ch;
      System.out.println("Invalid input.  Please try again.");
    }
  }

  /* Process the input ch */
  public static void process(char ch) {
    switch (ch) {
    case 'c':
      insertCoin();
      break;
    case 'b':
      buttonPress();
      break;
    case 'm':
      moneyReturn();
      break;
    }
  }

  /* A coin is inserted */
  public static void insertCoin() {
    System.out.print("Amount of coin in cents? ");
    int coin = in.readInt();
    if (coin!=10 && coin!=20 && coin!=50 && coin!=100 &&
        coin!=200 && coin!=500 && coin!=1000) {
      System.out.println("Invalid coin");
      return;
    }
    if (amount_received >= 600) {
      System.out.println("Drop $" + coin/100.0 + " coin.");
      return;
    }
    amount_received += coin;
  }

  /* A button is pressed */
  public static void buttonPress() {
    System.out.print("[O]range juice ($6.00) or [C]oke ($5.00)? ");
    char ch = in.readChar();
    ch = Character.toLowerCase(ch);
    if (ch != 'o' && ch != 'c') {
      System.out.println("No such button");
      return;
    }
    if (ch == 'o' && amount_received >= 600) {
      System.out.println("Drop orange juice");
      amount_received -= 600;
      moneyReturn();
    }
    if (ch == 'c' && amount_received >= 500) {
      System.out.println("Drop coke");
      amount_received -= 500;
      moneyReturn();
    }
  }

  /* Money return level is pressed */
  public static void moneyReturn() {
    while (amount_received > 0) {
      if (amount_received >= 1000) {
        amount_received -= 1000;
        System.out.println("Drop $10.0 coin");
        continue;
      }
      if (amount_received >= 500) {
        amount_received -= 500;
        System.out.println("Drop $5.0 coin");
        continue;
      }
      if (amount_received >= 200) {
        amount_received -= 200;
        System.out.println("Drop $2.0 coin");
        continue;
      }
      if (amount_received >= 100) {
        amount_received -= 100;
        System.out.println("Drop $1.0 coin");
        continue;
      }
      if (amount_received >= 50) {
        amount_received -= 50;
        System.out.println("Drop $0.5 coin");
        continue;
      }
      if (amount_received >= 20) {
        amount_received -= 20;
        System.out.println("Drop $0.2 coin");
        continue;
      }
      if (amount_received >= 10) {
        amount_received -= 10;
        System.out.println("Drop $0.1 coin");
        continue;
      }
    }
  }
}

