Project: Book Pile

Pre-requisites: Chapter 3

Imagine a pile of books with a special restriction: no book in the pile can be below a book with a shorter title (measured by number of characters including spaces):


For this project you’ll create the class BookPile which stores and manages this type of pile. Your program should implement at a minimum the requirements below (though you may add more functions if needed) and meet style guide requirements.

BookPile General Requirements:

· Store the pile inside BookPile as an array with 50 elements. Do not allow gaps to form in the array. For instance, if there are presently 10 books in the array then the elements at index 0 through 9 should be used.

· Empty string is not considered a legal book title.

· Books with the same title length may be stored in any order. Dune can be on top of Rust, or Rust can be on top of Dune. Either is legal.

BookPile Operations:

· Constructor – Include an appropriate constructor.

· int getTitleLength(string) – Private method that returns the number of characters in the string including spaces and punctuation. Other methods that need the length of a title should use this helper.

· bool removeBook(string) – Removes the book with title matching the parameter. Returns true if book was removed, false if it was not.

· bool removeBook(int) – Remove book at the parameter position where 1 is the top of the pile. Returns true if book was removed, false if it was not.

· bool addBook(string) – Add book with given title to the pile in the proper location. Do nothing if the book title is empty string. Also, do not allow duplicate books! If a duplicate title is given do nothing. Returns true if book was added, false if it was not.

· displayPile() – Displays the titles to the console numbered and in order from the top of the pile to the bottom followed by the number of characters in their title. So, a pile containing the books in the image above would be displayed as:

1. Brave New World (15)

2. Dragonflight (12)

3. Neuromancer (11)

4. Contact (7)

5. Cinder (6)

6. Dune (4)

· int findBook(string) – Returns the index of the book if it exists in the pile, -1 if it does not.

· string getBook(int) – Returns the book at the given position number where 1 is the top of the pile. Returns empty string if there is no book.

· bool renameBook(oldName, newName) – Allows the changing of the name of a book (if there is a typo in the title, for instance). Note this may mean the book changes position in the pile. This operation should make use of other operations to accomplish this. Do nothing if newName is empty string or if the book is not found in the pile. Returns true if the book was found and replaced, false otherwise.

· int bookCount() – Returns the number of books in the pile.

· bool isEmpty() – Returns true if the pile is empty, otherwise false.

· clear() – Removes all books from the pile.

Main Requirements

You main() should be in a file named main.cpp and do exactly the following in this order:

1. Create a BookPile object named myPile.

2. Display isEmpty()= then the output of isEmpty().

3. Add the following books to the pile in this order:

· Dune

· Dragonflight

· Neuromancer

· Contact

· Brave New World

· Cinder

· Rust

· Contact (yes, try to add this again)

4. Call displayPile().

5. Display bookCount()= and the output of bookCount().

6. Display getBook(3)= and the output of getBook(3).

7. Display findBook(“Neuromancer”)= and the output of findBook(“Neuromancer”).

8. Display removeBook(“Cinder”)= and the output of removeBook(“Cinder”).

9. Display removeBook(1)= and the output of removeBook(1).

10. Display renameBook(“Rust”, “Crust”)= and the output of renameBook(“Rust”, “Crust”).

11. Display bookCount()= and the output of bookCount().

12. Call displayPile().

13. Call clear().

14. Display bookCount()= and the output of bookCount().

Turn In

Upload BookPile.h, BookPile.cpp, and main.cpp prior to the due date.