Java - if statement failing when comparing an element of an array to a passed int to see if identical

70 views Asked by At

A bit confused here. The way I have it set, when called, it should cycle through each element of the array that's populated and check to see if that element's int accNum is equal to the passed int accNum and if so output a summary of the account.

Maybe my logic is wrong but in my mind it should work:

public void accountInfo(int accNum)
    {
        for (int i = 0; i < numAccounts; i++)
        {
            if (accNum == accounts[i].getAccNum())
            {
                accounts[i].summary();
            }
            else
            {
                System.out.println("No such account on record.");
            }
        }
    }

Account class:

public class Account
{
    private String ssn;
    private int accNum, accType;
    private double balance;
    private Customer accountHolder;

    public Account(Customer accountHolder, String ssn, int accNum, int accType, double balance)
    {
        this.accountHolder = accountHolder;
        this.ssn = ssn;
        this.accNum = accNum;
        this.accType = accType;
        this.balance = balance;
    }

    public String summary()
    {
        String fullType;
        switch (accType)
        {
            case 1:
                fullType = "Checking";
                break;
            case 2:
                fullType = "Savings";
                break;
            default:
                fullType = "Other";
        }

        return String.format(("\t- Number: %d\n\t- %s\n\t- Balance: $%.2f\n\t- Customer: %s"),
                               accNum, fullType, balance, accountHolder.getName());
    }

    public double getBalance()
    {
        return balance;
    }

    public int getAccNum()
    {
        return accNum;
    }

    public void deposit(double balance)
    {
        this.balance += balance;
    }

    public void withdraw(double balance)
    {
        this.balance -= balance;
    }

}

/*Note that an Account object can have only one Customer.
 */

And my output:

========== READ DATA ==========
========== DONE ==========

========== BANK INFORMATION ==========
Bank Name: CSUMB
Number of Customers: 4
    Tom Smith: 777-77-7777
    Alice Smith: 888-88-8888
    Joe Otter: 999-99-9999
    Monica Smith: 123-45-7777
Number of Accounts: 5
    1000: $10.00
    2000: $50.25
    3000: $100.00
    5000: $100.25
    6000: $500.25
Bank Total Balance: $760.75


========== ACCOUNT INFORMATION ==========
No such account on record.
No such account on record.
No such account on record.
No such account on record.

when it should be

========== ACCOUNT INFORMATION ==========
    - Number: 1000
    - Checking
    - Balance: $10.00
    - Customer: Tom Smith

========== ACCOUNT INFORMATION ==========
    - Number: 1000
    - Checking
    - Balance: $160.25
    - Customer: Tom Smith

========== ACCOUNT INFORMATION ==========
    - Number: 1000
    - Checking
    - Balance: $60.25
    - Customer: Tom Smith

========== ACCOUNT CLOSE ==========
Account closed.
1

There are 1 answers

1
Hosseinreza On

i donno u figured it out or not , but there are some problem in your code:

  • When you wanna use Class variables in the methods you have to use this before them , like this.name that you used in Bank method.
  • You used a method called getAccNum() but there is no method with this name.

Check each method outputs then combine them together, it will make you think better while coding because you know the method you wanna use later are works fine and if you get any error you can easily find where is the problem.

hope i could help you <3