Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [code]import java.util.Scanner;
- class ATM {
- public static void main(String[] args) {
- //Declare and/or initialize variables used throughout
- int clear;
- int x = -1;
- int loop = 1;
- int menuChoice = 0;
- int arrayCount = 0;
- String loginID = null;
- user[] userArray = new user[10];
- Scanner scanner = new Scanner(System.in);
- //Menu loop
- while (loop != 0) {
- //Printed menu for user
- System.out.println("Please make a selection: ");
- System.out.println("1 - Register");
- System.out.println("2 - Login");
- System.out.println("3 - Deposit");
- System.out.println("4 - Withdraw");
- System.out.println("5 - Check Balance");
- System.out.println("6 - Log Out");
- //Menu prompt for user input
- System.out.print("Make your selection (1-6): ");
- menuChoice = scanner.nextInt();
- //Register
- if (menuChoice == 1) {
- //Conditional to check for space in array
- if (arrayCount < 10) {
- //Creates new user obj, increments array position for next, reads user input for id, pin
- userArray[arrayCount] = new user();
- System.out.println("Please input your user ID:");
- userArray[arrayCount].id = scanner.next();
- System.out.println("Please input 4 digit PIN: ");
- userArray[arrayCount].pin = scanner.nextInt();
- //arrayCount incrementer to keep track of # of users
- arrayCount++;
- }
- //Warning of full user array
- else {
- System.out.println("Too many users!");
- }
- }
- //Login
- else if (menuChoice == 2) {
- //Clears all loginTokens
- clear = 0;
- while (clear < arrayCount) {
- userArray[clear].loginToken = false;
- clear++;
- }
- //User input for login, and PIN
- System.out.println("Please input your user ID (case insensitive): ");
- loginID = scanner.next();
- System.out.println("Please input your PIN: ");
- int loginPin = scanner.nextInt();
- // Uses user input to check id string input versus stored user id to find the
- //array position of the user
- x = checkID(arrayCount, loginID, userArray);
- // Checks the input PIN against the pin in stored instance variable in the
- // same array position as the id found in checkID
- userArray[x].loginToken = checkPin(x, loginPin, userArray);
- }
- //Deposit
- else if (menuChoice == 3) {
- //Retrieving array position of user
- x = checkID(arrayCount, loginID, userArray);
- if (x == -1 || userArray[x].loginToken == false) {
- System.out.println("Please login.");
- }
- if (x > -1 && userArray[x].loginToken == true) {
- System.out.print("How much would you like to deposit?: ");
- int deposit = scanner.nextInt();
- userArray[x].balance = userArray[x].balance + deposit;
- System.out.println("Your new balance is: $" + deposit);
- }
- }
- //Withdraw
- else if (menuChoice == 4) {
- if (x == -1 || userArray[x].loginToken == false) {
- System.out.println("Please login.");
- }
- if (x > -1 && userArray[x].loginToken == true) {
- System.out.print("How much would you like to withdraw?: ");
- int withdraw = scanner.nextInt();
- if (withdraw < userArray[x].balance) {
- userArray[x].balance = userArray[x].balance - withdraw;
- System.out.println("Your new balance is $" + userArray[x].balance);
- }
- if (withdraw > userArray[x].balance) {
- System.out.println("You have insufficient funds.");
- }
- }
- }
- //Check Balance
- else if(menuChoice==5){
- if (x == -1 || userArray[x].loginToken == false) {
- System.out.println("Please login.");
- }
- if (x > -1 && userArray[x].loginToken == true) {
- System.out.println("Your balance is: $"+ userArray[x].balance);
- }
- }
- //Log Out
- else if (menuChoice==6) {
- //Clears all loginTokens
- clear = 0;
- while (clear < arrayCount) {
- userArray[clear].loginToken = false;
- clear++;
- }
- }
- //Catches invalid entries at the menu
- else {
- System.out.println("Please make a valid selection!");
- }
- }
- }
- //Uses temporary variable idTemp to find array position of login credentials, if available.
- static int checkID(int arrayCount, String idTemp, user[] userArray){
- int x = 0;
- //Loops through all user objects to find match
- while(x < arrayCount) {
- if(userArray[x].id.equals(idTemp)) {
- return x;
- }
- if(idTemp != userArray[x].id){
- x++;
- }
- }
- if(x==userArray.length){
- System.out.println("User was not found!");
- }
- return x;
- }
- //Accepts the int return of checkID to compare user input pin against the stored pin at that array position.
- static boolean checkPin(int x, int inputPin, user[] userArray) {
- boolean pinBool = false;
- if(userArray[x].pin == inputPin){
- System.out.println("User " + userArray[x].id + " has been successfully logged in!");
- pinBool = true;
- }
- else{
- System.out.println("There was an error!");
- }
- return pinBool;
- }
- }
- //User object
- class user{
- String id;
- int pin;
- int balance;
- boolean loginToken = false;
- };
- [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement