-
Notifications
You must be signed in to change notification settings - Fork 0
/
StockOptionsWindow.java
195 lines (163 loc) · 5.99 KB
/
StockOptionsWindow.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
/**
* @version 1.0.0, 9 May 2023
* @author Andrew Kim and Dylan Nguyen
*/
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StockOptionsWindow extends JFrame {
Runtime runtime = Runtime.getRuntime();
public StockOptionsWindow(Stock stock, int x, int y) {
super();
// set up window
setLocation(x, y);
setTitle("Stock Options for " + stock.getTicker() + " - " + stock.getName());
setSize(450 * (Options.getFont().getSize() / 10), 200 * (Options.getFont().getSize() / 10));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setAlwaysOnTop(true);
setVisible(true);
reload(stock);
}
public void reload(Stock stock) {
revalidate();
repaint();
// set up layout
GridLayout layout = new GridLayout(0, 3);
JPanel panel = new JPanel();
add(panel, BorderLayout.CENTER);
panel.setLayout(layout);
// add components
// first row
// ticker
panel.add(new ScaledLabel(stock.getTicker()));
// current price
panel.add(new ScaledLabel("$ " + stock.getTransactionPrice()));
// day change
panel.add(new ScaledLabel(Broker.getStock(stock.getTicker()).getDayChangeFormatted()));
// 30-day trading metrics
panel.add(new ScaledLabel("Month / Year Change:"));
panel.add(new ScaledLabel(stock.getMonthChangeFormatted()));
panel.add(new ScaledLabel(stock.getYearChangeFormatted()));
// highs and lows
panel.add(new ScaledLabel("Year High / Low:"));
panel.add(new ScaledLabel("" + stock.getYearHigh()));
panel.add(new ScaledLabel("" + stock.getYearLow()));
// current balance label
panel.add(new ScaledLabel("Current Balance: "));
// current balance
panel.add(new ScaledLabel("$ " + Portfolio.getBalance()));
// @dylan what be this
double percentage = (stock.getTransactionPrice() / Portfolio.getBalance());
percentage = Math.round(percentage * 1000) / 10.0;
ScaledLabel percentOfTotal = new ScaledLabel(stock.getTicker() + " : " + percentage + "%");
panel.add(percentOfTotal);
panel.add(new ScaledLabel("Owned: "));
panel.add(new ScaledLabel("" + Portfolio.getStockAmount(stock.getTicker())));
ScaledLabel amountOwnedWorth = new ScaledLabel(
"$ " + Stock.round(stock.getTransactionPrice() * Portfolio.getStockAmount(stock.getTicker())));
panel.add(amountOwnedWorth);
panel.add(new ScaledLabel("Buy: "));
JTextField buyAmountInput = new JTextField();
buyAmountInput.setFont(Options.getFont());
buyAmountInput.setText("1");
panel.add(buyAmountInput);
Button buyButton = new Button("Buy");
buyButton.setFont(Options.getFont());
buyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// https://www.tutorialspoint.com/java/character_isdigit.htm
for (int i = 0; i < buyAmountInput.getText().length(); i++) {
if (!Character.isDigit(buyAmountInput.getText().charAt(i))) {
JOptionPane.showMessageDialog(null, "Please enter a valid number.");
return;
}
}
int amount = Integer.parseInt(buyAmountInput.getText());
double cost = Portfolio.buyStock(stock.getTicker(), amount);
if (cost != -1) {
if (Options.popupsEnabled())
JOptionPane.showMessageDialog(null,
"Successfully bought " + amount + " shares of " + stock.getTicker() + " for " + cost);
Options.saveOptions();
remove(panel);
reload(stock);
} else {
JOptionPane.showMessageDialog(null, "Could not buy stock. Please enter a lower number.");
}
}
});
panel.add(buyButton);
ScaledLabel label2 = new ScaledLabel("Sell: ");
panel.add(label2);
JTextField sellAmountInput = new JTextField();
sellAmountInput.setFont(Options.getFont());
sellAmountInput.setText("1");
panel.add(sellAmountInput);
Button sellButton = new Button("Sell");
sellButton.setFont(Options.getFont());
sellButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// https://www.tutorialspoint.com/java/character_isdigit.htm
for (int i = 0; i < sellAmountInput.getText().length(); i++) {
if (!Character.isDigit(sellAmountInput.getText().charAt(i))) {
JOptionPane.showMessageDialog(null, "Please enter a valid number.");
return;
}
}
int amount = Integer.parseInt(sellAmountInput.getText());
double cost = Stock.round(Portfolio.sellStock(stock.getTicker(), amount));
if (cost != -1) {
if (Options.popupsEnabled())
JOptionPane.showMessageDialog(null,
"Successfully sold " + amount + " shares of " + stock.getTicker() + " for " + cost);
Options.saveOptions();
remove(panel);
reload(stock);
} else
JOptionPane.showMessageDialog(null, "Could not sell stock. Please enter a lower number.");
}
});
panel.add(sellButton);
ScaledLabel viewGraphLabel = new ScaledLabel("View graph");
panel.add(viewGraphLabel);
JComboBox<String> graphOptions = new JComboBox<String>();
graphOptions.setFont(Options.getFont());
graphOptions.addItem("day");
graphOptions.addItem("month");
graphOptions.addItem("year");
panel.add(graphOptions);
Button viewGraph = new Button("View graph");
viewGraph.setFont(Options.getFont());
panel.add(viewGraph);
viewGraph.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
viewGraph.setLabel("Loading graph...");
System.out.println("Trying to run "
+ ("python ./data/graph.py " + graphOptions.getSelectedItem() + " " + stock.getTicker()));
String[] command = { "python", "./data/graph.py", (String) graphOptions.getSelectedItem(),
stock.getTicker()
};
runtime.exec(command).getErrorStream();
} catch (IOException e1) {
e1.printStackTrace();
}
viewGraph.setLabel("View graph");
}
});
revalidate();
repaint();
}
}