book class 1.4 [adding library class]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 9 Exercise 9
Using std_lib_facilities.h by Bjarne Stroustrup.
Using Chrono.h and Chrono.cpp by Bjarne Stroustrup,
including the Chrono.h header file and the Chrono.cpp source file. Chrono is an extensive date class used in this exercise.

My project includes the following files:
Chrono.h date class header file
Chrono.cpp date class source file
p340_9_book_main.cpp my main cpp file
p340_9_my_book_class.h my book class header file
p340_9_book_source.cpp my book class header file’s source file

p340_9_book_main.cpp my main cpp file

[code language=”cpp”]
// Philipp Siedler
// Bjarne Stroustrup’s PPP
// Chapter 9 Exercise 9

#include "p340_9_my_book_class.h"
using namespace Chrono;

Library my_Library;

void add_books() {
Book my_Book(ISBN(), "book1", "author1", Date(2000, Date::jul, 5), Genre::biography, false);
my_Library.add_book(my_Book);
}

int main()
try
{
add_books();
Patron examplePatron("Phil", 100);
cout << examplePatron;
keep_window_open();
}
catch (runtime_error e) {
cout << e.what() << endl;
keep_window_open();
}
catch (…) {
cout << "Exiting" << endl;
keep_window_open();
}

/*if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)

common year = 365 days
leap year = 366 days
*/
[/code]

p340_9_my_book_class.h header file

[code language=”cpp”]
#include "std_lib_facilities.h"
#include "Chrono.h"
using namespace Chrono;

//BOOK CLASS——
struct ISBN {
int n0, n1, n2;
char x;
ISBN(int n0, int n1, int n2, char x);
ISBN();
};

// exercise 7 genre enumerator
enum Genre {
fiction = 0,
nonfiction,
periodical,
biography,
children
};

class Book {
private:
ISBN isbn;
string title;
string author;
Date copyright_date;
Genre genre;
bool checked_out;

public:
Book(ISBN _isbn, string _title, string _author, Date _copyright_date, Genre _genrebook, bool _checked_out);
Book();
ISBN isbnf() { return isbn; }
string titlef() { return title; }
string authorf() { return author; }
Date copyright_datef() { return copyright_date; }
Genre genref() { return genre; }
bool checked_outf() { return checked_out; }
void book_out();
void book_in();
};

//exercise 6 operator overloading
ostream& operator << (ostream&, ISBN&);
ostream& operator << (ostream&, const Genre&);
ostream& operator << (ostream&, Book&);
bool operator ==(ISBN&, ISBN&);
bool operator !=(ISBN&, ISBN&);

//endBOOK CLASS——

//PATRON CLASS——
class Patron {
private:
string patron_name;
int patron_num;
bool fee_owed;
double fee_amount;
public:
Patron(string _patron_name, int _patron_num);
Patron();

string patron_namef() { return patron_name; }
int patron_numf() { return patron_num; }
bool fee_owedf() { return fee_owed; }
double fee_amountf() { return fee_amount; }

void add_fee(double _fee);
};

ostream& operator << (ostream&, Patron&);

//endPATRON CLASS——

//LIBRARY CLASS——

struct Transaction {
Book book;
Patron patron;
Date date;

Transaction(Book _book, Patron _patron, Date _date) {
book = _book;
patron = _patron;
date = _date;
}
};

class Library {
private:
vector<Book> books;
vector<Patron> patrons;
vector<Transaction> transactions;
public:

vector<Patron> patronsf() { return patrons; }
vector<Book> booksf() { return books; }

void add_book(Book);
void add_patron(Patron);
void add_transaction(Book, Patron, Date);
void fee_check();
};

bool operator ==(Book&, Book&);

//endLIBRARY CLASS——
[/code]

p340_9_book_source.cpp my book class header file’s source file

[code language=”cpp”]
#include "p340_9_my_book_class.h"

//BOOK CLASS_HELPER FUNCTIONS——

ISBN::ISBN(int _n0, int _n1, int _n2, char _x) {
n0 = _n0;
n1 = _n1;
n2 = _n2;
x = _x;
}

ISBN::ISBN() {
n0 = 0;
n1 = 0;
n2 = 0;
x = ‘0’;
}

Book::Book(ISBN _isbn, string _title, string _author, Date _copyright_date, Genre _genrebook, bool _checked_out) {
cout << "— Creating book —\n";
isbn = _isbn;
title = _title;
author = _author;
copyright_date = _copyright_date;
genre = _genrebook;
checked_out = _checked_out;
};

Book::Book() {
cout << "— Creating book —\n";
isbn = ISBN(0, 0, 0, 0);
title = "";
author = "";
copyright_date = Date();
genre = Genre();
checked_out = false;
}

void Book::book_out() {
checked_out = true;
};

void Book::book_in() {
checked_out = false;
};

ostream& operator << (ostream& os, ISBN& _isbn) {
os << _isbn.n0 << _isbn.n1 << _isbn.n2 << _isbn.x;
return os;
}

ostream& operator << (ostream& os, const Genre& _genre) {
switch (_genre) {
case Genre::biography:
os << "biography";
return os;
case Genre::children:
os << "children";
return os;
case Genre::fiction:
os << "fiction";
return os;
case Genre::nonfiction:
os << "nonfiction";
return os;
case Genre::periodical:
os << "periodical";
return os;
}
}

ostream& operator << (ostream& os, Book& _book) {
os << "ISBN: " << _book.isbnf() << "\n"
<< "Title: " << _book.titlef() << "\n"
<< "Author: " << _book.authorf() << "\n"
<< "Copyright Date: " << _book.copyright_datef() << "\n"
<< "Genre: " << _book.genref() << "\n"
<< "Checked out: " << boolalpha << _book.checked_outf() << "\n";
return os;
}

