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:

JavaScript

The classes which contains methods are:

JavaScript

and:

JavaScript

The text file contains:

JavaScript

Advertisement

Answer

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

JavaScript

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:

JavaScript

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

JavaScript

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

JavaScript

The full code snippet:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement