Skip to content
Advertisement

Calling methods and how to use them

The task I am trying to do is to utilise the given classes and methods are to take the money from the richest account and give it to the poorest so they have the same balance and to print “(name) has $(amount).”

I am clueless on which method to use and anything I try gives an error or and incorrect output. Thanks for reading and helping.

The main class I have to add the code is:

import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.stream.Collectors;
import java.util.List;

public class BankManager {

private static BankAccount[] openAccountFile(String filename) {

    try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {

        List<String> rawData = reader.lines().collect(Collectors.toList());
        BankAccount[] accounts = new BankAccount[rawData.size()];

        for (int i = 0; i < accounts.length; ++i) {
            String[] splitData = rawData.get(i).split(" ");
            accounts[i] = new BankAccount(splitData[0].trim(), Float.parseFloat(splitData[1]));
        }

        return accounts;
    }
    catch (FileNotFoundException e) {
        System.err.println("The input file does not exist.");
        System.err.println(e.getMessage());
    }
    catch (IOException e) {
        System.err.println("Something went wrong trying to read the file.");
        System.err.println(e.getMessage());
    }

    return null;

}

public static void main(String[] args) {

    BankAccount[] accounts = openAccountFile(args[0]);


    System.out.println("The bank has $" + AccountManagement.overallBalance(accounts) + ".");
    System.out.println("The richest account is " + AccountManagement.richest(accounts) + ".");
    System.out.println("The poorest account is " + AccountManagement.poorest(accounts) + ".");




} 

}

The classes which contains methods are:

public class AccountManagement {

public static String richest(BankAccount[] accounts) {
    BankAccount richest = accounts[0];

    for (BankAccount account : accounts) {
        if (account.currentBalance() > richest.currentBalance())
            richest = account;
    }

    return richest.accountName();
}

public static String poorest(BankAccount[] accounts) {
    BankAccount poorest = accounts[0];

    for (BankAccount account : accounts) {
        if (account.currentBalance() < poorest.currentBalance())
            poorest = account;
    }

    return poorest.accountName();
}

public static float overallBalance(BankAccount[] accounts) {
    float total = 0;

    for (BankAccount account : accounts) {
        total += account.currentBalance();
    }

    return total;
}

public static BankAccount findAccount(String accountName, BankAccount[] accounts) {
    for (BankAccount account : accounts) {
        if (account.accountName().equals(accountName)) return account;
    }

    return null;
}

}

and:

public class BankAccount {

private String accountName;
private int balance;

public BankAccount(String accountName, float initialBalance) {
    this.accountName = accountName;
    this.balance = Math.round(initialBalance*100);
}

public float currentBalance() {
    return (float)this.balance/100;
}

public void withdraw(float amount) {
    balance -= Math.round(amount*100);
}

public void deposit(float amount) {
    balance += Math.round(amount*100);
}

public String accountName() {
    return this.accountName;
}

}

The text file contains:

Luke 12.42
Cat 56.76
Nick 35.32
Mustafa 42.00

Advertisement

Answer

First, we need to find the richest and the poorest BankAccount. It can be achieved by using corresponding AccountManagement methods:

final BankAccount richest = AccountManagement.findAccount(AccountManagement.richest(accounts), accounts);
final BankAccount poorest = AccountManagement.findAccount(AccountManagement.poorest(accounts), accounts);

Then we should calculate target balance for richest and poorest accounts. Since we want them to be equal, it can be calculated just as an math average of account balances:

final float targetBalance = (richest.currentBalance() + poorest.currentBalance()) / 2;

Then we should correct the richest and poorest account balances in order to achieve desired amount (targetBalance):

richest.withdraw(richest.currentBalance() - targetBalance);
poorest.deposit(targetBalance - poorest.currentBalance());

And finally we just print the names and amounts of all accounts we have:

for (BankAccount account : accounts) {
    System.out.println(
        account.accountName() + " has $" + account.currentBalance()
    );
}

The full code snippet:

final BankAccount richest = AccountManagement.findAccount(AccountManagement.richest(accounts), accounts);
final BankAccount poorest = AccountManagement.findAccount(AccountManagement.poorest(accounts), accounts);

final float targetBalance = (richest.currentBalance() + poorest.currentBalance()) / 2;

richest.withdraw(richest.currentBalance() - targetBalance);
poorest.deposit(targetBalance - poorest.currentBalance());

for (BankAccount account : accounts) {
    System.out.println(
        account.accountName() + " has $" + account.currentBalance()
    );
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement