Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! I'm new to Java programming and Object-oriented programming. Please help me.
I have 6 classes here: Bank, Customer, Account, CheckingAccount, SavingsAccount, and TestBanking. A Bank can have multiple customers(but i initialize the num of customers to 6). A Customer can have multiple accounts(I initialize the num of accounts to 20). CheckingAccount and SavingsAccount extends to Account class indicating the types of Account.

TestBanking.java
/*
 * This class creates the program to test the banking classes.
 * It creates a set of customers, with a few accounts each,
 * and generates a report of current account balances.
 */

import java.text.NumberFormat;

public class TestBanking {

    public static void main(String[] args) {
        Bank     bank = new Bank();
        Customer customer;

        // Create several customers and their accounts
        bank.addCustomer("Jane", "Simms");
        customer = bank.getCustomer(0);
        customer.addAccount(new SavingsAccount(500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00, 400.00));

        bank.addCustomer("Owen", "Bryant");
        customer = bank.getCustomer(1);
        customer.addAccount(new CheckingAccount(200.00));

        bank.addCustomer("Tim", "Soley");
        customer = bank.getCustomer(2);
        customer.addAccount(new SavingsAccount(1500.00, 0.05));
        customer.addAccount(new CheckingAccount(200.00));

        bank.addCustomer("Maria", "Soley");
        customer = bank.getCustomer(3);
        // Maria and Tim have a shared checking account
        customer.addAccount(bank.getCustomer(2).getAccount(1));
        customer.addAccount(new SavingsAccount(150.00, 0.05));

        // Generate a report
        System.out.println("\t\t\tCUSTOMERS REPORT");
        System.out.println("\t\t\t================");



        for ( int cust_idx = 0; cust_idx < bank.getNumOfCustomers(); cust_idx++ ) {
            customer = bank.getCustomer(cust_idx);

            System.out.println();
            System.out.println("Customer: "
                    + customer.getLastName() + ", "
                    + customer.getFirstName());

            for ( int acct_idx = 0; acct_idx < customer.getNumOfAccounts(); acct_idx++ ) {
                Account account = customer.getAccount(acct_idx);
                String  account_type = "";

                //HERE! HELP
                if (account instanceof CheckingAccount){
                    account_type = "Checking Account";
                    System.out.println(account_type + ": current balance is " + account.getBalance());
                } else {
                    account_type = "Savings Account";
                    System.out.println(account_type + ": current balance is " + account.getBalance());
                }
               
                /* I want to determine the account type and              
                 set the instanceof operator to test what type of account
                 I have and set the account_type to an appropriate value, such
                 as "Savings Account" or "Checking Account".
                 Then, I want to print the type of account and current balance of the account.
                */
            }
        }
    }
}


Bank.java
public class Bank {
    
    private Customer [] customers;
    private int numberOfCustomers;

    public Bank() {
        customers = new Customer[6];
    }

    public void addCustomer(String f, String l){
        Customer customer = new Customer(f,l);
        customers[numberOfCustomers] = customer;
        numberOfCustomers++;
    }

    public int getNumOfCustomers() {
        return numberOfCustomers;
    }

    public Customer getCustomer(int index) {
        return customers[index];
    }
}


Customer.java
public class Customer {
    private String firstName;
    private String lastName;
    private Account [] accounts;
    private int numberOfAccounts;

    public Customer (String f, String l){
        this.firstName = f;
        this.lastName = l;
        accounts = new Account[20];
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setAccount(Account[] account) {
        this.accounts = account;
    }

    public Account getAccount(int num) {
        return accounts[num];
    }

    public void addAccount(Account account) {
        Account acct = new Account(account.balance);
        accounts[numberOfAccounts] = acct;
        numberOfAccounts++;
    }

    public int getNumOfAccounts() {
        return numberOfAccounts;
    }

}


Account.java
public class Account {
    protected double balance;

    public Account(double init_balance) {
        this.balance = init_balance;
    }

    public double getBalance() {
        return balance;
    }

    public boolean deposit(double amt) {
        balance += amt;                     
        return true;
    }

    public boolean withdraw(double amt) {
        if (amt < balance){
            balance -= amt;
            return true;
        } else {
            return false;
        }
    }
}


CheckingAccount.java
public class CheckingAccount extends Account{
    private double overdraftProtection;

    public CheckingAccount(double balance){
        super(balance);
    }

    public CheckingAccount(double balance, double protect){
        super(balance);
        this.overdraftProtection = protect;
    }

    @Override
    public boolean withdraw (double amt){
        if (super.balance > amt){
            super.balance -= amt;
        }
        else if (amt > overdraftProtection){
            return false;
        }
        else if (amt > super.balance && overdraftProtection > 0){
            super.balance -= amt;
            overdraftProtection = super.balance + overdraftProtection;
            super.balance = 0;
        }
        return true;
    }
}


SavingsAccount.java
public class SavingsAccount extends Account {
    private double interestRate;

    public SavingsAccount(double balance, double interest_rate){
        super(balance);
        this.interestRate = interest_rate;
    }
}


What I have tried:

So, in the TestBanking.java
//HERE! HELP
if (account instanceof CheckingAccount){
                    account_type = "Checking Account";
                    System.out.println(account_type + ": current balance is " + account.getBalance());
                } else {
                    account_type = "Savings Account";
                    System.out.println(account_type + ": current balance is " + account.getBalance());
                }


The output was:
CUSTOMERS REPORT
================

Customer: Simms, Jane
Savings Account: current balance is 500.0
Savings Account: current balance is 200.0

Customer: Bryant, Owen
Savings Account: current balance is 200.0

Customer: Soley, Tim
Savings Account: current balance is 1500.0
Savings Account: current balance is 200.0

Customer: Soley, Maria
Savings Account: current balance is 200.0
Savings Account: current balance is 150.0

Process finished with exit code 0


As you can see, only the "Savings Account" is shown and it's repeated many times. Look at the first customer(Simms, Jane). I have already set its 2nd account to CheckingAccount and yet it printed the Savings Account, then same applies to others. Why is that? Is there something I missed?
Posted
Updated 11-Jun-21 22:46pm
v3

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on this line:
Java
String  account_type = "";
And run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
I cannot reproduce (or test) all your code but a simple test worked for me. You may like to try the following change:
Java
// Create several customers and their accounts
bank.addCustomer("Jane", "Simms");
customer = bank.getCustomer(0);

Account newAccount = new SavingsAccount(500.00, 0.05);
customer.addAccount(newAccount);

newAccount = new CheckingAccount(200.00, 400.00);
customer.addAccount(newAccount);
 
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