-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyInterface.java
More file actions
109 lines (96 loc) · 4.02 KB
/
Copy pathBuyInterface.java
File metadata and controls
109 lines (96 loc) · 4.02 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
package ePortfolio;
import javax.swing.*;
import java.awt.*;
/**
* The BuyInterface class provides a graphical user interface for buying
* investments in the portfolio.
* Users can input details such as type, symbol, name, quantity, and price to
* purchase
* a new investment or add to an existing one.
*/
public class BuyInterface extends JFrame {
private final Portfolio portfolio;
private final JTextField symbolField, nameField, quantityField, priceField;
private final JComboBox<String> typeComboBox;
private final JTextArea messageArea;
/**
* Constructs the BuyInterface with input fields, buttons, and message display.
*
* @param parent the parent JFrame that invoked this interface
* @param portfolio the portfolio where the investment will be added
*/
public BuyInterface(JFrame parent, Portfolio portfolio) {
this.portfolio = portfolio;
setTitle("Buy Investment");
setSize(600, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Create components
JPanel inputPanel = new JPanel(new GridLayout(5, 2, 10, 10));
JLabel typeLabel = new JLabel("Type:");
typeComboBox = new JComboBox<>(new String[] { "Stock", "MutualFund" });
JLabel symbolLabel = new JLabel("Symbol:");
symbolField = new JTextField();
JLabel nameLabel = new JLabel("Name:");
nameField = new JTextField();
JLabel quantityLabel = new JLabel("Quantity:");
quantityField = new JTextField();
JLabel priceLabel = new JLabel("Price:");
priceField = new JTextField();
inputPanel.add(typeLabel);
inputPanel.add(typeComboBox);
inputPanel.add(symbolLabel);
inputPanel.add(symbolField);
inputPanel.add(nameLabel);
inputPanel.add(nameField);
inputPanel.add(quantityLabel);
inputPanel.add(quantityField);
inputPanel.add(priceLabel);
inputPanel.add(priceField);
JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 10));
JButton buyButton = new JButton("Buy");
JButton resetButton = new JButton("Reset");
buttonPanel.add(buyButton);
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
buyButton.addActionListener(e -> handleBuy());
resetButton.addActionListener(e -> resetFields());
setVisible(true);
}
/**
* Handles the "Buy" button click event.
* Validates input and attempts to buy the investment using the portfolio
* object.
* Displays a success or error message in the message area.
*/
private void handleBuy() {
try {
String type = (String) typeComboBox.getSelectedItem();
String symbol = symbolField.getText().trim();
String name = nameField.getText().trim();
int quantity = Integer.parseInt(quantityField.getText().trim());
double price = Double.parseDouble(priceField.getText().trim());
portfolio.buyInvestment(type, symbol, name, quantity, price);
messageArea.setText("Successfully purchased investment: " + symbol);
} catch (Exception ex) {
messageArea.setText("Error: " + ex.getMessage());
}
}
/**
* Resets all input fields in the form, except the investment type ComboBox.
*/
private void resetFields() {
symbolField.setText("");
nameField.setText("");
quantityField.setText("");
priceField.setText("");
}
}