-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.cpp
More file actions
125 lines (109 loc) · 3.21 KB
/
BankAccount.cpp
File metadata and controls
125 lines (109 loc) · 3.21 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
#include "BankAccount.h"
#include "Storage.h"
#include <sstream>
#include <fstream>
#include <vector>
// Utility function to split strings (if not already defined in Storage.h)
std::vector<std::string> splitString(const std::string& s, char delimiter) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
// BankAccount methods implementation
BankAccount::BankAccount(int userId, const std::string& accountId, const std::string& name, double initialBalance)
: userId(userId), accountId(accountId), name(name), balance(initialBalance) {}
int BankAccount::getUserId() const {
return userId;
}
std::string BankAccount::getAccountId() const {
return accountId;
}
std::string BankAccount::getName() const {
return name;
}
double BankAccount::getBalance() const {
return balance;
}
bool BankAccount::deposit(double amount) {
if (amount <= 0) {
return false;
}
balance += amount;
return true;
}
bool BankAccount::withdraw(double amount) {
if (amount <= 0 || amount > balance) {
return false;
}
balance -= amount;
return true;
}
void BankAccount::displayBalance() const {
std::cout << "Account: " << accountId << " | Name: " << name
<< " | Balance: $" << balance << std::endl;
}
std::string BankAccount::serialize() const {
std::stringstream ss;
ss << userId << "," << accountId << "," << name << "," << balance;
return ss.str();
}
BankAccount BankAccount::deserialize(const std::string& data) {
auto parts = splitString(data, ',');
if (parts.size() >= 4) {
int userId = std::stoi(parts[0]);
std::string accountId = parts[1];
std::string name = parts[2];
double balance = std::stod(parts[3]);
return BankAccount(userId, accountId, name, balance);
}
// Return a default account if data is invalid
return BankAccount(0, "", "", 0.0);
}
// Bank methods implementation
void Bank::addAccount(const BankAccount& account) {
accounts.push_back(account);
saveAccounts();
}
BankAccount* Bank::findAccount(const std::string& accountId) {
for (auto& account : accounts) {
if (account.getAccountId() == accountId) {
return &account;
}
}
return nullptr;
}
std::vector<BankAccount> Bank::findAccountsByUserId(int userId) {
std::vector<BankAccount> userAccounts;
for (const auto& account : accounts) {
if (account.getUserId() == userId) {
userAccounts.push_back(account);
}
}
return userAccounts;
}
void Bank::loadAccounts() {
accounts.clear();
std::ifstream file("accounts.csv");
if (file.is_open()) {
std::string line;
while (std::getline(file, line)) {
if (!line.empty()) {
accounts.push_back(BankAccount::deserialize(line));
}
}
file.close();
}
}
void Bank::saveAccounts() {
std::ofstream file("accounts.csv");
if (file.is_open()) {
for (const auto& account : accounts) {
file << account.serialize() << std::endl;
}
file.close();
}
}