-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturnBook.cpp
More file actions
109 lines (99 loc) · 3.12 KB
/
Copy pathreturnBook.cpp
File metadata and controls
109 lines (99 loc) · 3.12 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
/**
* @file returnBook.cpp
* @author Alex Lambert
*
* Description:
* - Returns a copy of the book from the patron to the library
*
* Assumptions/Implementation:
* - Inherits from Command interface
* - Assumes the book string was stored in the alternative book format
* (Books from the book data file are stored differently than in the command
* data file)
*/
#include "returnBook.h"
using namespace std;
//---------------------------------------------------------------------------
/** ReturnBook()
* Default Constructor
*
* Constructs a base instance of the ReturnBook command. This
* instance must not be executed.
* @param bookDB is the BookDatabase to be searched for the book
* @param patronDB is PatronDatabase to be searched for the patron
* @pre bookDB and patronDB should be for the same library
* @post ReturnBook command object exists for bookDB/patronDB
*/
ReturnBook::ReturnBook(BookDatabase* bookDB, PatronDatabase* patronDB)
{
this->bookDB = bookDB;
this->patronDB = patronDB;
}
//---------------------------------------------------------------------------
/** ~ReturnBook()
* Default Destructor
*
* Destroys this command
* @pre None
* @post Deallocates memory used by this command.
*/
ReturnBook::~ReturnBook()
{
bookDB = nullptr;
patronDB = nullptr;
}
//---------------------------------------------------------------------------
/** execute()
* Execute ReturnBook Command
*
* Checks to see if the book and patron exist, and if the patron can
* return the book. If so, the book has one copy returned to it and
* the patron loses that copy.
* @pre None
* @post Book and Patron are updated accordingly, if everything was valid
*/
void ReturnBook::execute()
{
Patron* patron = patronDB->getPatron(patronID);
if (!patron) {
cout << "ERROR: Couldn't find a patron with ID "
<< patronID << "." << endl;
return;
}
Book* book = bookDB->getBook(bookID, true);
if (!book) {
cout << patron->getName() << " to return." << endl;
return;
}
if (book->receiveBook(patron))
patron->returnBook(book);
else
cout << "ERROR: " << patron->getName() << " cannot return \'"
<< book->getTitle() << "\'." << endl;
}
//---------------------------------------------------------------------------
/** create()
* Create ReturnBook Command (factory)
*
* Creates a ReturnBook command that can be executed
* @param parameters is a reference to the ifstream, starting immediately
* after the command code
* @pre None
* @post No changes, just returns the new command. Parameters will read up
* until and including the end-of-line character
* @return returns the new executable command, or nullptr if the
* parameters were invalid
*/
LibraryCommand* ReturnBook::create(stringstream& parameters) const
{
ReturnBook* toReturn = new ReturnBook(bookDB, patronDB);
parameters >> toReturn->patronID;
parameters.get();
getline(parameters, toReturn->bookID);
if (toReturn->patronID.compare("9999") == 0) {
delete toReturn;
toReturn = nullptr;
return nullptr;
}
return toReturn;
}