-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode
More file actions
179 lines (166 loc) · 6.82 KB
/
Copy pathcode
File metadata and controls
179 lines (166 loc) · 6.82 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
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
using namespace std;
// Base class User
class User {
protected:
string username, password; // Username and password for the user
float balance; // Account balance
public:
User() : balance(0.0) {} // Default constructor initializing balance to 0.0
virtual void menu() = 0; // Pure virtual function for menu, implemented by derived classes
string getUsername() { return username; } // Getter for username
// Login function to verify credentials
bool login(string u, string p) {
return (u == username && p == password); // Returns true if username and password match
}
};
// Derived class Customer
class Customer : public User {
float fdAmount; // Fixed deposit amount
float loanAmount; // Loan amount
public:
// Constructor to initialize a customer with username, password, and balance
Customer(string u, string p) {
username = u;
password = p;
balance = 0.0; // Initial balance is 0.0
fdAmount = 0.0; // Initially, no fixed deposit
loanAmount = 0.0; // Initially, no loan
}
// Overridden menu function for customer-specific operations
void menu() override {
int choice;
do {
// Displaying customer menu options
cout << "\n[Customer] " << username << "'s Menu\n";
cout << "1. Check Balance\n2. Deposit\n3. Withdraw\n4. Fixed Deposit\n5. Apply Loan\n6. Logout\nChoice: ";
cin >> choice; // Taking customer input for menu choice
// Switch case for customer operations
switch (choice) {
case 1:
cout << "Current Balance: Rs." << balance << endl; // Show balance
break;
case 2: {
float amt;
cout << "Deposit Amount: Rs.";
cin >> amt; // Taking deposit amount as input
balance += amt; // Add deposit to balance
cout << "Deposited Successfully.\n";
break;
}
case 3: {
float amt;
cout << "Withdraw Amount: Rs.";
cin >> amt; // Taking withdrawal amount as input
if (amt <= balance) {
balance -= amt;
cout << "Withdrawn.\n"; // Update balance after withdrawal
}
else cout << "Insufficient balance.\n"; // Insufficient balance error
break;
}
case 4: {
float amt;
cout << "FD Amount: Rs.";
cin >> amt; // Taking Fixed Deposit amount as input
if (amt <= balance) {
fdAmount += amt; // Update fixed deposit amount
balance -= amt; // Deduct from balance
cout << "FD of Rs." << amt << " created.\n";
} else cout << "Insufficient balance.\n"; // Insufficient balance error
break;
}
case 5: {
float amt;
cout << "Loan Amount: Rs.";
cin >> amt; // Taking loan amount as input
loanAmount += amt; // Update loan amount
cout << "Loan Request Submitted.\n"; // Loan request confirmation
break;
}
case 6:
cout << "Logging out...\n"; // Logout operation
break;
default:
cout << "Invalid!\n"; // Invalid choice error
}
} while (choice != 6); // Loop continues until the user selects logout option (6)
}
};
// Derived class Admin
class Admin : public User {
public:
// Constructor initializing admin's default username and password
Admin() {
username = "admin";
password = "admin123";
}
// Overridden menu function for admin-specific operations
void menu() override {
cout << "\n[Admin] Welcome, Admin!\n";
cout << "(In full version: View all users, Approve loans, etc.)\n"; // Placeholder message
}
};
// Bank system class to manage customers and admin operations
class BankSystem {
vector<Customer> customers; // List of customers
Admin admin; // Single admin instance
// Function to save customer data to file
void saveToFile() {
ofstream fout("users.txt"); // Open file for writing
for (auto &c : customers) { // Iterate over customers
fout << c.getUsername() << endl; // Save each customer's username
}
fout.close(); // Close the file
}
public:
// Function to run the bank system
void run() {
int choice;
do {
// Displaying main menu
cout << "\n--- Welcome to CLI Bank ---\n";
cout << "1. Register\n2. Login as Customer\n3. Login as Admin\n4. Exit\nChoice: ";
cin >> choice;
if (choice == 1) {
// Register new customer
string u, p;
cout << "Choose Username: "; cin >> u;
cout << "Choose Password: "; cin >> p;
customers.push_back(Customer(u, p)); // Add new customer to the list
cout << "Registration Successful!\n";
saveToFile(); // Save customer data to file
} else if (choice == 2) {
// Customer login
string u, p;
cout << "Username: "; cin >> u;
cout << "Password: "; cin >> p;
bool found = false;
for (auto &c : customers) {
if (c.login(u, p)) { // Check if login credentials match
c.menu(); // Show customer menu if login is successful
found = true;
break;
}
}
if (!found) cout << "Login Failed.\n"; // Login failure message
} else if (choice == 3) {
// Admin login
string u, p;
cout << "Admin Username: "; cin >> u;
cout << "Admin Password: "; cin >> p;
if (admin.login(u, p)) admin.menu(); // If admin login is successful, show admin menu
else cout << "Invalid Admin Credentials.\n"; // Admin login failure message
}
} while (choice != 4); // Loop continues until user selects Exit option (4)
cout << "Thank you for using CLI Bank.\n"; // Exit message
}
};
int main() {
BankSystem bank; // Create an instance of BankSystem
bank.run(); // Run the bank system
return 0; // End of the program
}