Beispiel #1
0
  @Override
  public User logIn(long accountNumber, short pin) {
    // TODO Auto-generated method stub

    // create a session user and list the list of users
    User currentUser = new User();
    UserDatabase udb = new UserDatabase();
    List<User> listOfUsers = udb.getUsers();
    boolean looping = true;
    // if the attempted user is in the list, check his account number which
    // is unique and pin

    for (User user : listOfUsers) {
      // if both match, we set the session user and return it
      if (user.getAccountNumber() == accountNumber) {
        if (user.getPin() == pin) {
          currentUser = user;
          System.out.println("Logged in successfully.");
          looping = false;
          break;
        } else {
          System.out.println("Didn't log in, bad pin.");
        }

      } else {
        System.out.println("Didn't log in, bad user account.");
      }
    }
    while (looping) ;
    return currentUser;
  }
Beispiel #2
0
  /**
   * Method that displays admin's or user's menu
   *
   * @param user - check user's role to see whether admin's or user's menus are displayed
   */
  public void callAppropriateMenu(User user) {

    Scanner in = null;
    try {
      in = new Scanner(System.in);

      // get the list of users
      UserDatabase udb = new UserDatabase();
      List<User> listOfUsers = udb.getUsers();

      // if role == 1 (admin)
      if (user.getRole() == 1) {
        boolean isFinished = false;

        // keep the menu open untill we are finished
        while (!isFinished) {
          System.out.println(
              "Hello admin.\nPress 1 to check the state of bills in ATM.\nPress 2 to list all users.\nPress 3 to edit a user.\nPress 4 to add a user.\nPress 5 to add bills.\nPress 6 to leave");
          String choice = in.nextLine();

          // display the state of bills
          if (choice.equals("1")) {
            System.out.println(new Atm().toString());

            // display all users
          } else if (choice.equals("2")) {
            for (User u : listOfUsers) {
              System.out.println(u.toString());
            }

            // attempt to edit a user - this method is buggy thanks
            // to the stupid scanner class
          } else if (choice.equals("3")) {
            System.out.println("Enter the account id of the user you want to edit: ");

            // display all users
            for (User u : listOfUsers) {
              System.out.println(u.toString());
            }

            // select a user's unique id (account number)
            String id = in.nextLine();

            // selected user becomes active for editing, calling
            // method editUser()
            AdminPrivileges.editUser(Long.valueOf(id));

            // create and add user
          } else if (choice.equals("4")) {

            // create new user calling the admins method
            User newUser = AdminPrivileges.createUser();

            // if we successfully filled user's fields, proceed to
            // add him to database calling the addUserToDb
            if (newUser != null) {
              AdminPrivileges.addUserToDb(newUser);
            } else {
              System.out.println("Something went wrong.");
            }

            // add bills to atm machine if needed
          } else if (choice.equals("5")) {
            System.out.println("Press 1 to add 10s, 2 to add 20s, 3 to add 50s, 4 to add 100s: ");
            String billChoice = in.nextLine();

            // add 10s
            if (billChoice.equals("1")) {
              System.out.println("How many bills do you want to add?");
              String num = in.nextLine();
              AdminPrivileges.addTens(Integer.valueOf(num));

              // add 20s
            } else if (billChoice.equals("2")) {
              System.out.println("How many bills do you want to add?");
              String num = in.nextLine();
              AdminPrivileges.addTwenties(Integer.valueOf(num));

              // add 50s
            } else if (billChoice.equals("3")) {
              System.out.println("How many bills do you want to add?");
              String num = in.nextLine();
              AdminPrivileges.addFifties(Integer.valueOf(num));

              // add 100s
            } else if (billChoice.equals("4")) {
              System.out.println("How many bills do you want to add?");
              String num = in.nextLine();
              AdminPrivileges.addHundreds(Integer.valueOf(num));
            }

            // shut down the loop = program
          } else if (choice.equals("6")) {
            isFinished = true;
          }
        }

        // if the user has role 0 (regular user)
      } else if (user.getRole() == 0) {

        // display user's balance
        System.out.println(
            "Hello "
                + user.getFirstName()
                + ".\nYour account state is: "
                + user.getAccountBalance());
        System.out.println("Would you like to withdraw 10, 20, 50, 100?");

        // user has to enter 10 20 50 or 100
        String amount = in.nextLine();
        if (amount.equals("10")) {

          // if atm can pay him in 10s, proceed
          if (Atm.getNumOfTens() > 0) {

            // deduct funds from user's account
            BasicPrivileges.withdrawAmount(user, 10);
            // deduct bills from the atm
            Atm.setNumOfTens(Atm.getNumOfTens() - 1);
          } else {
            System.out.println(
                "This atm machine doesn't have those bills anymore, please find another machine.");
          }
        } else if (amount.equals("20")) {

          // if user can pay him in 20s, proceed
          if (Atm.getNumOfTwenties() > 0) {
            // deduct funds from user's account
            BasicPrivileges.withdrawAmount(user, 20);

            // deduct bills from the atm
            Atm.setNumOfTwenties(Atm.getNumOfTwenties() - 1);

            // if atm didn't have twenties, attempt to pay in 10
            // bills
          } else if (Atm.getNumOfTens() > 1) {

            BasicPrivileges.withdrawAmount(user, 20);

            // this time deduct two bills from the atm instead of
            // 1....
            Atm.setNumOfTens(Atm.getNumOfTens() - 2);
          } else {
            System.out.println("This machine cannot pay you, please find another machine.");
          }

          // attempt to pay in 50s
        } else if (amount.equals("50")) {

          // if there are 50s, deduct
          if (Atm.getNumOfFifties() > 0) {
            BasicPrivileges.withdrawAmount(user, 50);
            Atm.setNumOfFifties(Atm.getNumOfFifties() - 1);

            // if there are no 50s, attempt to pay in 20+20+10
          } else if (Atm.getNumOfTens() > 0 && Atm.getNumOfTwenties() > 1) {
            BasicPrivileges.withdrawAmount(user, 50);

            // update bills count
            Atm.setNumOfTens(Atm.getNumOfTens() - 1);
            Atm.setNumOfTwenties(Atm.getNumOfTwenties() - 2);
          } else {
            System.out.println("This machine cannot pay you, please find a different machine.");
          }

        } else if (amount.equals("100")) {

          // attempt to pay in 100 bill
          if (Atm.getNumOfHundreds() > 0) {

            // deduct funds and bill
            BasicPrivileges.withdrawAmount(user, 100);
            Atm.setNumOfHundreds(Atm.getNumOfHundreds() - 1);

            // if there are no 100 bills, attempt with 2x50
          } else if (Atm.getNumOfFifties() > 1) {
            BasicPrivileges.withdrawAmount(user, 100);
            Atm.setNumOfFifties(Atm.getNumOfFifties() - 2);

            // if there are no 50 bills, attempt with 5x20
          } else if (Atm.getNumOfTwenties() > 4) {
            BasicPrivileges.withdrawAmount(user, 100);
            Atm.setNumOfTwenties(Atm.getNumOfTwenties() - 5);

            // if there are no 20, attempt with 10x10
          } else if (Atm.getNumOfTens() > 9) {
            BasicPrivileges.withdrawAmount(user, 100);
            Atm.setNumOfTens(Atm.getNumOfTens() - 10);

            // i understand that there are different combinations
            // but at this hour i'm dead and can't develop the
            // algorithm....

          } else {
            System.out.println("This machine cannot pay you, please find another machine.");
          }
        } else {
          System.out.println("You have to enter 10, 20, 50 or 100.....");
        }
        System.out.println("Your balance is " + user.getAccountBalance());
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      in.close();
    }
  }