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

plz implement library.cpp and book.cpp Library.h #pragma once #include <iostream

ID: 3861334 • Letter: P

Question

plz implement library.cpp and book.cpp

Library.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;
   };
}

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 ***//

}

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 ***//

}

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;
}

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);
   }
}

Explanation / Answer

#include "Library.hpp"

using std::string;

using std::vector;

Library::Library() {

currentDate = 0;

}

void Library::addBook(Book *b) {

holdings.push_back(b);

}

void Library::addPatron(Patron *pat) {

members.push_back(pat);

}

string Library::checkOutBook(string pID, string bID) {

Book *book = getBook(bID);

Patron *patron = getPatron(pID);

if (!book) {

return "book not found";

}

if (!patron) {

return "patron not found";

}

Locale status = book->getLocation();

if (status == CHECKED_OUT) {

return "book already checked out";

}

if (status == ON_HOLD_SHELF) {

if (book->getRequestedBy() != patron) {

return "book on hold by other patron";

} else {

book->setRequestedBy(NULL);

}

}

// update Book's status and other info

book->setCheckedOutBy(patron);

book->setDateCheckedOut(currentDate);

book->setLocation(CHECKED_OUT);

patron->addBook(book);

return "check out successful";

}

string Library::returnBook(string bID) {

Book *book = getBook(bID); // Book to return

if (!book) {

return "book not found";

}

Locale status = book->getLocation();

if (status != CHECKED_OUT) {

return "book already in library";

}

Patron *patron = book->getCheckedOutBy();

patron->removeBook(book);

if (book->getRequestedBy()) {

book->setLocation(ON_HOLD_SHELF);

} else {

book->setLocation(ON_SHELF);

}

  

book->setCheckedOutBy(NULL);

return "return successful";

}

string Library::requestBook(string pID, string bID) {

Book *book = getBook(bID);

Patron *patron = getPatron(pID);

if (!book) {

return "book not found";

}

  

if (!patron) {

return "patron not found";

}

Locale status = book->getLocation();

if (status == ON_HOLD_SHELF) {

if (book->getRequestedBy() != patron) {

return "book on hold by other patron";

}

}

if (status == ON_SHELF) {

book->setLocation(ON_HOLD_SHELF);

}

book->setRequestedBy(patron);

return "request successful";

}

string Library::payFine(string pID, double payment) {

Patron *patron = getPatron(pID);

if (!patron) {

return "patron not found";

}

patron->amendFine(-payment);

return "payment successful";

}

void Library::incrementCurrentDate() {

// increment the current date

++currentDate;

  

int numHoldings = holdings.size();

for (int i = 0; i < numHoldings; i++) {

Book *book = holdings.at(i);

int checkOutLength = book->getCheckOutLength();

int dateCheckedOut = book->getDateCheckedOut();

int dueDate = dateCheckedOut + checkOutLength;

int daysOverdue = currentDate - dueDate;

if (daysOverdue > 0) {

Patron *patron = book->getCheckedOutBy();

if (patron != NULL) {

patron->amendFine(0.10);

}

}

}

}

Patron* Library::getPatron(string pID) {

int numMembers = members.size();

// search through all the members

for (int i = 0; i < numMembers; i++) {

string curID = members.at(i)->getIdNum();

// check for equality of strings

if (pID == curID) {

return members.at(i);

}

}

return NULL;

}

Book* Library::getBook(string bID) {

int numHoldings = holdings.size()

for (int i = 0; i < numHoldings; i++) {

string curID = holdings.at(i)->getIdCode();

if (bID == curID) {

return holdings.at(i);

}

}

return NULL;

}