bool operator ==(ISBN& book1, ISBN& book2) {
if (book1 == book2) {
return true;
}
else {
return false;
}
}

bool operator !=(ISBN& book1, ISBN& book2) {
if (book1 != book2) {
return true;
}
else {
return false;
}
}

//endBOOK CLASS_HELPER FUNCTIONS——

//PATRON CLASS_HELPER FUNCTIONS——

Patron::Patron(string _patron_name, int _patron_num) {
cout << "— Creating Patron —\n";
patron_name = _patron_name;
patron_num = _patron_num;
fee_owed = false;
fee_amount = 0;
}

Patron::Patron() {
cout << "— Creating Patron —\n";
patron_name = "";
patron_num = 0000;
fee_owed = false;
fee_amount = 0;
}

void Patron::add_fee(double _fee) {
fee_amount += _fee;
if (fee_amount != 0) {
fee_owed = true;
}
else {
fee_owed = false;
}
}

ostream& operator << (ostream& os, Patron& _patron) {
os << "Patron Name: " << _patron.patron_namef() << "\n"
<< "Patron Number: " << _patron.patron_numf() << "\n"
<< "Fee owed: " << boolalpha << _patron.fee_owedf() << "\n"
<< "Fee owed: " << _patron.fee_amountf() << "\n";
return os;
}

//endPATRON CLASS_HELPER FUNCTIONS——

//LIBRARY CLASS_HELPER FUNCTIONS——

bool operator ==(Book& _book1, Book& _book2) {
if (_book1.isbnf() == _book2.isbnf()
&& _book1.titlef() == _book2.titlef()
&& _book1.authorf() == _book2.authorf()) {
return true;
}
else {
return false;
}
}

bool operator ==(Patron& _patron1, Patron& _patron2) {
if (_patron1.patron_namef() == _patron2.patron_namef()
&& _patron1.patron_numf() == _patron2.patron_numf()) {
return true;
}
else {
return false;
}
}

void Library::add_book(Book _book) {
if (books.size() == 0) {
books.push_back(_book);
}
else {
int counter = 0;
for (int i = 0; i < books.size(); i++) {
if (books[i] == _book) {
cout << "Book already exists in Library\n";
counter++;
}
}
if (counter == 0) {
books.push_back(_book);
}
}
}

void Library::add_patron(Patron _patron) {
if (patrons.size() == 0) {
patrons.push_back(_patron);
}
else {
int counter = 0;
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i] == _patron) {
cout << "Patron already exists in Library\n";
counter++;
}
}
if (counter == 0) {
patrons.push_back(_patron);
}
}
}

void Library::add_transaction(Book _book, Patron _patron, Date _date) {
bool book_ckeck = false;
bool patron_check = false;

//book check
for (int i = 0; i < books.size(); i++) {
if (books[i] == _book) {
book_ckeck = true;
}
}
if (!book_ckeck) {
cout << "Book does not exist in Library\n";
}

//patron check
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i] == _patron) {
patron_check = true;
}
}
if (!patron_check) {
cout << "Patron does not exist in Library\n";
}

//adding transaction
if (!_book.checked_outf() && book_ckeck && patron_check) {
_book.book_out();
transactions.push_back(Transaction(_book, _patron, _date));
cout << "Book checked out!\n";
}
else {
_book.book_in();
transactions.push_back(Transaction(_book, _patron, _date));
cout << "Book checked in!\n";
}
}

void Library::fee_check() {
for (int i = 0; i < patrons.size(); i++) {
if (patrons[i].fee_owedf()) {
cout << patrons[i].patron_namef() << "\n";
cout << patrons[i].fee_amountf() << "\n";
}
}
}

//endLIBRARY CLASS_HELPER FUNCTIONS——
[/code]

Output:
--- Creating book ---
--- Creating Patron ---
Patron Name: Phil
Patron Number: 100
Fee owed: false
Fee owed: 0
Please enter a character to exit

Newsletter Updates

Enter your email address below to subscribe to our newsletter

3 Comments

  1. I don’t understand how you’re able to do these exercises.. i’ve been trying for hours to get the Chrono.h and Chrono.cpp from Bjarne’s website to just work enough so i could learn how to simply cout << today; and finish drill 1 in Chapter 9. I've nearly lost all motivation and steam from doing this for hours. So bummed out.

    • Quite strange. What exactly is the problem? You cannot get it to work if you do it the way I describe here?

      • oh dear sweet jesus i figured out what was happening.. ok so i made a Chrono.h and Chrono.cpp file and copied the code directly from the ebook copy that i have and it had a couple of errors that i couldnt get to work. THEN i found the code online at Bjarne website that was labeled “/Ch9/e9-11.cpp” which was just Chrono.h and chrono.cpp in one file. The problem was that i didnt realize it was suppose to be one file because i was getting so frustraited that i was splitting it up into 2 different files and there was no #include “chrono.h” in my created Chrono.cpp.. so my chrono.cpp file was telling me i had 128 errors. when compiled turned into 250+ something. i just went over this again and realized my mistake and now i’m going to just take a break and be happy that i can finally do this chapters drills and exercises since i now have something to work from.. And i’ll also see why the books example of Chrono didnt compile and come back and let you know.

        Side note. Awesome work on all these exercise solutions of yours. I’m really impressed with all the work you’ve done. It has helped me a lot being a total beginner and being able to look at your examples when i was completely lost and wasnt even sure of what i was suppose to be trying to code. Did you happen to do the drills in Chapter 9 by chance?

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124