-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemotePersonImpl.java
63 lines (52 loc) · 1.88 KB
/
RemotePersonImpl.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
package ru.ifmo.rain.vakhrushev.bank;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.concurrent.ConcurrentHashMap;
public class RemotePersonImpl implements RemotePerson {
protected String name;
protected String surname;
protected String passport;
protected ConcurrentHashMap<String, Account> accounts;
RemotePersonImpl(String name, String Surname, String passport) throws RemoteException {
this.name = name;
this.surname = Surname;
this.passport = passport;
accounts = new ConcurrentHashMap<>();
UnicastRemoteObject.exportObject(this, 0);
}
@Override
public String getName() throws RemoteException {
return name;
}
@Override
public String getSurname() throws RemoteException {
return surname;
}
@Override
public String getPassport() throws RemoteException {
return passport;
}
@Override
public ConcurrentHashMap<String, Account> getAccounts() throws RemoteException {
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 synchronized int setToAccount(String accountId, int amount) throws RemoteException {
Account tempAccount = this.getAccount(accountId);
tempAccount.setAmount(amount);
accounts.put(accountId, tempAccount);
return tempAccount.getAmount();
}
@Override
public int getAmount(String accountId) throws RemoteException {
Account tempAccount = this.getAccount(accountId);
return tempAccount.getAmount();
}
}