-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatroncontainer.cpp
More file actions
90 lines (81 loc) · 2.73 KB
/
Copy pathpatroncontainer.cpp
File metadata and controls
90 lines (81 loc) · 2.73 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
// PatonContainer class stores all patrons of the library and acts as an
// interface between the patron and the library class. The PatronContainer
// allows quick lookup for patrons.
//
// Assumtpions:
// -- There can only be 10,000 patrons
// -- userID will be unique identifier to lookup patrons
// Implementation:
// -- Patrons are stored in an array
//---------------------------------------------------------------------------
#include "patroncontainer.h"
#include <iostream>
//---------------------------------------------------------------------------
// constructor
PatronContainer::PatronContainer() {}
//---------------------------------------------------------------------------
// destructor
PatronContainer::~PatronContainer() {
for (int i = 0; i < PATRON_LIMIT; i++) {
if (patronList[i] != nullptr) {
delete patronList[i];
patronList[i] = nullptr;
}
}
}
//---------------------------------------------------------------------------
// insert
// data format: userID First Last
bool PatronContainer::insert(istream &inFile) {
bool success = false;
for (;;) {
int userID;
inFile >> userID;
if (inFile.eof()) {
success = true;
break;
}
inFile.get(); // clear empty space
// check for valid ID #
if (userID > 0 && userID < 10000) {
// create a new patron
Patron *patron = new Patron();
success = patron->buildPatron(inFile, userID);
// check if ID is already in use
if (patronList[userID] != nullptr) {
delete patron;
patron = nullptr;
cout << "User ID: " << userID << " is already in use" << endl;
} else {
// otherwise place it in the hash
patronList[userID] = patron;
}
} else {
cout << "User ID: " << userID << " is an invalid User ID " << endl;
}
}
return success;
}
//---------------------------------------------------------------------------
// retrieve
bool PatronContainer::retrieve(const int userID, Patron *&toReturn) const {
bool success = false;
if (userID > 0 && userID < 10000) {
if (patronList[userID] != nullptr) {
toReturn = patronList[userID];
success = true;
}
}
return success;
}
//---------------------------------------------------------------------------
// display
void PatronContainer::display() const {
for (int i = 0; i < PATRON_LIMIT; i++) {
if (patronList[i] != nullptr) {
string name = patronList[i]->getName();
int id = patronList[i]->getID();
cout << id << " " << name << endl;
}
}
}