-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransaction.cpp
More file actions
100 lines (85 loc) · 2.41 KB
/
Transaction.cpp
File metadata and controls
100 lines (85 loc) · 2.41 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
#define _CRT_SECURE_NO_WARNINGS
#include "Transaction.h"
#include <sstream>
#include <iomanip>
using namespace std;
Transaction::Transaction(
const string& sender,
const string& receiver,
double amount,
const string& description,
TransactionType type,
time_t timestamp
) : sender(sender),
receiver(receiver),
amount(amount),
description(description),
type(type),
timestamp(timestamp)
{
// Generate a unique transaction ID
stringstream ss;
ss << hex << timestamp << "_"
<< sender.substr(0, 3) << "_"
<< receiver.substr(0, 3) << "_"
<< fixed << setprecision(2) << amount;
transactionId = ss.str();
}
string Transaction::getSender() const {
return sender;
}
string Transaction::getReceiver() const {
return receiver;
}
double Transaction::getAmount() const {
return amount;
}
time_t Transaction::getTimestamp() const {
return timestamp;
}
TransactionType Transaction::getType() const {
return type;
}
string Transaction::getTransactionId() const {
return transactionId;
}
string Transaction::getDescription() const {
return description;
}
string Transaction::getFormattedTimestamp() const {
char buffer[26];
ctime_s(buffer, sizeof(buffer), ×tamp);
string timeStr(buffer);
if (!timeStr.empty() && timeStr.back() == '\n') {
timeStr.pop_back();
}
return timeStr;
}
string Transaction::getFormattedAmount() const {
stringstream ss;
ss << "$" << fixed << setprecision(2) << amount;
return ss.str();
}
string Transaction::toString() const {
stringstream ss;
ss << "[" << getFormattedTimestamp() << "] ";
switch (type) {
case TransactionType::TRANSFER:
ss << sender << " -> " << receiver << ": " << getFormattedAmount();
break;
case TransactionType::WITHDRAWAL:
ss << "Withdrawal from " << sender << ": " << getFormattedAmount();
break;
case TransactionType::INITIAL_DEPOSIT:
ss << "Initial deposit to " << receiver << ": " << getFormattedAmount();
break;
case TransactionType::SYSTEM_ADJUST:
ss << "System adjustment for " << receiver << ": " << getFormattedAmount();
break;
}
if (!description.empty()) {
ss << " (" << description << ")";
}
ss << " [ID: " << transactionId << "]";
return ss.str();
}