forked from stefanandonov/SI2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankTester.java
114 lines (89 loc) · 2.59 KB
/
BankTester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Account {
long id;
double balance;
public Account(long id, String balance) {
this.id = id;
this.balance = Double.parseDouble(balance.replace("$", ""));
}
void withdraw(double amount) throws Exception {
if (balance >= amount) {
balance -= amount;
} else {
throw new Exception();
}
}
void deposit(double amount) {
balance += amount;
}
}
abstract class Transaction {
protected final long fromId;
protected final long toId;
protected final String description;
protected final double amount;
public Transaction(long fromId, long toId, String description, String amount) {
this.fromId = fromId;
this.toId = toId;
this.description = description;
this.amount = Double.parseDouble(amount.replace("$", ""));
}
abstract double getProvision();
double getTotalAmount() {
return amount + getProvision();
}
}
class FlatAmountProvisionTransaction extends Transaction {
double provision;
public FlatAmountProvisionTransaction(long fromId, long toId, String description, String amount, String flatProvision) {
super(fromId, toId, description, amount);
this.provision = Double.parseDouble(flatProvision.replace("$", ""));
}
@Override
double getProvision() {
return this.provision;
}
}
class FlatPercentProvisionTransaction extends Transaction {
int centsPerDollar;
public FlatPercentProvisionTransaction(long fromId, long toId, String description, String amount, int centsPerDollar) {
super(fromId, toId, description, amount);
this.centsPerDollar = centsPerDollar;
}
@Override
double getProvision() {
//21.5$
return (int) amount * centsPerDollar / 100.0;
}
}
class Bank {
String name;
Account[] accounts;
public Bank(String name, Account[] account) {
this.name = name;
this.accounts = account;
}
Account findAccount(long id) {
for (Account account : accounts) {
if (account.id == id) {
return account;
}
}
return null;
}
boolean makeTransaction(Transaction t) {
Account from = findAccount(t.fromId);
Account to = findAccount(t.toId);
if (from == null || to == null) {
return false;
}
try {
from.withdraw(t.getTotalAmount());
to.deposit(t.amount);
return true;
} catch (Exception e) {
return false;
}
}
}
public class BankTester {
}