Two kinds of account share most of their behaviour but differ in their rules. Put the shared part in a superclass and the rules in overrides.
What to build
A bank with savings and current accounts that follow different rules.
Requirements
- Write an abstract class
Account(an id, a balance in whole cents as along,deposit, abstractwithdrawreturning a boolean, and abstractmonthEnd), and subclassesSavingsandCurrent. - Savings: a withdrawal may not take the balance below 0; at month end it earns 1% interest on its balance, rounded down to whole cents. Current: the balance may go down to -50000 cents (an overdraft of 500.00); at month end, if the balance is negative, a fee of 1000 cents is charged.
- Commands:
open savings ID,open current ID,deposit ID CENTS,withdraw ID CENTS(printID: refusedwhen not allowed),month(month end for every account, in the order opened),show ID(printID: Bwith B in units with two decimals, such as-12.50).
Input
Line 1: q. Then q commands.
Output
One line per refused withdrawal and per show.
Constraints
1 ≤ q ≤ 200; amounts are positive and at most 10^9; ids are distinct words.
Example
Input:
7
open savings S1
open current C1
deposit S1 100000
withdraw S1 150000
withdraw C1 20000
month
show S1
Output:
S1: refused
S1: 1010.00
Why it works
The bank loop calls withdraw and monthEnd on Account references, and each object applies its own rules; keeping money in whole cents as a long makes every sum exact.
Examples
7 open savings S1 open current C1 deposit S1 100000 withdraw S1 150000 withdraw C1 20000 month show S1
S1: refused S1: 1010.00
3 open current C withdraw C 60000 show C
C: refused C: 0.00
Hints
Hint 1
Put the shared balance and deposit in an abstract Account; each subclass overrides withdraw with its own rule.
Hint 2
monthEnd() is another overridden method: savings earn interest, current accounts pay a fee when overdrawn.
Solution
Show one correct solution (try it yourself first)
import java.util.Scanner;
abstract class Account {
final String id;
protected long balance = 0;
Account(String id) {
this.id = id;
}
void deposit(long cents) {
balance += cents;
}
abstract boolean withdraw(long cents);
abstract void monthEnd();
String show() {
long abs = Math.abs(balance);
return id + ": " + (balance < 0 ? "-" : "") + abs / 100 + "." + String.format("%02d", abs % 100);
}
}
class Savings extends Account {
Savings(String id) {
super(id);
}
@Override
boolean withdraw(long cents) {
if (balance - cents < 0) return false;
balance -= cents;
return true;
}
@Override
void monthEnd() {
balance += balance / 100;
}
}
class Current extends Account {
Current(String id) {
super(id);
}
@Override
boolean withdraw(long cents) {
if (balance - cents < -50000) return false;
balance -= cents;
return true;
}
@Override
void monthEnd() {
if (balance < 0) balance -= 1000;
}
}
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
Account[] accounts = new Account[q];
int size = 0;
for (int i = 0; i < q; i++) {
String cmd = in.next();
if (cmd.equals("month")) {
for (int k = 0; k < size; k++) accounts[k].monthEnd();
continue;
}
if (cmd.equals("open")) {
String kind = in.next();
String id = in.next();
accounts[size++] = kind.equals("savings") ? new Savings(id) : new Current(id);
continue;
}
String id = in.next();
Account a = null;
for (int k = 0; k < size; k++) if (accounts[k].id.equals(id)) a = accounts[k];
if (cmd.equals("deposit")) a.deposit(in.nextLong());
else if (cmd.equals("withdraw")) {
if (!a.withdraw(in.nextLong())) System.out.println(id + ": refused");
} else System.out.println(a.show());
}
}
}