-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStockTracker.java
54 lines (47 loc) · 1.51 KB
/
StockTracker.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
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;
public class StockTracker {
private String[] stocks = {"AAPL", "GOOG", "TSLA"};
private double[] prices = {120.0, 200.0, 500.0};
public void showUI() {
JFrame frame = new JFrame("Stock Tracker");
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JTextField stockField = new JTextField();
panel.add(stockField, BorderLayout.NORTH);
JButton getPriceButton = new JButton("Get Price");
getPriceButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String stock = stockField.getText();
int index = -1;
for (int i = 0; i < stocks.length; i++) {
if (stocks[i].equals(stock)) {
index = i;
break;
}
}
if (index == -1) {
JOptionPane.showMessageDialog(frame, "Stock not found");
} else {
JOptionPane.showMessageDialog(frame, "Price: $" + prices[index]);
}
}
});
panel.add(getPriceButton, BorderLayout.SOUTH);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
StockTracker tracker = new StockTracker();
tracker.showUI();
}
}