-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankSystemGUI.java
More file actions
208 lines (181 loc) · 8.76 KB
/
Copy pathBankSystemGUI.java
File metadata and controls
208 lines (181 loc) · 8.76 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
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
import java.awt.*;
import java.sql.*;
import javax.swing.*;
public class BankSystemGUI extends JFrame {
private static final String DB_URL = "jdbc:sqlite:bank.db";
private JTextField nameField, emailField, userIdField, balanceField, accountIdField, amountField;
private JTextArea outputArea;
public BankSystemGUI() {
initializeDatabase();
setTitle("Simple Bank System");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// Input Panel
JPanel inputPanel = new JPanel(new GridLayout(6, 2, 5, 5));
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
inputPanel.add(new JLabel("Name:"));
nameField = new JTextField();
inputPanel.add(nameField);
inputPanel.add(new JLabel("Email:"));
emailField = new JTextField();
inputPanel.add(emailField);
inputPanel.add(new JLabel("User ID (for account):"));
userIdField = new JTextField();
inputPanel.add(userIdField);
inputPanel.add(new JLabel("Initial Balance:"));
balanceField = new JTextField();
inputPanel.add(balanceField);
inputPanel.add(new JLabel("Account ID:"));
accountIdField = new JTextField();
inputPanel.add(accountIdField);
inputPanel.add(new JLabel("Amount (deposit/withdraw):"));
amountField = new JTextField();
inputPanel.add(amountField);
add(inputPanel, BorderLayout.NORTH);
// Button Panel
JPanel buttonPanel = new JPanel(new FlowLayout());
JButton addUserBtn = new JButton("Add User");
JButton createAccountBtn = new JButton("Create Account");
JButton depositBtn = new JButton("Deposit");
JButton withdrawBtn = new JButton("Withdraw");
JButton viewAccountsBtn = new JButton("View Accounts");
buttonPanel.add(addUserBtn);
buttonPanel.add(createAccountBtn);
buttonPanel.add(depositBtn);
buttonPanel.add(withdrawBtn);
buttonPanel.add(viewAccountsBtn);
add(buttonPanel, BorderLayout.CENTER);
// Output Area
outputArea = new JTextArea(10, 50);
outputArea.setEditable(false);
add(new JScrollPane(outputArea), BorderLayout.SOUTH);
// Action Listeners
addUserBtn.addActionListener(e -> addUser());
createAccountBtn.addActionListener(e -> createAccount());
depositBtn.addActionListener(e -> deposit());
withdrawBtn.addActionListener(e -> withdraw());
viewAccountsBtn.addActionListener(e -> viewAccounts());
setVisible(true);
}
private void initializeDatabase() {
try (Connection conn = DriverManager.getConnection(DB_URL);
Statement stmt = conn.createStatement()) {
stmt.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT NOT NULL)");
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) {
outputArea.append("Database error: " + e.getMessage() + "\n");
}
}
private void addUser() {
String name = nameField.getText();
String email = emailField.getText();
if (name.isEmpty() || email.isEmpty()) {
outputArea.append("Name and email cannot be empty.\n");
return;
}
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();
outputArea.append("User added successfully.\n");
nameField.setText("");
emailField.setText("");
} catch (SQLException e) {
outputArea.append("Error adding user: " + e.getMessage() + "\n");
}
}
private void createAccount() {
try {
int userId = Integer.parseInt(userIdField.getText());
double balance = Double.parseDouble(balanceField.getText());
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();
outputArea.append("Account created successfully.\n");
userIdField.setText("");
balanceField.setText("");
} catch (SQLException e) {
outputArea.append("Error creating account: " + e.getMessage() + "\n");
}
} catch (NumberFormatException e) {
outputArea.append("Invalid user ID or balance.\n");
}
}
private void deposit() {
try {
int accountId = Integer.parseInt(accountIdField.getText());
double amount = Double.parseDouble(amountField.getText());
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) {
outputArea.append("Deposit successful.\n");
} else {
outputArea.append("Account not found.\n");
}
accountIdField.setText("");
amountField.setText("");
} catch (SQLException e) {
outputArea.append("Error depositing: " + e.getMessage() + "\n");
}
} catch (NumberFormatException e) {
outputArea.append("Invalid account ID or amount.\n");
}
}
private void withdraw() {
try {
int accountId = Integer.parseInt(accountIdField.getText());
double amount = Double.parseDouble(amountField.getText());
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();
outputArea.append("Withdrawal successful.\n");
} else {
outputArea.append("Insufficient balance.\n");
}
} else {
outputArea.append("Account not found.\n");
}
accountIdField.setText("");
amountField.setText("");
} catch (SQLException e) {
outputArea.append("Error withdrawing: " + e.getMessage() + "\n");
}
} catch (NumberFormatException e) {
outputArea.append("Invalid account ID or amount.\n");
}
}
private 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")) {
outputArea.append("\nAccounts:\n");
while (rs.next()) {
outputArea.append(String.format("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) {
outputArea.append("Error viewing accounts: " + e.getMessage() + "\n");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new BankSystemGUI());
}
}