-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.cpp
More file actions
161 lines (134 loc) · 3.98 KB
/
Copy pathLibrary.cpp
File metadata and controls
161 lines (134 loc) · 3.98 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
/**
* @file Library.cpp
* @author Sam Emison
* @date 2024-11-08
* @brief Code for the functions in the library class
*
* Has the code for the functions inside of the library class. Gives it functionality
*/
#include "Library.h"
#include <fstream>
//Constructor
Library::Library() : head(nullptr) {}
//Destructor
Library::~Library() {
Book* current = head;
while (current != nullptr) {
Book* next = current->next;
delete current;
current = next;
}
}
//Insert a book in a order, sorted by author name
void Library::insert_sorted(const std::string &title, const std::string &author, const std::string &isbn, int pages, float price, int year) {
Book* new_book = new Book(title, author, isbn, pages, price, year);
if (!head || head-> author > author) {
new_book->next = head;
head = new_book;
}
else {
Book* current = head;
while (current->next && current->next->author < author) {
current = current->next;
}
new_book->next = current->next;
current->next = new_book;
}
}
//Find a book via title and return data
void Library::find_album(const std::string &title) const {
Book* current = head;
bool found = false;
while (current) {
if (current->title == title) {
std::cout << current->author + " - " + current->isbn + " (" << current->year << ")\n";
std::cout << "Price: " << current->price << std::endl;
found = true;
}
current = current->next;
}
if (!found) {
std::cout << "No books found with the title " << title << std::endl;
}
}
//Find books via author and print their title
void Library::find_author(const std::string &author) const {
Book* current = head;
bool found = false;
while (current) {
if (current->author == author) {
std::cout << current->title << " - " << current->isbn << " (" << current->year << ")\n";
std::cout << "Price: " << current->price << std::endl;
found = true;
}
current = current->next;
}
if(!found) {
std::cout << "No books found by " << author << std::endl;
}
}
//Delete a book via author and title
bool Library::deleteBook(const std::string &author, const std::string &title) {
if (!head) return false;
if (head->author == author && head->title == title) {
Book* temp = head;
head = head->next;
delete temp;
return true;
}
Book* current = head;
while (current->next) {
if (current->next->author == author && current->next->title == title) {
Book* temp = current->next;
current->next = current->next->next;
delete temp;
return true;
}
current = current->next;
}
return false;
}
//Writes the list to a file
bool Library::write_to_file(const std::string &filename) const {
std::ofstream outfile(filename);
if (!outfile.is_open()) {
return false;
}
Book* current = head;
while (current) {
outfile << current->title << "\n" << current->author << "\n" << current->isbn << "\n";
outfile << current->pages << "\n" << current->price << "\n" << current->year << "\n";
current = current->next;
}
outfile.close();
return true;
}
//Read books from file
bool Library::read_from_file(const std::string &filename) {
std::ifstream file(filename);
if (!file.is_open()) {
return false;
}
std::string title, author, isbn;
int pages, year;
float price;
while(std::getline(file, title) && std::getline(file, author) && std::getline(file, isbn) && file >> pages >> price >> year) {
file.ignore();
insert_sorted(title, author, isbn, pages, price, year);
}
file.close();
return true;
}
//Print all books in the library
void Library::print() const {
Book* current = head;
while (current) {
std::cout << "Title: " << current->title << "\n";
std::cout << "Author: " << current->author << "\n";
std::cout << "ISBN: " << current->isbn << "\n";
std::cout << "Pages: " << current->pages << "\n";
std::cout << "Price: " << current->price << "\n";
std::cout << "Year: " << current->year << "\n\n";
current = current->next;
}
}