-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookDatabase.cpp
More file actions
133 lines (120 loc) · 3.85 KB
/
Copy pathbookDatabase.cpp
File metadata and controls
133 lines (120 loc) · 3.85 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
/**
* @file bookDatabase.h
* @author Alex Lambert
*
* Description:
* - BookDatabase holds all of the books for a library
* - It can retrieve books, insert books, and display its contents
* - There is no functionality for removing books from the BookDatabase
*
* Assumptions/Implementation:
* - Stores each book type in its own BSTree instance
* - Uses bookFactory to determine the type of a book and create them
* - Automatically scales if new book types were to be added
*/
#include "bookDatabase.h"
using namespace std;
//---------------------------------------------------------------------------
/** BookDatabase()
* Default Constructor
*
* Constructs an empty BookDatabase
* @pre None
* @post BookDatabase is initialized with 1 BSTree for each book type
*/
BookDatabase::BookDatabase()
{
for (int i = 0; i < NUM_BOOK_TYPES; i++)
bookBSTs[i] = new BSTree();
bookFactory = new BookFactory();
}
//---------------------------------------------------------------------------
/** ~BookDatabase()
* Default Destructor
*
* Destroys the BookDatabase and every item inside it
* @pre None
* @post All memory used by the BookDatabase and its parts is deallocated
*/
BookDatabase::~BookDatabase()
{
for (int i = 0; i < NUM_BOOK_TYPES; i++) {
delete bookBSTs[i];
bookBSTs[i] = nullptr;
}
delete bookFactory;
bookFactory = nullptr;
}
//---------------------------------------------------------------------------
/** insertNewBook()
* Insert Book Function
*
* Inserts the given book into the correct BSTree
* @param bookString is the book to be inserted into the appropriate
* BSTree, using the main parsing method (altString = false)
* @pre bookString is properly formatted
* @post newBook is added to the correct BSTree, if the book is valid
* @return if newBook was successfully inserted
*/
bool BookDatabase::insertBook(string bookString)
{
int ID = bookFactory->getID(bookString[0]);
if (ID == -1)
return false;
Book* newBook = bookFactory->createBook(bookString);
if (newBook)
return bookBSTs[ID]->insert(newBook);
return false;
}
//---------------------------------------------------------------------------
/** getBook()
* Get Book Function
*
* Return a book object based on the information that is passed in if it
* is in a BSTree
* @param bookString stores the info about the book to be fetched
* @param altString represents if the book string is formatted in the
* normal or alternative format
* @pre bookString is properly formatted as per the formatting type
* @post None. No changes made.
* @return Pointer to the Book object that they are looking for, if it
* wasn't found, returns nullptr.
*/
Book* BookDatabase::getBook(string bookString, bool altString) const
{
int ID = bookFactory->getID(bookString[0]);
if (ID == -1) {
cout << "ERROR: \'" << bookString[0]
<< "\' is not a valid book type for ";
return nullptr;
}
Book* newBook = bookFactory->createBook(bookString, altString);
if (newBook) {
Book* toReturn = (Book*)bookBSTs[ID]->retrieve(newBook);
if (!toReturn) {
cout << "ERROR: Could not find \'" << newBook->getTitle() << "\' in the catalog for ";
}
delete newBook;
newBook = nullptr;
return toReturn;
}
return nullptr;
}
//---------------------------------------------------------------------------
/** displayAll()
* Display All Books Function
*
* Displays all of the books that have been inserted in the database,
* each type of book being sorted, each type being displayed in its own
* section
* @pre None.
* @post None, no changes.
*/
void BookDatabase::displayAll() const
{
for (int i = 0; i < NUM_BOOK_TYPES; i++) {
cout << endl;
cout << bookFactory->bookTypeHeader[i].str();
bookBSTs[i]->displayAll();
}
}