Loading...
Loading...
A customer has one savings account (S) and one current account (C). Savings must keep a minimum balance and earns rate% interest (rounded down) at month. Current may go below 0 down to minus its overdraft limit.
Write InsufficientFundsException extends Exception that also stores shortBy. Write Account (private code, protected long balance, deposit), then SavingsAccount and final class CurrentAccount that each add withdraw(long amount) throws InsufficientFundsException with their own rule. A move withdraws from one account and deposits into the other.
deposit A x, withdraw A x, move A B x, month or show (A, B are S or C).show: S s C c. For month: INTEREST i and then S s C c. On failure: FAILED reason, short by k, where reason is SAVINGS MINIMUM min or OVERDRAFT LIMIT od.Example
Input
5000 1000 2 0 3000 5 move S C 3000 withdraw C 5500 withdraw C 600 month move S C 200
Output
S 2000 C 3000 S 2000 C -2500 FAILED OVERDRAFT LIMIT 3000, short by 100 INTEREST 40 S 2040 C -2500 S 1840 C -2300
Current may go down to -3000, so from -2500 the 600 is short by 100. Interest is 2% of 2000.
Try the problem first. Open one hint at a time.
Each withdraw works out how much may leave (balance - minBalance or balance + overdraft), throws when the amount is larger, and only then subtracts.
The exception constructor takes the reason and shortBy, passes the reason to super(message) and keeps shortBy in a field with a getter.
For move, withdraw first: if it throws, the deposit line is never reached.
Two small static helpers in Main, take(code, x) and give(code, x), avoid writing the S/C choice four times.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor