-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (159 loc) · 4.55 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (159 loc) · 4.55 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
#include <ctime>
using namespace std;
class Transaction{
public:
string type;
double amount;
string timestamp;
Transaction(string t,double amt){
type=t;
amount=amt;
timestamp=currentTime();
}
string currentTime(){
time_t now=std::time(nullptr);
char* dt=ctime(&now);
string s(dt);
s.pop_back();
return s;
}
void print(){
cout<<left<<setw(12)<<type<<"| Rs. "<<setw(10)<<amount<<" | "<<timestamp<<endl;
}
};
class Account{
public:
string accNumber;
string accType;
double balance;
vector<Transaction> history;
Account(string num,string type,double bal){
accNumber=num;
accType=type;
balance=bal;
}
void deposit(double amount){
balance+=amount;
history.emplace_back("Deposit",amount);
}
bool withdraw(double amount){
if(amount>balance) return false;
balance-=amount;
history.emplace_back("Withdraw",amount);
return true;
}
void displayAccountDetails(){
cout<<"\nAccount Number: "<<accNumber;
cout<<"\nAccount Type: "<<accType;
cout<<"\nBalance: Rs. "<<balance<<"\n";
}
void printTransactionHistory(){
cout<<"\n--- Transaction History ---\n";
for(auto &t:history) t.print();
}
};
class Customer{
public:
string name;
string customerID;
Account account;
Customer(string n,string id,Account acc):name(n),customerID(id),account(acc){}
void showCustomerInfo(){
cout<<"\nCustomer Name: "<<name;
cout<<"\nCustomer ID: "<<customerID;
account.displayAccountDetails();
}
};
class BankSystem{
vector<Customer> customers;
public:
void createCustomer(){
string name,id,accNum,accType;
double balance;
cin.ignore();
cout<<"Enter Name: ";
getline(cin,name);
cout<<"Enter Customer ID: ";
getline(cin,id);
cout<<"Enter Account Number: ";
getline(cin,accNum);
cout<<"Enter Account Type: ";
getline(cin,accType);
cout<<"Enter Initial Balance: ";
cin>>balance;
Account newAcc(accNum,accType,balance);
Customer newCust(name,id,newAcc);
customers.push_back(newCust);
cout<<"\nCustomer and Account Created Successfully!\n";
}
Customer* findCustomer(const string &id){
for(auto &cust:customers){
if(cust.customerID==id) return &cust;
}
return nullptr;
}
void depositMoney(){
string id; double amt;
cout<<"Enter Customer ID: ";
cin>>id;
Customer* cust=findCustomer(id);
if(cust){
cout<<"Enter Amount to Deposit: ";
cin>>amt;
cust->account.deposit(amt);
cout<<"Deposit successful.\n";
}else cout<<"Customer not found.\n";
}
void withdrawMoney(){
string id; double amt;
cout<<"Enter Customer ID: ";
cin>>id;
Customer* cust=findCustomer(id);
if(cust){
cout<<"Enter Amount to Withdraw: ";
cin>>amt;
if(cust->account.withdraw(amt))
cout<<"Withdrawal successful.\n";
else
cout<<"Insufficient balance.\n";
}else cout<<"Customer not found.\n";
}
void viewAccountDetails(){
string id;
cout<<"Enter Customer ID: ";
cin>>id;
Customer* cust=findCustomer(id);
if(cust){
cust->showCustomerInfo();
cust->account.printTransactionHistory();
}else cout<<"Customer not found.\n";
}
};
int main(){
BankSystem bank;
int choice;
do{
cout<<"\n----BANKING SYSTEM MENU----\n";
cout<<"1. Create Customer and Account\n";
cout<<"2. Deposit\n";
cout<<"3. Withdraw\n";
cout<<"4. View Account Details\n";
cout<<"5. Exit\n";
cout<<"Choose an option: ";
cin>>choice;
switch(choice){
case 1: bank.createCustomer(); break;
case 2: bank.depositMoney(); break;
case 3: bank.withdrawMoney(); break;
case 4: bank.viewAccountDetails(); break;
case 5: cout<<"Exiting...\n"; break;
default: cout<<"Invalid choice. Try again.\n";
}
}while(choice!=5);
return 0;
}