-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode
More file actions
181 lines (155 loc) · 4.88 KB
/
Code
File metadata and controls
181 lines (155 loc) · 4.88 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
#include <iostream>
#include <string>
#include <iomanip>
#include <mysql/mysql.h>
using namespace std;
// Database connection details
const string DB_HOST = "localhost";
const string DB_USER = "root";
const string DB_PASS = "Harsh@2004";
const string DB_NAME = "bank_db";
// MySQL connection
MYSQL* connectToDatabase() {
MYSQL* conn = mysql_init(nullptr);
if (!mysql_real_connect(conn, DB_HOST.c_str(), DB_USER.c_str(), DB_PASS.c_str(), DB_NAME.c_str(), 0, nullptr, 0)) {
cerr << "MySQL Connection Failed: " << mysql_error(conn) << endl;
exit(EXIT_FAILURE);
}
return conn;
}
// Create a new account
void createAccount(MYSQL* conn) {
string name;
double initialDeposit;
cout << "Enter Account Holder's Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Initial Deposit Amount: ";
cin >> initialDeposit;
string query = "INSERT INTO accounts (name, balance) VALUES ('" + name + "', " + to_string(initialDeposit) + ")";
if (mysql_query(conn, query.c_str())) {
cerr << "Error: " << mysql_error(conn) << endl;
} else {
cout << "Account created successfully!" << endl;
}
}
// Deposit money
void depositMoney(MYSQL* conn) {
int accountId;
double amount;
cout << "Enter Account ID: ";
cin >> accountId;
cout << "Enter Deposit Amount: ";
cin >> amount;
string query = "UPDATE accounts SET balance = balance + " + to_string(amount) + " WHERE account_id = " + to_string(accountId);
if (mysql_query(conn, query.c_str())) {
cerr << "Error: " << mysql_error(conn) << endl;
} else {
cout << "Amount deposited successfully!" << endl;
}
}
// Withdraw money
void withdrawMoney(MYSQL* conn) {
int accountId;
double amount;
cout << "Enter Account ID: ";
cin >> accountId;
cout << "Enter Withdrawal Amount: ";
cin >> amount;
string checkQuery = "SELECT balance FROM accounts WHERE account_id = " + to_string(accountId);
if (mysql_query(conn, checkQuery.c_str())) {
cerr << "Error: " << mysql_error(conn) << endl;
return;
}
MYSQL_RES* res = mysql_store_result(conn);
MYSQL_ROW row = mysql_fetch_row(res);
if (row) {
double currentBalance = stod(row[0]);
if (currentBalance < amount) {
cout << "Insufficient balance!" << endl;
} else {
string updateQuery = "UPDATE accounts SET balance = balance - " + to_string(amount) + " WHERE account_id = " + to_string(accountId);
if (mysql_query(conn, updateQuery.c_str())) {
cerr << "Error: " << mysql_error(conn) << endl;
} else {
cout << "Amount withdrawn successfully!" << endl;
}
}
} else {
cout << "Account not found!" << endl;
}
mysql_free_result(res);
}
// Check balance
void checkBalance(MYSQL* conn) {
int accountId;
cout << "Enter Account ID: ";
cin >> accountId;
string query = "SELECT balance FROM accounts WHERE account_id = " + to_string(accountId);
if (mysql_query(conn, query.c_str())) {
cerr << "Error: " << mysql_error(conn) << endl;
return;
}
MYSQL_RES* res = mysql_store_result(conn);
MYSQL_ROW row = mysql_fetch_row(res);
if (row) {
cout << "Current Balance: " << fixed << setprecision(2) << row[0] << endl;
} else {
cout << "Account not found!" << endl;
}
mysql_free_result(res);
}
// Delete an account
void deleteAccount(MYSQL* conn) {
int accountId;
cout << "Enter Account ID to Delete: ";
cin >> accountId;
string query = "DELETE FROM accounts WHERE account_id = " + to_string(accountId);
if (mysql_query(conn, query.c_str())) {
cerr << "Error: " << mysql_error(conn) << endl;
} else {
cout << "Account deleted successfully!" << endl;
}
}
// Main menu
void showMenu() {
cout << "\n--- Bank Management System ---\n";
cout << "1. Create Account\n";
cout << "2. Deposit Money\n";
cout << "3. Withdraw Money\n";
cout << "4. Check Balance\n";
cout << "5. Delete Account\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
}
int main() {
MYSQL* conn = connectToDatabase();
while (true) {
showMenu();
int choice;
cin >> choice;
switch (choice) {
case 1:
createAccount(conn);
break;
case 2:
depositMoney(conn);
break;
case 3:
withdrawMoney(conn);
break;
case 4:
checkBalance(conn);
break;
case 5:
deleteAccount(conn);
break;
case 6:
mysql_close(conn);
cout << "Exiting... Goodbye!\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
}