-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTradingSystemGUI.java
More file actions
269 lines (214 loc) · 8.39 KB
/
TradingSystemGUI.java
File metadata and controls
269 lines (214 loc) · 8.39 KB
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
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
public class TradingSystemGUI {
private BPlusTree tradingSystem = new BPlusTree(5);
private DefaultTableModel tableModel;
private JTextField stockField, timeField, priceField, volumeField;
private JTextField startField, endField;
private JTextArea output;
public TradingSystemGUI() {
JFrame frame = new JFrame("Financial Trading System");
frame.setSize(1200, 700);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.getContentPane().setBackground(new Color(245, 247, 250));
frame.setLocationRelativeTo(null);
// ===== TITLE =====
JLabel title = new JLabel("Financial Trading Dashboard", JLabel.CENTER);
title.setFont(new Font("Segoe UI", Font.BOLD, 28));
title.setBorder(BorderFactory.createEmptyBorder(15, 10, 15, 10));
frame.add(title, BorderLayout.NORTH);
// ===== LEFT PANEL =====
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBorder(BorderFactory.createTitledBorder("Trade Input"));
leftPanel.setPreferredSize(new Dimension(300, 400));
leftPanel.setBackground(Color.WHITE);
JPanel inputPanel = new JPanel(new GridLayout(10, 1, 10, 10));
inputPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
inputPanel.setBackground(Color.WHITE);
stockField = new JTextField();
timeField = new JTextField();
priceField = new JTextField();
volumeField = new JTextField();
inputPanel.add(label("Stock ID", stockField));
inputPanel.add(label("Timestamp", timeField));
inputPanel.add(label("Price", priceField));
inputPanel.add(label("Volume", volumeField));
JButton insertBtn = button("Insert", new Color(46, 204, 113));
JButton updateBtn = button("Update", new Color(52, 152, 219));
JButton deleteBtn = button("Delete", new Color(231, 76, 60));
inputPanel.add(insertBtn);
inputPanel.add(updateBtn);
inputPanel.add(deleteBtn);
leftPanel.add(inputPanel);
frame.add(leftPanel, BorderLayout.WEST);
// ===== TABLE =====
String[] columns = {"Stock", "Timestamp", "Price", "Volume"};
tableModel = new DefaultTableModel(columns, 0);
JTable table = new JTable(tableModel);
table.setRowHeight(28);
table.setFont(new Font("Segoe UI", Font.PLAIN, 14));
table.getTableHeader().setFont(new Font("Segoe UI", Font.BOLD, 15));
table.setSelectionBackground(new Color(52, 152, 219));
table.setSelectionForeground(Color.WHITE);
table.setFillsViewportHeight(true);
JScrollPane scrollPane = new JScrollPane(table);
frame.add(scrollPane, BorderLayout.CENTER);
// ===== ANALYTICS PANEL =====
JPanel rightPanel = new JPanel(new GridLayout(6, 1, 15, 15));
rightPanel.setBorder(BorderFactory.createTitledBorder("Analytics"));
rightPanel.setPreferredSize(new Dimension(300, 400));
rightPanel.setBackground(Color.WHITE);
startField = new JTextField();
endField = new JTextField();
JButton rangeBtn = button("Range Query", new Color(155, 89, 182));
JButton trendBtn = button("Trend Analysis", new Color(241, 196, 15));
rightPanel.add(label("Start Timestamp", startField));
rightPanel.add(label("End Timestamp", endField));
rightPanel.add(rangeBtn);
rightPanel.add(trendBtn);
frame.add(rightPanel, BorderLayout.EAST);
// ===== OUTPUT =====
output = new JTextArea(6, 20);
output.setFont(new Font("Consolas", Font.PLAIN, 13));
output.setEditable(false);
frame.add(new JScrollPane(output), BorderLayout.SOUTH);
// ===== TABLE CLICK AUTO-FILL =====
table.getSelectionModel().addListSelectionListener(e -> {
int row = table.getSelectedRow();
if (row != -1) {
stockField.setText(tableModel.getValueAt(row, 0).toString());
timeField.setText(tableModel.getValueAt(row, 1).toString());
priceField.setText(tableModel.getValueAt(row, 2).toString());
volumeField.setText(tableModel.getValueAt(row, 3).toString());
}
});
// ===== BUTTON ACTIONS =====
insertBtn.addActionListener(e -> {
try {
Trade t = new Trade(
stockField.getText(),
Long.parseLong(timeField.getText()),
Double.parseDouble(priceField.getText()),
Integer.parseInt(volumeField.getText())
);
tradingSystem.insert(t);
tableModel.addRow(new Object[]{
t.getStockId(), t.getTimestamp(), t.getPrice(), t.getVolume()
});
output.append("Trade inserted successfully\n");
clear();
} catch (Exception ex) {
output.append("Invalid input\n");
}
});
updateBtn.addActionListener(e -> {
try {
long ts = Long.parseLong(timeField.getText());
double price = Double.parseDouble(priceField.getText());
int vol = Integer.parseInt(volumeField.getText());
if (tradingSystem.update(ts, price, vol)) {
updateTableRow(ts, price, vol);
output.append("Trade updated\n");
} else {
output.append("Trade not found\n");
}
} catch (Exception ex) {
output.append("Update error\n");
}
});
deleteBtn.addActionListener(e -> {
try {
long ts = Long.parseLong(timeField.getText());
if (tradingSystem.delete(ts)) {
removeTableRow(ts);
output.append("Trade deleted\n");
} else {
output.append("Trade not found\n");
}
} catch (Exception ex) {
output.append("Delete error\n");
}
});
rangeBtn.addActionListener(e -> {
try {
long start = Long.parseLong(startField.getText());
long end = Long.parseLong(endField.getText());
QueryResult r = tradingSystem.rangeQuery(start, end);
if (r.foundTrades) {
output.append("\nRange Results:\n");
output.append("Volume: " + r.totalVolume + "\n");
output.append("Max: " + r.maxPrice + "\n");
output.append("Min: " + r.minPrice + "\n");
} else {
output.append("No data\n");
}
} catch (Exception ex) {
output.append("Range error\n");
}
});
trendBtn.addActionListener(e -> {
try {
long start = Long.parseLong(startField.getText());
long end = Long.parseLong(endField.getText());
QueryResult r = tradingSystem.rangeQuery(start, end);
double vol = (r.maxPrice - r.minPrice) /
((r.maxPrice + r.minPrice) / 2);
output.append("\nTrend Analysis:\n");
output.append("Volatility: " + (vol * 100) + "%\n");
if (vol > 0.05)
output.append("HIGH VOLATILITY\n");
else
output.append("STABLE MARKET\n");
} catch (Exception ex) {
output.append("Trend error\n");
}
});
frame.setVisible(true);
}
// ===== HELPERS =====
private JPanel label(String text, JTextField field) {
JPanel p = new JPanel(new BorderLayout());
p.setBackground(Color.WHITE);
JLabel lbl = new JLabel(text);
lbl.setFont(new Font("Segoe UI", Font.BOLD, 13));
p.add(lbl, BorderLayout.NORTH);
p.add(field, BorderLayout.CENTER);
return p;
}
private JButton button(String text, Color color) {
JButton b = new JButton(text);
b.setBackground(color);
b.setForeground(Color.WHITE);
b.setFont(new Font("Segoe UI", Font.BOLD, 14));
b.setFocusPainted(false);
return b;
}
private void clear() {
stockField.setText("");
timeField.setText("");
priceField.setText("");
volumeField.setText("");
}
private void updateTableRow(long ts, double price, int vol) {
for (int i = 0; i < tableModel.getRowCount(); i++) {
if ((long) tableModel.getValueAt(i, 1) == ts) {
tableModel.setValueAt(price, i, 2);
tableModel.setValueAt(vol, i, 3);
break;
}
}
}
private void removeTableRow(long ts) {
for (int i = 0; i < tableModel.getRowCount(); i++) {
if ((long) tableModel.getValueAt(i, 1) == ts) {
tableModel.removeRow(i);
break;
}
}
}
public static void main(String[] args) {
new TradingSystemGUI();
}
}