PFKrang PFKrang - 4 years ago 206
Java Question

java bankaccount program issue with a method

So for a HW assignment I have created a BankAccount class and a Bank class. It is mostly complete except I am having issues with creating a method to add a monthly fee. I have searched around and while I have found a ton of similar examples but nothing that works with what I have.

Here is the BankAccount class:

public class BankAccount {

String owner;
int accountNumber;
double balance;

public BankAccount (String name, int acct){
owner = name;
accountNumber = acct;
}

public String toString() {
String str = owner + " owns the account " + accountNumber + " with the balance of " + String.format("$%,.2f", balance);
return str;
}

public double adjust(double amt) {
balance = balance + amt;
return balance;
}

public double getBalance() {
return balance;
}
}


Here is the Bank.java class:

import java.util.Arrays;

public class Bank {

BankAccount[] bAcct;

public Bank() {
//Constructor - will create an array that can hold up to 10 BankAccount objects.
bAcct = new BankAccount[10];
}

public void addAccount(BankAccount a) {
//This method will take a BankAccount object as a param and place it in the next avail entry in array.
for(int i = 0; i < bAcct.length; i++) {
if (bAcct[i] == null){
bAcct[i] = a;
break;
}
}
}

public BankAccount getAccount(int index) {
//will return a BankAccount object given an integer index value as a param
return bAcct[index];
}

public void printAccounts() {
//will display all of the BankAccount objects
for (int i = 0; i < bAcct.length; i++) {
if(bAcct[i] != null) {
System.out.println(bAcct[i]);
}
}
}

public double monthlyFee(double f) {
//will take a double value as a param and apply that value to every BankAccount object
for (int i = 0; i < bAcct.length; i++){
if(bAcct[i] !=null) {


}
}

}
}


and the test class:

public class BankTest
{
/*
* test - set up a bank and add accounts
*/
public static void main(String[] args)
{
// Code to test Bank and BankAccount classes
int errors = 0;
double fee = -2.95;

System.out.println("\nCreate bank1");
Bank bank1 = new Bank();
System.out.println("\nOne account");
BankAccount b1 = new BankAccount("Peter Chang", 3021);
b1.adjust(1000.0);
bank1.addAccount(b1);
bank1.printAccounts();
System.out.println("\nTwo accounts");
BankAccount b2 = new BankAccount("Roddy Piper", 3049);
b2.adjust(2000.0);
bank1.addAccount(b2);
bank1.printAccounts();
System.out.println("\nThree accounts");
BankAccount b3 = new BankAccount("Leeroy Jenkins", 4028);
b3.adjust(3000.0);
bank1.addAccount(b3);
bank1.printAccounts();
System.out.println("\nMonthly Fee");
bank1.monthlyFee(fee);
bank1.printAccounts();
System.out.println("\nErrors:");

if (bank1.getAccount(0).getBalance() != 997.05)
{
errors += 1;
System.out.println("Balance for account at index 0 does not match $997.05");
}
if (bank1.getAccount(1).getBalance() != 1997.05)
{
errors += 1;
System.out.println("Balance for account at index 1 does not match $1997.05");
}
if (bank1.getAccount(2).getBalance() != 2997.05)
{
errors += 1;
System.out.println("Balance for account at index 2 does not match $2997.05");
}
if (errors == 0)
System.out.println("No errors found!!!");
}
}


I'm stuck on the monthlyFee method in the Bank.java class. Any help is appreciated! Thanks in advance.

Answer Source

Something like that :

public void monthlyFee(double f) {
        //will take a double value as a param and apply that value to every BankAccount object
        for (int i = 0; i < bAcct.length; i++){
            if(bAcct[i] !=null) {
                bAcct[i].adjust(f);
            }
        }
    }

Removed the return from the monthlyFee method as you were not using it and it wasn't clear what you would expect it to return.

By the way if I were you, I'd use List and ArrayList instead of Array, that would save your some unecessary hassle.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download