-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockMarketSystem
398 lines (312 loc) · 14.3 KB
/
StockMarketSystem
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
public class StockMarketSystem {
private static DefaultTableModel stockTableModel;
private static DefaultTableModel userStockTableModel;
private static double totalAmount = 0.0;
private static double userBalance = 10000.0;
private static List<UserStock> userStocks = new ArrayList<>();
public static void main(String[] args) {
JFrame frame = new JFrame("Stock Market System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane();
JPanel stockPanel = createStockPanel();
JPanel buySellPanel = createBuySellPanel();
JPanel newsPanel = createNewsPanel();
JPanel indicesPanel = createIndicesPanel();
tabbedPane.addTab("Stocks", stockPanel);
tabbedPane.addTab("Buy/Sell", buySellPanel);
tabbedPane.addTab("News", newsPanel);
tabbedPane.addTab("Indices", indicesPanel);
tabbedPane.setBackground(Color.blue);
tabbedPane.setForeground(Color.white);
Font font = new Font("Arial", Font.CENTER_BASELINE, 20);
tabbedPane.setFont(font);
/* JLabel lab = new JLabel();
lab.setPreferredSize(new Dimension(500, 30));
JTabbedPane.setTabComponentAt(0, lab);
JLabel lab1 = new JLabel();
lab.setPreferredSize(new Dimension(500, 30));
jTabbedPane.setTabComponentAt(1, lab1);
JLabel lab2 = new JLabel();
lab.setPreferredSize(new Dimension(500, 30));
jTabbedPane.setTabComponentAt(2, lab2);
JLabel lab3 = new JLabel();
lab.setPreferredSize(new Dimension(500, 30));
jTabbedPane.setTabComponentAt(3, lab3);*/
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,550);
frame.setVisible(true);
}
private static JPanel createStockPanel() {
JPanel panel = new JPanel(new BorderLayout());
String[] columnNames = {"Stock Name", "Price (Rs.)", "Change in %(rise/fall)"};
Object[][] data = {
{"Zydus life", 625, 4.67},
{"Ashok Leyland", 173.55,2.84},
{"Tvs motor", 1646.95, 2.26},
{"BHEL", 125.30, -2.93},
{"ICICI Prudentia", 530.6, -1.36},
{"Infosys", 1391, - 0.95},
{"L&T Finance", 140.95, -0.63},
{"IRCTC", 676.90, -0.62},
{"Cipla", 1241, 24.55},
{"Torrent pharma", 2028.10, +1.49}
};
stockTableModel = new DefaultTableModel(data, columnNames);
JTable stockTable = new JTable(stockTableModel);
// Create custom renderer for the "Change" column
TableCellRenderer changeRenderer = new ChangeCellRenderer();
stockTable.getColumnModel().getColumn(2).setCellRenderer(changeRenderer);
stockTable.setBackground(Color.white);
stockTable.setForeground(Color.blue);
JTableHeader tableHeader = stockTable.getTableHeader();
Font headerFont = new Font("Arial", Font.BOLD,20);
tableHeader.setFont(headerFont);
JScrollPane scrollPane = new JScrollPane(stockTable);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
// Custom renderer for the "Change" column
private static class ChangeCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component renderer = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
double change = (double) value;
if (change > 0) {
// Positive change, display in green with a green triangle
renderer.setForeground(Color.GREEN);
// renderer.setIcon(new ImageIcon(new ImageIcon("C:\\Users\\a\\Downloads\\green triangle").getImage().getScaledInstance(12, 12, Image.SCALE_DEFAULT)));
} else if (change < 0) {
// Negative change, display in red with a red triangle
renderer.setForeground(Color.RED);
// renderer.setIcon(new ImageIcon(new ImageIcon("C:\\Users\\a\\Downloads\\red-triangle-pointed-down").getImage().getScaledInstance(12, 12, Image.SCALE_DEFAULT)));
} else {
// No change, use the default color and no icon
renderer.setForeground(table.getForeground());
//renderer.setIcon(null);
}
return renderer;
}
}
private static JPanel createBuySellPanel() {
JPanel panel = new JPanel(new GridLayout(4, 2));
JLabel stockLabel = new JLabel("Select Stock:");
JComboBox<String> stockComboBox = new JComboBox<>();
for (int row = 0; row < stockTableModel.getRowCount(); row++) {
stockComboBox.addItem((String) stockTableModel.getValueAt(row, 0));
}
JLabel quantityLabel = new JLabel("Enter Quantity:");
JTextField quantityField = new JTextField();
JLabel actionLabel = new JLabel("Action (Buy/Sell):");
JComboBox<String> actionComboBox = new JComboBox<>(new String[] {"Buy", "Sell"});
JButton executeButton = new JButton("Execute");
JButton currentStatusButton = new JButton("Current Status");
panel.add(stockLabel);
panel.add(stockComboBox);
panel.add(quantityLabel);
panel.add(quantityField);
panel.add(actionLabel);
panel.add(actionComboBox);
panel.add(currentStatusButton);
panel.add(executeButton);
executeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedStock = (String) stockComboBox.getSelectedItem();
int quantity = Integer.parseInt(quantityField.getText());
String action = (String) actionComboBox.getSelectedItem();
if (action.equals("Buy")) {
buyStock(selectedStock, quantity);
} else if (action.equals("Sell")) {
sellStock(selectedStock, quantity);
}
}
});
currentStatusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
showCurrentStatus();
}
});
return panel;
}
private static void createUserStockTableModel() {
if (userStockTableModel == null) {
String[] columnNames = {"Stock Name", "Quantity", "Action","Stock price"};
userStockTableModel = new DefaultTableModel(null, columnNames);
}
}
private static void buyStock(String stockName, int quantity) {
int row = findStockRowIndex(stockName);
if (row == -1) {
return;
}
double stockPrice = (double) stockTableModel.getValueAt(row, 1);
double totalPrice = stockPrice * quantity;
if (totalPrice <= getUserBalance()) {
// Update the user's stock data
int userRow = findUserStockRowIndex(stockName);
if (userRow == -1) {
userStocks.add(new UserStock(stockName, quantity, "Buy", stockPrice));
} else {
int currentQuantity = userStocks.get(userRow).getQuantity();
userStocks.get(userRow).setQuantity(currentQuantity + quantity);
}
updateUserBalance(getUserBalance() - totalPrice);
totalAmount += totalPrice;
userStockTableModel.setValueAt((double) userStockTableModel.getValueAt(row, 1) *quantity, row, 1);
showCurrentStatus();
} else {
JOptionPane.showMessageDialog(null, "Insufficient balance to buy this stock.");
}
}
private static void sellStock(String stockName, int quantity) {
int userRow = findUserStockRowIndex(stockName);
if (userRow == -1) {
JOptionPane.showMessageDialog(null, "You don't have any of this stock to sell.");
return;
}
int currentQuantity = userStocks.get(userRow).getQuantity();
if (currentQuantity >= quantity) {
userStocks.get(userRow).setQuantity(currentQuantity - quantity);
double stockPrice = (double) stockTableModel.getValueAt(findStockRowIndex(stockName), 1);
updateUserBalance(getUserBalance() + stockPrice * quantity);
totalAmount -= stockPrice * quantity;
userStockTableModel.setValueAt((double) userStockTableModel.getValueAt(findStockRowIndex(stockName), 1) + quantity, findStockRowIndex(stockName), 1);
showCurrentStatus();
} else {
JOptionPane.showMessageDialog(null, "You don't have enough of this stock to sell.");
}
}
private static void showCurrentStatus() {
createUserStockTableModel();
userStockTableModel.setRowCount(0);
for (UserStock userStock : userStocks) {
userStockTableModel.addRow(new Object[]{
userStock.getStockName(),
userStock.getQuantity(),
userStock.getAction(),
userStock.getStockPrice() // Add stock price
});
}
JFrame statusFrame = new JFrame("Current Stock Status");
statusFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
statusFrame.setSize(400, 300);
JTable userStockTable = new JTable(userStockTableModel);
userStockTable.setBackground(Color.white);
userStockTable.setForeground(Color.blue);
JTableHeader tableHeader = userStockTable.getTableHeader();
Font headerFont = new Font("Arial", Font.BOLD,20);
tableHeader.setFont(headerFont);
JScrollPane scrollPane = new JScrollPane(userStockTable);
// panel.add(scrollPane, BorderLayout.CENTER);
// JScrollPane scrollPane = new JScrollPane(userStockTable);
statusFrame.add(scrollPane);
JLabel totalAmountLabel = new JLabel("Total Stock Investment: Rs." + totalAmount+" "+"User Balance: Rs." + userBalance);
totalAmountLabel.setFont(new Font("Arial", Font.BOLD, 16));
statusFrame.add(totalAmountLabel,BorderLayout.SOUTH);
statusFrame.setVisible(true);
/* JLabel userBalanceLabel = new JLabel("User Balance: Rs." + userBalance);
userBalanceLabel.setFont(new Font("Arial", Font.BOLD, 16));
statusFrame.add(userBalanceLabel);
statusFrame.setVisible(true);*/
}
private static int findStockRowIndex(String stockName) {
for (int row = 0; row < stockTableModel.getRowCount(); row++) {
if (stockName.equalsIgnoreCase((String) stockTableModel.getValueAt(row, 0))) {
return row;
}
}
return -1;
}
private static int findUserStockRowIndex(String stockName) {
for (int i = 0; i < userStocks.size(); i++) {
if (stockName.equalsIgnoreCase(userStocks.get(i).getStockName())) {
return i;
}
}
return -1;
}
private static double getUserBalance() {
return userBalance;
}
private static void updateUserBalance(double newBalance) {
userBalance = newBalance;
}
public static class UserStock {
private String stockName;
private int quantity;
private String action;
private double stockPrice;
public UserStock(String stockName, int quantity, String action,double stockPrice) {
this.stockName = stockName;
this.quantity = quantity;
this.action = action;
this.stockPrice = stockPrice;
}
public String getStockName() {
return stockName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public String getAction() {
return action;
}
public double getStockPrice() {
return stockPrice;
}
}
private static JPanel createNewsPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JLabel newsLabel1 = new JLabel("->Buy Zydus wellness; Target of Rs.1800.");
newsLabel1.setFont(new Font("Arial", Font.BOLD, 18));
JLabel newsLabel2 = new JLabel("->Equity mutual funds get close to 20000 crore net inflow.");
newsLabel2.setFont(new Font("Arial", Font.BOLD, 18));
JLabel newsLabel3 = new JLabel("->Shubh mahurat: went to buy gold this year.");
newsLabel3.setFont(new Font("Arial", Font.BOLD, 18));
JLabel newsLabel4 = new JLabel("->Infosys preview: profit to jump on robust auto scales.");
newsLabel4.setFont(new Font("Arial", Font.BOLD, 18));
panel.add(newsLabel1);
panel.add(newsLabel2);
panel.add(newsLabel3);
panel.add(newsLabel4);
return panel;
}
private static JPanel createIndicesPanel() {
JPanel panel = new JPanel(new GridLayout(2, 1));
JLabel niftyLabel = createIndexLabel("Nifty", 15300, false);
JLabel sensexLabel = createIndexLabel("Sensex", 53700, true);
panel.add(niftyLabel);
panel.add(sensexLabel);
JLabel label=new JLabel();
ImageIcon icon = new ImageIcon(new ImageIcon("NIFTY.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
label.setIcon(icon);
//
// ImageIcon icon = new ImageIcon(new ImageIcon("NIFTY.png").getImage().getScaledInstance(200, 200, Image.SCALE_DEFAULT));
// JLabel displayField=new JLabel(icon);
// panel.add(displayField);
//panel.setVisible(true);
return panel;
}
private static JLabel createIndexLabel(String indexName, double value, boolean isLoss) {
JLabel label = new JLabel(indexName + ": " + value + " ");
label.setFont(new Font("Arial", Font.BOLD, 18));
label.setForeground(isLoss ? Color.RED : Color.GREEN);
return label;
}
}