-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockMarket.java
30 lines (27 loc) · 1.02 KB
/
StockMarket.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
public class StockMarket {
private HashMap<String, Stock> availableStocks;
private HashMap<String, User> users;
public StockMarket() {
availableStocks = new HashMap<>();
users = new HashMap<>();
// Initialize some stocks in the market
availableStocks.put("AAPL", new Stock("AAPL", 150.0));
availableStocks.put("GOOG", new Stock("GOOG", 2700.0));
}
public boolean buyStock(User user, String stockSymbol, int quantity) {
if (availableStocks.containsKey(stockSymbol)) {
double price = availableStocks.get(stockSymbol).getPrice();
double cost = price * quantity;
if (user.getBalance() >= cost) {
user.deductBalance(cost);
user.getPortfolio().buyStock(stockSymbol, quantity);
System.out.println("Purchase successful!");
return true;
}
}
return false;
}
public void addUser(User user) {
users.put(user.getUsername(), user);
}
}