-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.cpp
More file actions
321 lines (282 loc) · 8.89 KB
/
library.cpp
File metadata and controls
321 lines (282 loc) · 8.89 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* @file library.cpp
* @authors Dylan Daniels, Anna Hensley
* @date 2024-11-08
* @brief the cpp file for the library project
*
* reads in a list of books and using the STL List object
*/
#include "library.h"
#include<iostream>
#include<fstream>
#include<string>
#include<list>
using namespace std;
/**
* reads from file
*
* @param string fileName valid filename to be opened
* @pre the file is loaded properly
* @return void
* @post List is filled with data provided in the file
*
*/
void Library::read_from_file(string fileName){
string newTitle, newAuthor,newIsbn;
int newPages, newYear;
float newPrice;
ifstream infile(fileName);
if(!infile){
cout << "Error. Unable to open the input file." << endl;
}
getline(infile,newTitle);
getline(infile,newAuthor);
infile >> newPages;
infile.ignore(1, '\n'); // ignores the newline that gets left from the extraction operator
getline(infile,newIsbn);
infile >> newPrice >> newYear;
infile.ignore(1, '\n');
infile.ignore(1, '\n'); // the second ignore is so that the imput file can include
// a space for readablility
while(infile){
push_back(newTitle, newAuthor, newPages, newIsbn, newPrice, newYear);
getline(infile,newTitle);
getline(infile,newAuthor);
infile >> newPages;
infile.ignore(1, '\n');
getline(infile,newIsbn);
infile >> newPrice >> newYear;
infile.ignore(1, '\n');
infile.ignore(1, '\n');
}
infile.close();
}
/**
* writes to file
*
* @param string fileName is a valid file name to be written to
* @pre the list is properly loaded with data
* @return void
* @post the output file is written to with the data in the list
*
*/
void Library::write_to_file(string fileName) {
ofstream outFile(fileName);
if (!outFile) {
cout << "File could not be opened." << endl;
}
list<book>::iterator it;
for (it = bookList.begin(); it != bookList.end(); it++) {
outFile << it->title << endl
<< it->author << endl
<< it->pages << endl
<< it->isbn << endl
<< it->price << endl
<< it->year << endl << endl;
}
outFile.close();
cout << "The Library has been written to " << fileName << " successfully," << endl;
}
/**
* adds a new element to the front of the list
*
* @param string newTitle title of book
* @param string newAuthor name of author we wish to add
* @param int newPages number of pages of the book were adding
* @pre the list is created
* @return void
* @post this new book is added to the front of the list
*
*/
void Library::push_front(string newTitle, string newAuthor, int newPages,
string newIsbn, float newPrice, int newYear){
book temp;
temp.title = newTitle;
temp.author = newAuthor;
temp.pages = newPages;
temp.isbn = newIsbn;
temp.price = newPrice;
temp.year = newYear;
bookList.push_front(temp);
}
/**
* adds a new element to the back of the list
*
* @param string newTitle title of book we wish to add
* @param string newAuthor name of the author we wish to add
* @param int newPages number of pages of the book were adding
* @pre the list is created
* @return void
* @post the new element is added to the end of the list
*
*/
void Library::push_back(string newTitle, string newAuthor, int newPages,
string newIsbn, float newPrice, int newYear){
book temp;
temp.title = newTitle;
temp.author = newAuthor;
temp.pages = newPages;
temp.isbn = newIsbn;
temp.price = newPrice;
temp.year = newYear;
bookList.push_back(temp);
}
/**
* adds a new element in a sorted position inside the list
*
* @param string newTitle title of book we wish to add
* @param string newAuthor name of the author we wish to add
* @param int newPages number of pages of the book were adding
* @pre the list is created
* @return void
* @post the new element is inserted sorted in the list
*
*/
void Library::insert_sorted(string newTitle, string newAuthor, int newPages,
string newIsbn, float newPrice, int newYear){
book temp;
temp.title = newTitle;
temp.author = newAuthor;
temp.pages = newPages;
temp.isbn = newIsbn;
temp.price = newPrice;
temp.year = newYear;
list<book>::iterator it = bookList.begin();
while (it != bookList.end() && newTitle > it->title) {
it++;
}
bookList.insert(it, temp);
}
/**
* searchs through the list to see if there exits any books under the authors name
*
* @param string authorsName is the name of the author we wish to lookup
* @pre assumes that list is loaded properly, preferably with a book by the author we are looking up
* @return void
* @post prints all of the books that by the author
*
*/
void Library::find_author(string authorsName){
list<book>::iterator it = bookList.begin();
bool found = false;
cout << "Looking up books from: " << authorsName << endl << endl;
// loops through the list of books
while(it != bookList.end()) {
if (it->author == authorsName) {
found = true;
cout << "Title: " << it->title << endl;
cout << "Author: " << it->author << endl;
cout << "Pages: " << it->pages << endl;
cout << "ISBN: " << it->isbn << endl;
cout << "Price: $" << it->price << endl;
cout << "Release year: " << it->year << endl << endl;
}
it++;
}
if (!found) {
cout << "There are no books by " << authorsName << " in our library." << endl;
}
}
/**
* prints all of the data associated with the passed book
*
* @param string bookName is the book name were looking up
* @pre assumes the list is loaded, preferably incuding the name of the book we are searching for
* @return void
* @post prints the data associated with that book to the screen
*
*/
void Library::find_album(string bookName){
list<book>::iterator it = bookList.begin();
cout << "Looking info on the book: " << bookName << endl;
while((it->title != bookName) && (it != bookList.end())){
it++;
}
if(it->title == bookName){
cout << endl;
cout << "Title: "<< it->title << endl;
cout << "Author: " << it->author << endl;
cout << "Number of Pages: " << it->pages << endl;
cout << "ISBN: " << it->isbn << endl;
cout << "Shelf Price: $" << it->price << endl;
cout << "Release Year: " << it->year << endl;
cout << endl;
}
else if(it->title != bookName){
cout << bookName << " was not found in the library." << endl;
}
}
/**
* deletes a element in the list
*
* @param string authorsName is the name of the author tied to the book we wish to delete
* @param string bookName is the name of the book we wish to delete
* @pre the list is loaded with data, preferably with the book by the author we are trying to delete
* @return void
* @post the element is deleted if found
*
*/
void Library::genericDelete(string authorsName, string bookName){
list<book>::iterator it = bookList.begin();
cout << "Deleting " << bookName << " from " << authorsName <<" from the library."<< endl;
while((it->author != authorsName) && (it->title !=bookName) && (it != bookList.end())){
it++;
}
if((it->title == bookName) && (it->author == authorsName)){
cout << "Deleted " << it->title << " by " << it->author << endl;
bookList.erase(it);
return;
}
else if((it->title == bookName) && (it->author != authorsName)){
cout << "The book " << it->title << " is not by "
<< authorsName << " it is by " << it->author << endl;
return;
}
else if((it->title != bookName) && (it->author == authorsName)){
cout << "We do not have " << bookName << " from "
<< it->author << " but we have other titles from them." << endl;
return;
}
cout << "We do not have that title." << endl;
}
/**
* prints the library
*
* @pre the library is loaded with data
* @return void
* @post the library is printed to the screen
*
*/
void Library::print(){
bool swapped;
do {
swapped = false;
list<book>::iterator it1 = bookList.begin();
list<book>::iterator it2 = next(it1); // moves to next book in list
// traverse the list and swap as necessary to list alphabetically by name
while (it2 != bookList.end()) {
if (it1->author > it2->author) {
// swap by author's name
book temp = *it1;
*it1 = *it2;
*it2 = temp;
swapped = true;
}
it1++;
it2++;
}
} while (swapped); // repeats even if a swap happens
// prints list
list<book>::iterator it;
cout << endl << "printing list..." << endl << endl;
cout <<"+---------------------------+" << endl;
for(it = bookList.begin(); it != bookList.end(); it++){
cout << "Title: " << it->title << endl;
cout << "Author: " << it->author << endl;
cout << "Pages: " << it->pages << endl;
cout << "ISBN: " << it->isbn << endl;
cout << "Price: $" << it->price << endl;
cout << "Release year: " << it->year << endl << endl;
}
cout <<"+---------------------------+" << endl;
}