-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.cpp
More file actions
109 lines (89 loc) · 3.75 KB
/
Bank.cpp
File metadata and controls
109 lines (89 loc) · 3.75 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
// Bank.cpp
#include "Bank.h"
#include <iostream> // Add this line
//extern const std::string correctUsername = "Admin";
//extern const std::string correctPassword = "123456";
// Account class implementation
Account::Account(double balance) : balance(balance) {}
void Account::deposit(double amount) {
balance += amount;
}
void Account::withdraw(double amount) {
if (amount > balance) {
throw InsufficientFundsException(); // You might want to define this exception more thoroughly
}
balance -= amount;
}
double Account::getBalance() const {
return balance;
}
// SavingsAccount class implementation
SavingsAccount::SavingsAccount(double balance, double interestRate)
: Account(balance), interestRate(interestRate) {}
void SavingsAccount::addInterest() {
double interest = balance * interestRate;
deposit(interest);
}
// CheckingAccount class implementation
CheckingAccount::CheckingAccount(double balance, double feePerTransaction)
: Account(balance), feePerTransaction(feePerTransaction) {}
void CheckingAccount::deposit(double amount) {
Account::deposit(amount - feePerTransaction); // Deduct transaction fee for deposits
}
void CheckingAccount::withdraw(double amount) {
Account::withdraw(amount + feePerTransaction); // Include transaction fee for withdrawals
}
// Function to initialize student accounts
std::unordered_map<std::string, std::unique_ptr<Account>> initializeStudents() {
std::unordered_map<std::string, std::unique_ptr<Account>> students;
// Using std::unique_ptr directly instead of std::make_unique
students["John Garica"] = std::unique_ptr<Account>(new CheckingAccount(1000, 0.02));
students["Joe Rivas"] = std::unique_ptr<Account>(new SavingsAccount(2000, 0.05));
students["Martha Rosales"] = std::unique_ptr<Account>(new CheckingAccount(1500, 0.01));
students["Edward Gaytan"] = std::unique_ptr<Account>(new SavingsAccount(3000, 0.03));
students["Sarah Chavez"] = std::unique_ptr<Account>(new CheckingAccount(1200, 0.015));
return students; // Complete the return statement
}
Account* login(const std::unordered_map<std::string, std::unique_ptr<Account>>& students, std::string& loggedInUsername, const std::string& correctUsername, const std::string& correctPassword) {
std::string username;
std::string password;
std::cout << "Enter your username: ";
std::cin >> username;
std::cout << "Enter your password: ";
std::cin >> password;
auto it = students.find(username);
if (it != students.end() && username == correctUsername && password == correctPassword) {
// Username and password are correct
std::cout << "Login successful!\n";
loggedInUsername = username; // Store the logged-in username
return it->second.get();
} else {
// Incorrect username or password
std::cout << "Invalid username or password.\n";
return nullptr;
}
}
// Function to add money to the account
void addMoney(Account* account) {
double amount;
std::cout << "Enter the amount to deposit: ";
std::cin >> amount;
account->deposit(amount);
std::cout << "Deposit successful!\n";
}
// Function to withdraw money from the account
void withdrawMoney(Account* account) {
double amount;
std::cout << "Enter the amount to withdraw: ";
std::cin >> amount;
try {
account->withdraw(amount);
std::cout << "Withdrawal successful!\n";
} catch (InsufficientFundsException&) {
std::cout << "Insufficient funds.\n";
}
}
// Function to view the account balance
void viewAccount(Account* account) {
std::cout << "Your current balance is: " << account->getBalance() << "\n";
}