Hi all I'm trying to loop through an array using a password of 4 letters and an ID of 4 int numbers.
Thus far I can create the two 'keys' BUT I can't get them to search for the account on the arraylist I have despite it's creation. I really don't know how to fix this problem since the only way seems to be to just call for an 'If' statement or switch.
Here is where I have the code set up:
int acctID;
        int depositingAmount;
        //System.out.println("Input your ID and then your password to deposit money.");
        String inputID = JOptionPane.showInputDialog("Please input your ID to begin depositing money.");
        acctID = Integer.parseInt(inputID);
        String inputPassword = JOptionPane.showInputDialog("Please input your password to verify your account.");
        // int depositAmount;
        // do {
        for (int i = 0; i < bankAccounts.size(); i++)//Loops through accounts in my arraylist.
        {
            if (acctID == bankAccounts.get(i).getAccountId() && inputPassword == bankAccounts.get(i).getPassword())//If ID and password work are true then it goes in here.
            {
                String depositAmount = JOptionPane.showInputDialog("Please input how much money you want to "
                        + "input");//An here is where you would be able to spit out how much money to deposit.
                depositingAmount = Integer.parseInt(depositAmount);
                bankAccounts.get(i).deposit(depositingAmount);
                break;
            }
        }
Like you can see the loop in theory is suppose to go "Oh okay lets me start going though all of the accounts in the list until I get to yours" and when it finds it "oh hey I found your account, here let me give you the deposit feature". Sorry if I tried to simplify it too much in this paragraph its just that this problem is frustrating me too no end because I know that if I can solve this then the rest of my project is done. I have a withdraw feature also but that's basically the same thing but playing with different methods on the same class.
EDIT: Here are the constructors
public  BankAccount() {//ADDED TWO PARAMETERS
        //NO ARGUMENT CONSTRUCTOR
        accountId = (int) (Math.random() * 9000) + 999;
        //RANDBETWEEN
        setAccountBalance(0);
        Random passcode = new Random();
        //char charcs = (char)(passcode.nextInt(26) + 'a');
    //Added string. 
        String chars = "abcdefghijklmnopqrstuvwxyz";
        int length = chars.length();
        for (int i = 0; i < 4; i ++){
            password += chars.split("")[ (int) (Math.random() * (length - 1)) ];}
    }
    public  BankAccount(double accountBalance) { // ONE ARGUMENT CONSTRUCTOR
        accountId = (int) (Math.random() * 9000) + 1000;
        setAccountBalance(accountBalance);
        //Random passcode = new Random();
        //password = (String) (passcode.nextInt(26)) + 'a';
        //NEED A VARIABLE TO PASS IN
        //4 Digit password
        String chars = "abcdefghijklmnopqrstuvwxyz";
        int length = chars.length();
        for (int i = 0; i < 4; i ++){
            password += chars.split("")[ (int) (Math.random() * (length - 1)) ];}
    }
An here are the getters..
public int getAccountId() {
        return accountId;
    }
public String getPassword() {
        return password;
    }
Also here are the fields I created..
private int accountId;
    private double accountBalance;
    private static double annualInterestRate = 0.045;
    private static java.util.Date dateCreated = new java.util.Date();
    private String name;
    private String password = "";
Here is where I stand with the If statement
if (acctID == bankAccounts.get(i).getAccountId() && inputPassword.equals(bankAccounts.get(i).getPassword()))
I've seen the other thread that's been posted has a similar question but their answer of just putting .equals() is not enough. Unless i'm stuppose to just take out bankAccounts from the line.
EDIT 2: The entire code.
import java.util.Random;
public class BankAccount {
    private int accountId;
    private double accountBalance;
    private static double annualInterestRate = 0.045;
    private static java.util.Date dateCreated = new java.util.Date();
    private String name;
    private String password = "";
    public  BankAccount() {//ADDED TWO PARAMETERS
        //NO ARGUMENT CONSTRUCTOR
        accountId = (int) (Math.random() * 9000) + 999;
        //RANDBETWEEN
        setAccountBalance(0);
        Random passcode = new Random();
        //char charcs = (char)(passcode.nextInt(26) + 'a');
    //Added string. 
        String chars = "abcdefghijklmnopqrstuvwxyz";
        int length = chars.length();
        for (int i = 0; i < 4; i ++){
            password += chars.split("")[ (int) (Math.random() * (length - 1)) ];}
    }
    public  BankAccount(double accountBalance) { // ONE ARGUMENT CONSTRUCTOR
        accountId = (int) (Math.random() * 9000) + 1000;
        setAccountBalance(accountBalance);
        //Random passcode = new Random();
        //password = (String) (passcode.nextInt(26)) + 'a';
        //NEED A VARIABLE TO PASS IN
        //4 Digit password
        String chars = "abcdefghijklmnopqrstuvwxyz";
        int length = chars.length();
        for (int i = 0; i < 4; i ++){
            password += chars.split("")[ (int) (Math.random() * (length - 1)) ];}
    }
    public int getAccountId() {
        return accountId;
    }
    public double getAccountBalance() {
        return accountBalance;
    }
    public void setAccountBalance(double accountBalance) {
        this.accountBalance = accountBalance;
    }
    public double getAnnualInterestRate() {
        return annualInterestRate;
    }
    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }
    public void withdraw(double amountToWithdraw) {
        if (amountToWithdraw > accountBalance) {
            //putting together withdraw make sure to have a validation to stop ppl from going over.
            System.out.println("Sorry you cannot exceed the amount you have in your balance.");
        }
        else {
        accountBalance = accountBalance - amountToWithdraw;
        }
    }
    public void deposit(double amountToDeposit) {
        if (amountToDeposit < 0) {
            //deposits cannot be negative 
        System.out.println("You cannot deposit a negative amount of dallars.");
        }
        else {
        accountBalance = accountBalance + amountToDeposit;
        }
    }
    public double getMonthlyInterestRate() {
        int MonthsInAYear = 12;//method variable is small BUT just need to get results
        double monthlyInterestRate = annualInterestRate / MonthsInAYear;
        return monthlyInterestRate;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String toString(){
        String bankInfo = " Password: " + getPassword() + " Account ID: " + getAccountId();
        return bankInfo;
    }
}
public class CurrentAccount extends BankAccount{
    private static float overdraftLimit = 100;
    CurrentAccount(){
        super();
    }
    public static float getOverdraftLimit() {
        return overdraftLimit;
    }
    // MAKE A SETTER FOR THE OVERDRAFTLIMIT
    public void withdraw(double amountToWithdraw){
        if (amountToWithdraw <= getAccountBalance()+overdraftLimit) {
            setAccountBalance(getAccountBalance() - amountToWithdraw);
        }
        else {
            System.out.println("Error this transaction has been cancelled.");
        }
    }
}
public class SavingsAccount extends BankAccount{
    private static double interest;
    public SavingsAccount (){
        super();
        interest = 0;
    }
    public void addInterest(){
        interest = getAccountBalance() * getMonthlyInterestRate(); //GETTING ZEROS FROM ACCOUNTBALANCE
        //CHANGE getAccountBalance with some other initial payment.
        super.deposit(interest);
    }
    public double getInterest() {
        return interest;
    }
    public void setInterest(float interest) {
        this.interest = interest;
    }
}
public class Bank {
    Scanner input = new Scanner(System.in);
    Scanner Restart = new Scanner(System.in);
    private static ArrayList<BankAccount> bankAccounts; // MADE THIS STATIC
    public Bank() {
        bankAccounts = new ArrayList<BankAccount>();
    }
    public void openAccount() {
        Object[] possibilities = {"Regular Account", "Savings Account", "Checking Account"};
        Object inputValue = JOptionPane.showInputDialog(null, "Please pick an option from this menu: ",
                    "input", JOptionPane.INFORMATION_MESSAGE, null, possibilities, possibilities [0]);
            if (inputValue.equals("Regular Account")) {
                // Make a regular acct
                String inputName = JOptionPane.showInputDialog(null, "Please input your name");
                BankAccount newBankAccount = new BankAccount();
                newBankAccount.setName(inputName);
                bankAccounts.add(newBankAccount);
                JOptionPane.showMessageDialog(null, "Account ID: " + newBankAccount.getAccountId());
                JOptionPane.showMessageDialog(null, "Account Password: " + newBankAccount.getPassword());
            } else if (inputValue.equals("Savings Account")) {
                // Make a savings acct
                String inputNameSavings = JOptionPane.showInputDialog(null, "Please input your name");
                BankAccount newBankAccount = new SavingsAccount();
                newBankAccount.setName(inputNameSavings);
                bankAccounts.add(newBankAccount);
                JOptionPane.showMessageDialog(null, "Account ID: " + newBankAccount.getAccountId());
                JOptionPane.showMessageDialog(null, "Account Password: " + newBankAccount.getPassword());
            } else if (inputValue.equals("Checking Account")) {
                // Make a checking acct
                String inputNameChecking = JOptionPane.showInputDialog(null, "Please input your name");
                BankAccount newBankAccount = new CurrentAccount(); 
                newBankAccount.setName(inputNameChecking);
                bankAccounts.add(newBankAccount);
                JOptionPane.showMessageDialog(null, "Account ID: " + newBankAccount.getAccountId());
                JOptionPane.showMessageDialog(null, "Account Password: " + newBankAccount.getPassword());
            }
    }
    public static ArrayList<BankAccount> getBankAccounts() {
        return bankAccounts;//Moving this into the Open.Account?
    }
    public void closeAccount() {
        System.out.println("Hello give me the name of the" + " "
                + "account you would like to close.");
        String userName = input.next();// Gets input for first choice.
        // Ask for account id
        System.out.println("Please input your account ID");
        int userAccountId = input.nextInt();
        for (int i = 0; i < bankAccounts.size(); i++) {
            if (bankAccounts.get(i).getName().equals(userName)
                    && bankAccounts.get(i).getAccountId() == userAccountId) {
                bankAccounts.remove(i);
                System.out.println("Account removed");
            }
        }
        // anyway, you would set the name probably in the constructor function
        // for that particular instantiation of the class
    }
    public void update() {
        // UPDATES MY ACCOUNTS
        for (int i = 0; i < bankAccounts.size(); i++) {
            if (bankAccounts.get(i) instanceof SavingsAccount) {
                ((SavingsAccount) bankAccounts.get(i)).addInterest();
            } else if (bankAccounts.get(i) instanceof CurrentAccount) {
                if (bankAccounts.get(i).getAccountBalance() < 0) {
                    System.out.println("\nThe Account: " + bankAccounts.get(i).getName() + " has been OverDrafted.\n");
                }
            }
        }
    }// ends update
    public void withdraw(){
        System.out.println("Input your ID and then your password to withdraw money.");
         int acctID = input.nextInt();
         int withdrawAmount;
        // do {
        for (int i = 0; i < bankAccounts.size(); i++){
            //input needed.
            if (acctID == bankAccounts.get(i).getAccountId()){
                System.out.println("We have found your account. Please input how much money you would like to withdraw.");
                withdrawAmount = input.nextInt();
                bankAccounts.get(i).withdraw(withdrawAmount);
                break;
            }
                        //  } while (input != 1);
        }
    }
    public void deposit(){
        int acctID;
        int depositingAmount;
        //System.out.println("Input your ID and then your password to deposit money.");
        String inputID = JOptionPane.showInputDialog("Please input your ID to begin depositing money.");
        acctID = Integer.parseInt(inputID);
        String inputPassword = JOptionPane.showInputDialog("Please input your password to verify your account.");
        // int depositAmount;
        // do {
        for (int i = 0; i < bankAccounts.size(); i++)//Loops through accounts in my arraylist.
        {
            if (acctID == bankAccounts.get(i).getAccountId() && inputPassword.equals(bankAccounts.get(i).getPassword()))//If ID and password work are true then it goes in here.
            {
                String depositAmount = JOptionPane.showInputDialog("Please input how much money you want to "
                        + "input");//An here is where you would be able to spit out how much money to deposit.
                depositingAmount = Integer.parseInt(depositAmount);
                bankAccounts.get(i).deposit(depositingAmount);
                break;
            }
        }
    }   
    }
