-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB2.cpp
105 lines (95 loc) · 3.28 KB
/
LAB2.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
//============================================================================
// Name : LAB2.cpp
// Description : Lab2 Solution. The shorter version. Code Re Use through Inheritance.
//============================================================================
#include "LAB2.h"
#include <string>
using namespace std;
// CollectionIterator Class Definitions
CollectionIterator::CollectionIterator(const AbstractAggregate *collection) : _collection(collection){
_current = 0;
}
void CollectionIterator::First() {
_current = 0;
while ((_collection->get(_current)->getType() != getType()) &&
((_current < _collection->getCount()))) {
_current++;
}
}
void CollectionIterator::Next() {
_current++;
while ((_current < _collection->getCount()) &&
(_collection->get(_current)->getType() != getType())) {
_current++;
}
}
Item *CollectionIterator::CurrentItem() const { return (IsDone() ? NULL : _collection->get(_current));}
bool CollectionIterator::IsDone() const { return _current >= _collection->getCount();}
// BookIterator Class Definitions
BookIterator::BookIterator(const AbstractAggregate *collection) : CollectionIterator (collection) {};
int BookIterator::getType() {return 1;}
// CDIterator Class Definitions
CDIterator::CDIterator(const AbstractAggregate *collection) : CollectionIterator (collection) {};
int CDIterator::getType() {return 2;}
ToyIterator::ToyIterator(const AbstractAggregate *collection) : CollectionIterator (collection) {};
int ToyIterator::getType() {return 3;}
// Collection Class Definitions
AbstractIterator* Collection::CreateIterator(int type) {
if (type == 1 )
return new BookIterator(this);
else if (type == 2 )
return new CDIterator(this);
else if (type == 3 )
return new ToyIterator(this);
else
return NULL;
}
int Collection::getCount() const { return _items.size(); }
void Collection::add(Item *item) { _items.push_back(item); };
Item * Collection::get(int index) const { return _items[index]; };
void printAggregate(AbstractIterator& i) {
cout << "Iterating over collection:" << endl;
for (i.First(); !i.IsDone(); i.Next()) {
cout << (i.CurrentItem()->getName()) << endl;
}
cout << endl;
}
int main()
{
// Create Aggregate.
AbstractAggregate *aggregate = new Collection();
aggregate->add(new Toy("Item 0-Toy"));
aggregate->add(new Toy("Item 1-Toy"));
aggregate->add(new Toy("Item 2-Toy"));
aggregate->add(new Book("Item 3-Book"));
aggregate->add(new Toy("Item 4-Toy"));
aggregate->add(new Book("Item 5-Book"));
aggregate->add(new Book("Item 6-Book"));
aggregate->add(new CD("Item 7-CD"));
aggregate->add(new Book("Item 8-Book"));
aggregate->add(new CD("Item 9-CD"));
aggregate->add(new CD("Item 10-CD"));
aggregate->add(new Book("Item 11-Book"));
//Book - 1
//CD - 2
//Toy - 3
// Create Iterator
cout << "Finding books......" << '\n';
AbstractIterator *iterator = aggregate->CreateIterator(1);
if (iterator) {
printAggregate(*iterator);
delete iterator;
}
cout << "Finding CDs......" << '\n';
iterator = aggregate->CreateIterator(2);
if (iterator) {
printAggregate(*iterator);
delete iterator;
}
cout << "Finding Toys......" << '\n';
iterator = aggregate->CreateIterator(3);
if (iterator) {
printAggregate(*iterator);
delete iterator;
}
}