forked from ashishps1/awesome-low-level-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccount.java
43 lines (35 loc) · 1009 Bytes
/
Account.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
package onlinestockbrokeragesystem;
public class Account {
private final String accountId;
private final User user;
private double balance;
private final Portfolio portfolio;
public Account(String accountId, User user, double initialBalance) {
this.accountId = accountId;
this.user = user;
this.balance = initialBalance;
this.portfolio = new Portfolio(this);
}
public synchronized void deposit(double amount) {
balance += amount;
}
public synchronized void withdraw(double amount) {
if (balance >= amount) {
balance -= amount;
} else {
throw new InsufficientFundsException("Insufficient funds in the account.");
}
}
public String getAccountId() {
return accountId;
}
public User getUser() {
return user;
}
public double getBalance() {
return balance;
}
public Portfolio getPortfolio() {
return portfolio;
}
}