-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
390 lines (344 loc) · 13.6 KB
/
Copy pathAccount.java
File metadata and controls
390 lines (344 loc) · 13.6 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package classes;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
public class Account {
private static final String CustomrtId = null;
String CustomerName,CustomerId;
String stockSymbol, StockItemSymbol[];
int date;
double InitialDeposit;
double quantity;
double stockPrice;
double Accumulated_Balance;
double amount;
double cashAmount;
double totalStockValue;
double StockItemQuantity[], StockItemUnitValue[],StockItemCashValue[];
private static String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static String DB_URL = "jdbc:mysql://localhost:3306/cs565";
private static String DB_Account_IDNAME = "cs";
private static String DB_PASSWORD = "java";
public Account(String CustomerId, String CustomerName, int OpeningDate, double InitialDeposit){
this.CustomerId = CustomerId;
this.CustomerName = CustomerName;
this.InitialDeposit = InitialDeposit;
this.Accumulated_Balance = InitialDeposit;
this.date=OpeningDate;
}
public void addToAccountTable(){
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
//insert the account holder information with initial balance
String sql = "insert into Account (CustomerId, CustomerName, OpeningDate, OpeningBalance) " +
"values ('"+CustomerId+"', '"+CustomerName+"','"+date+"','"+InitialDeposit+"')";
stmt.executeUpdate(sql);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//check if enough funds are available to buy
public boolean isValidBuy (String stockSymbol, double quant) {
this.stockSymbol=stockSymbol;
this.quantity=quant;
Stock m1 = new Stock();
stockPrice=m1.getStockPrice(stockSymbol);
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
//show the account holder information with current account balance
sql = "Select * from Account where CustomerId = '"+CustomerId+"' ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
this.Accumulated_Balance=rs.getDouble("Accumulated_Balance");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return (Accumulated_Balance>quantity*stockPrice);
}
public void buy(String stockSymbol, double quantity) {
this.stockSymbol=stockSymbol;
this.quantity=quantity;
if(isValidBuy (stockSymbol, quantity)){
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
sql = "insert into TRANSACTIONS (CustomerId,Transaction_Type,StockSymbol, Quantity) " +
"values ('"+CustomerId+"', 'Buy', '"+stockSymbol+"', '"+quantity+"')";
try {
stmt.executeUpdate(sql);
}catch (Exception e1) {
System.out.println("mistakes, check the name and/or transaction type");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("NO enough fund to buy");
}
}
//check if the customer has that many shares of that stock to sell.
public boolean isValidSell (String stockSymbol, double quant) {
this.quantity=quant;
double AvailableQuantity=0;
this.stockSymbol=stockSymbol;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
//show the account holder information with current stock
sql = "Select * from Account_Stock where CustomerId ='"+CustomerId+"' and StockSymbol = '"+stockSymbol+"' ";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
AvailableQuantity=rs.getDouble("StockQuantity");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return (AvailableQuantity>quantity);
}
public void sell(String stockSymbol, double quantity) {
this.stockSymbol=stockSymbol;
this.quantity=quantity;
if(isValidSell (stockSymbol, quantity)){
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
sql = "insert into TRANSACTIONS (CustomerId,Transaction_Type,StockSymbol, Quantity) " +
"values ('"+CustomerId+"', 'Sell', '"+stockSymbol+"', '"+quantity+"')";
try {
stmt.executeUpdate(sql);
}catch (Exception e1) {
System.out.println("mistakes, check the name and/or transaction type");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}else{
System.out.println("NO enough stock to sell");
}
}
public void deposit(double money) {
this.amount =money;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
//try to insert new transactions information (check the transaction type and the account holder name)
sql = "insert into TRANSACTIONS (CustomerId,Transaction_Type,PriceOrDepositAmount)" +
"values ('"+CustomerId+"', 'Deposit', '"+amount+"')";
try {
stmt.executeUpdate(sql);
}catch (Exception e1) {
System.out.println("mistakes in the information, check the name and/or transaction type");
}
//fetch transaction information and print it
System.out.println("Now printting the transactions of this time:\n");
sql = "Select * from TRANSACTIONS";
ResultSet rs1 = stmt.executeQuery(sql);
while (rs1.next()) {
System.out.printf("Transaction_ID =%2d, CustomerId =%8s, Transaction_Type =%8s, PriceOrDepositAmount = $%9.2f\n",
rs1.getInt("Transaction_ID"), rs1.getString("CustomerId"), rs1.getString("Transaction_Type"), rs1.getBigDecimal("PriceOrDepositAmount") );
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public double getCashValue(int day) {
this.date =day;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
//fetch transaction information and print it
System.out.println("Now printting the cash of this accont houlder in this date:\n");
sql = "Select * from Account_Cash_History where CustomerId ='"+CustomerId+"' and CashDate = '"+date+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
cashAmount=rs.getDouble("CashAmount");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return cashAmount;
}
public double getStockValue(int day) {
this.date =day;
int i=0;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
/*//Stock m1 = new Stock();*/
//fetch transaction information and print it
System.out.println("Now printting the cash of this accont houlder in this date:\n");
//show the account holder information with current account balance
sql = "Select * from Account_StockHistory where CustomerId ='"+CustomerId+"' and date = '"+date+"'";
ResultSet rs = stmt.executeQuery(sql);
System.out.println(rs.next());
//SELECT COUNT(*) FROM TABLE_NAME
while (rs.next()) {
StockItemSymbol[i] = rs.getString("StockSymbol");
System.out.println(StockItemSymbol[i]);
StockItemUnitValue[i]=Stock.getStockPrice(StockItemSymbol[i], date);
System.out.println("dadad2");
StockItemQuantity[i] = rs.getDouble("StockQuantity");
System.out.println("dadad3");
StockItemCashValue[i]=StockItemUnitValue[i]*StockItemQuantity[i];
System.out.println("dadad4");
totalStockValue += StockItemCashValue[i];
System.out.println("dadad5");
i++;
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("dadad6");
return totalStockValue;
}
public double getCashValue() {
this.date =101;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
String sql;
//fetch transaction information and print it
System.out.println("Now printting the cash of this accont houlder in this date:\n");
sql = "Select * from Account_Cash_History where CustomerId ='"+CustomerId+"' and CashDate = '"+date+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
cashAmount=rs.getDouble("CashAmount");
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
return cashAmount;
}
public void current_protfolia(int date, String ID) {
this.date=date;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
//drop the account balance table if it exist already.
String sqlDropStatement = "DROP TABLE Account_Stock";
try {
stmt.executeUpdate(sqlDropStatement);
}catch (Exception e1) {
System.out.println("Trying to drop Account_Stock Table...");
}
String [] protf = null;
//show the account holder information with current account balance
String sql = "Select * from Account_Stock where Transcation_date= '"+date+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs.getString("CustomerId"), rs.getString("StockSymbol"),rs.getDouble("StockQuantity"));
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void current_protfolia(String ID) {
int date =101;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
//drop the account balance table if it exist already.
String sqlDropStatement = "DROP TABLE Account_Stock";
try {
stmt.executeUpdate(sqlDropStatement);
}catch (Exception e1) {
System.out.println("Trying to drop Account_Stock Table...");
}
String [] protf = null;
//show the account holder information with current account balance
String sql = "Select * from Account_Stock where Transcation_date= '"+date+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs.getString("CustomerId"), rs.getString("StockSymbol"),rs.getDouble("StockQuantity"));
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void Transcation_history(String ID) {
int date =101;
this.CustomerId=ID;
try {
Class.forName(JDBC_DRIVER);
Connection con = DriverManager.getConnection(DB_URL, DB_Account_IDNAME, DB_PASSWORD);
Statement stmt = con.createStatement();
//drop the account balance table if it exist already.
String sqlDropStatement = "DROP TABLE Account_Stock";
try {
stmt.executeUpdate(sqlDropStatement);
}catch (Exception e1) {
System.out.println("Trying to drop Account_Stock Table...");
}
String [] protf = null;
//show the account holder information with current account balance
String sql = "Select * from Account_Stock_history where CostomId= '"+CustomrtId+"'";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs.getString("CustomerId"), rs.getString("StockSymbol"),rs.getDouble("StockQuantity"));
}
sql = "Select * from Account_Stock_history where CostomId= '"+CustomrtId+"'and Transaction_Type='Sell'";
ResultSet rs2 = stmt.executeQuery(sql);
while (rs2.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs2.getString("CustomerId"), rs2.getString("StockSymbol"),rs2.getDouble("StockQuantity"));
}
sql = "Select * from Account_Stock_history where CostomId= '"+CustomrtId+"'and Transaction_Type='Buy'";
ResultSet rs3 = stmt.executeQuery(sql);
while (rs3.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs3.getString("CustomerId"), rs3.getString("StockSymbol"),rs3.getDouble("StockQuantity"));
}
sql = "Select * from Account_Stock_history where CostomId= '"+CustomrtId+"'and Transaction_Type='Deposot'";
ResultSet rs4 = stmt.executeQuery(sql);
while (rs4.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs4.getString("CustomerId"), rs4.getString("StockSymbol"),rs4.getDouble("StockQuantity"));
}
stmt.close();
sql = "Select * from Account_Stock_history where CostomId= '"+CustomrtId+"";
ResultSet rs41 = stmt.executeQuery(sql);
while (rs41.next()) {
System.out.printf("CustomerId =%8s, StockSymbol=%4s, StockQuantity= $%9.2f\n",
rs41.getString("CustomerId"), rs41.getString("StockSymbol"),rs41.getDouble("StockQuantity"));
}
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}