0

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.
NinStarRune
  • 23
  • 1
  • 7
  • Try to run the program with the debugger and check what you actually get back from `accounts[i].getAccNum()`. Alternatively, print the value before comparing it. – Henry Oct 07 '20 at 04:08
  • *if (accNum == accounts[i].getAccNum())* Where is the getAccNum method implementation? – Omar Abdel Bari Oct 07 '20 at 04:10
  • Please delete code from your question that is not directly required to show the problem in your `accountInfo()` method (ie most of it, especially the entire Bank class). Also, you haven't shown the Account class, upon whose `getAccNum()` your method relies. – Bohemian Oct 07 '20 at 04:22
  • Sorry, should have specified, getAccNum is a method in Accounts, it just returns the integer accNum associated with it. – NinStarRune Oct 07 '20 at 04:37
  • @NinStarRune it would still be interesting to see `getAccNum`. – Henry Oct 07 '20 at 07:43

1 Answers1

0

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

Hosseinreza
  • 561
  • 7
  • 18
  • 1
    No so: You don't have to use the `this` keyword to access instance fields/methods. In fact, I delete `this` from all source code I touch, unless it is required to disambiguate a reference, becuase it's "code noise". – Bohemian Oct 07 '20 at 04:23