-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturn.cpp
More file actions
141 lines (123 loc) · 3.92 KB
/
Copy pathreturn.cpp
File metadata and controls
141 lines (123 loc) · 3.92 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
//---------------------------------------------------------------------------
// return.cpp
//---------------------------------------------------------------------------
// Handles the operation of returning a book into the library system. Both by
// unassigning the book from the patron who checked it out, and modifying the
// values for currently checked in copies of the book.
//
// Assumptions:
// -- will only be called on a valid item in the library
// -- patronPtr will point to a valid patron
//
// Implementation:
// -- no destructor implementation for dynamic memory in create() method -
// -- memory deallocation to be handled by Patron class where commands are
// -- are stored in itemHistory list
//
//---------------------------------------------------------------------------
#include "return.h"
#include "Library.h"
//---------------------------------------------------------------------------
// constructor
Return::Return() { commType_ = 'R'; }
//---------------------------------------------------------------------------
// destructor
Return::~Return() {}
//---------------------------------------------------------------------------
// buildCommand
// data format: ID ItemType ItemFormat Item specific info
bool Return::buildCommand(istream &inStream, Library *&library) {
// read patron ID
int ID;
inStream.get(); // clear empty space
inStream >> ID;
inStream.get(); // clear empty space
// first verify the patron
Patron *patron = nullptr;
if (!library->getPatron(ID, patron)) {
cout << endl
<< "ERROR: User ID \"" << ID << "\" is an invalid patron ID"
<< endl;
return false;
} else {
patron_ = patron;
}
// set data member
patron_ = patron;
Item *target;
ItemFactory iFactory;
char itemType;
// read itemType
inStream >> itemType;
inStream.get(); // clear empty space
// check item type
target = iFactory.createItem(itemType);
if (target == nullptr) {
cout << endl
<< "ERROR: Item Type \"" << itemType
<< "\" is an invalid Item Type" << endl;
delete target;
target = nullptr;
return false;
}
// read item format
char format;
inStream >> format;
inStream.get(); // clear empty space
// check format
if (!target->setFormat(format)) {
cout << endl
<< "ERROR: Item Format: \"" << format << "\" is an invalid format"
<< endl;
delete target;
target = nullptr;
return false;
}
// set item specific information
target->fill(inStream);
// search library for the target item
Item *found;
if (library->getItem(*target, found)) {
item_ = found;
delete target;
target = nullptr;
} else {
// display error if item not found
target->errorDisplay();
cout << endl;
delete target;
target = nullptr;
return false;
}
return true;
}
//---------------------------------------------------------------------------
// execture
bool Return::execute() {
bool success = false;
// search patron for previous checkout
if (!patron_->searchCheckouts(item_)) {
cout << endl
<< "ERROR: Patron " << patron_->getID()
<< " is attempting to return a book they do not have checked "
"out."
<< endl;
} else {
// increase available copies by 1
if (item_->changeAvailable(1)) {
// add to patron history
patron_->addToHistory(this);
success = true;
}
}
return success;
}
//---------------------------------------------------------------------------
// display
void Return::display() {
cout << " Return ";
item_->historyDisplay();
}
//---------------------------------------------------------------------------
// return
Command *Return::create() { return new Return(); }