Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP would be greatly appreciated and I will thumbs up! The codes below is my ma

ID: 3717532 • Letter: H

Question

HELP would be greatly appreciated and I will thumbs up!

The codes below is my main.cpp

// READ BEFORE YOU START:

// You are given a partially completed program that creates a list of books.

// Each book has the corresponding information: name, noOfbooks and libNumber.

// In the Book.h file, you will find the definition for the enum for 'libNumber'.

// Books on the list can be chosen to be in 2 libraries : either Noble or Hayden.

// The classes Noble and Hayden are subclasses of the Book class (found in Book.h).

// Both of these classes will have their own use of the virtual displayBookInfo().

//

// To begin, you should trace through the given code and understand how it works.

// You are to assume that all input is valid:

// Valid name: String containing alphabetical letters

// Valid noOfbooks: a positive integer

// All input will be a valid length and no more than the allowed amount of memory will be used

// search for ‘project7’ in the project to find helpful comments

#include "Container.h"

#include "Book.h"

#include "Noble.h"

#include "Hayden.h"

#include <iostream>

#include <fstream>

#include <string>

#include <cstdlib>

using namespace std;

// forward declarations of functions already implemented:

void flushStdIn();

void callHelper(char);

void helper(char);

Book* searchBook(string, int, Library, string);

void displayDatabase();

void isValidPubDate(int);

void addBook(string, int, Library);

void removeBook(string, int, Library);

void deleteDatabase();

void saveDatabase(string);

void loadDatabase(string);

void sortDatabase(Container **);

Container *mergeSort(Container *);

Container *merge(Container *, Container *);

Container *getMiddle(Container *);

/* project7

newly defined functions (currently empty)

*/

int totalBooks(string name);

void booksAfter2010(Container *database);

Container* list = NULL; // global list

int main()

{

char c = '';

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

// comment when using g++

cout << " CSE220 Project 7: Library Database (overloading, exceptions)" << endl<<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;

/* project7

2.3 Make a new option ‘t’ in the menu to find total number of books of a book

*/

/* project7

4.2 Make a new option ‘n’ in the menu to display newly published books (2010 or later)

*/

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':

/* project7

2.3 , 4.2 make new cases for new manu options here

*/

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 or you risk failing the automated test cases.

void helper(char c)

{

string name;

int noOfbooks;

Library libNumber;

int libNumberInput = 2;

string date;

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;

/* project7

1.2 call isValidPubDate(pubDate) here to get publication date and check for its format

*/

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

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

{

if (bookResult == NULL)

{

addBook(name, noOfbooks, libNumber, date);

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();

changeNoOfBooks(bookResult, noOfbooks);

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, date);

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;

}

/* project7

2.3 make an if-else case for option ‘t’ and call totalBooks(name) here [5]

*/

/* project7

4.3 make an if-else case for option ‘n’ and call booksAfter2010(list) here [5]

*/

}

void changeNoOfBooks(Book *book, int noOfbooks)

{

book->noOfbooks = noOfbooks;

}

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

{

Container* newListMember = new Container();

if (libNumber == NobleLibrary)

newListMember->book = new Noble(name, noOfbooks, libNumber, date);

else // libNumber == HaydenLibrary

newListMember->book = new Hayden(name, noOfbooks, libNumber, date);

newListMember->next = NULL;

if (list == NULL)

{

list = newListMember;

return;

}

Container* currentListMember = list->next;

Container* previousListMember = list;

while (currentListMember != NULL)

{

previousListMember = currentListMember;

currentListMember = currentListMember->next;

}

previousListMember->next = newListMember;

}

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

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

{

Container* listMember = list;

while (listMember != NULL)

{

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

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

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

&& listMember->book->getPubDate() == date)

return listMember->book;

listMember = listMember->next;

}

return NULL;

}

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

{

Container* bookToRemove;

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

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

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

&& list->book->getPubDate() == date)

{

bookToRemove = list;

list = list->next;

delete bookToRemove->book;

delete bookToRemove;

return;

}

Container* currentListMember = list->next;

Container* previousListMember = list;

while (currentListMember != NULL)

{

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

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

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

&& currentListMember->book->getPubDate() == date)

