CatalogClass.h file
#pragma once #include #include using std::cout; using std::endl; using std::string; class Book { private: string title; string author; int ISBN; bool isCheckedOut; string checkedOutBy; public: //Constructor Book(string t = "N/A", string a = "N/A", int i = -1) { title = t; author = a; ISBN = i; isCheckedOut = false; checkedOutBy = ""; } //Getters string getTitle() { return title; } string getAuthor() { return author; } int getISBN() { return ISBN; } bool getStatus() { return isCheckedOut; } string getCheckedOutBy() { return checkedOutBy; } //Setters void setTitle(string new_t) { title = new_t; } void setAuthor(string new_a) { author = new_a; } void setISBN(int new_i) { ISBN = new_i; } // If the book is checked out, return false, //otherwise, set the checked out status to //true and update the name of the user that //checked out the book. bool checkOutBook(string n) { if (isCheckedOut == true) return false; else { isCheckedOut = true; checkedOutBy = n; return true; } } void returnBook() { isCheckedOut = false; checkedOutBy = ""; } void outputBook() { cout author = obj.author; this->title = obj.title; this->ISBN = obj.ISBN; this->checkedOutBy = obj.checkedOutBy; this->isCheckedOut = obj.isCheckedOut; return *this; } };
TestCatalogClass.cpp file
#include "CatalogClass.h" #include using std::cin; using std::getline; void populate_catalog(Catalog& my_catalog); int display_menu(); int main() { Catalog my_catalog; populate_catalog(my_catalog); cout = 1 && choice > choice; if (choice 8) { cout 8); return choice; } void populate_catalog(Catalog& my_catalog) { Book temp_obj; temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Sorcerers Stone"); temp_obj.setISBN(98346); my_catalog.addBook(temp_obj); temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Chamber of Secrets"); temp_obj.setISBN(19285); my_catalog.addBook(temp_obj); temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Prisioner of Azkaban"); temp_obj.setISBN(88224); my_catalog.addBook(temp_obj); temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Goblet of Fire"); temp_obj.setISBN(21001); my_catalog.addBook(temp_obj); temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Order of Phoenix"); temp_obj.setISBN(66754); my_catalog.addBook(temp_obj); temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Half-Blood Prince"); temp_obj.setISBN(50125); my_catalog.addBook(temp_obj); temp_obj.setAuthor("J.K. Rowling"); temp_obj.setTitle("Harry Potter and the Deathly Hallows"); temp_obj.setISBN(68304); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("The Lion, the Witch, and the Wardrobe"); temp_obj.setISBN(45336); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("Prince Caspian"); temp_obj.setISBN(76689); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("The Voyage of the Dawn Treader"); temp_obj.setISBN(34982); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("The Silver Chair"); temp_obj.setISBN(45993); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("A Horse and His Boy"); temp_obj.setISBN(42398); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("The Magician's Nephew"); temp_obj.setISBN(39203); my_catalog.addBook(temp_obj); temp_obj.setAuthor("C.S. Lewis"); temp_obj.setTitle("TheLast Battle"); temp_obj.setISBN(56342); my_catalog.addBook(temp_obj); temp_obj.setAuthor("E. B. White"); temp_obj.setTitle("Charlotte's Web"); temp_obj.setISBN(47851); my_catalog.addBook(temp_obj); temp_obj.setAuthor("F. Scott Fitzgerald"); temp_obj.setTitle("The Great Gasby"); temp_obj.setISBN(11934); my_catalog.addBook(temp_obj); temp_obj.setAuthor("S. E. Hinton"); temp_obj.setTitle("The Outsiders"); temp_obj.setISBN(72331); my_catalog.addBook(temp_obj); /* Feel free to add more books below to experiment with the catalog class! */ }
Part 1 Instructions In the previous assignment, we created a class called "Book, which stored basic information about a book at a library. The program used this class to create sample book objects to store information about popular novels. In this program, we are going to build a new class called "Catalog, which will be used to store and manage books in the library. The program should support the following menu commands: 1. Search Catalog a. By Title b. By Author c. By ISBN 2. Add New Book 3. Get Book Status 4. Check in a Book 5. Check Out a Book 6. Exit Descriptions of Notable Methods 1. listBookBy Author - prints out a list of books written by the author. Returns true if the author has at least 1 book in the catalog and false if not. 2. getBookBy
- These methods search the Catalog for a book matching the specified attribute. The "book" parameter is a reference parameter used to pass the book back by. If the book is found, assign the book parameter to the book found and return true. If not found, do not assign the book object to anything and return false. 3. getBookStatus - Gets the status of a book in the library. Returns true if the book is checked out, false otherwise. Also returns the person's name that has the book checked out via reference variable if the book is checked out. The string will be empty if the book is not checked out. 4. checkOutBook - Searches the catalog of the book for the ISBN number and checks the book out, if the book is found and is not checked out. Returns true if it successfully checks the book out, false if it does not. 5. checkInBook - Checks in a book in the catalog. Returns true if the book is found and checked in, and false otherwise. 6. addBook - Adds a book to the catalog. Takes in a book object and then copies that book object into the next available slot. If the "books array is at capacity, then growCatalog() should be called. 7. outputCatalog - Prints out the entire catalog of books. Calls the outputBook method from each book. 8. growCatalog - This function will be provided to you in the Appendix. This function should be copied into your Catalog class definition. Used to increase the number of elements in the "books" array to support more books. 9. emptyCatalog - This function deallocates all book objects in the "books" array. Use delete[] on the array to achieve this. Catalog -books: Book pointer -next_slot: int -capacity: int Catalog(init_size: int) ~Catalog() Getters and Setters +getNumBooks(): int -emptyCatalog(): void Utility Methods +getBookBy Title(title:string, book: Book&): bool +listBooksByAuthor(author:string): bool +getBookByISBN(isbn:int, book:Book&): bool +getBookStatus(isbn:int, checked OutBy:string&): bool +checkOutBook(isbn:int, name:string): bool +checkInBook(isbn: int): bool +addBook(new_book: Book): void +outputCatalog(): void -growCatalog(): void Hints 1. Start by creating the class definition first. Implement the methods in the class definition before working on the .cpp file. 2. Test each method individually in the main function before writing the code for the menu. Create a Catalog object to work with and add random books to the catalog. Call the methods and observe how they behave to test the Catalog class. Tip: You may add additional helper methods to your Catalog. One method you might add is the ISBN search method from Program 3 to determine the index location of a book in the array. This might be useful for check in and check out methods. 3. Once you feel confident the Catalog class works, begin working on the menu code. You may use either an if/else if statement or switch statement to decide what menu item is chosen. 4. Test each menu command to ensure they work. E: Classes\Spring 2021 CSE-1384\Labs\Program-4\Program-4\Debug\Program-4.exe Welcome to the Library! Main Menu 1. Search by Book Title 2. Search by Book ISBN 3. Output all books by Author 4. Add a New Book to the Catalog 5. Get Check Out Status of Book 6. Check in a book 7. Check out a book 8. Exit Program Enter your choice: 1 Enter the title of the book: E\Classes\Spring 2021\CSE-1384\Labs\Program-4\Program-4\Debug\Program-4.exe Main Menu 1. Search by Book Title 2. Search by Book ISBN 3. Output all books by Author 4. Add a New Book to the Catalog 5. Get Check Out Status of Book 6. Check in a book 7. Check out a book 8. Exit Program Enter your choice: 1 Enter the title of the book: Harry Potter and the Half-Blood Prince Harry Potter and the Half-Blood Prince by J.K. Rowling ISBN: 50125 Main Menu 1. Search by Book Title 2. Search by Book ISBN 3. Output all books by Author 4. Add a New Book to the Catalog 5. Get Check Out Status of Book 6. Check in a book 7. Check out a book 8. Exit Program Enter your choice