-
Notifications
You must be signed in to change notification settings - Fork 0
/
atmRefactor.java
133 lines (114 loc) · 6.38 KB
/
atmRefactor.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
package com.anton;
import java.awt.*;
import java.util.Arrays;
import java.util.Scanner;
import java.sql.*;
public class atm {
public static void main(String[] args) {
try (Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "Element1991");
Statement stmt = connect.createStatement();
Scanner scan = new Scanner(System.in)) {
// initialize and connect into our db.
// Class.forName("com.mysql.jdbc.Driver"); - DO NOT NEED -
// Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "Element1991");
// Statement obj : used for SENDING a static SQL statement and returning the results it produces.
// Statement stmt = connect.createStatement();
// createStatement() : creates a Statement object for SENDING SQL statements to the database.
// Scanner scan = new Scanner(System.in);
System.out.println("Hi, Welcome To Your ATM.");
System.out.println();
System.out.println("Please, Enter Your Pin Number.");
int pin = scan.nextInt();
/**
* ResultSet object : table of data representing a database result set,
** which is usually generated by executing a statement that queries the database. When it executes the
*** query through the Statement object, stmt .
*/
// String query = "SELECT * FROM new_table where ac_no=" +pin;
String query = "SELECT * FROM new_table where ac_no=?";
PreparedStatement pstmt = connect.prepareStatement(query);
pstmt.setInt(1, pin);
ResultSet rs = pstmt.executeQuery();
// Run a check if pin (aka ac_no) matches.
// ResultSet rs = stmt.executeQuery(query);
String name = null;
int balance=0;
int count=0;
/**
* "while there is a change in ResultSet, run while loop..."
* pin = ` ac_no ` - so when user enters pin the program SELECTS the appropriate row/column.
** If successful ( i.e. a change in ResultSet ), the while loop runs.
*/
while (rs.next()) {
name = rs.getString(3);
balance = rs.getInt(4);
count++;
}
// int userChoice;
int depositAmount = 0;
int withdrawAmount = 0;
// "if while loop is ran more than 0 times, run this code block."
if (count>0) {
System.out.println("Hi, " +name);
System.out.println();
while (true) {
System.out.println("Press 1 to check balance.");
System.out.println("Press 2 to deposit.");
System.out.println("Press 3 to withdraw.");
System.out.println("Press 4 to print receipt.");
System.out.println("Press 5 to exit.");
System.out.println();
System.out.println("Enter your choice : ");
int userChoice = scan.nextInt();
transactionType choice = transactionType.values()[userChoice - 1];
switch (choice) {
case CHECK_BALANCE:
System.out.println("You're balance is : " +balance);
System.out.println();
break;
case DEPOSIT:
System.out.println("How much would you like to deposit?");
depositAmount = scan.nextInt();
balance += depositAmount;
// SQL DB //
String depositQuery = "UPDATE new_table SET balance = " +balance+ " WHERE ac_no = " +pin;
int bal = stmt.executeUpdate(depositQuery);
System.out.println("Successfully deposited, your current balance is " +balance);
System.out.println();
break;
case WITHDRAW:
System.out.println("How much would you like to withdraw?");
withdrawAmount = scan.nextInt();
if (withdrawAmount > balance) {
System.out.println("Error: insufficient balance.");
System.out.println();
} else {
balance -= withdrawAmount;
// SQL DB //
String withdrawQuery = "UPDATE new_table SET balance = " +balance+ " WHERE ac_no = " +pin;
int subtract = stmt.executeUpdate(withdrawQuery);
System.out.println("Successful withdraw, your current balance is " + balance);
System.out.println();
break;
}
case PRINT_RECEIPT:
System.out.println("Thank You.");
System.out.println("Your current balance is : " +balance);
System.out.println("Withdraw amount : " +withdrawAmount);
System.out.println("Deposited amount : " +depositAmount);
System.out.println();
break;
}
if (choice == transactionType.EXIT) {
System.out.println("Thank you, come again!");
break;
}
}
} else {
System.out.println("Wrong Pin Number Entered.");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}