-
Notifications
You must be signed in to change notification settings - Fork 243
/
Banking system.cpp
131 lines (114 loc) · 3.93 KB
/
Banking system.cpp
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
#include <iostream>
#include <fstream>
#include <vector>
#include <stdexcept>
class BankAccount {
public:
BankAccount(const std::string& accountNumber, double balance)
: accountNumber(accountNumber), balance(balance) {}
const std::string& getAccountNumber() const {
return accountNumber;
}
double getBalance() const {
return balance;
}
void deposit(double amount) {
if (amount <= 0) {
throw std::invalid_argument("Deposit amount must be positive.");
}
balance += amount;
}
void withdraw(double amount) {
if (amount <= 0) {
throw std::invalid_argument("Withdrawal amount must be positive.");
}
if (amount > balance) {
throw std::runtime_error("Insufficient funds.");
}
balance -= amount;
}
private:
std::string accountNumber;
double balance;
};
void saveAccounts(const std::vector<BankAccount>& accounts) {
std::ofstream file("bank_accounts.txt");
if (!file) {
throw std::runtime_error("Unable to open the file for writing.");
}
for (const BankAccount& account : accounts) {
file << account.getAccountNumber() << " " << account.getBalance() << "\n";
}
}
int main() {
std::vector<BankAccount> accounts;
// Load account data from a file (if it exists)
std::ifstream file("bank_accounts.txt");
if (file) {
std::string accountNumber;
double balance;
while (file >> accountNumber >> balance) {
accounts.emplace_back(accountNumber, balance);
}
}
while (true) {
std::cout << "\nBanking System Menu:\n";
std::cout << "1. Create Account\n";
std::cout << "2. Deposit\n";
std::cout << "3. Withdraw\n";
std::cout << "4. Exit\n";
std::cout << "Enter your choice: ";
int choice;
std::cin >> choice;
if (choice == 1) {
std::string accountNumber;
double initialBalance;
std::cout << "Enter account number: ";
std::cin >> accountNumber;
std::cout << "Enter initial balance: ";
std::cin >> initialBalance;
accounts.emplace_back(accountNumber, initialBalance);
saveAccounts(accounts);
std::cout << "Account created successfully.\n";
} else if (choice == 2) {
std::string accountNumber;
double amount;
std::cout << "Enter account number: ";
std::cin >> accountNumber;
std::cout << "Enter deposit amount: ";
std::cin >> amount;
for (BankAccount& account : accounts) {
if (account.getAccountNumber() == accountNumber) {
account.deposit(amount);
saveAccounts(accounts);
std::cout << "Deposit successful. New balance: " << account.getBalance() << "\n";
break;
}
}
} else if (choice == 3) {
std::string accountNumber;
double amount;
std::cout << "Enter account number: ";
std::cin >> accountNumber;
std::cout << "Enter withdrawal amount: ";
std::cin >> amount;
for (BankAccount& account : accounts) {
if (account.getAccountNumber() == accountNumber) {
try {
account.withdraw(amount);
saveAccounts(accounts);
std::cout << "Withdrawal successful. New balance: " << account.getBalance() << "\n";
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
}
break;
}
}
} else if (choice == 4) {
break;
} else {
std::cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}