-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookFactory.cpp
33 lines (31 loc) · 1.13 KB
/
bookFactory.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
#include "bookFactory.h"
//----------------------------------------------------------------------------
//constructor
//sets the F Y and P locations to the respective book types
BookFactory::BookFactory() {
for (int i = 0; i < hashTableSize; i++) {
bookFactory[i] = nullptr;
}
bookFactory[hash('F')] = new Fiction;
bookFactory[hash('Y')] = new Youth;
bookFactory[hash('P')] = new Periodical;
}
//----------------------------------------------------------------------------
//destructor
BookFactory::~BookFactory() {
for (int i = 0; i < hashTableSize; i++) {
if (bookFactory[i] != nullptr) {
delete bookFactory[i];
bookFactory[i] = nullptr;
}
}
}
//----------------------------------------------------------------------------
//createIt
//returns null if book is not in the hashtable, otherwise returns
//a new object of that book type
Book* BookFactory::createIt(char ch) const {
int subscript = hash(ch);
if (bookFactory[subscript] == nullptr) return nullptr; //check for null
return bookFactory[subscript]->create(); //return new book
}