-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
126 lines (115 loc) · 3.39 KB
/
Copy pathbook.cpp
File metadata and controls
126 lines (115 loc) · 3.39 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
/**
* @file book.h
* @author Josh Helzerman
*
* Description:
* - A book object represents a unique book and the number of copies
* available for that book at any given time
* - Can be queried to display its information
* - Can be queried to see if there are any available copies
* - Can be compared with other books
* - Number of available books can be changed
*
*
* Assumptions/Implementation:
* - This is an abstract class
* - Some functions are virtual and some are pure virtual
* - One book object can represent multiple copies of the same book using
* the count member variable
* - Count can be decreased or increased
*/
#include "book.h"
//-----------------------------------------------------------------------
/** recieveBook()
* Add Copy of Book
*
* Adds 1 to the count of the book if the patron is eligible to return a
* copy
* @param patron is the patron returning the book
* @pre Patron isn't a nullptr
* @post count is incremented if the patron doesn't currently has a copy
* @return if the book was successfully returned
*/
bool Book::receiveBook(Patron* patron)
{
if (patron == nullptr)
return false;
if (count == maxCount)
return false;
// If the patron has the book checked out, check it back in
// If the patron does not, they can't return the book
for (int i = 0; i < maxCount + 1; i++) {
if (i == maxCount)
return false;
if (checkouts[i] == patron) {
checkouts[i] = nullptr;
break;
}
}
count++;
return true;
}
//-----------------------------------------------------------------------
/** giveBook()
* Subtract Copy of Book
*
* Subtracts 1 from count of the book if it is available for the patron
* to check out
* @param patron is the patron checking out the book
* @pre must have a copy of the book available. count > 0
* @post count is decremented if the patron can check out a copy
* @return true if book was available, false otherwise
*/
bool Book::giveBook(Patron* patron)
{
if (patron == nullptr)
return false;
if (!checkAvailability(patron))
return false;
for (int i = 0; i < maxCount; i++) {
if (checkouts[i] == nullptr) {
checkouts[i] = patron;
break;
}
}
count--;
return true;
}
//-----------------------------------------------------------------------
/** checkAvailability()
* Check if Book is Avaiable
*
* Checks if a copy of the book is available for patron to checkout
* @param patron is the patron checking out the book
* @pre patron isn't a nullptr
* @post None. Const Function
* @return If the patron can check out the book
*/
bool Book::checkAvailability(Patron* patron) const
{
// If there are no copies left, it is unavailable
if (count == 0)
return false;
if (patron == nullptr)
return false;
// If the patron is already checking out a copy, it's unavailable
for (int i = 0; i < maxCount; i++) {
if (patron == checkouts[i])
return false;
}
return true;
}
//-----------------------------------------------------------------------
/** getTitle()
* Get Ttile
*
* Returns the title of the book
* @pre Book type uses the title
* @post None. const function
* @return the title, or some other signifier if the book type
* doesn't use titles
*/
string Book::getTitle() const
{
return title;
}