-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.cpp
More file actions
226 lines (191 loc) · 8.02 KB
/
admin.cpp
File metadata and controls
226 lines (191 loc) · 8.02 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
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
#include"admin.h"
//Helper function to find customer to do specific functions on like withdraw etc.
namespace { // Using an anonymous namespace keeps this function private to this file
int find_customer(const std::vector<customer>& all_customers, const long& acc_no) {
for (size_t i = 0; i < all_customers.size(); ++i) {
if (all_customers[i].get_account_number() == acc_no) {
return i; // Found the customer, return their index
}
}
return -1; // Sentinel value indicating customer not found
}
}
//define functions of admin header file
bool admin::login(const std::string& correct_password){
std::string passwd;
std::cout<<"Welcome Admin !!\nEnter Your Password to Continue: ";
std::cin>> passwd;
if(encrypt(passwd) == correct_password) return true;
else return false;
}
long admin::generate_new_account_number(const std::vector<customer>& all_customers) {
long acc_no = 1001; // starting point
while (true) {
bool exists = false;
for (const auto& c : all_customers) {
if (c.get_account_number() == acc_no) {
exists = true;
break;
}
}
if (!exists) return acc_no; // found a gap
acc_no++;
}
}
void admin::create_account(std::vector<customer>& all_customers) {
std::string name, password;
double initial_deposit;
std::cout << "Enter customer's full name: ";
std::getline(std::cin, name);
std::cout << "Enter a temporary password: ";
std::cin >> password;
std::cout << "Enter initial deposit amount (minimum 500): ";
std::cin >> initial_deposit;
if (initial_deposit < 500) {
std::cout << "Error: deposit must be at least 500\n";
return;
}
long new_acc_no = generate_new_account_number(all_customers);
customer new_customer(new_acc_no, name, encrypt(password),
initial_deposit, 0.0, 4, 1);
all_customers.push_back(new_customer);
std::cout << "\nAccount created successfully!\n";
std::cout << "Account Number: " << new_acc_no << "\n";
std::cout << "Customer Name: " << name << "\n";
}
void admin::delete_account(std::vector<customer>& all_customers){
long acc_no;
std::cout << "Enter account number to DELETE: ";
std::cin >> acc_no;
int index = find_customer(all_customers, acc_no);
if (index != -1) {
// Customer found, remove them from the vector
std::string temp;
std::cout<<"Type DELETE to confirm Deletion: ";
std::cin>>temp;
if(temp == "DELETE"){
all_customers.erase(all_customers.begin() + index);
std::cout << "Account " << acc_no << " has been successfully deleted." << std::endl;
}else{
std::cout<<"Account "<< acc_no <<" was not deleted. \n";
}
} else {
std::cout << "Error: Customer not found." << std::endl;
}
}
void admin::withdraw(std::vector<customer>& all_customers, const AppConfig& config){
long acc_no;
double amount;
std::cout<<"Enter Account number to withdraw from: ";
std::cin>>acc_no;
std::cout<<"Enter The Amount to Withdraw: ";
std::cin>> amount;
int index = find_customer(all_customers, acc_no);
if (index != -1) {
customer& current_customer = all_customers[index];
current_customer.withdraw(amount, config, false); // Use the customer's own deposit method
} else {
std::cout << "Error: Customer not found." << std::endl;
}
}
void admin::deposit(std::vector<customer>& all_customers){
long acc_no;
double amount;
std::cout << "Enter account number to deposit into: ";
std::cin >> acc_no;
std::cout << "Enter amount to deposit: ";
std::cin >> amount;
//I am not Checking for negative amounts in Admin activities because Admin must know not to put negative values also because I am lazy :)
int index = find_customer(all_customers, acc_no);
if (index != -1) {
customer& current_customer = all_customers[index];
current_customer.deposit(amount, false); // Use the customer's own deposit method
} else {
std::cout << "Error: Customer not found." << std::endl;
}
}
void admin::transfer(std::vector<customer>& all_customers, const AppConfig& config){
long receiver_acc_no, sender_acc_no;
double amount;
std::cout<<"Enter Sender's Account No: ";
std::cin>>sender_acc_no;
std::cout<<"Enter Receiver's Account No: ";
std::cin>>receiver_acc_no;
std::cout<<"Amount to be Transferred: ";
std::cin>> amount;
int sender_index = find_customer(all_customers, sender_acc_no);
int receiver_index = find_customer(all_customers, receiver_acc_no);
if(sender_index != -1 && receiver_index != -1){
customer &sender = all_customers[sender_index];
customer &receiver = all_customers[receiver_index];
if(sender.withdraw(amount, config, false)){
receiver.deposit(amount, true);
}
}else{
std::cout << "Transaction Failed: Invalid Sender or Receiver Account Number.\n";
}
}
void admin::give_interest(std::vector<customer>& all_customers, double interest_rate){
for(customer &user : all_customers){
double interest_amount = user.get_balance() * interest_rate;
// Deposit the interest amount silently
user.deposit(interest_amount, true);
}
std::cout << "Interest of " << interest_rate * 100 << "% has been successfully applied to all accounts.";
}
void admin::loan_interest(std::vector<customer>& all_customers, const AppConfig& config) {
for (customer& user : all_customers) {
user.withdraw(config.maintenance_charge, config, true);
double interest_amount = user.get_loan_amount() * config.loan_charge;
user.edit_loan_amount(user.get_loan_amount() + interest_amount);
}
std::cout << "Maintenance Charges Deducted from all Accounts.\nLoan Interest Added to Accounts having loan.";
}
void admin::unlock_account(std::vector<customer>& all_customers){
long acc_no;
std::cout<<"Enter Account Number to Unlock: ";
std::cin>>acc_no;
int index = find_customer(all_customers, acc_no);
if(index!=-1){
customer& current_customer = all_customers[index];
current_customer.edit_account_status(1);
current_customer.edit_password_attempts_remaining(4);
std::cout<<"Congrats! Account is Now ACTIVE.\n";
}else{
std::cout<<"Account Not Found.\n";
}
}
void admin::give_loan(std::vector<customer>& all_customers){
long acc_no;
std::cout<<"Enter the Account Number of User: ";
std::cin>>acc_no;
int index = find_customer(all_customers, acc_no);
if(index!=-1){
customer& current_customer = all_customers[index];
std::cout<<"Enter The Amount of Loan to Approve: ";
double loan;
std::cin>>loan;
current_customer.edit_loan_amount(current_customer.get_loan_amount() + loan);
std::cout<<"Transaction Successful. \n";
}
}
void admin::view_customer_info(const std::vector<customer>& all_customers){
long acc_no;
std::cout<<"Enter the Account Number of User: ";
std::cin>>acc_no;
int index = find_customer(all_customers, acc_no);
if(index != -1){
const customer& current_customer = all_customers[index];
std::cout << "\n--- Customer Details ---" << std::endl;
std::cout << "Account Number: " << current_customer.get_account_number() << std::endl;
std::cout << "Customer Name: " << current_customer.get_customer_name() << std::endl;
std::cout << "Balance: Rs." << current_customer.get_balance() << std::endl;
std::cout << "Loan Amount: Rs." << current_customer.get_loan_amount() << std::endl;
std::cout << "Account Status: ";
if(current_customer.get_account_status() == 1) std::cout<<"Active. \n";
else std::cout<<"Locked. \n";
std::cout << "------------------------" << std::endl;
} else {
std::cout << "Error: Customer with account number '" << acc_no << "' not found." << std::endl;
}
}