-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
268 lines (258 loc) · 6.63 KB
/
main.cpp
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <iostream>
#include "Computer.h"
#include "Mouse.h"
#include "Keyboard.h"
#include "Branch.h"
#include "Tablet.h"
#include <vector>
#include <algorithm>
#include "MainOffice.h"
#include <string>
#include "HWExceptions.h"
// not all includes are a "must"
#define TEST_MAIN_SIZE 6
void print_branch_catalog(const Branch& branch)
{
std::vector<Item*> catalog = branch.getCatalog();
std::cout << "Printing KSF Branch in " << branch.getLocation() << std::endl;
//sort by id, you need to implement comparator
std::sort(catalog.begin(), catalog.end(), Item::itemPtrCompare);
std::cout << "There are " << catalog.size() << " items in the catalog: " << std::endl;
for (int i = 0; i < catalog.size(); i++)
{
std::cout << static_cast<std::string>(*(catalog[i]))<<std::endl;
}
}
int main()
{
//freopen("main_output.txt", "w", stdout);
MainOffice& mo = MainOffice::getInstance();
try
{
//this is try-catch block #1
mo.addBranch("Haifa", TEST_MAIN_SIZE);
mo.addBranch("Tel-Aviv", TEST_MAIN_SIZE);
mo.addBranch("Jerusalem", TEST_MAIN_SIZE);
}
catch (std::exception& e)
{
std::cout << "try-catch block #1" << std::endl;
std::cout << e.what() << std::endl;
return 1;
}
Branch* haifaBranch = NULL;
Branch* telAvivBranch = NULL;
Branch* jerusalemBranch = NULL;
try
{
haifaBranch = &mo.getBranch("Haifa");
telAvivBranch = &mo.getBranch("Tel-Aviv");
jerusalemBranch = &mo.getBranch("Jerusalem");
}
catch (std::exception& e)
{
std::cout << "try-catch block #2" << std::endl;
std::cout << e.what() << std::endl;
return 1;
}
if (!haifaBranch || !telAvivBranch || !jerusalemBranch)
{
// This if should not run, only for safety check
std::cout << "Error with branch retrieval, shutting down." << std::endl;
return 1;
}
Computer* computers[TEST_MAIN_SIZE];
Mouse* mouses[TEST_MAIN_SIZE];
Keyboard* keyboards[TEST_MAIN_SIZE];
for (int i = 0; i < TEST_MAIN_SIZE; i++)
{
computers[i] = new Computer(100 * (i)+10, "Manufacturer" + std::to_string(i), ((i % 2) ? "Red" : "Blue"), i % 2, i + 3);
mouses[i] = new Mouse(10 * i + 1, "Manufacturer" + std::to_string(i + 1), ((i % 2) ? "Green" : "Silver"), (i + 1) % 2, 50 * (i + 1));
keyboards[i] = new Keyboard(20 * i + 5, "Manufacturer" + std::to_string(i + 1), ((i % 2) ? "Orange" : "Gold"), i%2,20+i);
}
for (int i = 0; i < TEST_MAIN_SIZE-1; i++)
{
Branch* branchPtr = NULL;
switch (i % 3)
{
case 0:
{
branchPtr = haifaBranch;
break;
}
case 1:
{
branchPtr = telAvivBranch;
break;
}
case 2:
{
branchPtr = jerusalemBranch;
break;
}
}
try
{
branchPtr->addItem(computers[i]);
branchPtr->addItem(mouses[i]);
branchPtr->addItem(keyboards[i]);
}
catch (std::exception& e)
{
std::cout << "try-catch block #3" << std::endl;
std::cout <<e.what() << std::endl;
}
}
print_branch_catalog(*haifaBranch);
std::cout << std::endl;
try
{
haifaBranch->addItem(computers[TEST_MAIN_SIZE-1]);
}
catch (std::exception& e)
{
std::cout << "try-catch block #4" << std::endl;
std::cout << e.what() << std::endl;
}
try
{
haifaBranch->removeItem(computers[TEST_MAIN_SIZE - 1]->getId());
}
catch (std::exception& e)
{
std::cout << "try-catch block #5" << std::endl;
std::cout << e.what() << std::endl;
}
try
{
Item* removedItem = jerusalemBranch->removeItem(7);
delete removedItem;
}
catch (std::exception& e)
{
std::cout << "try-catch block #6" << std::endl;
std::cout << e.what() << std::endl;
}
try
{
Computer* bestComputer = jerusalemBranch->giveMeFinest(computers[0]);
}
catch (std::exception& e)
{
std::cout << "try-catch block #7" << std::endl;
std::cout << e.what() << std::endl;
}
std::cout << std::endl;
try
{
Computer* computer = haifaBranch->giveMeFinest(computers[0]);
std::cout << "Best computer in haifa: " << std::endl;
std::cout << static_cast<std::string>(*computer)<<std::endl;
}
catch (std::exception& e)
{
std::cout << "try-catch block #8" << std::endl;
std::cout << e.what() << std::endl;
}
try
{
keyboards[0]->connect(*(computers[0]));
}
catch (std::exception& e)
{
std::cout << "try-catch block #9" << std::endl;
std::cout << e.what() << std::endl;
}
try
{
mouses[0]->connect(*(computers[0]));
}
catch (std::exception& e)
{
std::cout << "try-catch block #10" << std::endl;
std::cout << e.what() << std::endl;
}
try
{
keyboards[1]->connect(*(computers[0]));
}
catch (std::exception& e)
{
std::cout << "try-catch block #11" << std::endl;
std::cout << e.what() << std::endl;
}
keyboards[0]->disconnect();
try
{
keyboards[1]->connect(*(computers[0]));
}
catch (std::exception& e)
{
std::cout << "try-catch block #12" << std::endl;
std::cout << e.what() << std::endl;
}
std::cout << std::endl;
computers[0]->printConnected();
Keyboard* bestKeyboard = NULL;
Computer* bestComputer = NULL;
try
{
bestKeyboard = telAvivBranch->giveMeFinest(keyboards[1]);
bestComputer = telAvivBranch->giveMeFinest(computers[1]);
if(bestKeyboard && bestComputer)
bestKeyboard->connect(*bestComputer);
}
catch (std::exception& e)
{
std::cout << "try-catch block #13" << std::endl;
std::cout << e.what() << std::endl;
}
if (bestComputer) bestComputer->printConnected();
try
{
if (bestKeyboard)
{
telAvivBranch->removeItem(bestKeyboard->getId());
delete bestKeyboard;
}
}
catch (std::exception& e)
{
std::cout << "try-catch block #14" << std::endl;
std::cout << e.what() << std::endl;
}
if (bestComputer) bestComputer->printConnected();
std::cout << std::endl;
Tablet* tablet = new Tablet(150, "Manufacturer007", "Cream", "CPU", 1, 14);
try
{
jerusalemBranch->addItem(tablet);
}
catch (std::exception& e)
{
std::cout << "try-catch block #15" << std::endl;
std::cout << e.what() << std::endl;
}
std::cout << "New tablet: \n" << static_cast<std::string>(*tablet) << std::endl<<std::endl;
mo.printBranchesByLocation(&print_branch_catalog);
std::cout << std::endl;
mo.printBranchesByValue(&print_branch_catalog);
std::cout << std::endl;
try
{
mo.removeBranch("Jerusalem");
}
catch (std::exception& e)
{
std::cout << "try-catch block #16" << std::endl;
std::cout << e.what() << std::endl;
}
std::cout << std::endl;
mo.printBranchesByValue(&print_branch_catalog);
std::cout << std::endl;
delete computers[TEST_MAIN_SIZE-1];
delete keyboards[TEST_MAIN_SIZE-1];
delete mouses[TEST_MAIN_SIZE-1];
std::cout << "\n#####End of main#####\n" << std::endl;
return 0;
}