-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStocksApp.java
324 lines (253 loc) · 8.05 KB
/
StocksApp.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
public class StocksApp extends Frame{
// Starting message
private static TextArea infoArea = new TextArea("Welcome To The Investment App");
public static void print(String text){
infoArea.setText(text);
}
private Agent agent;
private Panel ClientButtonsPanel;
// checks and outputs wether deposit, withdraw, buy or sell is successfull or unsuccessfull.
public void deposit(String n, int a){
boolean valid = agent.deposit(n,a);
if(valid != true){
print("Deposit Unsuccessfull");
}
else{
print("Deposit Successfull");
}
}
public void withdraw(String name, int amount){
boolean valid = agent.withdraw(name,amount);
if(valid != true){
print("Withdraw Unsuccessfull");
}
else{
print("Withdraw Successfull");
}
}
public void buy(String name, int amount){
boolean valid = agent.buyStock(name,amount);
if(valid != true){
print("Purchase Unsuccessfull");
}
else{
print("Purchase Successfull");
}
}
public void sell(String name, int amount){
boolean valid = agent.sellStock(name,amount);
if(valid != true){
print("Sell Unsuccessfull");
}
else{
print("Sell Successfull");
}
}
public void printClients(){
String text = agent.getListOfClientNames();
print(text);
}
public void printClientInfo(int index){
String text = agent.getClientInfo(index);
print(text);
}
public void addClient(String name){
agent.addClient(new Client(name));
int numOfClients = agent.getNumberOfClients();
Button btn = new Button("Client " + numOfClients);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn){
printClientInfo(numOfClients-1);
}
}
});
this.setVisible(false);
}
public void addStock(String name){
agent.addClient(new Client(name));
// Uncomment for R3
int numOfClients = agent.getNumberOfClients();
Button btn = new Button("Client " + numOfClients);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btn){
printClientInfo(numOfClients-1);
}
}
});
this.setVisible(false);
}
public StocksApp(){
this.agent = new Agent();
this.setLayout(new FlowLayout());
// Button to view instructions
Button homeButton=new Button("View Instructions");
homeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
print("Welcome to The Investment Trading App. In this App you will have the ability to View" + "\n " +
"your Portfolio, Buy securities, Sell securities, Deposit funds and Withdraw funds. Use " + "\n" +
"the features provided to navigate through the app and perform necessary tasks. For the purpose" + "\n" +
"of this Investment App you will be allocated with the Account 'Mohammad Al-Mousawi'" + "\n" +
"and starting amount of £500.");
}
});
this.add(homeButton);
// Button to view portfolio
Button reportButton=new Button("View Portfolio");
reportButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == reportButton){
printClients();
}
}
});
this.add(reportButton);
// Button to deposit and uses try and catch to make sure an integer was inputed.
Button depositButton = new Button("Deposit");
depositButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
Prompt acp = new Prompt();
TextField b = new TextField("Deposit Amount: ");
acp.add(b);
acp.addSubmitListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
try{
int amount = Integer.parseInt(b.getText());
deposit("Mohammad Al-Mousawi",amount);
}
catch (Exception e){
print("Input must be an Integer");
}
}
});
acp.activate();
}
});
this.add(depositButton);
// Button to withdraw and uses try and catch to make sure an integer was inputed.
Button withdrawButton = new Button("Withdraw");
withdrawButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
Prompt acp = new Prompt();
TextField b = new TextField("Withdraw Amount: ");
acp.add(b);
acp.addSubmitListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
try{
int amount = Integer.parseInt(b.getText());
withdraw("Mohammad Al-Mousawi",amount);
}
catch (Exception e){
print("Input must be an Integer");
}
}
});
//...
acp.activate();
}
});
this.add(withdrawButton);
// Button to buy stocks and prints stocks' prices, also uses try and catch to check if an integer is inputted.
Button buyButton = new Button("Buy Securities");
buyButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
print(agent.getClientInfo(0)+ "\n" +"Tesla: £1000 per Share" + "\n" + "Amazon: £500 per Share" + "\n" + "Apple: £750 per Share");
Prompt acp = new Prompt();
TextField a = new TextField("Enter Security name: ");
acp.add(a);
TextField b = new TextField("Buy Amount: ");
acp.add(b);
acp.addSubmitListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
String name = a.getText();
try{
int amount = Integer.parseInt(b.getText());
buy(name,amount);
}
catch (Exception e){
print("Input must be an Integer");
}
}
});
//...
acp.activate();
}
});
this.add(buyButton);
// Button to sell stocks and prints stocks' prices, also uses try and catch to check if an integer is inputted.
Button sellButton = new Button("Sell Securities");
sellButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
print(agent.getClientInfo(0)+"\n" +"Tesla: £1000 per Share" + "\n" + "Amazon: £500 per Share" + "\n" + "Apple: £750 per Share");
Prompt acp = new Prompt();
TextField a = new TextField("Enter Security name: ");
acp.add(a);
TextField b = new TextField("Sell Amount: ");
acp.add(b);
acp.addSubmitListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
String name = a.getText();
try{
int amount = Integer.parseInt(b.getText());
sell(name,amount);
}
catch (Exception e){
print("Input must be an Integer");
}
}
});
//...
acp.activate();
}
});
this.add(sellButton);
// Button that access the text file to print the stocks' prices from last year.
Button viewLastYearButton = new Button("View Last Year Stock Prices");
viewLastYearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
try {
BufferedReader inputStream = new BufferedReader(new FileReader("lastYear.txt"));
String l = inputStream.readLine() + "\n"+inputStream.readLine() + "\n"+inputStream.readLine() + "\n"
+inputStream.readLine() + "\n"+inputStream.readLine() + "\n";
print(l);
inputStream.close();
}catch (Exception e) {
print("Error occured");
}
}
});
this.add(viewLastYearButton);
infoArea.setEditable(false);
this.add(infoArea);
ClientButtonsPanel = new Panel();
ClientButtonsPanel.setLayout(new GridLayout(0,1));
ClientButtonsPanel.setVisible(true);
this.add(ClientButtonsPanel);
// Adding the client and the stocks to act as a client
this.addClient("Mohammad Al-Mousawi");
this.addClient("Tesla");
this.addClient("Apple");
this.addClient("Amazon");
deposit("Mohammad Al-Mousawi",500);
// This allows the window to close by pressing 'X'
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
this.setSize(500,500);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args){
new StocksApp();
}
}