Question: Calling Methods Just to flip things around for a moment, this exercise deals with how to use methods, once they are written. We have already

Calling Methods

Just to flip things around for a moment, this exercise deals with how to use methods, once they are written.

We have already been doing this throughout the subject so far, but now we can start doing it with things of which we can see the internals.

Here we have some classes (BankAccount.java and AccountManagement.java) with several methods in them, some static and some non-static, that deal with aspects of a simple bank account program. You should not modify these files, but you should definitely read them carefully.

The unfinished part of the program is in BankManager.java, this is where you will start. The program starts by creating a populated array of accounts (there will always be at least 2). The task is to use the available methods in AccountManagement and BankAccount to (in order):

  1. Print "The bank has $." Where is replaced with the total balance of all the accounts.

  2. Print "The richest account is ." Where is replaced by the account name of the account with the highest balance.

  3. Print "The poorest account is ." Where is replaced by the account name of the account with the lowest balance.

  4. On one line each, print out " has $." where and are replaced by the account name and balance for each account, in the order they appear in in the array.

To both assist with the testing and to introduce you to some new things (that you don't have to worry about yet if you don't want to), the input data will be read from a file, so if you want to modify the input, it's in AccountData.txt (different files will be used for the tests, so you can't mess anything up).

As an example, if you use the data in AccountData.txt without modifying it, your output should be:

The bank has $146.5

. The richest account is Cat.

The poorest account is

Luke. Luke has $34.59

. Cat has $34.59.

Nick has $35.32

. Mustafa has $42.0.

-----------------------------------------------------------------------------------------------------------------------------

Luke 12.42 Cat 56.76 Nick 35.32 Mustafa 42.00

--------------------------------------------------------------

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; } }

--------------------------------------------------------------------------------------------------------

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; } }

----------------------------------------------------------------------------------------------------------------

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 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]); //Your code goes in here. } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!