-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfictionbook.cpp
More file actions
202 lines (171 loc) · 6.57 KB
/
Copy pathfictionbook.cpp
File metadata and controls
202 lines (171 loc) · 6.57 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//---------------------------------------------------------------------------
// fictionbook.cpp
//---------------------------------------------------------------------------
// A fiction book object. Handles the creation and storage of a fiction book,
// containing title, author, number of copies "owned" by the library, and
// copies of this book currently checked in.
//
// Ficiton sorted by author, then title
//
// Implementation:
// -- initial character in input dictates 'f' for fiction
//---------------------------------------------------------------------------
#include "fictionbook.h"
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
//---------------------------------------------------------------------------
// default constructor
FictionBook::FictionBook() {
author_ = "";
year_ = 0;
title_ = "";
itemType_ = 'F';
availableCopies_ = COPIES;
totalCopies_ = COPIES;
}
//---------------------------------------------------------------------------
// destructor
FictionBook::~FictionBook() {}
//---------------------------------------------------------------------------
// buildBook
// file format: type author, title, year : istream passed in beginning at author
bool FictionBook::buildItem(istream &in) {
string author;
string title;
int year;
getline(in, author, ',');
in.get(); // remove blank space
getline(in, title, ',');
in.get(); // remove blank space
in >> year;
setAuthor(author);
setTitle(title);
setYear(year);
return (getAuthor() == author && getTitle() == title && getYear() == year);
}
//---------------------------------------------------------------------------
// create
Item *FictionBook::create() { return new FictionBook(); }
//---------------------------------------------------------------------------
// fill
// data format: author, title
void FictionBook::fill(istream &inFile) {
getline(inFile, author_, ',');
inFile.get(); // remove blank space
getline(inFile, title_, ',');
}
//---------------------------------------------------------------------------
// dsiplayBookHeader
void FictionBook::displayBookHeader() const {
cout << endl << "FICTION BOOKS " << endl << setw(39) << "AVAIL AUTHOR";
cout << "TITLE YEAR" << endl;
}
//---------------------------------------------------------------------------
// display
void FictionBook::display() const {
cout << setw(7) << left << availableCopies_ << setw(32) << left << author_
<< setw(36) << left << title_.substr(0, 28) << setw(5) << left << year_
<< endl;
}
//---------------------------------------------------------------------------
// errorDisplay
void FictionBook::errorDisplay() const {
cout << " \"" << setw(10) << left << title_.substr(0, 29) << "\" by "
<< setw(12) << left << author_;
}
//---------------------------------------------------------------------------
// historyDisplay
void FictionBook::historyDisplay() const {
cout << setw(32) << left << author_ << setw(25) << left
<< title_.substr(0, 22) << setw(5) << left << year_ << endl;
}
//---------------------------------------------------------------------------
// getCopiesAvailable
int FictionBook::getCopiesAvailable() const { return availableCopies_; }
//---------------------------------------------------------------------------
// setAuthor
void FictionBook::setAuthor(string author) { author_ = author; }
//---------------------------------------------------------------------------
// setYear
bool FictionBook::setYear(int year) {
bool success = false;
if (year > 0) {
year_ = year;
success = true;
}
return success;
}
//---------------------------------------------------------------------------
// getAuthor
string FictionBook::getAuthor() const { return author_; }
//---------------------------------------------------------------------------
// getYear
int FictionBook::getYear() const { return year_; }
//---------------------------------------------------------------------------
// changeAvailable
bool FictionBook::changeAvailable(int num) {
bool success = false;
if (num == 1 && availableCopies_ < totalCopies_) {
availableCopies_ += num;
success = true;
} else if (num == -1 && availableCopies_ > 0) {
availableCopies_ += num;
success = true;
}
return success;
}
//---------------------------------------------------------------------------
// overloaded comparison operators
// Fiction sorted by author, then title
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// overloaded operator<
bool FictionBook::operator<(const Item &rhsItem) const {
bool holder = false;
const FictionBook &rhsBookCasted =
static_cast<const FictionBook &>(rhsItem);
(getAuthor() < rhsBookCasted.getAuthor()) ? holder = true : holder = false;
// if the book authors are the same, compare titles
if (getAuthor() == rhsBookCasted.getAuthor()) {
(getTitle() < rhsBookCasted.getTitle()) ? holder = true
: holder = false;
}
return holder;
}
//---------------------------------------------------------------------------
// overloaded operator>
bool FictionBook::operator>(const Item &rhsItem) const {
bool holder = false;
// cast book to fiction book
const FictionBook &rhsBookCasted =
static_cast<const FictionBook &>(rhsItem);
// compare titles, adjust bool as needed
(getAuthor() > rhsBookCasted.getAuthor()) ? holder = true : holder = false;
// if the book titles are the same compare authors
if (getAuthor() == rhsBookCasted.getAuthor()) {
(getTitle() > rhsBookCasted.getTitle()) ? holder = true
: holder = false;
}
return holder;
}
//---------------------------------------------------------------------------
// overloaded operator ==
bool FictionBook::operator==(const Item &rhsItem) const {
bool holder = false;
// cast rhs book to fiction book
const FictionBook &rhsBookCasted =
static_cast<const FictionBook &>(rhsItem);
// compare author, title, and year between two books
if (rhsBookCasted.getAuthor() == getAuthor() &&
rhsBookCasted.getTitle() == getTitle()) {
holder = true;
}
return holder;
}
//---------------------------------------------------------------------------
// overloaded operator !=
bool FictionBook::operator!=(const Item &rhsItem) const {
return !(*this == rhsItem);
}