-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalPersonImpl.java
75 lines (63 loc) · 2.06 KB
/
LocalPersonImpl.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
package ru.ifmo.rain.vakhrushev.bank;
import java.util.concurrent.ConcurrentHashMap;
public class LocalPersonImpl implements LocalPerson {
private ConcurrentHashMap<String, Account> accounts;
private String name;
private String surname;
private String passport;
public LocalPersonImpl(String name, String surname, String passport) {
this.name = name;
this.surname = surname;
this.passport = passport;
accounts = new ConcurrentHashMap<>();
}
public LocalPersonImpl(Person p) {
try {
// actually no exception can be thrown
this.name = p.getName();
this.surname = p.getSurname();
this.passport = p.getPassport();
this.accounts = p.getAccounts();
} catch (Exception e) {
this.name = "";
this.surname = "";
this.passport = "";
this.accounts = new ConcurrentHashMap<>();
}
}
@Override
public String getName() {
return name;
}
@Override
public String getSurname() {
return surname;
}
@Override
public String getPassport() {
return passport;
}
@Override
public ConcurrentHashMap<String, Account> getAccounts() throws Exception {
return accounts;
}
private Account getAccount(String accountId) {
if (!accounts.containsKey(accountId)) {
String bankAccountId = passport + ":" + accountId;
accounts.put(accountId, new AccountImpl(bankAccountId));
}
return accounts.get(accountId);
}
@Override
public int setToAccount(String accountId, int amount) {
Account tempAccount = this.getAccount(accountId);
tempAccount.setAmount(amount);
accounts.put(accountId, tempAccount);
return tempAccount.getAmount();
}
@Override
public int getAmount(String accountId) {
Account account = getAccount(accountId);
return account.getAmount();
}
}