import java.lang.*;
import java.io.*;

public class ATM {
  public ATM() {
  }

  public static long getKeyboardLong(String s) {
    BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
    String temp_str = new String();
    long temp;

    System.out.print(s);
    try {
      temp_str = b.readLine();
    } catch (IOException e) {
    }

    return Long.parseLong(temp_str);
  }

  public static void main(String[] args) {
    Server server = new Server();
    TransactionReport t = new TransactionReport();

    boolean running;
    long id, pin, temp, accountno, othaccount, amount;
    long ayear, amon, aday, byear, bmon, bday;

    System.out.print("ATM program by Anthony Mills, Dale Rossetti, and Brian Turner\n\n");

    running = true;
    while( running ) {
      id = getKeyboardLong("Please enter your card number (no spaces) or 0 to quit: ");

      if( id == 0 ) {
        running = false;
      } else {
        pin = getKeyboardLong("\nPlease enter your PIN: ");
        if( (id / 1000000 / 100000) == 9 ) { // user is a manager
          if( !server.validManager(id, pin) ) {
            System.out.print("Manager login failed.\n\n");
          } else {
            temp = getKeyboardLong("\nEnter 1 for Print Report, 2 for Create Account: ");
            switch((int)temp) {
            case 1:
              ayear = getKeyboardLong("\nBeginning year: ") - 1900;
              amon = getKeyboardLong("Beginning month: ");
              aday = getKeyboardLong("Beginning day: ");
              byear = getKeyboardLong("\nEnding year: ") - 1900;
              bmon = getKeyboardLong("Ending month: ");
              bday = getKeyboardLong("Ending day: ");
              server.search(ayear, amon, aday, byear, bmon, bday);
              break;
            case 2:
              server.createAccount(getKeyboardLong("\nEnter new card number: "), getKeyboardLong("\nEnter new PIN: "));
              break;
            }
          }
        } else { // user is a customer
          temp = getKeyboardLong("\nEnter 1 for Withdraw, 2 for Deposit, 3 for Pay Bill, and 4 for Transfer: ");
          accountno = getKeyboardLong("\nEnter 1 for Savings, 2 for Chequing, 3 for Credit Card: ");
          if( temp == 4 ) {
            othaccount = getKeyboardLong("\nDestination Account: 1 for Savings, 2 for Chequing, 3 for Credit Card: ");
          } else {
            othaccount = 0;
          }
          amount = getKeyboardLong("\nEnter amount in cents: ");

          server.processTransaction(temp, id, pin, accountno, othaccount, amount);
        }
      }
    }
  }
}