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

You are going to build a simple bookshelf that creates two classes, Book and Boo

ID: 3692204 • Letter: Y

Question

You are going to build a simple bookshelf that creates two classes, Book and Bookshelf.
The Book class contains two private attributes, title and author. You need to create accessor functions
and mutator functions for both of these attributes.
Your Book class will need a default constructor and a destructor. Initialize the attributes to reasonable
default values in the default constructor. The Book class also needs a constructor that accepts values for
both attributes and initializes the attributes to those values.
The Bookshelf class uses the Book class. You must create a constructor and a destructor for the
Bookshelf class. Make sure you initialize the data in your object in the constructor.
In the private data for your Bookshelf class you will have an array of Book objects. You must be able to
support a maximum of 10 phone book entries.
In your class do something similar to the following:
class Bookshelf
{
// …
private:
static const int MAX_ENTRIES = 10;
Book entries[MAX_ENTRIES];
// …
};
You also need private data to keep track of how many entries you are actually using in the array.
Provide a default constructor. You will need to set initialize the number of entries in this constructor.
You will also need the following public member functions in your Bookshelf class.
void add(string _title, string _author);

void add(const Book& book);
void removeByTitle(string _title);
void removeByAuthor(string _author);
unsigned getSize() const;
const Book& bookAt(unsigned position) const;
Function add will fill in the next available Book with the passed in name and author or with the passed
in Book, based on the parameters to add. If there is no room for another entry on the bookshelf, the
function should throw a string describing the error as an exception.
The removeByTitle function will remove the first entry on the bookshelf it finds that has a title that
matches the passed in parameter. If no entry has that title the function should throw a string describing
the error as an exception.
You can remove the book in one of two ways:
1. You can simply replace the entry where title is found with the current last entry in the array. You
will need to decrement the count of valid entries. For example, if you find the title in the entry
at index 3 and there are 9 entries used in the array - you would copy the values from the entry
with index 8 (the 9th entry) into the entry with index 3 (the 4th entry).
2. You can move entries in the array around to fill in the now (removed) entry. This is more
complicated. If, as an example, you find the title in the entry with index 3 and you currently
using 9 of the 10 entries in the array you will need to copy the entry from index 4 to index 3. You
will then copy entry 5 to entry 4, entry 6 to entry 5, entry 7 to entry 6, and entry 8 to entry 7.
You will then decrement the number of entries used from 9 to 8. Entries 0, 1 and 2 are not
modified in any way.
It is your choice which of these two options you want to implement.
The removeByAuthor function removes the first entry on the bookshelf that has the author specified by
the input parameter. Like removeByTitle the function should throw a string describing the error as an
exception. You must implement the same removal logic (1 or 2 above in removeByTitle) that you used
with the removeByTitle function.
The getSize functions returns the number of entries currently in use. This could be anywhere from 0 to
10.
Finally, the bookAt function returns the current entry at the index passed to the function via the
position parameter. If position is greater than the current size you should throw a string describing the
error as an exception. If position is valid, return back that entry.
Finally, note that bookAt returns back a reference to a Book. This is a const reference. The programming
using the Book returned by the bookAt function cannot change the book object.

You need to define the Book and Bookshelf classes in two header files (Book.h and Bookshelf.h) and
you need to put the implementation of the member functions in two source files (Book.cpp and
Bookshelf.cpp).
The main function.
You are provided a file, named BookshelfDriver.cpp, which contains the main function. You must write
your Book and Bookshelf classes so they compile and execute properly when compiled with the
provided BookshelfDriver.cpp. Your submission will be tested by compiling and running the resulting
program with the provided BookshelfDriver.cpp file.

HERE'S WHAT I HAVE:

Book.h

#include <string>
#ifndef BOOK_H
#define BOOK_H

using namespace std;

class Book // beginning of the book class
{
private:
    string title; // attribute to hold the title of a book which is a string
    string author; // attribute to hold the name of an author which is a string
public:
    Book(); // default constructor
    Book(string new_title, string new_author); // constructor with paramaters for title and author
    ~Book(); // destructor

    string getTitle() const; // accessor function to get the title of a book
    void setTitle(string); // mutator function to set the title of a book
    string getAuthor() const; // accessor function to get the name of an author
    void setAuthor(string); // mutator function to set the name of an author


}; // end of the book class

#endif

Book.cpp

#include "Book.h"
#include <string>
#include <iostream>

Book::Book() // default constructor initalized to nothing
{
    title = " "; // title is initialized to blank space
    author = " "; // author is initialized to blank space
}

Book::Book(string new_title, string new_author) // parameterized constructor that is initialized
{
    title = new_title; // sets title to new_title
    author = new_author; // sets author to new_author
}

Book::~Book()
{

}

string Book::getAuthor() const // function that gets the name of the author
{
    return author; // returns the name of the author
}

void Book::setAuthor(string _author) // function that sets the name of the author
{
    author = _author; // sets the name of the author to the _author that we got
}

string Book::getTitle() const // function that gets the name of the book
{
    return title; // returns the name of the book
}

void Book::setTitle(string _title) // function that sets the name of the book
{
    title = _title; // sets the name of the book to the _title that we got
}

Bookshelf.h

#include "Book.h"
#include <string>
#include <iostream>

#ifndef BOOKSHELF_H
#define BOOKSHELF_H

using namespace std;

class Bookshelf : public Book
{
private:
    static const int MAX_ENTRIES = 10; // constant number of entries that is set to 10
    Book entries[MAX_ENTRIES]; // array of 10 book entries
    int NEXT_ENTRY; // integer to hold the number of books
    int NEXT_BOOK; // to hold the space for the books
public:
    Bookshelf(); // default constructor
    Bookshelf(string title, string author);
    ~Bookshelf(); // destructor
    void add(string _title, string _author);
    void add(const Book& book);
    void removeByTitle(string _title);
    void removeByAuthor(string _author);
    unsigned getSize() const;
    const Book& bookAt(unsigned position) const;
    class NoRoom {}; // exception if there is no room to add another title
    class InvalidTitle {}; // exception class for the title
    class InvalidAuthor {}; // exception class for the author
};
#endif

Bookshelf.cpp

#include "Bookshelf.h"
#include <string>
#include <iostream>

Bookshelf::Bookshelf() // default contructor set to default arguments
{
    NEXT_ENTRY = 0; // sets the next entry to zero
    NEXT_BOOK = 0; // sets the next book to zero

}

Bookshelf::~Bookshelf()
{

}

void Bookshelf::add(string inTitle, string inAuthor) // this function will get the book
{
    for (int i = 0; i < MAX_ENTRIES; i++)
    {
        entries[i].title[i]
    }


}

void Bookshelf::add(const Book& book) // this function will add the next book if possible
{
    if (NEXT_ENTRY >= MAX_ENTRIES) // this throws an exception if there is no more room for another book
    {
        cout << "There is no more room to add a book!" << endl;
        throw exception();
    }

   
}

void Bookshelf::removeByTitle(string _title) // this function will remove the first entry on the bookshelf it finds with the title passed
{
    for (int i = 0; i < MAX_ENTRIES; i++)
    {
        if (sameName(entries[i], _title))
        {
            entries[i] = " ";
            i--;
        }


    }
}

void Bookshelf::removeByAuthor(string _author) // this function removes the first entry on the bookshelf with the author specified
{

}

***This is the provided main function

#include <iostream>
#include <string>

#include "Book.h"
#include "Bookshelf.h"

using namespace std;

void main(void)
{
    Bookshelf bookshelf = Bookshelf();
    Book book;

    try
    {
        cout << "Retrieve book at position 0" << endl;
        book = bookshelf.bookAt(0);
        cout << "Book has title " << book.getTitle() << " with author " << book.getAuthor() << "'" << endl;
    }

    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    try
    {
        cout << "Adding The Three-Body Problem" << endl;
        bookshelf.add("The Three-Body Problem", "Cixin Liu");

        cout << "Adding The Dark Forest" << endl;
        book = Book("The Dark Forest", "Cixin Liu");
        bookshelf.add(book);

        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");

        cout << "There are " << bookshelf.getSize() << " books on the shelf" << endl;

    }

    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    try
    {
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");
        cout << "Adding Seveneves" << endl;
        bookshelf.add("Seveneves", "Neal Stephenson");

        cout << "There are " << bookshelf.getSize() << " books on the shelf" << endl;
    }

    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    try
    {
        cout << "Remove book by Cixin Liu" << endl;
        bookshelf.removeByAuthor("Cixin Liu");

        cout << "There are " << bookshelf.getSize() << " books on the shelf" << endl;
    }

    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    try
    {
        cout << "Remove The Three-Body Problem" << endl;
        bookshelf.removeByTitle("The Three-Body Problem");

        cout << "There are " << bookshelf.getSize() << " books on the shelf" << endl;
    }

    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    try
    {
        cout << "Remove Seveneves" << endl;
        bookshelf.removeByTitle("Seveneves");
        cout << "Remove Seveneves" << endl;
        bookshelf.removeByTitle("Seveneves");
        cout << "Remove Seveneves" << endl;
        bookshelf.removeByTitle("Seveneves");
        cout << "Remove Seveneves" << endl;
        bookshelf.removeByTitle("Seveneves");
        cout << "Remove Seveneves" << endl;
        bookshelf.removeByTitle("Seveneves");

        cout << "There are " << bookshelf.getSize() << " books on the shelf" << endl;
    }

    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    try
    {
        cout << endl;
        cout << "There are " << bookshelf.getSize() << " books on the shelf" << endl;
        cout << "The bookshelf contains the following books (the order does not matter):" << endl;

        for (unsigned currentBook = 0; currentBook <= bookshelf.getSize(); ++currentBook)
        {
            cout << "Book has title '" << bookshelf.bookAt(currentBook).getTitle() << "' with author '"
                << bookshelf.bookAt(currentBook).getAuthor() << "'" << endl;
        }
    }
   
    catch (string s)
    {
        cout << "EXCEPTION CAUGHT: " << s << endl;

    }

    cout << "Hit <enter>";
    cin.get();
}

