-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatron.cpp
More file actions
130 lines (111 loc) · 3.68 KB
/
Copy pathpatron.cpp
File metadata and controls
130 lines (111 loc) · 3.68 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
//---------------------------------------------------------------------------
// patron.cpp
//---------------------------------------------------------------------------
// A Patron class to store information on Patrons of the library
// Patron name, ID, book rental history and currently checked out books
// are all stored.
// Patron Objects are initiated in the Patron Factory.
//
// Assumptions:
// -- rentalHistory has no limit
// -- currentCheckOuts has no limit
// -- userID will be unique identifier
// Implementation:
// -- rentalHistory Book objects are stored in a STL list
// -- currentCheckOuts Book objects are stored in a STL list
// -- Once a book is added to currentCheckOuts it is added to rentalHistory
//---------------------------------------------------------------------------
#include "patron.h"
#include <sstream>
#include <vector>
//---------------------------------------------------------------------------
// Constructor
Patron::Patron() {
// initialize member fields
nameLastFirst_ = "";
userID_ = 0;
}
//---------------------------------------------------------------------------
// destructor
Patron::~Patron() {
for (list<Command *>::iterator it2 = itemHistory.begin();
it2 != itemHistory.end(); it2++) {
delete (*it2);
}
}
//---------------------------------------------------------------------------
// buildPatron
bool Patron::buildPatron(istream &inFile, int userID) {
string nameLastFirst = "";
userID_ = userID;
// take name from inFile
getline(inFile, nameLastFirst);
nameLastFirst_ = nameLastFirst;
return true;
}
//---------------------------------------------------------------------------
// getFirst
string Patron::getName() const { return nameLastFirst_; }
//---------------------------------------------------------------------------
// getID
int Patron::getID() const { return userID_; }
//---------------------------------------------------------------------------
// printHistory
void Patron::printHistory() {
// if the list is not empty
if (!itemHistory.empty()) {
// long iterator loop
for (list<Command *>::iterator it2 = itemHistory.begin();
it2 != itemHistory.end(); it2++) {
(*it2)->display();
}
}
}
//---------------------------------------------------------------------------
// addToHistory
bool Patron::addToHistory(Command *comm) {
// push history object to back of list (newest)
itemHistory.push_back(comm);
return true;
}
//---------------------------------------------------------------------------
// displayPatron()
void Patron::displayPatron() {
char splitChar = ' ';
string firstName;
string lastName;
vector<string> result;
stringstream ss(nameLastFirst_);
string item;
while (getline(ss, item, splitChar)) {
result.push_back(item);
}
cout << endl
<< getID() << " " << result.at(0) << ", " << result.at(1) << ":"
<< endl;
}
//---------------------------------------------------------------------------
// searchCheckouts
bool Patron::searchCheckouts(const Item *target) {
int checkCount = 0;
int returnCount = 0;
// for each command object stored in patron
for (list<Command *>::iterator it = itemHistory.begin();
it != itemHistory.end(); it++) {
if (*target == *(*it)->getItem()) {
if ((*it)->getCommandType() == 'C') {
checkCount++;
}
if ((*it)->getCommandType() == 'R') {
returnCount++;
}
} else {
continue;
}
if (checkCount - returnCount > 0) {
return true;
}
return false;
}
return false;
}