I have the following c++ data structures class I am working on. I think I am alm
ID: 664908 • Letter: I
Question
I have the following c++ data structures class I am working on. I think I am almost done, but I am unsure of what my code should be for checkinBook and checkoutBook in the Library class. I will post the assignment followed by my code.
Assignment:
Following the information provided below, implement the Library and Book class. The class Books represents authored, readable material that can be checked out from a Library. This Book class should keep track of the number of times it has been checked out. The class Library is a collection of Book instances, as shown in the diagram below.
A Library accepts new books to be added to the collection via calls to addBookToCollection( ... ). A Library allows its books to be checked out via calls to the checkoutBook( ... ) method.
Practice working with inheritance by subclassing std::logic_error to create BookNotFound errors when users try doing silly things with the Library. The class std::logic_error already exists and is part of the namespace std. When you #include <stdexcept>, you will have access to that class which can be created by passing a string to its constructor.
HINT: Please start by getting the Book class to work. Focus on Library only after you have Book working.
Library
Library();
void addBookToCollection( string author, string title ); void checkoutBook( string author, string title ) throw (BookNotFound); void checkinBook( string author, string title ) throw (BookNotFound);
friend operator<< ( ostream& out, const Library & l );
Book myBooks[ 20 ]; int myNumberOfBooksSeenSoFar;
Santa Monica College CS 20A – Summer 2015 Programming Project #2
Book
Book(); Book( string author, string title );
string getTitle() const; string getAuthor() const; int getNumberOfTimesCheckedOut() const;
void increaseNumberOfTimesCheckedOut( int amount=1 ); friend operator<< ( ostream& out, const Book & b );
string my_Author; string my_Title; int my_NumberOfTimesCheckedOut;
Driver Code
Library l; l.addBookToCollection( "J. K. Rowlings", "Harry Potter and the Deathly Hallows" ); l.addBookToCollection( "Barack Obama", "The Audacity of Hope" ); l.addBookToCollection( "Vera Brittain", "Testament of Youth" );
// Print out the library and its book cout << l << endl;
try { l.checkoutBook( "Barack Obama", "The Audacity of Hope" ); l.checkinBook( "Barack Obama", "The Audacity of Hope" );
l.checkoutBook( "Vera Brittain", "Testament of Youth" ); } catch( BookNotFound bnf ) { cout << "An error occurred here for the reason:"; cout << bnf.what() << endl; }
// Print out the library and its book - notice the difference cout << l << endl;
// Let's try to throw some errors... try { // can't checkout a book not in the Library l.checkoutBook( "Barry Manilow", "Sweet Life : Adventures On The Way To Paradise" );
My Code:
Book.h
#ifndef BOOK_H
#define BOOK_H
#include <iostream>
#include <string>
namespace cs20a
{
class Book
{
public:
Book();
Book( std::string author, std::string title );
std::string getTitle() const;
std::string getAuthor() const;
int getNumberOfTimesCheckedOut() const;
void increaseNumberOfTimesCheckedOut( int amount=1 );
friend std::ostream operator<< ( std::ostream& out, const Book & b );
private:
std::string my_Author;
std::string my_Title;
int my_NumberOfTimesCheckedOut;
}
};
#endif
Book.cpp
#include <iostream>
#include <string>
#include "Book.h"
using namespace std;
namespace cs20a
{
Book::Book( std::string author, std::string title )
{
my_Author = author;
my_Title = title;
}
string Book::getTitle() const
{
return(my_Title);
}
string Book::getAuthor() const
{
return(my_Author);
}
void Book::increaseNumberOfTimesCheckedOut( int amount=1 )
{
my_NumberOfTimesCheckedOut += amount;
}
int Book::getNumberOfTimesCheckedOut() const
{
return(my_NumberOfTimesCheckedOut);
}
std::ostream operator<< ( std::ostream& out, const Book & b )
{
out << "Title: " << b.my_Title << endl;
out << "Author: " << b.my_Author << endl;
out << "Number of time checkout out: " << b.my_NumberOfTimesCheckedOut;
return(out);
}
}
Library.h
#ifndef LIBRARY_H
#define LIBRARY_H
#include <iostream>
#include <string>
#include <stdexcept>
#include "Book.h"
namespace cs20a
{
class Library
{
public:
Library();
void addBookToCollection( std::string author, std::string title );
void checkoutBook( std::string author, std::string title ) throw (...);
void checkinBook( std::string author, std::string title ) throw (...);
friend std::ostream& operator<< ( std::ostream& out, const Library & l );
private:
Book myBooks[ 20 ];
int myNumberOfBooksSeenSoFar;
};
};
Library.cpp
#include <iostream>
#include <string>
#include <stdexcept>
#include "Library.h"
#include "BookNotFound.h"
#include "Book.h"
using namespace std;
namespace cs20a
{
Library::Library()
{
}
void Library::checkinBook(std::string author, std::string title) throw(...) //not sure how to implement this
{
Book book(author, title);
int booklocation;
bool bookfound = false;
for(int i = 0; i<myNumberOfBooksSeenSoFar; i++)
{
if(myBooks[i].getTitle() == book.getTitle() && myBooks[i].getAuthor() == book.getAuthor())
{
bookfound = true;
booklocation = 1;
}
}
if(bookfound)
{
}
else
{
throw BookNotFound();
}
}
void Library::checkoutBook(std:: string author, std:: string title) throw(...) //not sure how to implement this
{
Book book(author, title);
int booklocation;
bool bookfound = false;
for(int i = 0; i<myNumberOfBooksSeenSoFar; i++)
{
if(myBooks[i].getTitle() == book.getTitle() && myBooks[i].getAuthor() == book.getAuthor())
{
bookfound = true;
booklocation = 1;
}
}
if(!bookfound)
{
myBooks[booklocation].increaseNumberOfTimesCheckedOut();
}
else
{
throw BookNotFound();
}
}
void Library::addBookToCollection(std::string author, std::string title)
{
Book book(author, title);
myBooks[myNumberOfBooksSeenSoFar] = book;
myNumberOfBooksSeenSoFar++;
}
ostream& operator<< ( std::ostream& out, const Library & l )
{
for(int i = 0; i<l.myNumberOfBooksSeenSoFar; i++)
{
out << "Title: " << l.myBooks[i].getTitle() << endl << "Author: " << l.myBooks[i].getAuthor() << endl;
}
return out;
}
}
BookNotFound.h
#pragma once
#include <stdexcept>
#include <iostream>
#include <string>
namespace cs20a
{
class BookNotFound : public std::logic_error
{
public:
BookNotFound():logic_error("Error"){};
BookNotFound(std::string e):logic_error(e){};
};
};
Main.cpp
#include <iostream>
#include "Library.h"
#include "Book.h"
#include "BookNotFound.h"
using namespace std;
int main()
{
using namespace cs20a;
Library l;
l.addBookToCollection( "J. K. Rowlings", "Harry Potter and the Deathly Hallows" );
l.addBookToCollection( "Barack Obama", "The Audacity of Hope" );
l.addBookToCollection( "Vera Brittain", "Testament of Youth" );
// Print out the library and its book cout << l << endl;
try
{
l.checkoutBook( "Barack Obama", "The Audacity of Hope" );
l.checkinBook( "Barack Obama", "The Audacity of Hope" );
l.checkoutBook( "Vera Brittain", "Testament of Youth" );
}
catch( BookNotFound bnf )
{
cout << "An error occurred here for the reason:";
cout << bnf.what() << endl;
}
// Print out the library and its book - notice the difference cout << l << endl;
// Let's try to throw some errors... try { // can't checkout a book not in the Library l.checkoutBook( "Barry Manilow", "Sweet Life : Adventures On The Way To Paradise" );
}
Explanation / Answer
void Library::checkinBook(std::string author, std::string title) throw(...) //not sure how to implement this
{
Book book(author, title);
int booklocation;
bool bookfound = false;
for(int i = 0; i<myNumberOfBooksSeenSoFar; i++)
{
if(myBooks[i].getTitle() == book.getTitle() && myBooks[i].getAuthor() == book.getAuthor())
{
bookfound = true;
booklocation = 1;
}
}
if(bookfound)
{
//add a book to myBooks
}
else
{
throw BookNotFound();
}
}
void Library::checkoutBook(std:: string author, std:: string title) throw(...) //not sure how to implement this
{
Book book(author, title);
int booklocation;
bool bookfound = false;
for(int i = 0; i<myNumberOfBooksSeenSoFar; i++)
{
if(myBooks[i].getTitle() == book.getTitle() && myBooks[i].getAuthor() == book.getAuthor())
{
bookfound = true;
booklocation = 1;
}
}
if(!bookfound)
{
//remove book from myBooks
myBooks[booklocation].increaseNumberOfTimesCheckedOut();
}
else
{
throw BookNotFound();
}
you have to remove books from your books list when you are checking out. simply remove book from mybooks variable.
you have to add books from your books list when you are checking in . add a entry to mybook variable.