-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankingSystem.java
More file actions
182 lines (161 loc) · 7.38 KB
/
BankingSystem.java
File metadata and controls
182 lines (161 loc) · 7.38 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
package sqlconnect;
import java.sql.*;
import java.util.Scanner;
public class BankingSystem {
private static final String URL = "jdbc:mysql://localhost:3306/bankdb";
private static final String USER = "root"; // username
private static final String PASS = "1331"; // password
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try (Connection conn = DriverManager.getConnection(URL, USER, PASS)) {
int choice;
do {
System.out.println("\n--- ATM / Banking System ---");
System.out.println("1. Create Account");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> createAccount(conn, sc);
case 2 -> login(conn, sc);
case 3 -> System.out.println("Thank you for using our ATM.");
default -> System.out.println("Invalid choice!");
}
} while (choice != 3);
} catch (SQLException e) {
e.printStackTrace();
}
sc.close();
}
// Create a new account
private static void createAccount(Connection conn, Scanner sc) throws SQLException {
sc.nextLine(); // clear buffer
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Set a 4-digit PIN: ");
int pin = sc.nextInt();
String sql = "INSERT INTO accounts (name, pin, balance) VALUES (?, ?, 0)";
try (PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
ps.setString(1, name);
ps.setInt(2, pin);
ps.executeUpdate();
try (ResultSet rs = ps.getGeneratedKeys()) {
if (rs.next()) {
System.out.println("Account created successfully! Your Account No is: " + rs.getInt(1));
}
}
}
}
// Login
private static void login(Connection conn, Scanner sc) throws SQLException {
System.out.print("Enter Account No: ");
int accNo = sc.nextInt();
System.out.print("Enter PIN: ");
int pin = sc.nextInt();
String sql = "SELECT * FROM accounts WHERE account_no = ? AND pin = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, accNo);
ps.setInt(2, pin);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
System.out.println("Welcome, " + rs.getString("name"));
accountMenu(conn, sc, accNo);
} else {
System.out.println("Invalid Account No or PIN.");
}
}
}
}
// Account menu after login
private static void accountMenu(Connection conn, Scanner sc, int accNo) throws SQLException {
int choice;
do {
System.out.println("\n--- Account Menu ---");
System.out.println("1. Check Balance");
System.out.println("2. Deposit");
System.out.println("3. Withdraw");
System.out.println("4. View Transactions");
System.out.println("5. Logout");
System.out.print("Enter choice: ");
choice = sc.nextInt();
switch (choice) {
case 1 -> checkBalance(conn, accNo);
case 2 -> deposit(conn, sc, accNo);
case 3 -> withdraw(conn, sc, accNo);
case 4 -> viewTransactions(conn, accNo);
case 5 -> System.out.println("Logging out...");
default -> System.out.println("Invalid choice!");
}
} while (choice != 5);
}
private static void checkBalance(Connection conn, int accNo) throws SQLException {
String sql = "SELECT balance FROM accounts WHERE account_no = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, accNo);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
System.out.println("Current Balance: ₹" + rs.getDouble("balance"));
}
}
}
}
private static void deposit(Connection conn, Scanner sc, int accNo) throws SQLException {
System.out.print("Enter deposit amount: ");
double amount = sc.nextDouble();
String sql = "UPDATE accounts SET balance = balance + ? WHERE account_no = ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setDouble(1, amount);
ps.setInt(2, accNo);
ps.executeUpdate();
}
recordTransaction(conn, accNo, "Deposit", amount);
System.out.println("₹" + amount + " deposited successfully.");
}
private static void withdraw(Connection conn, Scanner sc, int accNo) throws SQLException {
System.out.print("Enter withdrawal amount: ");
double amount = sc.nextDouble();
String checkSql = "SELECT balance FROM accounts WHERE account_no = ?";
try (PreparedStatement psCheck = conn.prepareStatement(checkSql)) {
psCheck.setInt(1, accNo);
try (ResultSet rs = psCheck.executeQuery()) {
if (rs.next()) {
double currentBalance = rs.getDouble("balance");
if (currentBalance >= amount) {
String updateSql = "UPDATE accounts SET balance = balance - ? WHERE account_no = ?";
try (PreparedStatement psUpdate = conn.prepareStatement(updateSql)) {
psUpdate.setDouble(1, amount);
psUpdate.setInt(2, accNo);
psUpdate.executeUpdate();
}
recordTransaction(conn, accNo, "Withdraw", amount);
System.out.println("₹" + amount + " withdrawn successfully.");
} else {
System.out.println("Insufficient funds.");
}
}
}
}
}
private static void viewTransactions(Connection conn, int accNo) throws SQLException {
String sql = "SELECT type, amount, date FROM transactions WHERE account_no = ? ORDER BY date DESC";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, accNo);
try (ResultSet rs = ps.executeQuery()) {
System.out.println("---- Transaction History ----");
while (rs.next()) {
System.out.println(rs.getString("date") + " | " + rs.getString("type") + " | ₹" + rs.getDouble("amount"));
}
}
}
}
private static void recordTransaction(Connection conn, int accNo, String type, double amount) throws SQLException {
String sql = "INSERT INTO transactions (account_no, type, amount) VALUES (?, ?, ?)";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, accNo);
ps.setString(2, type);
ps.setDouble(3, amount);
ps.executeUpdate();
}
}
}