hwk5.pdf

CSCI2312 – Object Oriented Programming Section 003

Homework 5 Fall 2022 Page 1 of 4

Homework #5

The goal of this homework is to exercise Object-Oriented Programming concepts in C++ language using Dynamic Allocated Objects and References (Pointers).

1. Problem Description You will implement an application to handle the basic operations of a library, including adding patrons, adding books, and checking out and returning books. The following UML class diagram depicts the classes you will use to implement your application; the main program is not depicted. The following section will provide details and implementation hints.

CSCI2312 – Object Oriented Programming Section 003

Homework 5 Fall 2022 Page 2 of 4

1.1. Implementation Details

1.1.1. Source Files

• Place your main program in the main.cpp file.

• Implement each class using a .h and .cpp (e.g., Book.h and Book.cpp) files (a total of ten files).

1.1.2. Main Program

• The main program will display a menu providing the library’s users with several functionalities. The primary application will present the following menu: ---------------------------------

Main Menu

------------------------------

1) Add Patron

2) Add Book

3) Search for Patron

4) Search for Book

5) Check Out Book

6) Return Book

99) EXIT

------------------------------

• As the program starts, it instantiates the Library class. This object will represent the library, and all the calls to implement options 1 through 6 should call methods from this object.

• Option (1): the program asks the user for the patron’s name, address, and phone number and calls the library addPatron method.

• Option (2): the program asks the user for the book’s title and the number of pages and calls the library addBook method.

• Option (3): the program asks for a patron’s name and calls the searchPatron method from the library. If a patron matches the name (equality), it will print all the patron’s information to the console.

• Option (4): the program asks for a book’s title and calls the searchBook method from the library. If a book matches the title, it will print the book’s title and the number of pages to the console.

• Option (5): the program asks for a book’s title, a patron’s name, and the date the book will be due (format year month day). Then, search for the book and patron (see options 3 and 4). Call the checkOutBook method to complete the loan if both patron and book were found.

• Option (6): similarly, searches for book and patron and calls the returnBook method from the library object.

• Option (99): asks the user for confirmation and terminates the program.

o The program should keep running until the user selects to terminate the program.

All objects (library, patron, books and loans) should be allocated dynamically using pointers. Date objects can be created with regular variables.

CSCI2312 – Object Oriented Programming Section 003

Homework 5 Fall 2022 Page 3 of 4

1.1.1. Classes Methods Descriptions

Here are details about the methods. Getters and Setters are not described.

Class Library

• Constructors: initialize the object correctly. Be careful with initializing the vector objects.

• Destructor: deletes all loans, books, and patrons. Be careful with the order.

• searchBook: searches the books list for a book that matches the title. Returns the pointer to the object or nullptr.

• searchPatron: searches the patrons list for a patron that matches the name. Returns the pointer to the object or nullptr.

• addBook: when a book with the given title does not exist already (uses searchBook), it creates a new Book and adds it to the books list. If the book exists, do nothing.

• addPatron: when a patron with the given title does not exist already (uses searchPatron), it creates a new Patron and adds it to the patrons list. If the patron exists, do nothing.

• checkOutBook: takes a book, a patron and a date’s data (checks object pointers are not null). If the book is

not checked out, create a Loan object for that book, patron, and date, and add it to the loans list. Adds the book to the patrons (addChekedBook) and the patron to the book (checkOut)

• returnBook: takes a book and a patron (checks not null). Searches for the loan that has that book and patron. When found, remove the book from the patron object and remove the patron from the book object. Finally, remove the Loan pointer from the loans list.

Class Book

• Constructors: initialize the object correctly.

• Destructor: sets all pointers to nullptr. The Book class does not create or delete objects.

• checkOut: sets the checkedOutBy attribute to the given patron.

• isCheckedOut: returns true when checkedOutBy is not nullptr.

• returnBook: sets the checkedOutBy attribute to nullptr.

Class Book

• Constructors: initialize the object correctly. Be careful with initializing the vector objects.

• Destructor: sets all pointers to nullptr. The Patron class does not create or delete objects.

• addCheckedBook: adds the book pointer to the checked books list.

• removeCheckedBook: searches for a pointer in the checked books list to match the given object. When found, the pointer is removed from the list.

• isBookCheckedOutByPatron: searches for a pointer in the checked books list to match the given book object. Returns true when found; false otherwise.

Class Date

- It is provided for you. Handles basic date operations.

1.2. Development Tips Create all classes. Then add the methods to the .h files and generate the implementation. Compile to check for no errors. Then implement classes as Black Boxes as we did in class. The order of implementation shouldn’t change the results. I would recommend implementing Book, Patron, and Loan classes, then the menu, and one operation from the menu to the Library and test it; e.g., add patron functionality completely. You can use the debugger to see that the objects are correctly created and stored in the library’s vectors. Be careful with the destructor implementation.

CSCI2312 – Object Oriented Programming Section 003

Homework 5 Fall 2022 Page 4 of 4

2. Deliverables / Submission 1. Code should follow the guidelines regarding naming conventions, coding style, and comments.

a. Comment your code—no need to comment on every single line of code but add comments explaining what you are doing.

2. Develop your code in CLion. You will submit the entire CLion Project.

3. Create a makefile to compile and run your code in CSE. Capture a screenshot of the compilation output and program running (see 4.b below).

4. You need to submit a total of two files to the Canvas Assignment page: a. Hwk5.zip

i. A compressed file that includes all CLion files for the homework. ii. It should contain at least the main.cpp, Date.h, Date.cpp, Library.h,

Library.cpp, Book.h, Book.cpp, Patron.h, Patron.cpp, Loan.h, and Loan.cpp.

iii. Include a Readme.MD file in the CLion Project. iv. makefile

1. The makefile you used to compile the program in CSE Grid. 2. Place this file in your CLion folder before zipping it.

b. hwk5.png or hwk5.jpg. i. A screenshot of your program running on CSE Grid.

ii. Include the result of searching for a book.

5. Submit both files in one single submission. Otherwise, you will be overwriting your previous submission.

  • 1. Problem Description
    • 1.1. Implementation Details
      • 1.1.1. Source Files
      • 1.1.2. Main Program
      • 1.1.1. Classes Methods Descriptions
        • Class Library
        • Class Book
        • Class Book
        • Class Date
    • 1.2. Development Tips
  • 2. Deliverables / Submission