-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionManager.cpp
More file actions
133 lines (111 loc) · 5.19 KB
/
TransactionManager.cpp
File metadata and controls
133 lines (111 loc) · 5.19 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
// Blake Berry
// 03/08/2022
// Homework 4
// This file is an interface for the TransactionManager class. The
// TransactionManager stores customers and their purchase history for the
// collectibles store. The transactionManager does not own the memory to the
// customer or the items in the transaction it simply logs and can provide
// hisotrical information about a csutomers or the stores transaction history
//-----------------------------------------------------------------------------
#include "TransactionManager.h"
#include "ItemsManager.h"
#include "CustomerManager.h"
#include <fstream>
#include <iostream>
//-------------------------- TransactionManager ----------------------------
// creates a transaction manager that is empty of transaction to manage
// Postconditions: creates a transaction manager that is empty of
// transactions
TransactionManager::TransactionManager() :
transactionHistory_(nullptr)
{
transactionHistory_ = new SearchTree();
}
//------------------------- Destructor ----------------------------
// Frees the transactionHistory tree of its nodes
// Preconditions : Assumes that the collectibles and customers free their
// own dynamic memory
// Postconditions: Frees the memory associated with the transactionManager
TransactionManager::~TransactionManager()
{
delete transactionHistory_;
}
//------------------------- logTransaction ----------------------------
// Given a customer, item, and transaction type a transaction object is
// created and stored in the transaction history
// Preconditions : Assumes that the collectibles and customers free their
// own dynamic memory
// Postconditions: Frees the memory associated with the transactionManager
bool TransactionManager::logTransaction(const Customer*& responsible,
const Collectible*& item,
std::string transactionType)
{
if (responsible == nullptr) {
// throw an error
return false;
}
// make new transaction with refrence to the true memory
Transaction* newTransaction = new Transaction(item, transactionType);
// make a new transaction log
TransactionLogEntry* transactionEntry = nullptr;
transactionEntry = new TransactionLogEntry(responsible, newTransaction);
const Comparable* toAdd = static_cast<const Comparable*>(transactionEntry);
// insert returns false if the entry is already present
// that is an entry with that exact customer is already present still
// need to add transaction
if (!transactionHistory_->insert(toAdd)) {
// if the entry was already in the table the count was increased but
// we need to add the transaction
const Comparable* entryToAddTo = transactionHistory_->retrieve(*toAdd);
const TransactionLogEntry* addHere = nullptr;
addHere = static_cast<const TransactionLogEntry*>(entryToAddTo);
// add the transaction
addHere->addTransaction(new Transaction(*newTransaction));
// clear the already present entry -- deletes origional transaction
delete transactionEntry;
}
return true;
}
//------------------------- DisplayTransactionHistory ----------------------
// Traverses the transaction history tree and prints out all of the
// customers in alphebtic order and prints their transaction in chrological
// order
// Preconditions : Assumes that the print method exists for TransactionEntry
// Postconditions: prints to the console the transaction history for each
// customer. Each transaction will appear on one line
// indented by a tab under the customers name
void TransactionManager::displayTransactionHistory() const
{
std::string boarder2 = "------------------------";
std::string boarder = "--------------";
std::cout << boarder + " Transaction History " + boarder2 << std::endl;
std::cout << *transactionHistory_ << std::endl;
}
//------------------------- DisplayCustomerHistory -------------------------
// Traverses the tree looking for a given customer. If found the customers
// transaction history is displayed in chronological order
// Preconditions : Assumes that the print method exists for TransactionEntry
// Postconditions: prints to the console the transaction history for the
// customer. Each transaction will appear on one line
// indented by a tab under the customers name
//
// if the customer has not made a transaction an exception
// is thrown
void TransactionManager::displayCustomersHistroy(const Customer*& responsible)
const
{
std::string boarder1 = "------------- ";
std::string boarder2 = "-----------------------";
std::cout << boarder1 << responsible->getName() << " Transaction History"
+ boarder2 << std::endl;
TransactionLogEntry dummyToSearch(responsible);
const Comparable* toMatch = static_cast<const Comparable*>(&dummyToSearch);
const Comparable* found = transactionHistory_->retrieve(*toMatch);
if (found == nullptr) {
std::string name = responsible->getName();
std::cout << name + " has not made any transactions" << std::endl;
}
else {
std::cout << *found << std::endl;
}
}