-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
243 lines (210 loc) · 7.79 KB
/
main.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include<cctype> // isdigit
#include <algorithm> // std::all_of()
#include <fstream> // working with files
#include <vector>
#include <string> // getline()
#include <unordered_map> // to store multiple accounts
using namespace std;
class Account
{
private:
string ownerName;
string accNumber;
string accPin;
double balance = 0;
vector <string> transactionHistory;
public:
Account() {}
Account(string name, string num, string pin) : ownerName(name), accNumber(num), accPin(pin){}
bool authenticate(string pin) {
return pin == accPin;
}
void deposit(double amount){
balance += amount;
string transaction = "Deposited $: " + to_string(amount);
transactionHistory.push_back(transaction);
save_to_file();
cout << transaction <<endl;
}
void withdraw(double amount){
if (balance >= amount)
{
balance -= amount;
string transaction = "Withdrew $: "+to_string(amount);
transactionHistory.push_back(transaction);
save_to_file();
cout << transaction <<endl;
}
else{
cout << "Not Enough Balance!\n";
}
}
void viewBalance() const{
cout << "Your current balance is $: " << balance << endl;
}
void viewTransaction() const{
cout << "Transaction History: \n";
for (int i = 0; i < transactionHistory.size(); i++)
{
cout << transactionHistory[i] << endl;
}
}
void viewProfile() const{
cout << "Account Number: " << accNumber << endl;
cout << "Account Holder's Name: " << ownerName << endl;
cout << "Account PIN (confidential): " << accPin << endl;
cout << "Account Balance: " << balance << endl;
}
void save_to_file() const{
ofstream file(accNumber + ".txt");
if (file.is_open())
{
file << "Account Number: " << accNumber << endl;
file << "Account Holder Name: " << ownerName << endl;
file << "Current Balance ($): " << balance << endl;
file << "Account Pin (Confidential): " << accPin << endl;
file << "Transaction History: \n";
for (int i = 0; i < transactionHistory.size(); i++)
{
file << transactionHistory[i] << endl;
}
file.close();
}
else{
cout << "Unable to open the file!\n";
}
}
};
int main(){
unordered_map<string, Account> accounts;
while (true)
{
int choice;
cout << "\n\n******* Welcome to the Banking System! *******\n\n";
cout << "1: Create Account\n";
cout << "2: Access Account\n";
cout << "3: Exit\n";
cout << "Enter your choice: ";
cin>> choice;
cin.ignore();
switch (choice)
{
case 1:
{
string name, number, pin;
cout << "Enter the Account number (12-digits): ";
getline(cin, number);
while (number.size() != 12 || !all_of(number.begin(), number.end(), ::isdigit)) {
cout << "Invalid Account Number! Please enter again: ";
getline(cin, number);
}
cout << "Enter the Account holder's first name: ";
getline(cin, name);
while (name.size() < 2 || name.size() > 10 || !all_of(name.begin(), name.end(), ::isalpha)) {
cout << "Invalid Name! Please enter again: ";
getline(cin, name);
}
cout << "Set a 4-digit PIN: ";
getline(cin, pin);
while (pin.size() != 4 || !all_of(pin.begin(), pin.end(), ::isdigit)) {
cout << "Invalid PIN! Please enter a 4-digit PIN: ";
getline(cin, pin);
}
accounts.emplace(number, Account(name,number,pin));
cout << "Account Created Successfully!\n";
accounts[number].save_to_file();
break;
}
case 2:
{
string accNumber, pin;
while (true)
{
cout << "Enter Account Number: ";
cin >> accNumber;
cin.ignore();
if (accounts.find(accNumber) != accounts.end()) //to check if the account exists
{
cout << "Enter the 4-digit PIN: ";
cin >> pin;
cin.ignore();
if (accounts[accNumber].authenticate(pin))
{
int accChoice;
bool session = true;
double amount;
while (session)
{
cout << "\n1: Deposite\n";
cout << "2: Withdraw\n";
cout << "3: View Balance\n";
cout << "4: Transaction History\n";
cout << "5: View Profile\n";
cout << "6: Exit\n";
cout << "Enter choice: ";
cin >> accChoice;
cin.ignore();
cout << endl;
switch (accChoice)
{
case 1:
cout << "Enter the amount to deposite ($): ";
cin >> amount;
cin.ignore();
cout << endl;
accounts[accNumber].deposit(amount);
break;
case 2:
cout << "Enter the amount to withdraw ($): ";
cin >> amount;
cin.ignore();
cout << endl;
accounts[accNumber].withdraw(amount);
break;
case 3:
accounts[accNumber].viewBalance();
break;
case 4:
accounts[accNumber].viewTransaction();
break;
case 5:
accounts[accNumber].viewProfile();
break;
case 6:
cout << "Exiting...\n";
session = false;
break;
default:
cout << "Invalid Choice!\n";
break;
}
}
}
else cout << "Incorrect PIN!\n";
}
else
{
cout << "Account not found!\n";
char exit;
cout << "Exit? (Y/N): \n";
cin >> exit;
cin.ignore();
if (exit == 'y' || exit =='Y')
{
cout << "Exiting...\n";
break;
}
else continue;
}
}
}
case 3:
cout << "Thank you for using the Banking System!\n";
return 0;
default:
cout << "Invalid choice! Try again.\n";
}
}
return 0;
}