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
|
#include "LibraryBanking.h"
#include "Transaction.h"
#include <fstream>
#include <iostream>
void LibraryBanking::free() {
delete[] clerks;
delete[] transactions;
}
void LibraryBanking::copyFrom(const LibraryBanking& other) {
clerksAlloc = other.clerksAlloc;
clerksCount = other.clerksCount;
clerks = new BankClerk[clerksAlloc];
for (unsigned i = 0; i < clerksCount; ++i)
clerks[i] = other.clerks[i];
transactionsAlloc = other.transactionsAlloc;
transactionsCount = other.transactionsCount;
transactions = new Transaction[transactionsAlloc];
for (unsigned i = 0; i < transactionsCount; ++i)
transactions[i] = other.transactions[i];
}
void LibraryBanking::resizeClerks() {
clerksAlloc = clerksAlloc == 0 ? 8 : clerksAlloc * 2;
BankClerk* newClerks = new BankClerk[clerksAlloc];
for (unsigned i = 0; i < clerksCount; ++i)
newClerks[i] = clerks[i];
delete[] clerks;
clerks = newClerks;
}
void LibraryBanking::resizeTransactions() {
transactionsAlloc = transactionsAlloc == 0 ? 8 : transactionsAlloc * 2;
Transaction* newTransactions = new Transaction[transactionsAlloc];
for (unsigned i = 0; i < transactionsCount; ++i)
newTransactions[i] = transactions[i];
delete[] transactions;
transactions = newTransactions;
}
LibraryBanking::LibraryBanking() : clerks(nullptr), clerksCount(0), clerksAlloc(0), transactions(nullptr), transactionsCount(0), transactionsAlloc(0)
{ }
LibraryBanking::~LibraryBanking() {
free();
}
LibraryBanking::LibraryBanking(const LibraryBanking& other) {
copyFrom(other);
}
LibraryBanking& LibraryBanking::operator=(const LibraryBanking& other) {
if (this != &other) {
free();
copyFrom(other);
}
return *this;
}
LibraryBanking::LibraryBanking(LibraryBanking&& other) {
clerks = other.clerks;
clerksCount = other.clerksCount;
clerksAlloc = other.clerksAlloc;
transactions = other.transactions;
transactionsCount = other.transactionsCount;
transactionsAlloc = other.transactionsAlloc;
other.clerks = nullptr;
other.clerksCount = other.clerksAlloc = 0;
other.transactions = nullptr;
other.transactionsCount = other.transactionsAlloc = 0;
}
LibraryBanking& LibraryBanking::operator=(LibraryBanking&& other) {
if (this != &other) {
free();
clerks = other.clerks;
clerksCount = other.clerksCount;
clerksAlloc = other.clerksAlloc;
transactions = other.transactions;
transactionsCount = other.transactionsCount;
transactionsAlloc = other.transactionsAlloc;
other.clerks = nullptr;
other.clerksCount = other.clerksAlloc = 0;
other.transactions = nullptr;
other.transactionsCount = other.transactionsAlloc = 0;
}
return *this;
}
void LibraryBanking::RegisterClerk(const BankClerk& clerk) {
if (clerksCount >= clerksAlloc)
resizeClerks();
clerks[clerksCount++] = clerk;
}
BankClerk* LibraryBanking::LoginClerk(unsigned clerkNumber, unsigned dynamicPassword, const String& staticPassword) {
for (unsigned i = 0; i < clerksCount; ++i) {
if (clerks[i].Login(clerkNumber, dynamicPassword, staticPassword))
// "clerks + i" е същото като "&clerks[i]"
return clerks + i;
}
return nullptr;
}
void LibraryBanking::ListPendingTransactions() {
for (unsigned i = 0; i < transactionsCount; ++i) {
if (transactions[i].GetStatus() == STATUS_PENDING) {
std::cout << '[' << i << "] ";
transactions[i].PrintTransactionInfo();
}
}
}
Transaction* LibraryBanking::GetTransaction(unsigned index) {
if (index >= transactionsCount)
return nullptr;
// "transactions + index" е същото като "&transactions[index]"
return transactions + index;
}
const Transaction* LibraryBanking::RegisterTransaction(const Transaction& transaction) {
if (transactionsCount >= transactionsAlloc)
resizeTransactions();
transactions[transactionsCount] = transaction;
if (transaction.GetSum() == 0.0)
transactions[transactionsCount].SetStatus(0, STATUS_ACCEPTED);
return transactions + transactionsCount++;
}
void LibraryBanking::ExportNonPendingTransactions(const String& fileName) {
std::ofstream exportFile(fileName.GetStr(), std::ios::trunc);
if (!exportFile.is_open()) {
std::cout << "Couldn't open file!" << std::endl;
}
else {
for (unsigned i = 0; i < transactionsCount; ++i) {
if (transactions[i].GetStatus() != STATUS_PENDING) {
exportFile << i << ",";
transactions[i].Export(exportFile);
}
}
}
}
|