-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankSystemConsole.java
More file actions
158 lines (144 loc) · 6.67 KB
/
Copy pathBankSystemConsole.java
File metadata and controls
158 lines (144 loc) · 6.67 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
import java.sql.*;
import java.util.Scanner;
public class BankSystemConsole {
private static final String DB_URL = "jdbc:sqlite:bank.db";
public static void main(String[] args) {
initializeDatabase();
Scanner scanner = new Scanner(System.in);
boolean running = true;
System.out.println("Simple Bank System (Console)");
while (running) {
System.out.println("\n1. Add User\n2. Create Account\n3. Deposit\n4. Withdraw\n5. View Accounts\n6. Exit");
System.out.print("Choose option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addUser(scanner);
break;
case 2:
createAccount(scanner);
break;
case 3:
deposit(scanner);
break;
case 4:
withdraw(scanner);
break;
case 5:
viewAccounts();
break;
case 6:
running = false;
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid option.");
}
}
scanner.close();
}
private static void initializeDatabase() {
try (Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement()) {
// Create users table
stmt.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL)");
// Create accounts table
stmt.execute("CREATE TABLE IF NOT EXISTS accounts (account_id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, balance REAL, FOREIGN KEY(user_id) REFERENCES users(id))");
} catch (SQLException e) {
System.out.println("Database error: " + e.getMessage());
}
}
private static void addUser(Scanner scanner) {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter email: ");
String email = scanner.nextLine();
try (Connection conn = DriverManager.getConnection(DB_URL);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users (name, email) VALUES (?, ?)")) {
pstmt.setString(1, name);
pstmt.setString(2, email);
pstmt.executeUpdate();
System.out.println("User added successfully.");
} catch (SQLException e) {
System.out.println("Error adding user: " + e.getMessage());
}
}
private static void createAccount(Scanner scanner) {
System.out.print("Enter user ID: ");
int userId = scanner.nextInt();
System.out.print("Enter initial balance: ");
double balance = scanner.nextDouble();
try (Connection conn = DriverManager.getConnection(DB_URL);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO accounts (user_id, balance) VALUES (?, ?)")) {
pstmt.setInt(1, userId);
pstmt.setDouble(2, balance);
pstmt.executeUpdate();
System.out.println("Account created successfully.");
} catch (SQLException e) {
System.out.println("Error creating account: " + e.getMessage());
}
}
private static void deposit(Scanner scanner) {
System.out.print("Enter account ID: ");
int accountId = scanner.nextInt();
System.out.print("Enter amount to deposit: ");
double amount = scanner.nextDouble();
try (Connection conn = DriverManager.getConnection(DB_URL);
PreparedStatement pstmt = conn.prepareStatement("UPDATE accounts SET balance = balance + ? WHERE account_id = ?")) {
pstmt.setDouble(1, amount);
pstmt.setInt(2, accountId);
int rows = pstmt.executeUpdate();
if (rows > 0) {
System.out.println("Deposit successful.");
} else {
System.out.println("Account not found.");
}
} catch (SQLException e) {
System.out.println("Error depositing: " + e.getMessage());
}
}
private static void withdraw(Scanner scanner) {
System.out.print("Enter account ID: ");
int accountId = scanner.nextInt();
System.out.print("Enter amount to withdraw: ");
double amount = scanner.nextDouble();
try (Connection conn = DriverManager.getConnection(DB_URL)) {
PreparedStatement selectStmt = conn.prepareStatement("SELECT balance FROM accounts WHERE account_id = ?");
selectStmt.setInt(1, accountId);
ResultSet rs = selectStmt.executeQuery();
if (rs.next()) {
double balance = rs.getDouble("balance");
if (balance >= amount) {
PreparedStatement updateStmt = conn.prepareStatement("UPDATE accounts SET balance = balance - ? WHERE account_id = ?");
updateStmt.setDouble(1, amount);
updateStmt.setInt(2, accountId);
updateStmt.executeUpdate();
System.out.println("Withdrawal successful.");
} else {
System.out.println("Insufficient balance.");
}
} else {
System.out.println("Account not found.");
}
} catch (SQLException e) {
System.out.println("Error withdrawing: " + e.getMessage());
}
}
private static void viewAccounts() {
try (Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT u.id, u.name, u.email, a.account_id, a.balance " +
"FROM users u LEFT JOIN accounts a ON u.id = a.user_id")) {
System.out.println("\nAccounts:");
while (rs.next()) {
System.out.printf("User ID: %d, Name: %s, Email: %s, Account ID: %s, Balance: %.2f%n",
rs.getInt("id"), rs.getString("name"), rs.getString("email"),
rs.getString("account_id") != null ? rs.getString("account_id") : "None",
rs.getDouble("balance"));
}
} catch (SQLException e) {
System.out.println("Error viewing accounts: " + e.getMessage());
}
}
}