Question: Please help me with questions 4, 5a and 5b Thanks advance!!! #include Container.h #include Book.h #include Noble.h #include Hayden.h #include #include #include using namespace std;

Please help me with questions 4, 5a and 5b

Thanks advance!!!

#include "Container.h"

#include "Book.h"

#include "Noble.h"

#include "Hayden.h"

#include

#include

#include

using namespace std;

// forward declarations of functions already implemented:

void flushStdIn();

void callHelper(char);

void helper(char);

Book* searchBook(string, int, Library);

void displayDatabase();

// forward declarations of functions that need implementation:

void addBook(string, int, Library); // 10 points

void removeBook(string, int, Library); // 10 points

void deleteDatabase(); // 10 points

void saveDatabase(string); // 10 points

void loadDatabase(string); // 10 points

void sortDatabase(Container **); // 10 points

Container* list = NULL; // global list

int main()

{

char c = '\0';

_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS

// comment when using g++

cout << " CSE220 Project 6: Library Database using classes" << endl<

loadDatabase("DB.txt"); // During first execution, there will be no DB.txt in source directory. DB.txt is generated by saveDatabase() while exiting the program.

do {

cout << "Please select one option: " << endl;

cout << "a: Add a new book to the database" << endl;

cout << "r: Remove a book from the database" << endl;

cout << "c: Change the available number of books" << endl;

cout << "d: Display all books in the database" << endl;

cout << "s: Sort the list of books in the database" << endl;

cout << "e: Exit and save the database in DB.txt" << endl;

cin >> c;

flushStdIn();

callHelper(c);

} while (c != 'e');

saveDatabase("DB.txt");

deleteDatabase();

list = NULL;

system("pause"); // comment out when using g++

return 0;

}

void flushStdIn()

{

int c;

do c = getchar(); while (c != ' ' && c != EOF);

}

void callHelper(char c)

{

switch (c) {

case 'a':

case 'c':

case 'r':

case 's':

case 'd': helper(c);

break;

case 'e': break;

default: cout << "Invalid option" << endl;

}

}

// The helper function is used to determine how much data is needed and which function to send that data to.

// It uses pointers and values that are returned from some functions to produce the correct ouput.

// There is no implementation needed here, but you should study this function and know how it works.

// It is always helpful to understand how the code works before implementing new features.

// Do not change anything in this function.

void helper(char c)

{

string name;

int noOfbooks;

Library libNumber;

int libNumberInput = 2;

if(c == 'a' || c == 'r' || c == 'c')

{

cout << endl << "Please enter the book's name: " << endl;

getline(cin, name);

cout << "Please enter the number of books available: " << endl;

cin >> noOfbooks;

while (!(libNumberInput == 0 || libNumberInput == 1))

{

cout << endl << "Please select the library number: " << endl;

cout << "0. Noble Library " << endl;

cout << "1. Hayden Library" << endl;

cin >> libNumberInput;

}

libNumber = (Library)libNumberInput;

Book* bookResult = searchBook(name, noOfbooks, libNumber);

if (c == 'a') // add book

{

if (bookResult == NULL)

{

addBook(name, noOfbooks, libNumber);

cout << endl << "Book added to the library database." << endl << endl;

}

else

cout << endl << "Book already present in the library database." << endl << endl;

}

else if (c == 'c') // change number of books

{

if (bookResult == NULL)

{

cout << endl << "Book not found." << endl << endl;

return;

}

cout << endl << "Please enter the new number of available books: " << endl;

cin >> noOfbooks;

flushStdIn();

// Q3c: Call changeNoOfBooks()

// call it here

cout << endl << "Number of books changed." << endl << endl;

}

else if (c == 'r') // remove book

{

if (bookResult == NULL)

{

cout << endl << "Book not found." << endl << endl;

return;

}

removeBook(name, noOfbooks, libNumber);

cout << endl << "Book removed from the database." << endl << endl;

}

}

else if (c == 'd')

{

displayDatabase();

}

else if (c == 's')

{

sortDatabase(&list);

cout << endl << "Database sorted. Use 'd' option to display the sorted list." << endl << endl;

}

}

// Q3b: Define Friend Function changeNoOfBooks()

// Define the function changeNoOfBooks()that is declared within the Book.h file.

// This function sets the new noOfbooks value of that book. The new value is passed in 'noOfbooks' input parameter.

// Use 'd' option after using 'c' option to check if the new value is actually effective.

void changeNoOfBooks(Book *book, int noOfbooks)

{

}

// Q4: Add Book

// This function will be used to add a new book to the tail of the global linked list.

// You will need to use the enum Library variable to determine which constructor to use. So if the user chooses Noble library for the book, then you need to use Noble class and constructor to create new book info.

// Note that searchBook() is called before this function, therefore, the new book is not on the list.

void addBook(string name, int noOfbooks, Library libNumber)

{

}

// No implementation needed here, however it may be helpful to review this function

Book* searchBook(string name, int noOfbooks, Library libNumber)

{

Container* listMember = list;

while (listMember != NULL)

{

if (listMember->book->getName() == name

&& listMember->book->getLibraryNumber() == libNumber

&& listMember->book->getNoOfBooks() == noOfbooks)

return listMember->book;

listMember = listMember->next;

}

return NULL;

}

// Q5a: Implement removeBook() to remove the node that matches name, noOfbooks, and Library number.

void removeBook(string name, int noOfbooks, Library libNumber)

{

}

// Q5b: Implement deleteDatabase() to remove all nodes in the list. Make sure no memory leak will occur

// This function is already called when exiting the program. You do not need to call it any where else.

void deleteDatabase()

{

}

// This function uses the virtual displayBookInfo() method of the Noble and Hayden classes to print all Books.

void displayDatabase()

{

Container *listMember = list;

if (list == NULL)

cout << endl << "Database is empty!" << endl << endl;

while (listMember != NULL)

{

listMember->book->displayBookInfo();

listMember = listMember->next;

}

}

// Q6a: saveDatabase ()

// Save the linked list of books to a file DB.txt using ofstream.

// You will need to come up with a way to store the number of members in linked list. That will help in load().

// One way to store is:

//

//

//

//

//

//

//

// ...

// Hint: You may want to cast the enum Library to an int before writing it to the file.

// This function is already called when exiting the program. You do not need to call it anywhere else.

void saveDatabase(string fileName)

{

}

// Q6b: loadDatabase()

// Load the linked list of books from a file using ifstream.

// You will need to create the linked list in the same order that is was saved to the file.

// You will need to create a new node for the linked list, then add it to the tail of the list.

// Hint: If you casted the enum Library to an int, you will need to cast it back to a Library.

// You will use the Library variable read from the file to determine which constructor to use.

// This function is already called at the beginning of main(). You do not need to call it anywhere else.

void loadDatabase(string fileName)

{

}

// sortDatabase()

// Q7 Use a recursive function, such as quick sort or merge sort, to sort the

// container list by the book name in alphabetical order.

// You can create a new list. In this case, you must delete the old list(garbage collection).

// You may browse books and websites for a suitable implementation of sorting and modify it as per the need of this program.

// You must cite the book/website that you used.

void sortDatabase(Container **head)

{

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!