Exemple #1
0
  public static void withdraw() {
    System.out.println("      Withdrawal Menu");
    System.out.println("------------------------------");
    System.out.println("1.             Withdraw");
    System.out.println("2.             Main Menu");
    int choice = input.nextInt();

    if (choice == 1) {
      System.out.println("How much to withdraw?");
      balance -= input.nextDouble();
      System.out.println("Your new balance: " + balance);
      System.out.println("\n");
      withdraw();
    } else {
      process();
    }
  }
Exemple #2
0
 public static void process() {
   int choice = mainMenu();
   switch (choice) {
     case 1:
       checkBalance();
       break;
     case 2:
       deposit();
       break;
     case 3:
       withdraw();
       break;
     case 4:
       exit();
       break;
     default:
       System.out.println("Error: Enter a choice between 1 and 4.");
       System.out.println("\n");
   }
 }
  /** The logic here is specific to our protocol. We parse the string according to that protocol. */
  private Float executeCommand(String commandLine) throws ATMException {
    // Break out the command line into String[]
    StringTokenizer tokenizer = new StringTokenizer(commandLine);
    String commandAndParam[] = new String[tokenizer.countTokens()];
    int index = 0;
    while (tokenizer.hasMoreTokens()) {
      commandAndParam[index++] = tokenizer.nextToken();
    }

    // store the command
    String command = commandAndParam[0];

    // Dispatch BALANCE request without further ado.
    if (command.equalsIgnoreCase(Commands.BALANCE.toString())) {
      return atmImplementation.getBalance();
    }

    // Must have 2nd arg for amount when processing DEPOSIT/WITHDRAW commands
    if (commandAndParam.length < 2) {
      throw new ATMException("Missing amount for command \"" + command + "\"");
    }

    // execute deposit or withdraw command
    try {
      float amount = Float.parseFloat(commandAndParam[1]);
      if (command.equalsIgnoreCase(Commands.DEPOSIT.toString())) {
        atmImplementation.deposit(amount);
      } else if (command.equalsIgnoreCase(Commands.WITHDRAW.toString())) {
        atmImplementation.withdraw(amount);
      } else {
        throw new ATMException("Unrecognized command: " + command);
      }
    } catch (NumberFormatException nfe) {
      throw new ATMException("Unable to make float from input: " + commandAndParam[1]);
    }
    // BALANCE command returned result above.  Other commands return null;
    return null;
  }