-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookList.cpp
241 lines (199 loc) · 6.5 KB
/
BookList.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <algorithm> // find(), move(), move_backward(), equal(), swap(), lexicographical_compare()
#include <cstddef> // size_t
#include <iomanip> // setw()
#include <string>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include "BookList.hpp"
namespace // unnamed, autonomous namespace
{
// Avoid direct equality comparisons on floating point numbers. Two values are equal if they are "close enough", which is
// represented by Epsilon. Usually, this is a pretty small number, but since we are dealing with money (only two, maybe three
// decimal places) we can be a bit more tolerant. Two floating point values are considered equal if they are withing EPSILON of
// each other.
constexpr auto EPSILON = 1.0E-4;
}
/**********************************************
** Private implementations, types, and objects
** for class Book
************************************************/
/************************
** Constructor
************************/
Book::Book( const std::string& title,
const std::string& author,
const std::string& isbn,
const double & price )
// Use the constructor initialization list
: _isbn (isbn), _title (title), _author( author ),
_price ( price )
{} // Avoid setting values in constructor's body (when possible)
/*************************
** Queries or getters
**************************/
std::string Book::get_isbn() const
{ return _isbn; }
std::string Book::get_title() const
{ return _title; }
std::string Book::get_author() const
{ return _author; }
double Book::get_price() const
{ return _price; }
/***********************************
** Mutators or setters
************************************/
void Book::set_isbn( const std::string& newIsbn )
{ _isbn = newIsbn; }
void Book::set_title( const std::string& newTitle )
{ _title = newTitle; }
void Book::set_author( const std::string& newAuthor )
{ _author = newAuthor; }
void Book::set_price( double newPrice )
{ _price = newPrice; }
/***********************************
** Friend functions: read and write
************************************/
std::istream & operator>>( std::istream & stream, Book & book )
{
Book workingItem;
char delimiter = ',';
// Assume fields are separated by commas and string attributes are
// enclosed with double quotes. For example:
// "9789998287532", "Over in the Meadow", "Ezra Jack Keats", 91.11
stream >> std::quoted( workingItem._isbn ) >> delimiter
>> std::quoted( workingItem._title ) >> delimiter
>> std::quoted( workingItem._author ) >> delimiter
>> workingItem._price;
// If all okay, move workingItem into the returned book. Copying the
// information also "works" but moving is more efficient. Note this
// uses Book's move assignment operator.
if(stream) book = std::move( workingItem );
return stream;
}
std::ostream & operator<<( std::ostream & stream, const Book & book )
{
const std::string delimiter = ", ";
stream << std::quoted( book.get_isbn() ) << delimiter
<< std::quoted( book.get_title() ) << delimiter
<< std::quoted( book.get_author() ) << delimiter
<< book.get_price();
return stream;
}
/*************************************************
** Ordinary functions: Relational Operators
**************************************************/
bool operator==( const Book & lhs, const Book & rhs )
{
// Implement equality in terms of less than to enforce identical
// column priority ordering and floating point epsilon comparison tolerance.
return lhs._isbn == rhs._isbn
&& lhs._title == rhs._title
&& lhs._author == rhs._author
&& std::abs( lhs._price - rhs._price ) < EPSILON;
}
bool operator!=( const Book & lhs, const Book & rhs )
{ return !( lhs == rhs ); }
/**********************************************
** Private implementations, types, and objects
** for class BookList
************************************************/
/*************************************
** Friend functions: read and write
**************************************/
std::ostream & operator<<( std::ostream & stream, const BookList & bookList )
{
for (unsigned i=0; i< bookList._books_array_size; i++) {
stream << '\n' << std::setw(5) << i << ": " << bookList[i];
}
return stream;
}
std::istream & operator>>( std::istream & stream, BookList & bookList )
// read at most <_capacity> books
{
Book book;
unsigned counter = 0;
while(stream && counter < bookList._capacity) {
stream >> book;
bookList._bookArray[counter++] = std::move(book);
}
if (counter < bookList._capacity)
// read less books than <_capacity>
// so set it to actual number of books read
bookList._books_array_size = counter;
return stream;
}
/***********************
** Constructor
************************/
// TO DO
BookList::BookList(const std::size_t & newSize)
{
_books_array_size = 0;
_bookArray = new Book[newSize];
_capacity = newSize;
}
/************************
** Assignment operator
************************/
// TO DO
BookList & BookList::operator+=( const BookList & rhs)
{
// Concatenate the righthand side book list of books to this list
// by repeatedly adding each book at the end of the current book list
// as long as it does not exceed <_capacity>
// If exceeds, then stop adding
for (int i = 0; i < rhs._books_array_size; i++)
{
_bookArray[_books_array_size] = rhs._bookArray[i];
_books_array_size ++;
if (_books_array_size >= _capacity)
break;
}
return *this;
}
/*********************
** Destructor
*********************/
// TO DO
BookList::~BookList()
{
delete[] _bookArray;
}
/***********************
** Queries or getters
************************/
// TO DO
std::size_t BookList::size() const
{
// return the size of the dynamic array
return _books_array_size;
}
//TO DO
std::size_t BookList::find( const Book & book ) const
// Locate the book in this book list and return the zero-based position
// of that book. If the book does not exist, return the size of this
// book list as an indicator the book does not exist.
{
for (int i = 0; i < _books_array_size; i++)
{
if (_bookArray[i] == book)
return i;
}
return _capacity;
}
Book BookList::operator[](std::size_t index) const {
return _bookArray[index];
}
/*************************
** Read from the file
**************************/
void BookList::readInFile(const std::string& filename) {
std::ifstream inFile(filename);
Book newBook;
unsigned i=0;
// Read each line
while ((inFile >> newBook) && (i < _capacity)) {
_bookArray[i++] = newBook;
}
_books_array_size = i;