Explanation / Answer

#include<iostream>
#include<string.h>

using namespace std;

//PhoneEntry Class represents each entry in the PhoneBook
class PhoneEntry
{
    //Private access variables
    private:
        //Name and number of contact
        string myNumber;
        string myName;

    //Public access specifier
    public:
        //Setter method to set the number which is a private variable
        void setNumber(string num)
        {
           myNumber=num;
        }

        //Setter method to set the name which is a private variable
        void setName(string name)
        {
            myName=name;
        }

        //Getter method to get Number
        string getNumber()
        {
            return myNumber;
        }

        //Getter method to get Name
        string getName()
        {
            return myName;
        }
};

//PhoneBook class represents multiple PhoneEntries
class phoneBook
{
    //Private Access Specifier
    private:
        //Static and constant integer to represent maximum number of entries in the phonebook
        static const int MAX_ENTRIES=10;

        //Array of PhoneEntry objects to represent each entry in the phonebook
        PhoneEntry ph[MAX_ENTRIES];

        //Static variable to keep count of number of entries currently in the phonebook
        static int currentCount;

    //Public Access Specifier
    public:
        //Method to add new entry in the phoneBook
        void add(string name,string number)
        {
            ph[currentCount].setName(name);
            ph[currentCount].setNumber(number);

            //Incrementing current counter to represent added entry
            currentCount++;
        }

        //Function that returns reference to the entry at the specified position
        const PhoneEntry& phoneEntryAt(int position) const
        {
            //If position is less than 0 return the first entry in the phonebook
            if(position < 0)
                return ph[0];
            //If position is greter than maximum entries, return the last entry in the phonebook
            else if(position > currentCount)
                return ph[currentCount];
            //If position is valid, then return entry at that position
            else
                return ph[position];
        }

        //Method to remove Entry by Name
        void removeByName(string name)
        {
            int i=0,j=0;
            for(i=0;i<currentCount;i++)
            {
                //When entry with a matching name is found
                if(ph[i].getName() == name)
                {
                    //Shift all entries one index up
                    for(j=0;j<currentCount;j++)
                    {
                        ph[j].setName(ph[j+1].getName());
                        ph[j].setNumber(ph[j+1].getNumber());
                    }
                    //Decrement current count variable to represent the removed entry
                    currentCount--;
                }
            }
        }

        //Method to remove entry by Number
        void removeByNumber(string number)
        {
            int i=0,j=0;
            for(i=0;i<currentCount;i++)
            {
                //When a matching number is found
                if(ph[i].getNumber() == number)
                {
                    //Shift all subsequent entries one index up in the array
                    for(j=0;j<currentCount;j++)
                    {
                        ph[j].setName(ph[j+1].getName());
                        ph[j].setNumber(ph[j+1].getNumber());
                    }
                    //Decrement current count variable to represent the removed entry
                    currentCount--;
                }
            }
        }

        //Method to return the size of the phoneBook
        int getSize()
        {
            return currentCount;
        }
};

//Declaration of static variable currentCount
int phoneBook::currentCount;

int main()
{
    //Object of phoneBook class
    phoneBook book;

    //Object of PhoneEntry class
    PhoneEntry ph;

    int choice=0;

    //Character to know if user wants to continue
    char menuChoice='y';

    //Temporary variables for storing name and number for operations
    string name, number;

    //Position variable for getEntryAt method
    int position;

    while(menuChoice == 'y')
    {
        cout << "1. Add an entry ";
        cout << "2. Remove entry by name ";
        cout << "3. Remove entry by number ";
        cout << "4. Get total count ";
        cout << "5. Get entry at position ";
        cout << ":: ";
        cin >> choice;

        switch(choice)
        {
            case 1:
                    cout <<"Enter Name and Number: ";
                    cin >> name >> number;
                    book.add(name, number);
                    cout << " Entry Added!";
                    break;
            case 2:
                    cout << "Enter Name to remove: ";
                    cin >> name;
                    book.removeByName(name);
                    break;
            case 3:
                    cout << "Enter number to remove: ";
                    cin >> number;
                    book.removeByNumber(number);
                    break;
            case 4:
                    cout << "Total Count: " << book.getSize();
                    break;

            case 5:
                    const PhoneEntry myReturnObj;
                    cout << "Enter Position: ";
                    cin >> position;
                    PhoneEntry temp = book.phoneEntryAt(position);
                    cout << "Name: " << temp.getName();
                    cout << "Number: " << temp.getNumber();
                    break;
        }

        cout << " Do you want to Continue? ";
        cin >> menuChoice;
    }

    return 0;
}


sample output

1. Add an entry                                                                                                                                             
2. Remove entry by name                                                                                                                                     
3. Remove entry by number                                                                                                                                   
4. Get total count                                                                                                                                          
5. Get entry at position                                                                                                                                    
:: 1                                                                                                                                                        
Enter Name and Number: sowmi 9715254323                                                                                                                    
                                                                                                                                                            
Entry Added!                                                                                                                                                
Do you want to Continue? no                                                                                                                                 

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote