-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSellInterface.java
More file actions
96 lines (83 loc) · 3.41 KB
/
Copy pathSellInterface.java
File metadata and controls
96 lines (83 loc) · 3.41 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
package ePortfolio;
import javax.swing.*;
import java.awt.*;
/**
* The SellInterface class provides a graphical user interface for selling
* investments in the portfolio.
* Users can input details such as symbol, quantity, and price to sell an
* existing investment.
*/
public class SellInterface extends JFrame {
private final Portfolio portfolio;
private final JTextField symbolField, quantityField, priceField;
private final JTextArea messageArea;
/**
* Constructs the SellInterface with input fields, buttons, and a message area.
*
* @param parent the parent JFrame that invoked this interface
* @param portfolio the portfolio from which investments will be sold
*/
public SellInterface(JFrame parent, Portfolio portfolio) {
this.portfolio = portfolio;
setTitle("Sell Investment");
setSize(600, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Components
JPanel inputPanel = new JPanel(new GridLayout(3, 2, 10, 10));
JLabel symbolLabel = new JLabel("Symbol:");
symbolField = new JTextField();
JLabel quantityLabel = new JLabel("Quantity:");
quantityField = new JTextField();
JLabel priceLabel = new JLabel("Price:");
priceField = new JTextField();
inputPanel.add(symbolLabel);
inputPanel.add(symbolField);
inputPanel.add(quantityLabel);
inputPanel.add(quantityField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 10));
JButton sellButton = new JButton("Sell");
JButton resetButton = new JButton("Reset");
buttonPanel.add(sellButton);
buttonPanel.add(resetButton);
messageArea = new JTextArea(10, 50);
messageArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(messageArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// Layout
setLayout(new BorderLayout(10, 10));
add(inputPanel, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.CENTER);
add(scrollPane, BorderLayout.SOUTH);
// Action listeners
sellButton.addActionListener(e -> handleSell());
resetButton.addActionListener(e -> resetFields());
setVisible(true);
}
/**
* Handles the "Sell" button click event.
* Validates input and attempts to sell the specified investment using the
* portfolio object.
* Displays a success or error message in the message area.
*/
private void handleSell() {
try {
String symbol = symbolField.getText().trim();
int quantity = Integer.parseInt(quantityField.getText().trim());
double price = Double.parseDouble(priceField.getText().trim());
double proceeds = portfolio.sellInvestment(symbol, quantity, price);
messageArea.setText("Successfully sold investment. Proceeds: $" + proceeds);
} catch (Exception ex) {
messageArea.setText("Error: " + ex.getMessage());
}
}
/**
* Resets all input fields in the form.
*/
private void resetFields() {
symbolField.setText("");
quantityField.setText("");
priceField.setText("");
}
}