Design and implement a Book class, such as you can imagine as part of software f
ID: 3773102 • Letter: D
Question
Design and implement a Book class, such as you can imagine as part of software for a library. Class Book should have members for the ISBN. title, author , and copyright date. Also store data on whether or not the book is checked out. Create functions for returning those data values. Breate functions for checking a book in and out. Do simple validation of data entered into a Book; for example, accept ISDNs only of the form n-n-n-x where n is an integer and x is a digit or a letter. Store an ISBN as a string.
Explanation / Answer
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/lexical_cast.hpp>
using namespace boost::gregorian;
namespace Library
{
class Book
{
public:
Book();
std::string get_isbn() const
{
return isbn13;
}
std::string get_author() const
{
return author[Book::first] + " " + author[Book::last];
}
std::string get_copyright()
{
return boost::gregorian::to_simple_string(copyright);
}
bool is_bookin() const
{
return is_checkedin;
}
void print();
void read_isbn();
void read_author();
void read_copyright();
void checkout()
{
std::cout << "DEBUG: checkout() ";is_checkedin=false;
}
void checkin()
{
std::cout << "DEBUG: checkin() ";is_checkedin=true;
}
private:
std::string isbn13;
static const uint isbn13_length=13;
std::string title;
enum Name
{
first, last
}
name;
//std::string author;
std::vector<std::string> author;
boost::gregorian::date copyright;
bool is_checkedin;
};
bool isint(std::string data);
bool islong(std::string data);
}
Alternative way:
#ifndef BOOK_H_GUARD
#define BOOK_H_GUARD
#include <iostream>
#include <string>
class Book
{
public:
Book();
std::string get_title()
{
return title;
}
std::string get_author()
{
return author;
}
int get_copyRightDate()
{
return copyRightDate;
}
bool get_inOut()
{
return inOut;
}
std::string get_isbn();
bool operator == (Book& b)
{
if (isbn.n1 != b.isbn.n1)
return false;
if (isbn.n2 != b.isbn.n2)
return false;
if (isbn.n3 != b.isbn.n3)
return false;
if (isbn.x != b.isbn.x)
return false;
return true;
}
bool operator != (Book& b)
{
if (isbn.n1 == b.isbn.n1)
return false;
if (isbn.n2 == b.isbn.n2)
return false;
if (isbn.n3 == b.isbn.n3)
return false;
if (isbn.x == b.isbn.x)
return false;
return true;
}
private:
struct ISBN
{
int n1;
int n2;
int n3;
char x;
};
std::string title;
std::string author;
int copyRightDate;
bool inOut;
ISBN isbn;
};
std::ostream& operator << (std::ostream& os, Book& a)
{
return os << "Author: " << a.get_author() << " "<< "Title: " << a.get_title() << " " << "Copyright: " << a.get_copyRightDate() << " " << "ISBN: " << a.get_isbn() << " " << "Status: " << a.get_inOut() << " ";
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.