-
Notifications
You must be signed in to change notification settings - Fork 1
/
BankingSystem.java
369 lines (352 loc) · 11.2 KB
/
BankingSystem.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
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
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;
/**
* Manage connection to database and perform SQL statements.
*/
public class BankingSystem {
// Connection properties
private static String driver;
private static String url;
private static String username;
private static String password;
// JDBC Objects
private static Connection con;
private static Statement stmt;
private static ResultSet rs;
/**
* Initialize database connection given properties file.
* @param filename name of properties file
*/
public static void init(String filename) {
try {
Properties props = new Properties(); // Create a new Properties object
FileInputStream input = new FileInputStream(filename); // Create a new FileInputStream object using our filename parameter
props.load(input); // Load the file contents into the Properties object
driver = props.getProperty("jdbc.driver"); // Load the driver
url = props.getProperty("jdbc.url"); // Load the url
username = props.getProperty("jdbc.username"); // Load the username
password = props.getProperty("jdbc.password"); // Load the password
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Test database connection.
*/
public static void testConnection() {
System.out.println(":: TEST - CONNECTING TO DATABASE");
try {
Class.forName(driver);
con = DriverManager.getConnection(url, username, password);
con.close();
System.out.println(":: TEST - SUCCESSFULLY CONNECTED TO DATABASE");
} catch (Exception e) {
System.out.println(":: TEST - FAILED CONNECTED TO DATABASE");
e.printStackTrace();
}
}
/**
* Create a new customer.
* @param name customer name
* @param gender customer gender
* @param age customer age
* @param pin customer pin
*/
public static void newCustomer(String name, String gender, String age, String pin)
{
try {
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("INSERT INTO P1.CUSTOMER (NAME, GENDER, AGE, PIN) VALUES ('%s','%s', %s,%s)", name, gender.toUpperCase(), age, pin);
stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
rs = stmt.getGeneratedKeys();
if (rs.next()) {
String id = rs.getString(1);
String output = String.format(":: CREATE NEW CUSTOMER #%s - SUCCESS\n",id);
System.out.println(output);
}
con.close();
} catch (Exception err) {
System.out.println(":: CREATE NEW CUSTOMER - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Open a new account.
* @param id customer id
* @param type type of account
* @param amount initial deposit amount
*/
public static boolean authCustomer(String id, String pin)
{
try {
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("SELECT * FROM P1.CUSTOMER WHERE (ID=%s AND PIN=%s)", id, pin);
rs = stmt.executeQuery(query);
if (rs.next()) {
String output = String.format(":: AUTHENTICATED CUSTOMER #%s - SUCCESS\n",id);
System.out.println(output);
con.close();
return true;
}
con.close();
} catch (Exception err) {
System.out.println(":: AUTHENTICATED CUSTOMER - FAILURE");
System.out.println(err);
System.out.println();
}
return false;
}
/**
* Open a new account.
* @param id customer id
* @param type type of account
* @param amount initial deposit amount
*/
public static void openAccount(String id, String type, String amount)
{
try {
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("INSERT INTO P1.ACCOUNT (ID, BALANCE, TYPE) VALUES (%s, %s, '%s')", id, amount, type.toUpperCase());
stmt.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
rs = stmt.getGeneratedKeys();
if (rs.next()) {
id = rs.getString(1);
String output = String.format(":: OPEN ACCOUNT #%s - SUCCESS\n",id);
System.out.println(output);
}
con.close();
} catch (Exception err) {
System.out.println(":: OPEN ACCOUNT #%s - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Close an account.
* @param accNum account number
*/
public static void closeAccount(String accNum)
{
try {
String output = ":: CLOSE ACCOUNT - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("UPDATE P1.ACCOUNT SET BALANCE=0, STATUS='I' WHERE (NUMBER=%s)", accNum);
int res = stmt.executeUpdate(query);
if (res > 0) {
output = String.format(":: CLOSE ACCOUNT #%s - SUCCESS\n",accNum);
}
con.close();
System.out.println(output);
} catch (Exception err) {
System.out.println(":: CLOSE ACCOUNT - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Deposit into an account.
* @param accNum account number
* @param amount deposit amount
*/
public static void deposit(String accNum, String amount)
{
try {
String output = ":: DEPOSIT - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("UPDATE P1.ACCOUNT SET BALANCE=BALANCE+%s WHERE (NUMBER=%s AND STATUS='A')", amount, accNum);
int res = stmt.executeUpdate(query);
if (res > 0) {
output = String.format(":: DEPOSIT INTO ACCOUNT #%s - SUCCESS\n",accNum);
}
con.close();
System.out.println(output);
} catch (Exception err) {
System.out.println(":: DEPOSIT - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Withdraw from an account.
* @param accNum account number
* @param amount withdraw amount
*/
public static void withdraw(String accNum, String amount)
{
try {
String output = ":: WITHDRAW - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("UPDATE P1.ACCOUNT SET BALANCE=BALANCE-%s WHERE (NUMBER=%s AND STATUS='A')", amount, accNum);
int res = stmt.executeUpdate(query);
if (res > 0) {
output = String.format(":: WITHDRAW FROM ACCOUNT #%s - SUCCESS\n",accNum);
}
con.close();
System.out.println(output);
} catch (Exception err) {
System.out.println(":: WITHDRAW - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Transfer amount from source account to destination account.
* @param srcAccNum source account number
* @param destAccNum destination account number
* @param amount transfer amount
*/
public static void transfer(String srcAccNum, String destAccNum, String amount)
{
try {
String output =":: TRANSFER - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("UPDATE P1.ACCOUNT SET BALANCE=BALANCE-%s WHERE (NUMBER=%s AND STATUS='A')", amount, srcAccNum);
int res = stmt.executeUpdate(query);
if (res > 0) {
query = String.format("UPDATE P1.ACCOUNT SET BALANCE=BALANCE+%s WHERE (NUMBER=%s AND STATUS='A')", amount, destAccNum);
res = stmt.executeUpdate(query);
if (res > 0) {
output = String.format(":: TRANSFER FROM ACCOUNT #%s TO ACCOUNT #%s - SUCCESS\n", srcAccNum, destAccNum);
}
}
con.close();
System.out.println(output);
} catch (Exception err) {
System.out.println(":: TRANSFER - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Display account summary.
* @param accNum account number
*/
public static void accountSummary(String id)
{
try {
rs = null;
String output =":: ACCOUNT SUMMARY - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("SELECT * FROM P1.ACCOUNT WHERE (ID=%s AND STATUS='A')", id);
rs = stmt.executeQuery(query);
if (rs.next()) {
output = String.format(":: ACCOUNT SUMMARY FOR CUSTOMER #%s - SUCCESS\n", id);
output += String.format("%-6s %-12s", "NUMBER","BALANCE");
System.out.println(output);
rs = stmt.executeQuery(query);
while (rs.next()) {
output = String.format("%-6s %-12s", rs.getString(1), rs.getString(3));
System.out.println(output);
}
} else {
System.out.println(":: ACCOUNT SUMMARY - FAILURE\n");
}
con.close();
} catch (Exception err) {
System.out.println(":: ACCOUNT SUMMARY - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Display Report A - Customer Information with Total Balance in Decreasing Order.
*/
public static void reportA()
{
try {
String output =":: REPORT A - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = "SELECT * FROM V_TOTAL_BALANCE";
rs = stmt.executeQuery(query);
if (rs.next()) {
rs = stmt.executeQuery(query);
output = ":: REPORT A - SUCCESS\n";
output += String.format("%-5s %-12s %-4s %-6s %-12s\n", "ID","NAME","AGE","GENDER","BALANCE");
System.out.println(output);
while(rs.next()) {
output = String.format("%-5s %-12s %-4s %-6s %-12s", rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5));
System.out.println(output);
}
} else {
System.out.println(output);
}
con.close();
} catch (Exception err) {
System.out.println(":: REPORT A - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* Display Report B - Customer Information with Total Balance in Decreasing Order.
* @param min minimum age
* @param max maximum age
*/
public static void reportB(String min, String max)
{
try {
rs = null;
String output =":: REPORT B - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("SELECT AVG(BALANCE) FROM V_TOTAL_BALANCE WHERE AGE>=%s AND AGE<=%s",min, max);
rs = stmt.executeQuery(query);
if (rs != null) {
output = ":: REPORT B - SUCCESS";
System.out.println(output);
}
while (rs.next()) {
output = String.format("Average Total Balance For Age %s to %s: %s", min, max, rs.getString(1));
System.out.println(output);
}
con.close();
} catch (Exception err) {
System.out.println(":: REPORT B - FAILURE");
System.out.println(err);
System.out.println();
}
}
/**
* find account - verify if account number associated with customer id
* @param id customer id
* @param number account number
*/
public static boolean findAccount(String id, String number)
{
try {
rs = null;
String output =":: VERIFYING ACCOUNT - FAILURE\n";
con = DriverManager.getConnection(url,username,password);
stmt = con.createStatement();
String query = String.format("SELECT * FROM P1.ACCOUNT WHERE(ID=%s AND NUMBER=%s AND STATUS='A')", id, number);
rs = stmt.executeQuery(query);
if (rs.next()) {
output =":: VERIFYING ACCOUNT - SUCCESS";
System.out.println(output);
return true;
} else {
System.out.println(output);
}
con.close();
} catch (Exception err) {
System.out.println(":: VERIFYING ACCOUNT - FAILURE");
System.out.println(err);
System.out.println();
}
return false;
}
}