Pages

Wednesday, 13 June 2018

Chapter 9 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio Community 2017 and the header file "std_lib_facilities.h" which can be found here:

http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


Chapter 9 // Exercise 8


Create a Patron class for the library. The class will have a user's name, library card number, and library fees (if owed). have functions that access this data, as well as a function to set the fee of the user. Have a helper function that returns a Boolean (bool) depending on whether or not the user owes a fee.

bookclass.h

//bookclass.h
#ifndef _BOOKCLASS_H_
#define _BOOKCLASS_H_

#include "std_lib_facilities.h"

//enum for genres
enum class Genre
{
 default = 1, fiction, nonfiction, periodical, biography, children
};

//class Book
class Book
{
public:
 Book();
 ~Book();

 Genre  getGenre() const { return m_genre; }
 string getISBN() const { return m_isbn; }
 string getTitle() const { return m_title; }
 string getAuthor() const { return m_author; }
 int    getDate() const { return m_date; }
 bool   getStatus() const { return m_checkedOut; }

 void setGenre(Genre genre) { m_genre = genre; }
 void setISBN(string num1, string num2, string num3, string char1);
 void setTitle(string title) { m_title = title; }
 void setAuthor(string author) { m_author = author; }
 void setDate(string year);

 void checkBookOut() { m_checkedOut = true; };
 void checkBookIn() { m_checkedOut = false; };

private:
 //m_ prefix to denote member variable
 Genre m_genre;
 string m_isbn, m_title, m_author;
 int m_date;
 bool m_checkedOut;
};

//overloads
bool operator==(const Book& a, const Book& b);
bool operator!=(const Book& a, const Book& b);
ostream& operator<<(ostream& os, const Book& book);


#endif // !_BOOKCLASS_H_

bookclass.cpp

//bookclass.cpp
#include "bookclass.h"

//--OVERLOAD OPERATORS--//

//compare ISBN numbers
bool operator==(const Book& a, const Book& b)
{
 return a.getISBN() == b.getISBN();
}

bool operator!=(const Book& a, const Book& b)
{
 return a.getISBN() != b.getISBN();
}

//print out book
ostream& operator<<(ostream& os, const Book& book)
{
 string genre;
 switch (book.getGenre())
 {
 case Genre::fiction:
  genre = "Fiction";
  break;
 case Genre::nonfiction:
  genre = "Non-Fiction";
  break;
 case Genre::periodical:
  genre = "Periodical";
  break;
 case Genre::biography:
  genre = "Biography";
  break;
 case Genre::children:
  genre = "Children";
  break;
 default:
  genre = "Default";
  break;
 }
 return os << "Title: "     << book.getTitle()  << endl
     << "Author: "  << book.getAuthor() << endl
     << "Genre: "     << genre            << endl
     << "Copyright: "  << book.getDate()   << endl
     << "ISBN: "  << book.getISBN()   << endl
     << "Checked out:" << book.getStatus() << endl
     << endl;
}

//constructor
Book::Book()
{
 m_genre = Genre::default;
 m_isbn = m_title = m_author = "";
 m_date = 0;
 m_checkedOut = false;
}

//deconstructor
Book::~Book() {}

//set the ISBN
void Book::setISBN(string num1, string num2, string num3, string char1)
{
 size_t check;
 //check first number
 check = num1.find_first_not_of("0123456789");
 while (check != string::npos)
 {
  cout << "Not a valid ISBN number, try again: ";
  cin >> num1;
  check = num1.find_first_not_of("0123456789");
 }

 //check second number
 check = num2.find_first_not_of("0123456789");
 while (check != string::npos)
 {
  cout << "Not a valid ISBN number, try again: ";
  cin >> num2;
  check = num2.find_first_not_of("0123456789");
 }

 //check third number
 check = num3.find_first_not_of("0123456789");
 while (check != string::npos)
 {
  cout << "Not a valid ISBN number, try again: ";
  cin >> num3;
  check = num3.find_first_not_of("0123456789");
 }

 //check last character
 check = char1.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXY");
 while (check != string::npos)
 {
  cout << "Not a valid ISBN digit or letter, try again: ";
  cin >> char1;
  check = char1.find_first_not_of("0123456789ABCDEFGHIJKLMNOPQRSTUVWXY");
 }

 //set isbn
 m_isbn = "";
 m_isbn.append(num1);
 m_isbn.append("-");
 m_isbn.append(num2);
 m_isbn.append("-");
 m_isbn.append(num3);
 m_isbn.append("-");
 m_isbn.append(char1);
}

//set and check the year of copyright
void Book::setDate(string year)
{
 size_t check;
 check = year.find_first_not_of("0123456789");
 while (check != string::npos || year.size() != 4)
 {
  cout << "Not a valid year try again: ";
  cin >> year;
  check = year.find_first_not_of("0123456789");
 }

 m_date = stoi(year);
}


patronclass.h

//patronclass.h
#ifndef _PATRONCLASS_H_
#define _PATRONCLASS_H_

#include "std_lib_facilities.h"

//class patron
class Patron
{
public:
 Patron();
 ~Patron();

 string getName() const { return m_name; }
 int getCardNum() const { return m_cardNum; }
 float getLibFees() const { return m_libFees; }
 bool isFeeOwed() const { return m_feeOwed; }

 void setName(string name) { m_name = name; }
 void setCardNum(int num) { m_cardNum = num; }
 void setFees(float fee);
 
private:
 string m_name;
 int m_cardNum;
 float m_libFees;
 bool m_feeOwed;
};

//helper functions
ostream& operator<<(ostream& os, const Patron& patron);

#endif // !_PATRONCLASS_H_

patronclass.cpp

//patronclass.cpp

#include "patronclass.h"

//constructor
Patron::Patron()
{
 m_name = "";
 m_cardNum = 0;
 m_libFees = 0.00f;
 m_feeOwed = false;
}

//deconstructor
Patron::~Patron() {}

//set fee
void Patron::setFees(float fee)
{
 m_libFees = fee;
 if (m_libFees <= 0)
  m_feeOwed = false;
 else
  m_feeOwed = true;
}

//print out patron
ostream& operator<<(ostream& os, const Patron& patron)
{
 string owed;
 if (patron.isFeeOwed())
  owed = "Yes";
 else
  owed = "No";

 return os << "Name: "   << patron.getName() << endl
     << "Card Number: " << patron.getCardNum() << endl
     << "Fees Owed?: "  << owed << endl
     << "Fee Amount: "  << patron.getLibFees() << endl
     << endl;
}

main.cpp

//main

#include "bookclass.h"
#include "patronclass.h"

int main()
{
 Book lotr;
 Patron user1;

 lotr.setTitle("Lord Of The Rings");
 lotr.setAuthor("J.R.R Tolkien");
 lotr.setGenre(Genre::fiction);
 lotr.setDate("1954");
 lotr.setISBN("1111", "1111", "1111", "J");
 cout << lotr;

 lotr.setISBN("123", "0", "4444", "6");
 lotr.checkBookOut();
 cout << lotr;

 user1.setName("Gandalph The Grey");
 user1.setCardNum(123456789);
 user1.setFees(1.23);
 cout << user1;

 user1.setFees(0.00f);
 cout << user1;

 keep_window_open();

 return 0;
}

For this exercise I separated everything into their own files just to clean the code up a little bit. It also means I'll only be posting files that are updated in the following exercises. 

No comments:

Post a Comment