-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooks.cpp
More file actions
350 lines (314 loc) · 9.04 KB
/
Books.cpp
File metadata and controls
350 lines (314 loc) · 9.04 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
Lisa Lin (lisalin@my.unt.edu)
Homework 3 Assignment - Library Management System
Started: 3/12/2021
Books storage cpp file
*/
#include "Books.h"
#include "Book.h"
#include <iostream>
#include <iterator>
#include <fstream>
#include <map>
using namespace std;
bool Books::emptyBook() {
return books.empty();
}
Book* Books::findBook(int id) {
map<int, Book*>::iterator it = books.find(id);
if (it != books.end()) {
return it->second;
}
return NULL;
}
void Books::addBook() {
Book* book;
int bookID, isbn;
float cost;
string status = "IN";
string author, title;
char choice;
cout << "<<< ADD BOOK >>>" << endl << endl;
do {
cout << "Enter book ID number: ";
cin >> bookID;
book = findBook(bookID);
if (book != NULL) { // if book ID already exists
cout << "Book with this ID alreay exists." << endl;
do {
cout << "Would you like to enter a new book ID? (y/n) ";
cin >> choice;
if ((choice != 'y') && (choice != 'n'))
{
cout << "Invalid option. Please try again." << endl;
}
else
{
if (choice == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (book != NULL);
cin.ignore();
cout << "Enter the title: ";
getline(cin, title);
cout << "Enter the author: ";
getline(cin, author);
cout << "Enter the book's ISBN: ";
cin >> isbn;
cout << "Enter the book's cost (in USD): ";
cin >> cost;
books[bookID] = new Book(author, title, bookID, isbn, cost, status);
cout << title << " by " << author << " has been added!" << endl;
// add book to the map
}
void Books::editBook() {
int edit;
char c = 'y';
Book* book;
int id, isbn;
string author, title;
float cost;
string status;
cout << "<<< EDIT BOOK >>>" << endl << endl;
if (books.empty())
{
cout << "No books have been entered." << endl;
return;
}
do {
cout << "Enter the book ID: ";
cin >> id;
book = findBook(id);
if (book == NULL) {
cout << "Book not found" << endl;
do {
cout << "Would you like to enter a new book ID? (y/n) ";
cin >> c;
if ((c != 'y') && (c != 'n'))
{
cout << "Choice not recongized, please try again." << endl;
}
else
{
if (c == 'n')
{
return;
}
else
{
break;
}
}
} while (true);
}
} while (book == NULL);
cout << endl;
do {
book->printBook();
cout << "<<< EDITING MENU FOR " << book->getTitle() << " >>>" << endl;
cout << endl << "----------" << endl;
cout << "0 - Exit editing menu" << endl;
cout << "1 - Edit title" << endl;
cout << "2 - Edit author" << endl;
cout << "3 - Edit ID" << endl;
cout << "4 - Edit ISBN" << endl;
cout << "5 - Edit cost" << endl;
cout << "6 - Edit status" << endl;
cout << "NOTE - To edit book status to repair for a book associated with a loan, enter loan operations." << endl << endl;
cout << "Select an option: ";
cin >> edit;
switch(edit)
{
case 0:
break;
case 1:
{ //edit title
cin.ignore();
cout << "Enter new title: ";
getline(cin, title);
book->setTitle(title);
cout << "Successfully updated title." << endl;
break;
}
case 2:
{ //edit author
cout << "Enter new author: ";
cin.ignore();
getline(cin, author);
book->setAuthor(author);
cout << "Successfully updated author." << endl;
break;
}
case 3:
{ //edit ID
cout << "Enter new ID: ";
cin >> id;
book->setID(id);
cout << "Successfully updated ID." << endl;
break;
}
case 4:
{ //edit ISBN
cout << "Enter new ISBN: ";
cin >> isbn;
book->setIsbn(isbn);
cout << "Successfully updated ISBN." << endl;
break;
}
case 5:
{ //edit cost
cout << "Enter new cost: ";
cin >> cost;
book->setCost(cost);
cout << "Successfully updated cost." << endl;
break;
}
case 6:
{
string status = book->getStatus();
string selection;
if (status == "OUT") {
cout << "Book is currently being checked out. Please enter loans to edit." << endl;
}
else if (status == "IN") {
cout << "Book is currently IN." << endl;
cout << "Enter new status in all CAPS that is not OUT: " << endl;
getline(cin, selection);
if (selection == status) {
cout << "Book has this status.";
}
else if (selection == "OUT") {
cout << "Book must be associated with patron if it is to be marked OUT. Please check out book." << endl;
}
else if (selection == "REPAIR") {
book->setStatus(selection);
cout << "Book status updated to repair." << endl;
}
else if (selection == "LOST") {
cout << "Book must be reported lost by a patron. Please go to patron operations." << endl;
}
}
break;
}
default:
cout << "Invalid option." << endl;
break;
}
} while (edit != 0);
}
void Books::deleteBook(int bookID) {
cout << "<<< DELETE BOOK >>>" << endl;
Book* book = findBook(bookID);
if (book != NULL)
{
delete book; // deletes the book
books.erase(bookID); // deletes the map entity using bookID
}
}
void Books::searchBook() {
Book* book;
int id; //book id
cout << "<<< FIND BOOK >>>" << endl << endl;
cout << "Enter book ID to check if it exists: ";
cin >> id;
book = findBook(id);
if (book != NULL)
{
cout << "Book ID " << id << " exists in the system." << endl;
}
else
{
cout << "Book ID " << id << " does not exist in the system." << endl;
}
}
void Books::printAllBooks() {
cout << "<<< PRINT ALL BOOKS >>>" << endl << endl;
if (books.empty())
{
cout << "No books have been created." << endl;
return;
}
map<int, Book*>::iterator it;
cout << "Information for All Books" << endl;
for (it = books.begin(); it != books.end(); it++) {
((*it).second)->printBook();
}
}
void Books::findAndPrint() {
int id;
cout << "<<< FIND AND PRINT BOOKS>>>" << endl << endl;
if (books.empty())
{
cout << "No books have been stored." << endl;
return;
}
do {
cout << "Enter book ID: ";
cin >> id;
if (findBook(id) == NULL)
cout << "Book cannot be found." << endl;
} while (findBook(id) == NULL);
books.at(id)->printBook();
}
int Books::numOfBooks() {
Book* book;
map<int, Book*>::iterator it;
int n = 0;
for (it = books.begin(); it != books.end(); it++)
{
n++;
}
return n; //returns number of books
}
void Books::store(){
ofstream fout;
fout.open("books.dat");
Book* book;
map<int, Book*>::iterator it;
fout << numOfBooks() << endl;
for (it = books.begin(); it != books.end(); it++)
{
cout << (*it).first << endl;
book = ((*it).second);
fout << book->getID() << endl;
fout << book->getAuthor() << endl;
fout << book->getTitle() << endl;
fout << book->getCost() << endl;
fout << book->getIsbn() << endl;
fout << book->getStatus() << endl;
}
fout.close();
}
void Books::load() {
ifstream fin;
int numBooks;
fin.open("books.dat"); // u should probably check to see if it actually opened
/*
* variable declarations
*/
string title;
int id, isbn;
string status, author;
float cost;
fin >> numBooks;
for (int i = 0; i < numBooks; i++)
{
fin >> id;
fin.ignore();
getline(fin, author);
getline(fin, title);
fin >> cost;
fin >> isbn;
getline(fin, status);
books[id] = new Book(author, title, id, isbn, cost, status);
}
fin.close();
}