-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshelf.cpp
More file actions
154 lines (136 loc) · 4.55 KB
/
Copy pathshelf.cpp
File metadata and controls
154 lines (136 loc) · 4.55 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
//---------------------------------------------------------------------------
// shelf.cpp
//---------------------------------------------------------------------------
// Holds containers of items of different genres.
//
// Assumptions:
// -- each genre is uniquely identified by a letter
// -- there cannot be two genres with the same letter
// Implementation
// -- containers[]
// -- uses a hash table to store containers for differdnt genres
// -- each genre is associated with a letter e.g: F- fiction
// -- array size is 26, letters a...z correlate directly to index 0...25
// -- uses a hash table of binary search trees to store the books
// -- first letter of genre is stored as char
// -- no destructor - no dynamically allocated memory
//---------------------------------------------------------------------------
#include "shelf.h"
#include <iostream>
#include <string> //for getline()
using namespace std;
//---------------------------------------------------------------------------
// constructor
Shelf::Shelf() {
// initialize known bookCodes_
// 2 = childrenbook 5 = fictionbook 15 = periodicalbook
for (int i = 0; i < ITEM_TYPES; i++) {
if (i == 2 || i == 5 || i == 15) {
containers[i] = new ItemContainer();
validCodes[i] = true;
} else {
containers[i] = nullptr;
validCodes[i] = false;
}
}
containers[2]->setGenre('C');
containers[5]->setGenre('F');
containers[15]->setGenre('P');
}
//---------------------------------------------------------------------------
// destructor
Shelf::~Shelf() {
for (int i = 0; i < ITEM_TYPES; i++) {
delete containers[i];
containers[i] = nullptr;
}
}
//---------------------------------------------------------------------------
// insert
bool Shelf::insert(Item *toInsert) {
bool success = false;
// check for type
// check if valid and call insert on correlated BookContainer
int subscript = hash(toInsert->getType());
if (containers[subscript] != nullptr) {
success = containers[subscript]->insert(toInsert);
}
return success;
}
//---------------------------------------------------------------------------
// buildLibrary
bool Shelf::buildLibrary(istream &in) {
bool success = false;
ItemFactory itemF;
char code;
for (;;) {
// read bookCode (genre type)
in >> code;
if (in.eof()) {
break;
}
// test for end of file
in.get(); // get and ignore next blank space
// newBook stores newly created book object
Item *newItem = nullptr;
// check for type validity
if (validCodes[hash(code)]) {
newItem = itemF.createItem(code);
// book class responsible for filling in rest of book information
if (newItem->buildItem(in)) {
// if book info was set insert in containers
success = insert(newItem);
if (!success) {
cout << "ERROR: Item ";
newItem->errorDisplay();
cout << " already exists in library" << endl;
delete newItem;
}
} else {
delete newItem;
}
}
// skip over the rest of the line
else {
cout << "ERROR: Book code: \"" << code << "\" is an invalid code"
<< endl;
string garbage;
getline(in, garbage);
}
}
return success;
}
//---------------------------------------------------------------------------
// checkOut
bool Shelf::retrieve(Item &target, Item *&toReturn) {
// check for type
// send to hash
// call checkout on bookcontainer type
bool success = false;
int subscript = hash(target.getType());
if (containers[subscript] != nullptr) {
Item *retrieved;
if (containers[subscript]->retrieve(target, retrieved)) {
toReturn = retrieved;
success = true;
}
}
return success;
}
//---------------------------------------------------------------------------
// display
void Shelf::display() const {
// loop through container and display in order each BookContainer stored
for (int i = 0; i < ITEM_TYPES; i++) {
if (containers[i] != nullptr) {
containers[i]->display();
}
}
}
//---------------------------------------------------------------------------
// hash
int Shelf::hash(char code) const {
code = toupper(code);
int subscript = code - 'A';
return subscript;
}