-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
128 lines (108 loc) · 3.82 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
#include <iostream>
#include <vector>
#include <iomanip>
#include <string>
class BankAccount {
private:
std::string accountHolder;
double balance;
std::string password;
public:
BankAccount(const std::string& holder, double initialBalance, const std::string& pwd)
: accountHolder(holder), balance(initialBalance), password(pwd) {}
const std::string& getAccountHolder() const {
return accountHolder;
}
double getBalance() const {
return balance;
}
bool checkPassword(const std::string& pwd) const {
return password == pwd;
}
void deposit(double amount) {
if (amount > 0) {
balance += amount;
std::cout << "Deposit successful. New balance: " << balance << std::endl;
} else {
std::cout << "Invalid deposit amount." << std::endl;
}
}
void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
std::cout << "Withdrawal successful. New balance: " << balance << std::endl;
} else {
std::cout << "Invalid withdrawal amount or insufficient funds." << std::endl;
}
}
};
class ATM {
private:
std::vector<BankAccount> accounts;
public:
void createAccount(const std::string& holder, double initialBalance, const std::string& pwd) {
accounts.emplace_back(holder, initialBalance, pwd);
std::cout << "Account created successfully." << std::endl;
}
BankAccount* findAccount(const std::string& holder, const std::string& pwd) {
for (auto& account : accounts) {
if (account.getAccountHolder() == holder && account.checkPassword(pwd)) {
return &account;
}
}
return nullptr;
}
void displayBalance(BankAccount* account) {
if (account != nullptr) {
std::cout << "Account Holder: " << account->getAccountHolder() << std::endl;
std::cout << "Balance: " << account->getBalance() << std::endl;
} else {
std::cout << "Account not found or incorrect password." << std::endl;
}
}
};
int main() {
ATM atm;
// Create two testing accounts
atm.createAccount("1234qwerty", 1000.0, "qwerty1234");
atm.createAccount("4321ytrewq", 500.0, "ytrewq4321");
while (true) {
std::cout << "Enter account holder name (or 'exit' to quit): ";
std::string accountHolder;
std::cin >> accountHolder;
if (accountHolder == "exit") {
break;
}
std::cout << "Enter account password: ";
std::string password;
std::cin >> password;
BankAccount* account = atm.findAccount(accountHolder, password);
if (account != nullptr) {
std::cout << "1. Display Balance\n2. Deposit\n3. Withdraw\nChoose option (1-3): ";
int option;
std::cin >> option;
switch (option) {
case 1:
atm.displayBalance(account);
break;
case 2:
double depositAmount;
std::cout << "Enter deposit amount: ";
std::cin >> depositAmount;
account->deposit(depositAmount);
break;
case 3:
double withdrawalAmount;
std::cout << "Enter withdrawal amount: ";
std::cin >> withdrawalAmount;
account->withdraw(withdrawalAmount);
break;
default:
std::cout << "Invalid option. Please choose again." << std::endl;
}
} else {
std::cout << "Account not found or incorrect password. Please enter valid account holder name and password." << std::endl;
}
}
return 0;
}