{

bookToRemove = currentListMember;

currentListMember = currentListMember->next;

previousListMember->next = currentListMember;

// Need to remove the Noble/Hayden object before removing the container object

delete bookToRemove->book;

delete bookToRemove;

return;

}

previousListMember = currentListMember;

currentListMember = currentListMember->next;

}

}

void deleteDatabase()

{

while (list != NULL) // Need to remove the dog/cat object before removing the container object

{

Container* listMember = list;

list = list->next;

delete listMember->book;

delete listMember;

}

}

// 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;

}

}

void saveDatabase(string fileName)

{

int count = 0;

Container* listMember = list;

while (listMember != NULL) // count number of Containers in linked list

{

listMember = listMember->next;

count++;

}

ofstream myfile;

myfile.open(fileName); // myfile.open(fileName.c_str()); when using g++

if (myfile.is_open())

{

listMember = list;

myfile << count;

while (listMember != NULL)

{

myfile << listMember->book->getName() << endl;

myfile << listMember->book->getNoOfBooks() << endl;

myfile << (int)listMember->book->getLibraryNumber() << endl;

myfile << listMember->book->getPubDate() << endl;

listMember = listMember->next;

}

myfile.close();

}

}

void loadDatabase(string fileName)

{

ifstream myfile;

myfile.open(fileName); // myfile.open(fileName.c_str()); when using g++

if (myfile.is_open())

{

int libNumber_as_int=0, count = 0;

string name, temp;

int noOfbooks=0;

Library libNumber;

string date;

Container* listMember = list;

myfile >> count;

for (int i = 0; i < count; i++)

{

Container* newListMember = new Container();

std::getline(myfile, name);

std::getline(myfile, temp);

noOfbooks = stoi(temp);

std::getline(myfile, temp);

libNumber_as_int = stoi(temp);

libNumber = (Library)libNumber_as_int;

if (libNumber == NobleLibrary)

newListMember->book = new Noble(name, noOfbooks, libNumber, date);

else // libNumber == HaydenLibrary

newListMember->book = new Hayden(name, noOfbooks, libNumber, date);

newListMember->next = NULL;

if (list == NULL)

{

newListMember->next = list;

list = newListMember;

}

else

{

listMember = list;

while (listMember->next != NULL)

listMember = listMember->next;

listMember->next = newListMember;

}

}

myfile.close();

}

}

void sortDatabase(Container **head)

{

*head = mergeSort(*head);

}

Container *mergeSort(Container *head)

{

// Merge Sort based on Java implementation here: http://stackoverflow.com/a/8238253

Container *middle;

Container *secondHalf;

if (head == NULL || head->next == NULL) return head;

middle = getMiddle(head);

secondHalf = middle->next;

middle->next = NULL;

return merge(mergeSort(head), mergeSort(secondHalf));

}

Container *merge(Container *a, Container *b)

{

Container *tempHead, *current, *helper;

tempHead = new Container();

current = tempHead;

while (a != NULL && b != NULL)

{

if (a->book->getName().compare(b->book->getName()) <= 0)

{

current->next = a;

a = a->next;

}

else

{

current->next = b;

b = b->next;

}

current = current->next;

}

current->next = (a == NULL) ? b : a;

helper = tempHead;

tempHead = tempHead->next;

delete helper;

return tempHead;

}

// Returns the element in the middle of the list (for splitting)

Container *getMiddle(Container *head)

{

if (head == NULL) return head;

Container *slow, *fast;

slow = fast = head;

while (fast->next != NULL && fast->next->next != NULL)

{

// Slow node moves 1 step at a time, fast moves 2 steps at a time.

// When fast reaches the end of the list, slow will have moved half as many

// steps, putting it in the middle of the list.

slow = slow->next;

fast = fast->next->next;

}

return slow;

}

/* project7

1.2 define isValidPubDate(pubDate) here to get publication date and check for its format

*/

int isValidPubDate(string pubDate)

{

if(pubDate.size() == 7){

return(isdigit(pubDate[0]) && isdigit(pubDate[1]) && pubDate[2] == '/' && isdigit(pubDate[3]) && isdigit(pubDate[4]) && isdigit(pubDate[5]) && isdigit(pubDate[6])) ||

(isalpha(pubDate[0]) && isalpha(pubDate[1]) && isalpha(pubDate[2]) && isdigit(pubDate[3]) && isdigit(pubDate[4]) && isdigit(pubDate[5]) && isdigit(pubDate[6]));

}

else {

return 0;

}

}

/* project7

2.2 define totalBooks(string name) here [10]

*/

int totalBooks(string name)

{

return 0; // Added this line to avoid compile error. REMOVE this line when coding for this function.

}

/* project7

4.1 define booksAfter2010(Container *database) here [20]

The function should use operator= overloaded function of Book class

*/

void booksAfter2010(Container *database)

{

}

Q2: Function and operator overloading Book+ (20 points) In the given program (project 6), we can add books with same name and they can be in different or same library. In Main.cpp, write a function int totalBooks(string name) to search for book 'name' and display the sum of number of books named 'name'. 2.1 For adding the number of books, use operator+ overloading. You need to write 2 operator+ overloading functions of Book class: The first function takes in Book object as input parameter and returns sum of number of books of this' object and number of books of input object. The second operator+ overloading function takes an integer as input parameter and returns the sum of number of books of 'this' object and input integer. [5] 2.2 You need to define both the functions above but call any one of this operator+ overloading function in totalBooks(string name) to calculate and display total number of books of the given 'name' book. Define totalBooks(string name) [10] 3 | Page CSE220 Programming for Computer Engineering Project 07 2.3 Make a new option 't' in the menu to find total number of books of a book. In helper(), make an if-else case for option t' and call int totalBooks(string name) there. [5] Name: Intro to Java 5 books available in Noble library Published in 08/2805 Name: Comp Arch B books available in Noble library Published in jan2012 Book name: Comp Arch 3 books available in Hayden library Published in 85/2815 Please select one option: a: Add a new book to the database r: Remove a book from the database c: Change the available number of books t: Display total number of books of a book n: Display new books (published 2010 or later) d: Display all books in the database s: Sort the list of books in the database e: Exit and save the database in DB.txt Please enter the book's name : Comp Arch Total books = 11 8 + 3

Explanation / Answer

// READ BEFORE YOU START:
// You are given a partially completed program that creates a list of books.
// Each book has the corresponding information: name, noOfbooks and libNumber.
// In the Book.h file, you will find the definition for the enum for 'libNumber'.
// Books on the list can be chosen to be in 2 libraries : either Noble or Hayden.
// The classes Noble and Hayden are subclasses of the Book class (found in Book.h).
// Both of these classes will have their own use of the virtual displayBookInfo().
//
// To begin, you should trace through the given code and understand how it works.
// You are to assume that all input is valid:
// Valid name: String containing alphabetical letters
// Valid noOfbooks: a positive integer
// All input will be a valid length and no more than the allowed amount of memory will be used

// search for ‘project7’ in the project to find helpful comments


#include "Container.h"
#include "Book.h"
#include "Noble.h"
#include "Hayden.h"


#include < iostream > #include < fstream > #include < string > #include < cstdlib >

using namespace std;

// forward declarations of functions already implemented:
void flushStdIn();
void callHelper(char);
void helper(char);
Book * searchBook(string, int, Library, string);
void displayDatabase();

void isValidPubDate(int);
void addBook(string, int, Library);
void removeBook(string, int, Library);
void deleteDatabase();
void saveDatabase(string);
void loadDatabase(string);
void sortDatabase(Container * * );
Container * mergeSort(Container * );
Container * merge(Container * , Container * );
Container * getMiddle(Container * );

/* project7
newly defined functions (currently empty)
*/
int totalBooks(string name);
void booksAfter2010(Container * database);

Container * list = NULL; // global list

int main() {
char c = '';
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); // Use to check for memory leaks in VS
// comment when using g++
cout << " CSE220 Project 7: Library Database (overloading, exceptions)" << endl << 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;
    /* project7
    2.3 Make a new option ‘t’ in the menu to find total number of books of a book
    */
    /* project7
    4.2 Make a new option ‘n’ in the menu to display newly published books (2010 or later)
    */
    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':
    /* project7
    2.3 , 4.2 make new cases for new manu options here
    */
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 or you risk failing the automated test cases.
void helper(char c) {
string name;
int noOfbooks;
Library libNumber;
int libNumberInput = 2;
string date;

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;

    /* project7
    1.2 call isValidPubDate(pubDate) here to get publication date and check for its format
    */
    flushStdIn();
       while(1) {
           cout << endl;
           cout << "Please enter the book's publication date" << endl;
           cout << "Valid formats are mm/yyyy and AAAyyyy : ";
           getline(cin, date);
           if(isValidPubDate(date)) {
               break;
           }
           cout << endl;
       }

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

    if (c == 'a') // add book
    {
      if (bookResult == NULL) {
        addBook(name, noOfbooks, libNumber, date);
        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();

      changeNoOfBooks(bookResult, noOfbooks);

      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, date);
      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;
}
/* project7
2.3 make an if-else case for option ‘t’ and call totalBooks(name) here [5]
*/

/* project7
4.3 make an if-else case for option ‘n’ and call booksAfter2010(list) here [5]
*/
}

void changeNoOfBooks(Book * book, int noOfbooks) {
book - > noOfbooks = noOfbooks;
}

void addBook(string name, int noOfbooks, Library libNumber, string date) {
Container * newListMember = new Container();

if (libNumber == NobleLibrary)
    newListMember - > book = new Noble(name, noOfbooks, libNumber, date);
else // libNumber == HaydenLibrary
    newListMember - > book = new Hayden(name, noOfbooks, libNumber, date);

newListMember - > next = NULL;

if (list == NULL) {
    list = newListMember;
    return;
}

Container * currentListMember = list - > next;
Container * previousListMember = list;

while (currentListMember != NULL) {
    previousListMember = currentListMember;
    currentListMember = currentListMember - > next;
}

previousListMember - > next = newListMember;
}

// No implementation needed here, however it may be helpful to review this function
Book * searchBook(string name, int noOfbooks, Library libNumber, string date) {
Container * listMember = list;

while (listMember != NULL) {
    if (listMember - > book - > getName() == name && listMember - > book - > getLibraryNumber() == libNumber && listMember - > book - > getNoOfBooks() == noOfbooks && listMember - > book - > getPubDate() == date)

      return listMember - > book;

    listMember = listMember - > next;
}

return NULL;
}

void removeBook(string name, int noOfbooks, Library libNumber, string date) {
Container * bookToRemove;

if (list - > book - > getName() == name && list - > book - > getNoOfBooks() == noOfbooks && list - > book - > getLibraryNumber() == libNumber && list - > book - > getPubDate() == date) {
    bookToRemove = list;
    list = list - > next;
    delete bookToRemove - > book;
    delete bookToRemove;
    return;
}

Container * currentListMember = list - > next;
Container * previousListMember = list;

while (currentListMember != NULL) {
    if (currentListMember - > book - > getName() == name && currentListMember - > book - > getNoOfBooks() == noOfbooks && currentListMember - > book - > getLibraryNumber() == libNumber && currentListMember - > book - > getPubDate() == date) {
      bookToRemove = currentListMember;
      currentListMember = currentListMember - > next;
      previousListMember - > next = currentListMember;
      // Need to remove the Noble/Hayden object before removing the container object
      delete bookToRemove - > book;
      delete bookToRemove;
      return;
    }
    previousListMember = currentListMember;
    currentListMember = currentListMember - > next;
}
}

void deleteDatabase() {
while (list != NULL) // Need to remove the dog/cat object before removing the container object
{
    Container * listMember = list;
    list = list - > next;
    delete listMember - > book;
    delete listMember;
}
}

// 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;
}
}

void saveDatabase(string fileName) {
int count = 0;
Container * listMember = list;

while (listMember != NULL) // count number of Containers in linked list
{
    listMember = listMember - > next;
    count++;
}

ofstream myfile;
myfile.open(fileName); // myfile.open(fileName.c_str()); when using g++

if (myfile.is_open()) {
    listMember = list;
    myfile << count;

    while (listMember != NULL) {
      myfile << listMember - > book - > getName() << endl;
      myfile << listMember - > book - > getNoOfBooks() << endl;
      myfile << (int) listMember - > book - > getLibraryNumber() << endl;
      myfile << listMember - > book - > getPubDate() << endl;
      listMember = listMember - > next;
    }
    myfile.close();
}
}

void loadDatabase(string fileName) {
ifstream myfile;
myfile.open(fileName); // myfile.open(fileName.c_str()); when using g++

if (myfile.is_open()) {
    int libNumber_as_int = 0, count = 0;
    string name, temp;
    int noOfbooks = 0;
    Library libNumber;
    string date;

    Container * listMember = list;

    myfile >> count;

    for (int i = 0; i < count; i++) {
      Container * newListMember = new Container();
      std::getline(myfile, name);
      std::getline(myfile, temp);
      noOfbooks = stoi(temp);
      std::getline(myfile, temp);
      libNumber_as_int = stoi(temp);
      libNumber = (Library) libNumber_as_int;

      if (libNumber == NobleLibrary)
        newListMember - > book = new Noble(name, noOfbooks, libNumber, date);
      else // libNumber == HaydenLibrary
        newListMember - > book = new Hayden(name, noOfbooks, libNumber, date);

      newListMember - > next = NULL;

      if (list == NULL) {
        newListMember - > next = list;
        list = newListMember;
      } else {
        listMember = list;
        while (listMember - > next != NULL)
          listMember = listMember - > next;
        listMember - > next = newListMember;
      }
    }
    myfile.close();
}
}

void sortDatabase(Container * * head) { * head = mergeSort( * head);
}

Container * mergeSort(Container * head) {
// Merge Sort based on Java implementation here: http://stackoverflow.com/a/8238253
Container * middle;
Container * secondHalf;
if (head == NULL || head - > next == NULL) return head;
middle = getMiddle(head);
secondHalf = middle - > next;
middle - > next = NULL;

return merge(mergeSort(head), mergeSort(secondHalf));
}

Container * merge(Container * a, Container * b) {
Container * tempHead, * current, * helper;
tempHead = new Container();
current = tempHead;
while (a != NULL && b != NULL) {
    if (a - > book - > getName().compare(b - > book - > getName()) <= 0) {
      current - > next = a;
      a = a - > next;
    } else {
      current - > next = b;
      b = b - > next;
    }
    current = current - > next;
}
current - > next = (a == NULL) ? b : a;
helper = tempHead;
tempHead = tempHead - > next;
delete helper;
return tempHead;
}

// Returns the element in the middle of the list (for splitting)
Container * getMiddle(Container * head) {
    if (head == NULL) return head;
    Container * slow, * fast;
    slow = fast = head;
    while (fast - > next != NULL && fast - > next - > next != NULL) {
      // Slow node moves 1 step at a time, fast moves 2 steps at a time.
      // When fast reaches the end of the list, slow will have moved half as many
      // steps, putting it in the middle of the list.
      slow = slow - > next;
      fast = fast - > next - > next;
    }
    return slow;
}
/* project7
1.2 define isValidPubDate(pubDate) here to get publication date and check for its format
*/
int isValidPubDate(string pubDate) {
if (pubDate.size() == 7) {
    return (isdigit(pubDate[0]) && isdigit(pubDate[1]) && pubDate[2] == '/' && isdigit(pubDate[3]) && isdigit(pubDate[4]) && isdigit(pubDate[5]) && isdigit(pubDate[6])) ||
      (isalpha(pubDate[0]) && isalpha(pubDate[1]) && isalpha(pubDate[2]) && isdigit(pubDate[3]) && isdigit(pubDate[4]) && isdigit(pubDate[5]) && isdigit(pubDate[6]));
} else {
    return 0;
}
}

/* project7
2.2 define totalBooks(string name) here [10]
*/
int totalBooks(string name) {

return 0; // Added this line to avoid compile error. REMOVE this line when coding for this function.
}

/* project7
4.1 define booksAfter2010(Container *database) here [20]
The function should use operator= overloaded function of Book class
*/
void booksAfter2010(Container * database) {}