-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckout.cpp
More file actions
146 lines (126 loc) · 3.84 KB
/
Copy pathcheckout.cpp
File metadata and controls
146 lines (126 loc) · 3.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
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
// Subclass of command.
// Handles checking out a book, will attempt to decrement avaialble copies
// of the book by one.
//
// Assumptions:
// -- will only be called on a valid item in the library
//
// Implementation:
// -- Execute calls changeAvailable method on Item
// -- Item to do error handling with available copies
// -- 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 "checkout.h"
#include "Library.h"
#include <iostream>
//---------------------------------------------------------------------------
// constructor
Checkout::Checkout() {
commType_ = 'C';
item_ = nullptr;
patron_ = nullptr;
libraryPtr_ = nullptr;
}
//---------------------------------------------------------------------------
// destructor
Checkout::~Checkout() {}
//---------------------------------------------------------------------------
// buildCommand
// data format: PatronID itemType itemFormat Iteminfo
bool Checkout::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;
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 = nullptr;
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;
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;
}
// display error message and deallocate memory if not found
else {
target->errorDisplay();
cout << endl;
delete target;
target = nullptr;
return false;
}
return true;
}
//---------------------------------------------------------------------------
// display
void Checkout::display() {
cout << " CheckOut ";
item_->historyDisplay();
}
//---------------------------------------------------------------------------
// execute
// item_ and patron_ data fields are set by command.buildCommand
bool Checkout::execute() {
bool success = false;
// decrement available copies
if (item_->changeAvailable(-1)) {
// add this checkout to patron history
patron_->addToHistory(this);
success = true;
}
// if false then no copies available
else {
cout << endl
<< "ERROR: Patron " << patron_->getID()
<< " checkout failed due to no copies available of";
item_->errorDisplay();
cout << endl;
}
return success;
}
//---------------------------------------------------------------------------
// create
Command *Checkout::create() { return new Checkout(); }