-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.cpp
More file actions
180 lines (168 loc) · 7.18 KB
/
Copy pathTransaction.cpp
File metadata and controls
180 lines (168 loc) · 7.18 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
#include "Transaction.h"
#include <iostream>
#include <fstream>
#include <mutex>
#include <sstream>
using namespace std;
static mutex log_mutex;
static const int PIN_LENGTH = 4;
void Transaction::deposit(Account& acc, double amount, const string& pin) {
if (!acc.authenticate(pin)) {
cout << "Error: Authentication failed for Account " << acc.account_id << "\n";
return;
}
lock_guard<mutex> lock(*acc.mtx);
acc.balance += amount;
cout << "Deposited " << amount << " to Account " << acc.account_id << ". New balance: " << acc.balance << "\n";
log_transaction(acc.account_id, "deposit", amount);
}
void Transaction::withdraw(Account& acc, double amount, const string& pin) {
if (!acc.authenticate(pin)) {
cout << "Error: Authentication failed for Account " << acc.account_id << "\n";
return;
}
lock_guard<mutex> lock(*acc.mtx);
double available = acc.balance + acc.overdraft_limit;
if (available >= amount) {
acc.balance -= amount;
cout << "Withdrew " << amount << " from Account " << acc.account_id << ". New balance: " << acc.balance << "\n";
log_transaction(acc.account_id, "withdraw", amount);
} else {
cout << "Error: Insufficient funds for Account " << acc.account_id << "\n";
}
}
void Transaction::transfer(Account& from, Account& to, double amount, const string& pin) {
if (!from.authenticate(pin) || !to.is_active) {
cout << "Error: Invalid account or authentication failed\n";
return;
}
unique_lock<mutex> lock_from(*from.mtx, defer_lock);
unique_lock<mutex> lock_to(*to.mtx, defer_lock);
if (from.account_id < to.account_id) {
lock_from.lock();
lock_to.lock();
} else {
lock_to.lock();
lock_from.lock();
}
if (from.balance + from.overdraft_limit >= amount) {
from.balance -= amount;
to.balance += amount;
cout << "Transferred " << amount << " from Account " << from.account_id << " to " << to.account_id << "\n";
log_transaction(from.account_id, "transfer_out", amount);
log_transaction(to.account_id, "transfer_in", amount);
} else {
cout << "Error: Insufficient funds for transfer from Account " << from.account_id << "\n";
}
}
void Transaction::request_loan(Account& acc, double amount, const string& pin) {
if (!acc.authenticate(pin)) {
cout << "Error: Authentication failed for Account " << acc.account_id << "\n";
return;
}
lock_guard<mutex> lock(*acc.mtx);
double max_loan = (acc.account_type == AccountType::BUSINESS) ? 5000.0 : 1000.0;
if (amount > 0 && acc.loan_balance + amount <= max_loan) {
acc.loan_balance += amount;
acc.balance += amount;
cout << "Loan of " << amount << " granted to Account " << acc.account_id
<< ". New balance: " << acc.balance << ", Loan balance: " << acc.loan_balance << "\n";
log_transaction(acc.account_id, "loan", amount);
} else {
cout << "Error: Loan request denied for Account " << acc.account_id << "\n";
}
}
void Transaction::pay_loan(Account& acc, double amount, const string& pin) {
if (!acc.authenticate(pin)) {
cout << "Error: Authentication failed for Account " << acc.account_id << "\n";
return;
}
lock_guard<mutex> lock(*acc.mtx);
if (acc.loan_balance >= amount && acc.balance >= amount) {
acc.loan_balance -= amount;
acc.balance -= amount;
cout << "Paid " << amount << " towards loan for Account " << acc.account_id
<< ". Remaining loan: " << acc.loan_balance << "\n";
log_transaction(acc.account_id, "loan_payment", amount);
} else {
cout << "Error: Insufficient funds or invalid loan payment for Account " << acc.account_id << "\n";
}
}
void Transaction::apply_interest(Account& acc) {
lock_guard<mutex> lock(*acc.mtx);
if (acc.is_active && acc.balance > 0) {
double interest = acc.balance * acc.interest_rate / 12;
acc.balance += interest;
cout << "Applied interest " << interest << " to Account " << acc.account_id
<< ". New balance: " << acc.balance << "\n";
log_transaction(acc.account_id, "interest_earned", interest); // Renamed to clarify
}
}
void Transaction::delete_account(Account& acc, const string& pin) {
if (!acc.authenticate(pin)) {
cout << "Error: Authentication failed for Account " << acc.account_id << "\n";
return;
}
lock_guard<mutex> lock(*acc.mtx);
if (acc.balance == 0.0 && acc.loan_balance == 0.0) {
acc.is_active = false;
cout << "Account " << acc.account_id << " deleted\n";
log_transaction(acc.account_id, "delete", 0.0);
} else {
cout << "Error: Cannot delete Account " << acc.account_id << " with balance "
<< acc.balance << " or loan " << acc.loan_balance << "\n";
}
}
void Transaction::generate_statement(const Account& acc, const string& pin, const vector<TransactionLog>& logs) {
if (!acc.authenticate(pin)) {
cout << "Error: Authentication failed for Account " << acc.account_id << "\n";
return;
}
cout << "\nStatement for Account " << acc.account_id << " ("
<< (acc.account_type == AccountType::SAVINGS ? "Savings" :
acc.account_type == AccountType::CHECKING ? "Checking" : "Business") << "):\n";
cout << "Holder: " << acc.account_holder_name << " | Age: " << acc.age << "\n";
cout << "Balance: " << acc.balance << " | Loan Balance: " << acc.loan_balance
<< " | Overdraft Limit: " << acc.overdraft_limit << "\n";
cout << "Recent Transactions:\n";
for (const auto& log : logs) {
if (log.account_id == acc.account_id) {
string time_str = ctime(&log.timestamp);
time_str.pop_back();
cout << log.type << ": " << log.amount << " at " << time_str << "\n";
}
}
}
vector<TransactionLog> Transaction::load_transaction_logs() {
vector<TransactionLog> logs;
ifstream log_file("transactions.log");
if (log_file.is_open()) {
string line;
while (getline(log_file, line)) {
TransactionLog log;
stringstream ss(line);
string timestamp_str;
getline(ss, timestamp_str, '|');
log.account_id = stoi(timestamp_str);
getline(ss, log.type, '|');
ss >> log.amount;
getline(ss, timestamp_str, '|');
getline(ss, timestamp_str);
log.timestamp = time(nullptr); // Simplified for Windows
logs.push_back(log);
}
log_file.close();
}
return logs;
}
void Transaction::log_transaction(int account_id, const string& type, double amount) {
lock_guard<mutex> lock(log_mutex);
ofstream log_file("transactions.log", ios::app);
if (log_file.is_open()) {
time_t now = time(nullptr);
string time_str = ctime(&now);
time_str.pop_back();
log_file << account_id << " | " << type << " | " << amount << " | " << time_str << "\n";
log_file.close();
}
}