-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionLogEntry.cpp
More file actions
203 lines (164 loc) · 6.97 KB
/
TransactionLogEntry.cpp
File metadata and controls
203 lines (164 loc) · 6.97 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Blake Berry
// 03/08/2022
// Homework 4
// This file is an implimentation for the TransactionLogEntry class. The
// TransactionLogEntry class is a sortable class that sorts based upon the
// customer name. The TransactionLogEntry class couples a customer with a list
// of transactions made by that customer. These transactions are stored in
// chronlogical order
//-----------------------------------------------------------------------------
#include "TransactionLogEntry.h"
//-------------------------- Default Constructor ---------------------------
// Creates an empty entry filled with a null customer and empty transition
// log
// Postconditions: Creates an empty entry filled with a null customer and empty
// transition log
TransactionLogEntry::TransactionLogEntry() :
customer_(nullptr),
transactionLog_(std::vector<Transaction*>())
{
}
//-------------------------- Constructor -----------------------------------
// Creates a log entry given a customer and an initial transaction
// Postconditions: Creates an entry for a given customer and logs the
// transaction
TransactionLogEntry::TransactionLogEntry(const Customer*& cust, Transaction*& initialTransaction) :
customer_(cust),
transactionLog_(std::vector<Transaction*>())
{
transactionLog_.push_back(initialTransaction);
}
//-------------------------- Constructor -----------------------------------
// Creates a dummy transitionLogEntry for a given customer
// Postconditions: Creates an entry for a given customer
TransactionLogEntry::TransactionLogEntry(const Customer*& cust) :
customer_(cust),
transactionLog_(std::vector<Transaction*>())
{
}
//------------------------- Destructor -------------------------------------
// Frees the transaction of any dynamic memory, the transaction does not
// own the item data -- it does not own the customer, that is to be freed
// by the customer Manager
// Postconditions: frees any deynamic memory associated with the
// transactionLogEntry
TransactionLogEntry::~TransactionLogEntry()
{
int size = transactionLog_.size();
for (int i = 0; i < size; i++) {
delete transactionLog_[i];
transactionLog_[i] = nullptr;
}
}
//-------------------------- operator== ------------------------------------
// Checks if two transactionLogEntries are equivilent. Equivilance is
// defined by the customer name the object stores
// Postconditions: Returns true if both objects are equivilent
// Returns false if the objects are not equivilent.
bool TransactionLogEntry::operator==(const Comparable& right) const
{
const TransactionLogEntry& rightHandSide = static_cast<const TransactionLogEntry&>(right);
// if they have the same customer they're the same entry
if (customer_ == rightHandSide.customer_) {
return true;
}
else if (customer_->getID() == rightHandSide.customer_->getID()) {
return true;
}
return false;
}
//-------------------------- operator!= ------------------------------------
// Checks if two transactionLogEntries are not equivilent. Equivilance is
// quivilance is defined by the customer name the object stores
// Postconditions: Returns true if both objects are not equivilent
// Returns false if the objects are equivilent.
bool TransactionLogEntry::operator!=(const Comparable& right) const
{
return !(*this == right);
}
//-------------------------- operator> -------------------------------------
// Checks if two TransactionLogEntries have a greater than relationship.
// Value is determined by the customer name associated with the entry
// Postconditions: Returns true if the right hand side is smaller
// than the left hand side. Otherwise, false is returned
bool TransactionLogEntry::operator>(const Comparable& right) const
{
const TransactionLogEntry& rightHandSide = static_cast<const TransactionLogEntry&>(right);
std::string toCompCustName = rightHandSide.customer_->getName();
std::string toCompCustID = rightHandSide.customer_->getID();
if (customer_->getName() > toCompCustName) {
return true;
}
else if (customer_->getName() == toCompCustName) {
if (customer_->getID() > toCompCustID) {
return true;
}
}
return false;
}
//-------------------------- operator< -------------------------------------
// Checks if two TransactionLogEntries have a less than relationship.
// Value is determined by the customer name associated with the entry
// Postconditions: Returns false if the right hand side is smaller
// than the left hand side. Otherwise, true is returned
bool TransactionLogEntry::operator<(const Comparable& right) const
{
const TransactionLogEntry& rightHandSide = static_cast
<const TransactionLogEntry&>(right);
std::string toCompCustID = rightHandSide.customer_->getID();
std::string toCompCustName = rightHandSide.customer_->getName();
if (customer_->getName() < toCompCustName) {
return true;
}
else if (customer_->getName() == toCompCustName) {
if (customer_->getID() < toCompCustID) {
return true;
}
}
return false;
}
//-------------------------- print --------------------------------------
// Prints the customer name on one line then each transaction in chrono
// order one tab under the customer name
// Postconditions: prints to the console a representation of the object
void TransactionLogEntry::print() const
{
std::cout << customer_->getName() << std::endl;
int size = static_cast<int>(transactionLog_.size());
for (int i = 0; i < size; i++) {
const Collectible* item = transactionLog_[i]->getItemTransacted();
std::string transactionType = transactionLog_[i]->getTransactionType();
// bc friend access
//const Comparable* toPrint = static_cast<const Comparable*>(item);
if (transactionType == "B") {
transactionType = "Purachased";
}
else {
transactionType = "Sold";
}
std::cout << "\t" << transactionType << " " << *item;
std::cout << "\n\t\t" << std::endl;
}
}
//-------------------------- addTransaction --------------------------------
// Adds a transaction for a given customer to their transactionLog
// Preconditions: Assumes that the transaction is valid and that the
// customer is valid
// Postconditions: adds the transaction to the end of the customers log
bool TransactionLogEntry::addTransaction(Transaction* toAdd) const
{
transactionLog_.push_back(toAdd);
return true;
}
//-------------------------- copy ------------------------------------------
// creates a deep copy of the current entry and returns a non-modifyable
// pointer to it
// preconditions : The caller must means to deallocate the memory
// associated
// Postconditions: returns a constant pointer deep copy of the current
// entry
const Comparable* TransactionLogEntry::copy() const
{
const Comparable* newLog = new TransactionLogEntry(*this);
return newLog;
}