Today we'll tackle WithdrawCommand. It's the most complex operation.
1. Implement the following algorithm for WithdrawCommand:
1.1. Read the currency code (the method already exists).
1.2. Get a manipulator for the specified currency.
1.3. Until the user enters valid data, do the following:
1.3.1. Ask the user to enter the amount.
1.3.2. If invalid data is entered, inform the user and return to item 1.3.
1.3.3. Check whether there's enough money in the account.
To do this, create a boolean isAmountAvailable(int expectedAmount) method in the manipulator. It should return true if there is enough money to make the withdrawal.
If there is not enough, then return to item 1.3.
1.3.4. Deduct the money from the account. To do this, in the manipulator class, create a Map<Integer, Integer> withdrawAmount(int expectedAmount) that returns a Map<denomination, quantity>.
For details on the logic of this method, see item 2.
1.3.5. Display the result from item 1.3.4. in the following form:
<tab><denomination> - <quantity>.
The lines should be sorted from larger denominations to smaller ones.
Display a message that the transaction was successful.
1.3.6. Catch InsufficientFundsExceptions, notify the user about the insufficient banknotes, and return to item 1.3.
2. Here's the logic for the withdrawAmount method:
2.1. Please note that the withdrawAmount method should return the smallest number of banknotes that can be used to provide the requested amount.
Use a greedy algorithm (google it).
If there are several options, then use the one that has the largest number of banknotes of the highest denomination.
If the result is three banknotes for a withdrawal amount of 600, i.e. 500 50 50 = 200 200 200, then issue banknotes according to the first option.
Suppose you need to issue 600.
The manipulator has the following banknotes:
500 - 2
200 - 3
100 - 1
50 - 12
The issued banknotes should be:
500 - 1
100 - 1
i.e. only two banknotes (this is the minimum number of banknotes) with the denominations 500 and 100.
2.2. We can't issue the same banknotes more than once, so if we find a valid option for issuing the money (item 2.1 is successful), then remove all these notes from the map in the manipulator.
2.3. The withdrawAmount method should throw an InsufficientFundsException if it is impossible to issue the requested amount.
Suppose you need to issue 600.
The manipulator has the following banknotes:
500 - 2
200 - 2
The available banknotes cannot provide the requested amount. Throw an exception.Don't forget that maps sometimes throw ConcurrentModificationExceptions.
- The CurrencyManipulator class must have a boolean isAmountAvailable(int expectedAmount) method.
- The isAmountAvailable method should return true if there is enough money for the withdrawal.
- The CurrencyManipulator class must contain a Map<Integer, Integer> withdrawAmount(int expectedAmount) method.
- The withdrawAmount method should return a map according to the task conditions.
- The WithdrawCommand class's execute method must implement an algorithm for WithdrawCommand according to the task conditions.