Java - for loop skipping over values?

54 views Asked by At

The following code is supposed to do the following:

  • loop through an array of accounts, consisting of (the AccountHolder(which consists of name, address, and ssn), the ssn, the 4 digit account number, the single digit account type (which will equate to either checking or savings), and the balance in the account)
  • if it finds an account whose SSN and account number are the same, assume they own the account, store the name of the account holder for later then run tests:
  • continue to loop through
  • if it locates the same customer again, check if they have the other account type, and if so, deny making a new account based on only being able to have 1 checking and 1 savings max
  • if they instead already have an account of that type, deny based on only being able to have a maximum of 1 checking or 1 savings at once (i.e. no < 1 checking)
  • otherwise, if it detects the same SSN but different account number, deny based on having to have a unique account number
  • if it passes all the checks, add to the array of accounts.
    public void newAccount(String ssn, int accNum, int accType, double initialBalance)
    {
        Boolean dupeAccType = false, sameAccNum = false, threeAccs = false;
        String checkOrSave = null;
        Customer tempCustomer = null;

        for (int i = 0; i < numAccounts; i++)   //for all existing accounts
        {
            if (accounts[i] != null)
            {
                System.out.println(accounts[i].getCustomer().getSsn().equals(ssn));
                System.out.println(ssn);
                System.out.println(accounts[i].getAccNum());
                System.out.println(accNum);
                System.out.println("");

                if (accounts[i].getCustomer().getSsn().equals(ssn) &&   //if same ssn
                    accounts[i].getAccNum() == accNum)                  //and same accNum (if we already have this customer)
                {
                    tempCustomer = accounts[i].getCustomer();

                    System.out.println(accounts[i].getAccType());
                    System.out.println(accType);


                    for (int j = i; j < numAccounts; j++)   //loop through the rest of accounts
                    {
                        if (accounts[j] != null)
                        {
                            if (accounts[j].getCustomer().getSsn().equals(ssn) &&   //if same customer again
                                    accounts[j].getAccNum() == accNum)
                            {
                                if (accounts[j].getAccType() != accType)    //if customer has other account
                                {
                                    threeAccs = true;
                                    break;
                                }
                            }
                            if (accounts[i].getAccType() == accType)    //if already have that type of account
                            {
                                dupeAccType = true;

                                if (accType == 1)
                                {
                                    checkOrSave = "checking";
                                }
                                else
                                {
                                    checkOrSave = "savings";
                                }
                                break;
                            }
                        }

                    }
                }
                else if ((!accounts[i].getCustomer().getSsn().equals(ssn)) &&   //if same accNum
                        accounts[i].getAccNum() == accNum)
                {
                    sameAccNum = true;
                    break;
                }
            }

        }
        if (dupeAccType)
        {
            System.out.println("Account creation failed - " + tempCustomer.getName() + " (" + ssn + ") already has a " + checkOrSave + " account");
        }
        else if (sameAccNum)
        {
            System.out.println("Account creation failed - Account " + accNum + " already exists");
        }
        else if (threeAccs)
        {
            System.out.println("Account creation failed - can only have one checking and one savings account");
        }
        else
        {
            System.out.println("Account creation - Number: " + accNum + ", Customer: " + tempCustomer);

            Account tempAccount = new Account(tempCustomer, ssn, accNum, accType, initialBalance);

            accounts[numAccounts + 1] = tempAccount;
            numAccounts++;
        }
    }

My issue is that for some reason, when looping through, it seems to skip over values:

true
777-77-7777
1000
4000

false
777-77-7777
2000
4000

true
777-77-7777
3000
4000

false
777-77-7777
5000
4000

false
777-77-7777
6000
4000

Account creation - Number: 4000, Customer: null

Here, we post the following:

                System.out.println(accounts[i].getCustomer().getSsn().equals(ssn));
                System.out.println(ssn);
                System.out.println(accounts[i].getAccNum());
                System.out.println(accNum);

Note how it never compares 4000 to 4000. I'm wondering why this is. Surely it's my logic but I'm just not seeing it.

0

There are 0 answers