Question

A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection.

Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the library’s collection. When a book is checked out, checkouts are incremented; when a book is checked in, checkouts are decremented. The number of checkouts must always be greater than or equal to zero and less than or equal to the number of copies available.

If an exception occurs when calling a member function, throw a “logic_error” with a descriptive error message. To access “logic_error,” include the “stdexcept” library. For example, when attempting to check out a book, if no copies of the book exist, throw a “logic_error” stating that the title was not found: “throw logic_error(title + " not found.");”.

Each class requires a separate header and implementation file, for example, “Library.h” and “Library.cpp.” Do not place implementation code in the header file; you will lose points for this. The class header and implementation files must be contained within the “cs20a” namespace. All member functions in the class diagram must be implemented; no additional member functions are needed.

Implement the Book and Library classes. Copy the code provided below for the DynamicArray, the Book and Library header and implementation files, and the Library driver.

Please compile the code from your answer and check for errors before submitting code that is not working.

Library books: DynamicArray<Book Library() addBook(string author, string title): void deleteBook (string author, string title): void checklnBook (string author, string title void checkout Book (string author, string title): void get Total Books() const: int has Book (string author, string title) const: bool book Available (string author, string title) const: bool friend operator (ostream& outs, const Library& library): ostream& findBook (string author, string title) const: int Book author: string title: string Copies int checked Out: int Book Book (string author, string title nt copies 1 int checkedout 0) getAuthor( Const: String getTitle() Const: string setAuthor(string author) void setTitle (string title): void getCopies() const: int getCheckedOut() const: int addCopy(): void delete Copy(): void checkOut(): void checkln(): void friend operator (ostream& outs, const Book& book): ostream&

DynamicArray.h:

#pragma once

#include <stdexcept>

namespace cs20a

{  

   const int INITIAL_CAPACITY = 2;

   const int GROWTH_FACTOR = 2;

   const double MINIMUM_SIZE_ALLOWED = 0.25;

   template<class T>

   class DynamicArray

   {

   public:

       DynamicArray();

       DynamicArray(int capacity);

       DynamicArray(const DynamicArray<T> &rhs);

       ~DynamicArray();

void add(const T& value);

       void removeAt(int index);

       void insert(int index, const T& value);

       bool remove(const T& value);

       int indexOf(const T& value) const;

       bool contains(const T& value) const;

void clear();

int getSize() const;

       int getCapacity() const;

       bool isEmpty() const;

       DynamicArray<T>& operator =(const DynamicArray<T>& rhs);

       T& operator[](int index);

       const T& operator[](int index) const;

       bool operator==(DynamicArray<T> & a) const;

       bool operator!=(DynamicArray<T> & a) const;

   private:

       int size, capacity;

       T *elements;

       void deepCopy(const DynamicArray<T>& rhs);

       void setCapacity(int newCapacity);

       bool isCapacityAdjustmentNeeded() const;

       bool isIndexInRange(int index) const;

   };

   template<class T>

   DynamicArray<T>::DynamicArray() : size(0), capacity(INITIAL_CAPACITY)

   {

       elements = new T[capacity];

   }

   template<class T>

   DynamicArray<T>::DynamicArray(int capacity) : size(0), capacity(capacity)

   {

       elements = new T[capacity];

   }

   template<class T>

   DynamicArray<T>::DynamicArray(const DynamicArray<T> &rhs)

   {

       deepCopy(rhs);

   }

   template <class T>

   DynamicArray<T>& DynamicArray<T>::operator =(const DynamicArray<T>& rhs) {

       if (this != &rhs) {

           delete[] elements;

           deepCopy(rhs);

       }

       return *this;

   }

   template<class T>

   void DynamicArray<T>::deepCopy(const DynamicArray<T>& rhs)

   {

       size = rhs.size;

       capacity = rhs.capacity;

       elements = new T[capacity];

       for (int i = 0; i < size; i++)

           elements[i] = rhs.elements[i];

   }

   template<class T>

   DynamicArray<T>::~DynamicArray()

   {

       delete[] elements;

   }

   template<class T>

   void DynamicArray<T>::add(const T& value)

   {

       if (isCapacityAdjustmentNeeded())

           setCapacity(size + 1);

       T item = value;

       elements[size++] = item;

   }

   template<class T>

   void DynamicArray<T>::removeAt(int index)

   {

       if (!isIndexInRange(index))

           throw std::out_of_range("Index out of range.");

       for (int i = index; i < (size - 1); i++)

           elements[i] = elements[i + 1];

size--;

       if (isCapacityAdjustmentNeeded())

           setCapacity(size + 1);

   }

   template<class T>

   bool DynamicArray<T>::remove(const T& value)

   {

       int i = indexOf(value);

       if (i >= 0) {

           removeAt(i);

           return true;

       }

       else

           return false;

}

template<class T>

   void DynamicArray<T>::insert(int index, const T& value)

   {

       if (!isIndexInRange(index))

           throw std::out_of_range("Index out of range.");

       if (isCapacityAdjustmentNeeded())

           setCapacity(size + 1);

       for (int i = size; i > index; i--)

           elements[i] = elements[i - 1];

       elements[index] = value;

       size++;

   }

template<class T>

   int DynamicArray<T>::indexOf(const T& value) const

   {

       for (int i = 0; i < size; i++)

           if (elements[i] == value)

               return i;

return -1;

   }

   template<class T>

   bool DynamicArray<T>::contains(const T& value) const

   {

       return indexOf(value) > -1;

   }

   template<class T>

   int DynamicArray<T>::getSize() const

   {

       return size;

   }

   template<class T>

   int DynamicArray<T>::getCapacity() const

   {

       return capacity;

   }

   template<class T>

   bool DynamicArray<T>::operator==(DynamicArray<T> & rhs) const

   {

       if (this != &rhs) {

           if (rhs.size != size)

               return false;

           for (int i = 0; i < size; i++)

               if (rhs[i] != elements[i])

                   return false;

       }

       return true;

   }

   template<class T>

   bool DynamicArray<T>::operator!=(DynamicArray<T>& rhs) const

   {

       return !(this == &rhs);

   }

   template<class T>

   bool DynamicArray<T>::isCapacityAdjustmentNeeded() const

   {

       return !((size + 1) > MINIMUM_SIZE_ALLOWED*capacity && size < capacity);

   }

   template<class T>

   bool DynamicArray<T>::isIndexInRange(int index) const

   {

       return (index >= 0 && index <= (size - 1));

   }

   template<class T>

   void DynamicArray<T>::setCapacity(int minCapacity)

   {

       if (minCapacity < size)

           throw std::logic_error("Capacity must be greater than current size.");

       if (minCapacity >= 0)

       {

           int limit = 1;

           while (limit <= minCapacity)

               limit *= GROWTH_FACTOR;

           T *tarray = new T[limit];

           for (int i = 0; i < size; i++)

               tarray[i] = elements[i];

           delete[] elements;

           elements = tarray;

           capacity = limit;

       }

   }

   template<class T>

   T& DynamicArray<T>::operator[](int index)

   {

       if (!isIndexInRange(index))

           throw std::out_of_range("Index out of range.");

       return elements[index];

   }

   template<class T>

   const T& DynamicArray<T>::operator[](int index) const

   {

       if (!isIndexInRange(index))

           throw std::out_of_range("Index out of range.");

       return elements[index];

   }

   template<class T>

   void DynamicArray<T>::clear()

   {

       delete[] elements;

       size = 0;

       capacity = INITIAL_CAPACITY;

       elements = new T[capacity];

   }

   template<class T>

   bool DynamicArray<T>::isEmpty() const

   {

       return (size == 0);

   }

}

Book.h:

#pragma once

#include <iostream>

#include <string>

namespace cs20a {

   using namespace std;

   class Book

   {

   public:

       Book();

       Book(string author, string title, int copies = 1, int checkedOut = 0);

       string getAuthor() const;

       string getTitle() const;

       void setAuthor(string author);

       void setTitle(string title);

       int getCopies() const;

       int getCheckedOut() const;

       void addCopy();

       void deleteCopy();

       void checkOut();

       void checkIn();

       friend ostream& operator << (ostream& outs, const Book& book);

   private:

       string author, title;

       int copies, checkedOut;

   };

}

Book.cpp:

#include <stdexcept>

#include "Book.h"

namespace cs20a {

   Book::Book() : author(), title(), copies(1), checkedOut(0)

   {}

   Book::Book(string author, string title, int copies, int checkedOut)

           : author(author), title(title), copies(copies), checkedOut(checkedOut)

   {}

ostream & cs20a::operator<<(ostream & outs, const Book & book)

   {

       cout << book.title << ", by " << book.author << " Copies: "

           << book.copies << " Checked out: " << book.checkedOut;

       return outs;

   }

   //*** Code goes here ***//

   //*** Code goes here ***//

   //*** Code goes here ***//

}

Library.h:

#pragma once

#include <iostream>

#include <string>

#include "Book.h"

#include "DynamicArray.h"

namespace cs20a {

   class Library {

   public:

       Library();

       void addBook(string author, string title);

       void deleteBook(string author, string title);

       void checkOutBook(string author, string title);

       void checkInBook(string author, string title);

       int getTotalBooks() const;

       bool hasBook(string author, string title) const;

       bool bookAvailable(string author, string title) const;

friend ostream& operator << (ostream& outs, const Library& library);

   private:

       DynamicArray<Book> books;

       int findBook(string author, string title) const;

   };

}

Library.cpp:

#include <stdexcept>

#include "Library.h"

namespace cs20a {

   Library::Library()

   {}

   ostream & operator<<(ostream & outs, const Library & library)

   {

       for (int i = 0; i < library.getTotalBooks(); i++)

           outs << library.books[i] << endl;

       return outs;

   }

   //*** Code goes here ***//

   //*** Code goes here ***//

   //*** Code goes here ***//

}

LibraryDriver.cpp:

#include <iostream>

#include "Library.h"

#include "Book.h"

using namespace std;

using namespace cs20a;

int main()

{

   Library library;

   try

   {

       cout << "--> Before Initial State." << endl;

       library.addBook("Ernest Hemingway", "For Whom the Bell Tolls");

       library.addBook("Ernest Hemingway", "For Whom the Bell Tolls");

       library.addBook("John Steinbeck", "The Grapes of Wrath");

       library.addBook("John Steinbeck", "The Grapes of Wrath");

       library.addBook("John Steinbeck", "East of Eden");

       library.addBook("Mark Twain", "Huckleberry Finn");

cout << library;

   }

   catch (const std::exception& e)

   {

       cout << "*** " << e.what() << " ***" << endl;

   }

   cout << "--> After Initial State." << endl << endl;

try

   {

       cout << "--> Before deleting Main Street and For Whom the Bell Tolls." << endl;

       if (library.hasBook("Sinclair Lewis", "Main Street"))

           library.deleteBook("Sinclair Lewis", "Main Street");

       library.deleteBook("Ernest Hemingway", "For Whom the Bell Tolls");

       library.deleteBook("Ernest Hemingway", "For Whom the Bell Tolls");

       cout << library;

   }

   catch (const std::exception& e)

   {

       cout << "*** " << e.what() << " ***" << endl;

   }

   cout << "--> After deleting Main Street and For Whom the Bell Tolls." << endl << endl;

   try

   {

       cout << "--> Before checking out For Whom the Bell Tolls and The Grapes of Wrath." << endl;

       if (library.hasBook("Ernest Hemingway", "For Whom the Bell Tolls"))

           library.checkOutBook("Ernest Hemingway", "For Whom the Bell Tolls");

       if (library.hasBook("John Steinbeck", "The Grapes of Wrath"))

           library.checkOutBook("John Steinbeck", "The Grapes of Wrath");

       cout << library;

   }

   catch (const std::exception& e)

   {

       cout << "*** " << e.what() << " ***" << endl;

   }

   cout << "--> After checking out For Whom the Bell Tolls and The Grapes of Wrath." << endl << endl;

   try

   {

       cout << "--> Before checking checking in For Whom the Bell Tolls." << endl;

       library.checkInBook("Ernest Hemingway", "For Whom the Bell Tolls");

       library.checkInBook("Ernest Hemingway", "For Whom the Bell Tolls");

       cout << library;

   }

   catch (const std::exception& e)

   {

       cout << "*** " << e.what() << " ***" << endl;

   }

   cout << "--> After checking checking in For Whom the Bell Tolls." << endl << endl;

   try

   {

       cout << "--> Before checking in The Grapes of Wrath." << endl;

library.checkInBook("John Steinbeck", "The Grapes of Wrath");

       cout << library;

   }

   catch (const std::exception& e)

   {

       cout << "*** " << e.what() << " ***" << endl;

   }

   cout << "--> After checking in The Grapes of Wrath." << endl << endl;

   try

   {

       cout << "--> Before checking in Huckleberry Finn." << endl;

       library.checkInBook("Mark Twain", "Huckleberry Finn");

       cout << endl;

       cout << library;

   }

   catch (const std::exception& e)

   {

       cout << "*** " << e.what() << " ***" << endl;

   }

   cout << "--> After checking in Huckleberry Finn." << endl << endl;

   system("pause");

   return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Library.h

#pragma once
#include <iostream>
#include <string>
#include "Book.h"
#include "DynamicArray.h"
namespace cs20a {
class Library {
public:
Library();
void addBook(string author, string title);
void deleteBook(string author, string title);
void checkOutBook(string author, string title);
void checkInBook(string author, string title);
int getTotalBooks() const;
bool hasBook(string author, string title) const;
bool bookAvailable(string author, string title) const;
friend ostream& operator << (ostream& outs, const Library& library);

private:
DynamicArray<Book> books;
int findBook(string author, string title) const;
};
}

//Library.cpp

// Library.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdexcept>
#include "Library.h"
namespace cs20a
{
Library::Library()
{
}
   ostream& operator << (ostream& outs, const Library& library)
       {
           for (int i = 0; i < library.getTotalBooks(); i++)
               outs << library.books[i] << endl;
           return outs;
       }
void Library::addBook(string author, string title)
{

   //find the book , if already present then just increment its copies
int i = findBook(author, title);
if ( i >= 0 )
{
books[i].addCopy();
}
else{
       //otherwise create a new book object and put into the library
Book newBook;
newBook.setAuthor(author);
newBook.setTitle(title);
books.add(newBook);
}
}

void Library::deleteBook(string author, string title){
  
int i = findBook(author,title) ;
if ( i >= 0 )
{
       //find the book , if found then just decrement its copies

books[i].deleteCopy();

if ( books[i].getCopies() == 0 )
{
           //when the last copy is deleted , remove the book object from library
books.remove(books[i]);
}
}
else
{
throw logic_error(title +" Not found.");
}
}

void Library::checkOutBook(string author, string title)
{
int i = findBook(author,title) ;
if ( i >= 0 )
{
books[i].checkOut();
}
else
{
throw logic_error(title +" Not found.");
}
}

void Library::checkInBook(string author, string title)
{
int i = findBook(author,title) ;
if ( i >= 0 )
{
books[i].checkIn();
}
else
{
throw logic_error(title +" Not found.");
}
}

int Library::getTotalBooks() const
{


       int size = books.getSize();

      

       return size;
}

bool Library::hasBook(string author, string title) const
{
int i = findBook(author,title) ;
if ( i >= 0 )
{
return true;
}
else
{
return false;
}
}

bool Library::bookAvailable(string author, string title) const
{
int i = findBook(author,title) ;
if ( i >= 0 && ( books[i].getCopies() - books[i].getCheckedOut() ) > 0)
{
return true;
}
else
{
return false;
}
}

int Library::findBook(string author, string title) const
{
int size = books.getSize();
Book tempBook;
tempBook.setAuthor(author);
tempBook.setTitle(title);

for (int i=0; i< size; i++) //search the array, if found then return the index else return -1
{
if ( tempBook == books[i] )
{
return i;
}
}
return -1;
}
}

//Book.h

#pragma once
#include <iostream>
#include <string>
namespace cs20a {
using namespace std;
class Book
{
public:
Book();
Book(string author, string title, int copies = 1, int checkedOut = 0);
string getAuthor() const;
string getTitle() const;
void setAuthor(string author);
void setTitle(string title);
int getCopies() const;
int getCheckedOut() const;
void addCopy();
void deleteCopy();
void checkOut();
void checkIn();
  
bool operator==(const Book &rhs) const;

friend ostream& operator << (ostream& outs, const Book& book)
private:
string author, title;
int copies, checkedOut;
};
}

//Book.cpp

#include "StdAfx.h"
#include <stdexcept>
#include "Book.h"
namespace cs20a {


   ostream& operator << (ostream& outs, const Book& book){
cout << book.title << ", by " << book.author << " Copies: "
<< book.copies << " Checked out: " << book.checkedOut;
return outs;
}
Book::Book() : author(), title(), copies(1), checkedOut(0)
{}
Book::Book(string author, string title, int copies, int checkedOut)
: author(author), title(title), copies(copies), checkedOut(checkedOut)
{}

string Book::getAuthor() const
{
return author;
}

string Book::getTitle() const
{
return title;
}
void Book::setAuthor(string author)
{
this->author = author;
}

void Book::setTitle(string title)
{
this->title = title;
}

int Book::getCopies() const
{
return copies;
}

int Book::getCheckedOut() const
{
return checkedOut;
}

void Book::addCopy()
{
copies++;
}

void Book::deleteCopy()
{
   //just decrement
if (copies > 0)
copies--;
else
throw logic_error(" No copies to delete.");
}

void Book::checkOut()
{
   // checkout can't be more than copies
if ( (copies - checkedOut) > 0)
{
checkedOut++;
}
else
throw logic_error(" No copies to checkout.");
}

void Book::checkIn()
{
checkedOut--;
}

bool Book::operator==(const Book &rhs) const {

if ( author == rhs.author && title == rhs.title )
return true;
else
return false;
}

}Before checking out For Whom the Bell Tolls and The Grapes of rath The Grapes of Wrath, by John Steinbeck Copies: 2 Checked o

Add a comment
Know the answer?
Add Answer to:
A library maintains a collection of books. Books can be added to and deleted from and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

  • I need help solving this question from the practice final exam given to us to prepare...

    I need help solving this question from the practice final exam given to us to prepare for the final in C++ #include <iostream> using namespace std; /* The following is code for an OrderedCollection container and its related iterator. The container has a capacity determined by a constructor parameter. The container does not grow. Code that adds elements to the container ensures that the capacity of the container is never exceeded. An attempt to add an item to a full...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • I need to complete the C++ program for hotel reservation. In the main cpp you have...

    I need to complete the C++ program for hotel reservation. In the main cpp you have to display the hotel information from the hotel.txt based on customer preferences(which can be taken from the User.txt) For example, if the customer's budget is 200$, then only the hotels with prices below that amount should be displayed. Then the user has an option to choose which one to reserve for the particular time(which also can be found in the user.txt) The last two...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT