Question: Help with this C++ program please As provided, the starter code has a flaw. It allows multiple patrons to check out the same book at

Help with this C++ program please

As provided, the starter code has a flaw. It allows multiple patrons to check out the same book at the same time, even though it shouldnt. The code allows the following sequence of events to occur: 1. Action: Patron A checks out book 1. Effect: The system shows that the book has been checked out to patron A, and patron A shows the book in its linked list of checked-out books. Everything is correct so far. 2. Action: Patron B checks out book 1. Effect. The system shows that the book has been checked out to patron B (not patron A). Both patron A and patron B show the book in their inventories. 3. Action: Patron B returns book 1. The system indicates that book 1 is not checked out. It does not show up in patron Bs inventory, but still shows up in patron As inventory. 4. Action: Patron A returns book 1. Now the system appears to be back in a correct state. The software should not allow more than one person to check out a book at a time. Modify the code to ensure this is always true. Hint: you could add a Boolean variable to the Book class that indicates whether the book is checked out or not. Use that for error checking. Add enough functionality and error checking to make sure the following statements are always true:

A book can only be checked out to one person at a time.

A book cannot be added to the library if it is already present in the librarys system.

A person cannot check out a book that he/she already has checked out.

A person cannot return a book that she/he does not have checked out.

A non-existent person cannot return a book. o Right now, the system says misspelled person then waits to reread the patrons name. Provide an improved error message for a missing author then bounce back to the menu.

A person cannot check out a book from an author that is not in the catalog.

Right now, the system says misspelled author then waits to reread the author. Provide an improved error message for a missing author then bounce back to the menu.

A person cannot check out a book that the library does not have.

Right now, the system says misspelled book then waits to reread the book title. Provide an improved error message for a missing author then bounce back to the menu. A successfully returned book must no longer be in any patrons inventory. Be sure you include comment documentation for any new code you add. Your code should always provide appropriate messages to the user when an illegal action is attempted..

Here is the starter code...

#include  #include  #include  #include  using namespace std; class Patron; // forward declaration; class Book { public: Book() { patron = 0; } bool operator== (const Book& bk) const { return strcmp(title, bk.title) == 0; } private: char *title; Patron *patron; ostream& printBook(ostream&) const; friend ostream& operator<< (ostream& out, const Book& bk) { return bk.printBook(out); } friend class CheckedOutBook; friend Patron; friend void includeBook(); friend void checkOutBook(); friend void returnBook(); }; class Author { public: Author() { } bool operator== (const Author& ar) const { return strcmp(name, ar.name) == 0; } private: char *name; list books; ostream& printAuthor(ostream&) const; friend ostream& operator<< (ostream& out, const Author& ar) { return ar.printAuthor(out); } friend void includeBook(); friend void checkOutBook(); friend void returnBook(); friend class CheckedOutBook; friend Patron; }; class CheckedOutBook { public: CheckedOutBook(list::iterator ar, list::iterator bk) { author = ar; book = bk; } bool operator== (const CheckedOutBook& bk) const { return strcmp(author->name, bk.author->name) == 0 && strcmp(book->title, bk.book->title) == 0; } private: list::iterator author; list::iterator book; friend void checkOutBook(); friend void returnBook(); friend Patron; }; class Patron { public: Patron() { } bool operator== (const Patron& pn) const { return strcmp(name, pn.name) == 0; } private: char *name; list books; ostream& printPatron(ostream&) const; friend ostream& operator<< (ostream& out, const Patron& pn) { return pn.printPatron(out); } friend void checkOutBook(); friend void returnBook(); friend Book; }; list catalog['Z' + 1]; list people['Z' + 1]; ostream& Author::printAuthor(ostream& out) const { out << name << endl; list::const_iterator ref = books.begin(); for (; ref != books.end(); ref++) out << *ref; // overloaded << return out; } ostream& Book::printBook(ostream& out) const { out << " * " << title; if (patron != 0) out << " - checked out to " << patron->name; // overloaded << out << endl; return out; } ostream& Patron::printPatron(ostream& out) const { out << name; if (!books.empty()) { out << " has the following books: "; list::const_iterator bk = books.begin(); for (; bk != books.end(); bk++) out << " * " << bk->author->name << ", " << bk->book->title << endl; } else out << " has no books "; return out; } template ostream& operator<< (ostream& out, const list& lst) { for (typename list::const_iterator ref = lst.begin(); ref != lst.end(); ref++) out << *ref; // overloaded << return out; } char* getString(const char *msg) { char s[82], i, *destin; cout << msg; cin.get(s, 80); while (cin.get(s[81]) && s[81] != ' '); // discard overflowing destin = new char[strlen(s) + 1]; // characters; for (i = 0; destin[i] = toupper(s[i]); i++); return destin; } void status() { register int i; cout << "Library has the following books: "; for (i = 'A'; i <= 'Z'; i++) if (!catalog[i].empty()) cout << catalog[i]; cout << " The following people are using the library: "; for (i = 'A'; i <= 'Z'; i++) if (!people[i].empty()) cout << people[i]; } void includeBook() { Author newAuthor; Book newBook; newAuthor.name = getString("Enter author's name: "); newBook.title = getString("Enter the title of the book: "); list::iterator oldAuthor = find(catalog[newAuthor.name[0]].begin(), catalog[newAuthor.name[0]].end(), newAuthor); if (oldAuthor == catalog[newAuthor.name[0]].end()) { newAuthor.books.push_front(newBook); catalog[newAuthor.name[0]].push_front(newAuthor); } else (*oldAuthor).books.push_front(newBook); } void checkOutBook() { Patron patron; Author author; Book book; list::iterator authorRef; list::iterator bookRef; patron.name = getString("Enter patron's name: "); while (true) { author.name = getString("Enter author's name: "); authorRef = find(catalog[author.name[0]].begin(), catalog[author.name[0]].end(), author); if (authorRef == catalog[author.name[0]].end()) cout << "Misspelled author's name "; else break; } while (true) { book.title = getString("Enter the title of the book: "); bookRef = find((*authorRef).books.begin(), (*authorRef).books.end(), book); if (bookRef == (*authorRef).books.end()) cout << "Misspelled title "; else break; } list::iterator patronRef; patronRef = find(people[patron.name[0]].begin(), people[patron.name[0]].end(), patron); CheckedOutBook checkedOutBook(authorRef, bookRef); if (patronRef == people[patron.name[0]].end()) { // a new patron patron.books.push_front(checkedOutBook); // in the library; people[patron.name[0]].push_front(patron); (*bookRef).patron = &*people[patron.name[0]].begin(); } else { (*patronRef).books.push_front(checkedOutBook); (*bookRef).patron = &*patronRef; } } void returnBook() { Patron patron; Book book; Author author; list::iterator patronRef; list::iterator bookRef; list::iterator authorRef; while (true) { patron.name = getString("Enter patron's name: "); patronRef = find(people[patron.name[0]].begin(), people[patron.name[0]].end(), patron); if (patronRef == people[patron.name[0]].end()) cout << "Patron's name misspelled "; else break; } while (true) { author.name = getString("Enter author's name: "); authorRef = find(catalog[author.name[0]].begin(), catalog[author.name[0]].end(), author); if (authorRef == catalog[author.name[0]].end()) cout << "Misspelled author's name "; else break; } while (true) { book.title = getString("Enter the title of the book: "); bookRef = find((*authorRef).books.begin(), (*authorRef).books.end(), book); if (bookRef == (*authorRef).books.end()) cout << "Misspelled title "; else break; } CheckedOutBook checkedOutBook(authorRef, bookRef); (*bookRef).patron = 0; (*patronRef).books.remove(checkedOutBook); } int menu() { int option; cout << " Enter one of the following options: " << "1. Include a book in the catalog 2. Check out a book " << "3. Return a book 4. Status 5. Exit " << "Your option? "; cin >> option; cin.get(); // discard ' '; return option; } int main() { while (true) switch (menu()) { case 1: includeBook(); break; case 2: checkOutBook(); break; case 3: returnBook(); break; case 4: status(); break; case 5: return 0; default: cout << "Wrong option, try again: "; } return 0; }

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!