-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUser.java
43 lines (35 loc) · 1.23 KB
/
User.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
import java.util.HashMap;
public class User {
private String username;
private String password;
private HashMap<String, Integer> portfolio; // Stock name and amount
public User(String username, String password) {
this.username = username;
this.password = password;
this.portfolio = new HashMap<>();
}
public String getUsername() {
return username;
}
public boolean checkPassword(String password) {
return this.password.equals(password);
}
// Adds stock to the user's portfolio
public void addStock(String stockName, int amount) {
portfolio.put(stockName, portfolio.getOrDefault(stockName, 0) + amount);
}
// Removes stock from the user's portfolio
public void removeStock(String stockName, int amount) {
if (portfolio.containsKey(stockName) && portfolio.get(stockName) >= amount) {
portfolio.put(stockName, portfolio.get(stockName) - amount);
}
}
// Returns portfolio (stock name and amount)
public HashMap<String, Integer> getPortfolio() {
return portfolio;
}
@Override
public String toString() {
return "Username: " + username + ", Portfolio: " + portfolio.toString();
}
}