Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm done my code and have everything done, except for the part where i have to use a multi dimensional array in my program or two dimensional arrays, if you have any suggestions please post it as a solution thanks in advance.

What I have tried:

/*Skandan Vecham
 *2018-11-08
 * Mini Bank Application
 */
import java.util.*;
public class ATM {

    public static void main(String[] args) {
        Scanner input=new Scanner (System.in);
        //variables that store account number and pin
        String acctNum,pin="";
        // original balance which helps decide if the account information is true or false
        String originalBalance="error";
        //variables to store deposit and withdrawal amounts
        double depositAmount, withdrawAmount=0;
        // main menu option
        int mainMenu=0;
        // counter to lock user out
        int counter=3;
        // loop for user input of account number and pin
        do
        {
            System.out.println("Enter the account number:");
            acctNum=input.next();
            System.out.println("Enter the pin:");
            pin=input.next();
            originalBalance=validation(acctNum,pin);
            counter--;
            // if statements to decide if user has tried to many times
            // if statement that kicks user out of program
            if(counter==0)
            {
                System.out.println("\nMaximum amount of attempts reached\nPlease visit a branch or call the bank at your convinence");
                System.exit(0);
            }
            // if statement that breaks the loop and gets on with the program provided that the information given is true
            else if(!(originalBalance.equals("error")))
            {
                break;
            }
        }while(originalBalance.equals("error"));
        // converts balance which is in string to double for calculations
        double balance=Double.parseDouble(originalBalance);
        // do while loop to repeat the mainMenu multiple times
        do
        {
            mainMenu=mainMenuOption();
            // if statements that take user to desired place
            // if statement 1 takes user to withdrawal
            if (mainMenu==1)
            {
                //Asks how much money the user would like to withdraw, and if withdraw amount is 
                // greater than current balance it will not process the request
                System.out.println("How much money would you like to withdraw");
                withdrawAmount=input.nextDouble();    
                // while loop repeats the process
                while(withdrawAmount>balance)
                {
                    System.out.println("Insufficient Funds\nBalance= $"+balance+"\nTry again");
                    withdrawAmount=input.nextDouble();    
                }
                balance=withdraw(withdrawAmount,balance);
            }
            // opens deposit method and deposits users choice of money
            else if (mainMenu==2)
            {
                System.out.println("How much money would you like to deposit");
                depositAmount=input.nextDouble();
                balance=deposit(depositAmount,balance);
            }
            // method that displays balance
            else if(mainMenu==3)
            {
                displayBalance(balance);
            }
            // method that ends the program
            else if(mainMenu==4)
            {
                System.out.println("Thank you for visiting the Bank of Skandanavia");
                break;
            }
        }while(mainMenu!=4);
    
}
    // shows the balance of the account
    public static void displayBalance(double balance) {
        //printf $%.2f\n rounds decimals
        System.out.printf("Account Balance= $%.2f\n",balance);
    }
// withdraw method which contains  the withdrawAmount and balance
    public static double withdraw(double withdrawAmount, double balance) 
    {
        //printf $%.2f\n rounds decimals
        double newBalance=0;
        newBalance=balance-withdrawAmount;
        System.out.printf("Balance= $%.2f\n",newBalance);
        return newBalance;
    }
// deposit method that deposits userinput into their accounts
    public static double deposit(double depositAmount, double balance) 
    {
        double newBalance=0;
        newBalance=balance+depositAmount;
        System.out.printf("Balance= $ %.2f\n",newBalance);
        return newBalance;
    }


// main menu method, directs user in the desired direction    
    public static int mainMenuOption() {
        Scanner input=new Scanner(System.in);
        int userInput=0;
        System.out.println("\nWhat would you like to do today?");
        System.out.println("1. withdrawal");
        System.out.println("2. Deposit");
        System.out.println("3. Bank Balance");
        System.out.println("4. Exit");
        userInput=input.nextInt();
        return userInput;
    }
// validation of the users information
    public static String validation(String acctNum,String pin) {
        // array that holds account number, account pin, and account balance
        String accountNums[]= {"1234567","2345678","3456789","456789","76589"};
        String accountPin[]= {"1234","2345","3456","4567","5678"};
        double accountBal[]= {56.8,89.6,97.3,110.9,996.8};
        // result variable helps the original balance in the main method to decide if user input is false
        String result="";
        // to convert double account balance to string so it can return to main method
        String accountBalS="";
        int counter=0;
        // for loop to check if account information matches arrays 
        for(int x=0;x<5;x++)
        {
            // actual conversion of double to string
            accountBalS=Double.toString(accountBal[x]);
            counter+=1;
            if((accountNums[x].equals(acctNum))&&(accountPin[x].equals(pin)))
            {
                result=accountBalS;
                return result;
            }
            // if account number or pin is invalid then this if statement will run
            else if(!(accountNums[x].equals(acctNum)||(accountPin[x].equals(pin)))&&counter==5)
            {
                System.out.println("Account number or pin is invalid try again");
                result="error";
                return result;
            }
        }
        return result;
    }
}
Posted
Updated 8-Nov-18 4:40am
Comments
CPallini 8-Nov-18 9:02am    
Why multidimensional arrays? An array of 'account' objects would fit nicely.
Member 14045850 8-Nov-18 9:12am    
It is required for my assignment
CPallini 8-Nov-18 16:57pm    
Your assignment is bogus.
Anyway, I understand, you have to comply.

Looks like you get to do some re-factoring for your account methods...

Java
String AccountInfo[][] = {
  {"1234567","1234", "56.8" },
  {"2345678", ""2345", "89.6"},
  {"3456789", "3456", "97.3"},
  {"456789", "4567", "110.9"},
  {"76589", "5678", "996.8"}
}
 
Share this answer
 
v2
Comments
Member 14045850 8-Nov-18 10:45am    
Thanks, (supposed to be a 5 start)
CPallini 8-Nov-18 16:56pm    
Looks like you a have a dimension too much.
MadMyche 8-Nov-18 17:36pm    
yep I do
Member 14045850 8-Nov-18 17:43pm    
Hey guys thanks a lot for your help, i just handed in the assignment, and i think I'll do prty good.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900