-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockMarketSimulatorGUI.java
239 lines (194 loc) · 8.17 KB
/
StockMarketSimulatorGUI.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
public class StockMarketSimulatorGUI {
private static Authentication auth = new Authentication();
private static User currentUser = null;
private static JFrame frame;
public static void main(String[] args) {
frame = new JFrame("Stock Market Simulator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
showLoginPanel();
frame.setVisible(true);
}
// Login Screen
private static void showLoginPanel() {
JPanel loginPanel = new JPanel();
loginPanel.setLayout(new BoxLayout(loginPanel, BoxLayout.Y_AXIS));
JLabel titleLabel = new JLabel("Welcome to the Stock Market Simulator!");
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
loginPanel.add(titleLabel);
JLabel usernameLabel = new JLabel("Username:");
JTextField usernameField = new JTextField();
loginPanel.add(usernameLabel);
loginPanel.add(usernameField);
JLabel passwordLabel = new JLabel("Password:");
JPasswordField passwordField = new JPasswordField();
loginPanel.add(passwordLabel);
loginPanel.add(passwordField);
JButton loginButton = new JButton("Login");
JButton registerButton = new JButton("Register");
// Login logic
loginButton.addActionListener(e -> {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
User user = auth.login(username, password);
if (user != null) {
currentUser = user;
showMainMenu();
} else {
JOptionPane.showMessageDialog(frame, "Oops! Invalid username or password.");
}
});
// Register logic
registerButton.addActionListener(e -> {
String username = usernameField.getText();
String password = new String(passwordField.getPassword());
boolean success = auth.registerUser(username, password);
if (success) {
JOptionPane.showMessageDialog(frame, "You're all set up! Login now.");
} else {
JOptionPane.showMessageDialog(frame, "This username's already taken. Try another one.");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.add(loginButton);
buttonsPanel.add(registerButton);
loginPanel.add(buttonsPanel);
frame.getContentPane().removeAll();
frame.getContentPane().add(loginPanel);
frame.revalidate();
}
// Main Menu after successful login
private static void showMainMenu() {
JPanel mainMenuPanel = new JPanel();
mainMenuPanel.setLayout(new BoxLayout(mainMenuPanel, BoxLayout.Y_AXIS));
JLabel welcomeLabel = new JLabel("Welcome back, " + currentUser.getUsername() + "!");
welcomeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
mainMenuPanel.add(welcomeLabel);
JButton viewPortfolioButton = new JButton("View Portfolio");
JButton tradeStocksButton = new JButton("Trade Stocks");
JButton logoutButton = new JButton("Logout");
// View portfolio logic
viewPortfolioButton.addActionListener(e -> showPortfolioPanel());
// Stock trading logic
tradeStocksButton.addActionListener(e -> showStockListPanel());
// Logout logic
logoutButton.addActionListener(e -> {
currentUser = null;
showLoginPanel();
});
mainMenuPanel.add(viewPortfolioButton);
mainMenuPanel.add(tradeStocksButton);
mainMenuPanel.add(logoutButton);
frame.getContentPane().removeAll();
frame.getContentPane().add(mainMenuPanel);
frame.revalidate();
}
// Display available stocks and let the user trade
private static void showStockListPanel() {
JPanel stockListPanel = new JPanel();
stockListPanel.setLayout(new BoxLayout(stockListPanel, BoxLayout.Y_AXIS));
JLabel stockListLabel = new JLabel("Here are some stocks you can trade:");
stockListLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
stockListPanel.add(stockListLabel);
// Example stocks
String[] stockSymbols = {"AAPL", "GOOG", "AMZN"};
double[] stockPrices = {150.25, 2800.75, 3400.50};
for (int i = 0; i < stockSymbols.length; i++) {
JPanel stockPanel = new JPanel();
stockPanel.setLayout(new FlowLayout());
JLabel stockLabel = new JLabel(stockSymbols[i] + " - $" + stockPrices[i]);
JButton buyButton = new JButton("Buy");
JButton sellButton = new JButton("Sell");
// Buy stock logic
buyButton.addActionListener(e -> {
double price = stockPrices[i];
currentUser.buyStock(stockSymbols[i], price);
JOptionPane.showMessageDialog(frame, "You've bought 1 share of " + stockSymbols[i]);
});
// Sell stock logic
sellButton.addActionListener(e -> {
double price = stockPrices[i];
currentUser.sellStock(stockSymbols[i], price);
JOptionPane.showMessageDialog(frame, "You've sold 1 share of " + stockSymbols[i]);
});
stockPanel.add(stockLabel);
stockPanel.add(buyButton);
stockPanel.add(sellButton);
stockListPanel.add(stockPanel);
}
JButton backButton = new JButton("Back to Main Menu");
backButton.addActionListener(e -> showMainMenu());
stockListPanel.add(backButton);
frame.getContentPane().removeAll();
frame.getContentPane().add(stockListPanel);
frame.revalidate();
}
// Show user portfolio
private static void showPortfolioPanel() {
JPanel portfolioPanel = new JPanel();
portfolioPanel.setLayout(new BoxLayout(portfolioPanel, BoxLayout.Y_AXIS));
JLabel portfolioLabel = new JLabel("Here's your portfolio:");
portfolioLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
portfolioPanel.add(portfolioLabel);
// Show user’s stocks (just sample data for now)
HashMap<String, Integer> portfolio = currentUser.getPortfolio();
for (String stockSymbol : portfolio.keySet()) {
JLabel stockLabel = new JLabel(stockSymbol + " - " + portfolio.get(stockSymbol) + " shares");
portfolioPanel.add(stockLabel);
}
JButton backButton = new JButton("Back to Main Menu");
backButton.addActionListener(e -> showMainMenu());
portfolioPanel.add(backButton);
frame.getContentPane().removeAll();
frame.getContentPane().add(portfolioPanel);
frame.revalidate();
}
}
class User {
private String username;
private double balance;
private HashMap<String, Integer> portfolio;
public User(String username) {
this.username = username;
this.balance = 10000; // Starting balance
this.portfolio = new HashMap<>();
}
public String getUsername() {
return username;
}
public HashMap<String, Integer> getPortfolio() {
return portfolio;
}
public void buyStock(String symbol, double price) {
if (balance >= price) {
balance -= price;
portfolio.put(symbol, portfolio.getOrDefault(symbol, 0) + 1);
}
}
public void sellStock(String symbol, double price) {
if (portfolio.containsKey(symbol) && portfolio.get(symbol) > 0) {
portfolio.put(symbol, portfolio.get(symbol) - 1);
balance += price;
}
}
}
class Authentication {
private HashMap<String, User> users;
public Authentication() {
users = new HashMap<>();
}
public boolean registerUser(String username, String password) {
if (users.containsKey(username)) {
return false; // Username already taken
}
users.put(username, new User(username));
return true;
}
public User login(String username, String password) {
return users.get(username); // Just checking the username for now
}
}