An finally the class i'm running it on...
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class TestBankAccount {
    public static void main(String[] args) {
    //  JOptionPane.showMessageDialog(null, "Hello User.");
    Object[] menuPossibilities = {"Create a New Account", "Deposit", "Withdraw", "Display Balance", "Exit"};
    Object menuValues = JOptionPane.showInputDialog(null, "Please pick an option from this menu: ",
                "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);
    while (!menuValues.equals("Exit")){
        Bank newBank = new Bank();
        // Bank newBank1 = new Bank();
        // Bank newBank2 = new Bank(); Do the same thing as below but switch out
        // bank1 and bank2 as a substitute.
        ArrayList<BankAccount> bankList = newBank.getBankAccounts();
            if (menuValues.equals("Create a New Account")){
                newBank.openAccount();
            }
            else if (menuValues.equals("Deposit")){
                newBank.deposit();
            }
            else if (menuValues.equals("Withdraw")){
                newBank.withdraw();
            }
            else if (menuValues.equals("Display Balace")){
                newBank.deposit();
            }
            else if (menuValues.equals("Exit")){
                System.out.println("Thank you for using our service.");
            }
        menuValues = JOptionPane.showInputDialog(null, "Since you did not pick 5 please pick another option: ",
                   "input", JOptionPane.INFORMATION_MESSAGE, null, menuPossibilities, menuPossibilities [0]);
    }
    }
}
 
    