-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectibleStore.cpp
More file actions
249 lines (205 loc) · 8.46 KB
/
Copy pathCollectibleStore.cpp
File metadata and controls
249 lines (205 loc) · 8.46 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Blake Berry
// 03/08/2022
// Homework 4
// This file is an implimentation for the Collectible store class. The
// CollectibleStore class inherets from the store abstract base class.
// The CollectibleStore class is constructed from two files containing a
// list of customers and a list of collectible items that the store has along
// with their inventory. The CollectibleStore class will run the store given
// a commands file
//------------------------------------------------------------------------------
#include "CollectibleStore.h"
//------------------------ getCommandType ---------------------------------
// takes a string and abstracts the command type
// PreConditions : The command type must be one char, the first char in the
// string. Assumed to be 'A' - 'Z'
// Postconditions: returns a string for the command type and modifies the
// instructions string removing this char
std::string CollectibleStore::getCommandType(std::string& instructions) const
{
std::string command = "";
char commandType = instructions[0];
command.push_back(commandType);
instructions.erase(0, 1);
return command;
}
void CollectibleStore::initalizeItemsManager(std::ifstream& readInventory)
{
iManager_->fillInventory(readInventory);
}
//----------------- initalizeTransactionManager ----------------------------
// Creates the transaction manager
// PreConditions : Needs to be initialized before the store is ran
// Postconditions: The transaction manager is created
void CollectibleStore::initalizeTransactionManager()
{
}
//-------------------- initalizeCustomerManager ----------------------------
// Creates the customer manager, initalizing the customer list from a file
// PreConditions : Needs to be initialized before the store is ran
// The file is assumed to be formatted but not necessarily
// valid
// Postconditions: The customer manager is created along with the stores
// customerlist
void CollectibleStore::initalizeCustomerManager(std::ifstream& readCustomers)
{
cManager_->fillCustomerLog(readCustomers);
}
//------------------------ hashCommands ---------------------------------
// takes a string command and generates a hash based off that command
// PreConditions : The command type must be one char, the first char in the
// string. Assumed valid if 'A' - 'Z'
// Postconditions: returns an integer hash 'Char' - 'A' for the given
// command
int CollectibleStore::hashCommands(std::string key) const
{
return key[0] - 'A';
}
//-------------------------- Constructor -----------------------------------
// Creates a store with an empty managers. The command factory is filled
// manually based upon the hash of that command.
// Preconditions : The Manager classes and factory classes must be
// implimented
//
// There are at most 26 commands with no commands sharing
// the same key
//
// Assumes no collisions
// Postconditions: A store with an inventory, customer list, and empty
// transaction log is created
CollectibleStore::CollectibleStore()
{
iManager_ = new ItemsManager();
tManager_ = new TransactionManager();
cManager_ = new CustomerManager();
generateCommand_ = new Command * [NUM_COMMANDS];
for (int i = 0; i < NUM_COMMANDS; i++) {
generateCommand_[i] = nullptr;
}
std::vector<Command*> toProcess;
Command* buy = new CommandBuy();
Command* sell = new CommandSell();
Command* display = new CommandDisplay();
Command* history = new CommandHistory();
Command* customer = new CommandCustomer();
toProcess.push_back(buy);
toProcess.push_back(sell);
toProcess.push_back(display);
toProcess.push_back(history);
toProcess.push_back(customer);
int toProcessSize = toProcess.size();
for (int i = 0; i < toProcessSize; i++) {
int hash = hashCommands(toProcess[i]->getID());
if (hash >= 0 && hash < NUM_COMMANDS) {
generateCommand_[hash] = toProcess[i];
}
}
}
//-------------------------- destructor -----------------------------------
// Frees the memory associated with the store
// Preconditions : The managers desructors free their associated memory
// Postconditions: The store is cleared of all the dynamic memory that is
// associated
CollectibleStore::~CollectibleStore()
{
delete tManager_;
delete iManager_;
delete cManager_;
// clearing out the command factory
for (int i = 0; i < NUM_COMMANDS; i++) {
if (generateCommand_[i] != nullptr) {
delete generateCommand_[i];
generateCommand_[i] = nullptr;
}
}
delete[] generateCommand_;
}
//------------------------ runStore ----------------------------------------
// Operates the store, processes store commands from a file, and displays to
// the console.
// PreConditions : The store managers must be initialized
// The infile must be formatted correctly but not
// necessarily valid
// Postconditions: The store managers are modified by the commands file
// and the store operations that can display to the console
// display to the console.
void CollectibleStore::runStore(std::ifstream& readCommands)
{
std::string err1 = "Undefined Command Type";
std::string curCommand = "";
while (readCommands.peek() != EOF) {
std::getline(readCommands, curCommand);
// assumes command type is first
std::string commandType = getCommandType(curCommand);
// hash the command
int commandIndex = hashCommands(commandType);
if (commandIndex < 0 || commandIndex >= NUM_COMMANDS) {
std::cout << err1 + " '" + commandType + "'\n" << std::endl;
}
else {
Command* toDo = generateCommand_[commandIndex];
if (toDo == nullptr) {
std::cout << err1 + " '" + commandType + "'\n" << std::endl;
}
else {
try {
toDo->execute(tManager_, iManager_, cManager_, curCommand);
}
catch (CollectiblesStoreError err) {
std::cout << "\n-----------------";
std::cout << "An Error Occured During Transaction";
std::cout << "-----------------" << std::endl;
std::cout << err.what() << "\n" << std::endl;
continue;
}
}
}
}
}
//------------------------ fillStoreInventory ---------------------------
// Fills the store with items and outputs any errors to the console
// PreConditions : The input file must be formatted correctly even if the
// data is invalid
// Postconditions: The store is filled with items and any errors are
// output to the console
void CollectibleStore::fillStoreInventory(std::ifstream& readInventory)
{
initalizeItemsManager(readInventory);
}
//-------------------- fillStoreCustomers ---------------------------------
// Fills the store with known customers and outputs any errors to the
// console
// PreConditions : The input file must be formatted correctly even if the
// data is invalid
// Postconditions: The store is filled with a customer list and any errors
// are output to the console
void CollectibleStore::fillStoreCustomers(std::ifstream& readCustomers)
{
initalizeCustomerManager(readCustomers);
}
//--------------------------------- main -------------------------------
// Creates a store, filling it with customers and items. Then operates
// the store using a file of commands
// PreConditions : The files must be formatted properly and all implimentations
// are the handle any dynamic memory appropriately
// Postconditions: The stores commands are displayed to the console if
// applicable and any error during processing is displayed
// while that input line is skipped
int main() {
// customer file
std::ifstream custs("C:/Users/Trident/source/repos/StoreTesting/StoreTesting/Text.txt");
// inventory file
std::ifstream items("C:/Users/Trident/source/repos/StoreTesting/StoreTesting/Text1.txt");
// commands file
std::ifstream commands("C:/Users/Trident/source/repos/StoreTesting/StoreTesting/Text2.txt");
CollectibleStore* store = new CollectibleStore();
// use items file
store->fillStoreInventory(items);
// use customer file
store->fillStoreCustomers(custs);
// use commands file
store->runStore(commands);
// free store memory
delete store;
return 0;
}