-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitemfactory.cpp
More file actions
62 lines (53 loc) · 1.84 KB
/
Copy pathitemfactory.cpp
File metadata and controls
62 lines (53 loc) · 1.84 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
//---------------------------------------------------------------------------
// itemfactory.cpp
//---------------------------------------------------------------------------
// A Item factory class. Handles the creation of Item objects based on the
// Item code being taken from input. Works as an intermidiate class to process
// any type of Item creation in one class. Able to create periodicals, fiction,
// and children's books based on incoming Item code.
//
// Assumptions:
// -- valid character corresponding to book type from input
// -- book type character will correspond to present Item type
//---------------------------------------------------------------------------
#include "itemfactory.h"
#include "childrenbook.h"
#include "fictionbook.h"
#include "periodicalbook.h"
#include <ctype.h>
//---------------------------------------------------------------------------
// constructor
ItemFactory::ItemFactory() {
for (int i = 0; i < TYPES; i++) {
types[i] = nullptr;
}
types[2] = new ChildrenBook;
types[5] = new FictionBook;
types[15] = new PeriodicalBook;
}
//---------------------------------------------------------------------------
// destructor
ItemFactory::~ItemFactory() {
for (int i = 0; i < TYPES; i++) {
delete types[i];
types[i] = nullptr;
}
}
//---------------------------------------------------------------------------
// createBook
Item *ItemFactory::createItem(char type) {
Item *toReturn = nullptr;
int subscript = hash(type);
if (types[subscript] != nullptr) {
toReturn = types[subscript]->create();
}
return toReturn;
}
//---------------------------------------------------------------------------
// hash
int ItemFactory::hash(char type) {
// change to uppercase if it's not
type = toupper(type);
int subscript = type - 'A';
return subscript;
}