-
Notifications
You must be signed in to change notification settings - Fork 0
/
BookList.cpp
86 lines (74 loc) · 2.18 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
#include "BookList.h"
int BookList::save(const std::string &fname) {
std::ofstream ofile(fname);
if(ofile.bad()) {
return -1;
}
ListElem *elem = get_head();
if(elem == nullptr) {
return -1;
}
do {
Book *book = (Book *) elem->_payload;
ofile << book->get_id() << "|" << book->get_author() << "|" << book->get_title() << "|" << book->get_avail() << std::endl;
elem = get_next(elem);
} while(elem != nullptr);
ofile.close();
return 0;
}
int BookList::load(const std::string &fname) {
int total_read = 0;
std::ifstream bookFile(fname);
while(bookFile.good()) {
std::string line;
std::getline(bookFile, line);
if(line.empty()) {
continue;
}
Book *newBook = Book::from_raw_string(line);
if(newBook != nullptr) {
push(newBook);
total_read++;
} else {
std::cerr << "Line error" << std::endl;
}
}
return total_read;
}
Book *BookList::find_by_id(unsigned int id) {
ListElem *current_elem = get_head();
do {
Book *book = (Book *) current_elem->_payload;
if(book->get_id() == id) {
return book;
}
current_elem = GenericList::get_next(current_elem);
} while(current_elem != nullptr);
return nullptr;
}
Book *BookList::find_by_title(const std::string& title) {
ListElem *current_elem = get_head();
do {
Book *book = (Book *) current_elem->_payload;
if(book->get_title() == title) {
return book;
}
current_elem = GenericList::get_next(current_elem);
} while(current_elem != nullptr);
return nullptr;
}
BookList *BookList::avail_by_author(const std::string& author) {
BookList *result = new BookList();
ListElem *current_elem = get_head();
do {
Book *book = (Book *) current_elem->_payload;
if(book->get_author() == author && book->get_avail() > 0) {
result->push(book);
}
current_elem = GenericList::get_next(current_elem);
} while(current_elem != nullptr);
if(result->size() == 0) {
return nullptr;
}
return result;
}