· 5 years ago · Oct 03, 2020, 01:54 AM
1package banking;
2
3import java.sql.*;
4import java.util.*;
5
6class Account {
7 private String cardNumber;
8 private String pin;
9 private int balance;
10
11 protected Account() {
12
13 Random random = new Random();
14 String tempNumber = "400000" + (random.nextInt(899) + 100) +
15 (random.nextInt(899) + 100) + (random.nextInt(899) + 100);
16
17 // Luhn Algorithm
18
19 long numberCheck = Long.parseLong(tempNumber);
20 int sum = 0, checksum = 0;
21 long tempSum, adder = 0;
22 boolean odd = true;
23
24 while (numberCheck > 0) {
25
26 if (odd) {
27 tempSum = (numberCheck % 10) * 2;
28
29 if (tempSum > 9) {
30 while (tempSum > 0) {
31 adder += tempSum % 10;
32 tempSum /= 10;
33 }
34
35 tempSum = adder;
36 }
37
38
39 odd = false;
40
41 } else {
42 tempSum = numberCheck % 10;
43 odd = true;
44 }
45
46 adder = 0;
47 sum += tempSum;
48 numberCheck /= 10;
49
50 }
51
52 while (sum % 10 != 0) {
53 sum++;
54 checksum++;
55 }
56
57 cardNumber = tempNumber + checksum;
58 int tempPin = random.nextInt(9999);
59
60 if (tempPin <= 999 && tempPin >= 100) {
61 pin = "0" + tempPin;
62 } else if (tempPin <= 99 && tempPin >= 10) {
63 pin = "00" + tempPin;
64 } else if (tempPin <= 9) {
65 pin = "000" + tempPin;
66 } else {
67 pin = "" + tempPin;
68 }
69 balance = 0;
70 }
71
72 public String getCardNumber() {
73 return cardNumber;
74 }
75
76 public String getPin() {
77 return pin;
78 }
79
80 public int getBalance() {
81 return balance;
82 }
83}
84
85class InsertData {
86 private String fileName;
87
88 public InsertData(String fileName){
89 this.fileName = fileName;
90 }
91
92 public Connection connect () {
93
94 String url = "jdbc:sqlite:" + fileName;
95 Connection conn = null;
96
97 try {
98 conn = DriverManager.getConnection(url);
99 } catch (SQLException e) {
100 System.out.println(e.getMessage());
101 }
102
103 return conn;
104 }
105
106 public void Insert (long number, int pin, int balance){
107 String sql = "INSERT INTO card (number,pin,balance) VALUES(?,?,?)";
108
109 try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)){
110 pstmt.setLong(1,number);
111 pstmt.setInt(2,pin);
112 pstmt.setInt(3, balance);
113 pstmt.executeUpdate();
114 } catch (SQLException e){
115 System.out.println(e.getMessage());
116 }
117 }
118}
119
120class RemoveData{
121 private String fileName;
122
123 public RemoveData(String fileName){
124 this.fileName = fileName;
125 }
126
127 public Connection connect () {
128
129 String url = "jdbc:sqlite:" + fileName;
130 Connection conn = null;
131
132 try {
133 conn = DriverManager.getConnection(url);
134 } catch (SQLException e) {
135 System.out.println(e.getMessage());
136 }
137
138 return conn;
139 }
140
141 public void Remove(long cardNumber){
142 String sql = "DELETE FROM card WHERE number = ?";
143 try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)){
144 pstmt.setLong(1,cardNumber);
145 pstmt.executeUpdate();
146 } catch (SQLException e) {
147 System.out.println(e.getMessage());
148 }
149 }
150}
151
152class SelectData {
153 private String fileName;
154
155 public SelectData (String fileName){
156 this.fileName = fileName;
157 }
158
159 public Connection connect () {
160
161 String url = "jdbc:sqlite:" + fileName;
162 Connection conn = null;
163
164 try {
165 conn = DriverManager.getConnection(url);
166 } catch (SQLException e) {
167 System.out.println(e.getMessage());
168 }
169
170 return conn;
171 }
172
173 public boolean checkNumber (long cardNumber) {
174 boolean isCard = false;
175 String sql = "SELECT number FROM card WHERE number=?";
176
177 try (Connection conn = this.connect();
178 PreparedStatement pstmt = conn.prepareStatement(sql)) {
179
180 // Setting parameter
181
182 pstmt.setLong(1, cardNumber);
183 ResultSet rs = pstmt.executeQuery();
184
185 isCard = rs.next();
186
187 } catch (SQLException e) {
188 System.out.println(e.getMessage());
189 }
190
191 return isCard;
192 }
193
194 public boolean checkPin(int pinNumber){
195 boolean isPinValid = false;
196 String sql = "SELECT pin FROM card WHERE pin=?";
197
198 try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)){
199 pstmt.setLong(1,pinNumber);
200 ResultSet rs = pstmt.executeQuery();
201 isPinValid = rs.next();
202 } catch (SQLException e){
203 System.out.println(e.getMessage());
204 }
205 return isPinValid;
206 }
207
208 public int returnBalance (long cardNumber){
209 String sql = "SELECT number, balance FROM card WHERE number=?";
210
211 try (Connection conn = this.connect();
212 PreparedStatement pstmt = conn.prepareStatement(sql)) {
213
214 pstmt.setLong(1, cardNumber);
215 ResultSet rs = pstmt.executeQuery();
216
217 if (rs.next()) {
218 return rs.getInt("balance");
219 }
220
221 } catch (SQLException e) {
222 System.out.println(e.getMessage());
223 }
224
225 return 0;
226 }
227}
228
229class UpdateData {
230 private String fileName;
231
232 public UpdateData(String fileName) {
233 this.fileName = fileName;
234 }
235
236 public Connection connect () {
237
238 String url = "jdbc:sqlite:" + fileName;
239 Connection conn = null;
240
241 try {
242 conn = DriverManager.getConnection(url);
243 } catch (SQLException e) {
244 System.out.println(e.getMessage());
245 }
246
247 return conn;
248 }
249
250 public void Update(long sender, long receiver, int amount){
251 String sql = "UPDATE card SET balance=(balance-?) WHERE number=?";
252
253 try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
254
255 pstmt.setInt(1,amount);
256 pstmt.setLong(2,sender);
257
258 pstmt.executeUpdate();
259 } catch (SQLException e) {
260 System.out.println(e.getMessage());
261 }
262
263 String nextSql = "UPDATE card SET balance=(balance+?) WHERE number=?";
264
265 try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(nextSql)) {
266
267 pstmt.setInt(1, amount);
268 pstmt.setLong(2,receiver);
269
270 pstmt.executeUpdate();
271 } catch (SQLException e) {
272 System.out.println(e.getMessage());
273 }
274 }
275
276 public void addAmount(long cardNumber, int amount) {
277 String sql = "UPDATE card SET balance=(balance+?) WHERE number=?";
278
279 try (Connection conn = this.connect();
280 PreparedStatement pstmt = conn.prepareStatement(sql)) {
281
282 // Setting parameters
283
284 pstmt.setInt(1, amount);
285 pstmt.setLong(2, cardNumber);
286
287 // Updating
288
289 pstmt.executeUpdate();
290
291 } catch (SQLException e) {
292 System.out.println(e.getMessage());
293 }
294 }
295}
296
297
298public class Main {
299 static String fileName;
300
301 static void printData(Account card) {
302 System.out.println();
303 System.out.println("Your card has been created");
304 System.out.println("Your card number:");
305 System.out.println(card.getCardNumber());
306 System.out.println("Your card PIN:");
307 System.out.println(card.getPin());
308 }
309
310 static void createAccount() {
311 InsertData app = new InsertData(fileName);
312 Account account = new Account();
313
314 // Inserting data into an SQL database
315
316 app.Insert(Long.parseLong(account.getCardNumber()), Integer.parseInt(account.getPin()),
317 account.getBalance());
318
319 printData(account);
320 }
321
322 static void logIn() {
323
324 System.out.println();
325 Scanner scanner = new Scanner(System.in);
326 SelectData app = new SelectData(fileName);
327 boolean loggedIn = false;
328
329 System.out.println("Enter your card number:");
330 String numberInput = scanner.nextLine();
331 System.out.println("Enter your PIN:");
332 String pinInput = scanner.nextLine();
333
334 // Checking correctness of card number & PIN
335
336 if (app.checkNumber(Long.parseLong(numberInput)) && app.checkPin(Integer.parseInt(pinInput))) {
337
338 loggedIn = true;
339 System.out.println("You have successfully logged in!");
340 menu(Long.parseLong(numberInput));
341
342 }
343
344 if (!loggedIn) {
345 System.out.println("Wrong card number or PIN!");
346 }
347 }
348
349 static void menu(long cardNumber) {
350 System.out.println();
351 System.out.println("1. Balance");
352 System.out.println("2. Add income");
353 System.out.println("3. Do transfer");
354 System.out.println("4. Close account");
355 System.out.println("5. Log out");
356 System.out.println("0. Exit");
357 Scanner scanner = new Scanner(System.in);
358 SelectData app = new SelectData(fileName);
359 int secondChoice = scanner.nextInt();
360
361 switch (secondChoice) {
362 case 0:
363 System.out.println();
364 System.out.println("Bye!");
365 System.exit(0);
366 case 1:
367 System.out.println();
368 System.out.println("Balance: " + app.returnBalance(cardNumber));
369 break;
370 case 2:
371 System.out.println();
372 System.out.println("Enter income:");
373 int income = scanner.nextInt();
374
375 UpdateData update = new UpdateData(fileName);
376 update.addAmount(cardNumber, income);
377
378 System.out.println("Income was added!");
379
380 break;
381 case 3:
382 System.out.println();
383 System.out.println("Transfer");
384 System.out.println("Enter card number:");
385
386 long cardInput = scanner.nextLong();
387 boolean correct = true;
388
389 // Checking if number passes Luhn Algorithm
390
391 long numberCheck = cardInput;
392 int sum = 0;
393 long tempSum, adder = 0;
394 boolean odd = false;
395
396 while (numberCheck > 0) {
397
398 if (odd) {
399 tempSum = (numberCheck % 10) * 2;
400
401 if (tempSum > 9) {
402 while (tempSum > 0) {
403 adder += tempSum % 10;
404 tempSum /= 10;
405 }
406
407 tempSum = adder;
408 }
409
410 odd = false;
411
412 } else {
413 tempSum = numberCheck % 10;
414 odd = true;
415 }
416
417 adder = 0;
418 sum += tempSum;
419 numberCheck /= 10;
420 }
421
422 if (sum % 10 != 0) {
423 correct = false;
424 }
425
426 if (!correct) {
427
428 System.out.println("Probably you made mistake in the card number. Please try again!");
429 break;
430
431 } else {
432
433 if (!app.checkNumber(cardInput)) {
434
435 System.out.println("Such a card does not exist.");
436 break;
437
438 } else {
439
440 System.out.println("Enter how much money you want to transfer:");
441 int moneyTransfer = scanner.nextInt();
442
443 if (app.returnBalance(cardNumber) < moneyTransfer) {
444
445 System.out.println("Not enough money!");
446 break;
447
448 } else {
449
450 UpdateData update1 = new UpdateData(fileName);
451 update1.Update(cardNumber, cardInput, moneyTransfer);
452 System.out.println("Success!");
453
454 }
455
456 }
457
458 break;
459
460 }
461 case 4:
462 RemoveData deleteApp = new RemoveData(fileName);
463 deleteApp.Remove(cardNumber);
464
465 System.out.println();
466 System.out.println("The account has been closed!");
467 case 5:
468 System.out.println();
469 System.out.println("You have sucessfully logged out!");
470 return;
471 default:
472 System.out.println();
473 System.out.println("Unknown command");
474 break;
475 }
476 menu(cardNumber);
477 }
478
479 public static void createDatabase(String fileName) {
480 String url = "jdbc:sqlite:" + fileName;
481 try (Connection conn = DriverManager.getConnection(url)) {
482 if (conn != null) {
483 DatabaseMetaData meta = conn.getMetaData();
484 }
485 } catch (SQLException e) {
486 System.out.println(e.getMessage());
487 }
488 }
489
490 public static void createTable(String fileName) {
491 String url = "jdbc:sqlite:" + fileName;
492
493
494 String sql = "CREATE TABLE IF NOT EXISTS card (" +
495 " `id` INTEGER NOT NULL PRIMARY KEY," +
496 " `number` TEXT," +
497 " `pin` TEXT," +
498 " `balance` INTEGER DEFAULT 0"
499 + ");";
500
501 try (Connection conn = DriverManager.getConnection(url);
502 Statement stmt = conn.createStatement()) {
503
504
505 stmt.execute(sql);
506
507 } catch (SQLException e) {
508 System.out.println(e.getMessage());
509 }
510 }
511
512 public static void main(String[] args) {
513 fileName = "cards.db";
514 Scanner scanner = new Scanner(System.in);
515
516 int choice = -1;
517
518 // Creating an SQL database using functions
519
520 createDatabase(fileName);
521 createTable(fileName);
522
523 while (choice != 0) {
524
525 System.out.println("1. Create an account");
526 System.out.println("2. Log into account");
527 System.out.println("0. Exit");
528
529 choice = scanner.nextInt();
530
531 switch (choice) {
532
533 case 0:
534
535 System.out.println();
536 System.out.println("Bye!");
537 break;
538
539 case 1:
540
541 createAccount();
542 break;
543
544 case 2:
545
546 logIn();
547 break;
548
549 default:
550
551 System.out.println();
552 System.out.println("Unknown command");
553 break;
554
555 }
556 }
557 }
558}
559
560