Last active
April 19, 2018 01:25
-
-
Save jessiewestlake/eb568f0198f4a579e44016b1619adc9e to your computer and use it in GitHub Desktop.
Book Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bookType.h" | |
#include <iostream> | |
#include <iomanip> | |
bookType::bookType() | |
{ | |
price = 0.0; | |
year = 0; | |
} | |
bookType::bookType(string title, string ISBN, string publisher, int year, double price, int stock) | |
{ | |
setTitle(title); | |
setISBN(ISBN); | |
setPublisher(publisher); | |
setYear(year); | |
setPrice(price); | |
setStockCount(stock); | |
} | |
// MEMBER MUTATORS | |
void bookType::setTitle(string title) | |
{ | |
this->title = title; | |
} | |
void bookType::addAuthor(string author) | |
{ | |
this->authors.push_back(author); | |
} | |
void bookType::addAuthor(string author1, string author2) | |
{ | |
this->authors.insert(authors.end(), { author1, author2 }); | |
} | |
void bookType::addAuthor(string author1, string author2, string author3) | |
{ | |
this->authors.insert(authors.end(), { author1, author2, author3 }); | |
} | |
void bookType::addAuthor(string author1, string author2, string author3, string author4) | |
{ | |
this->authors.insert(authors.end(), { author1, author2, author3, author4 }); | |
} | |
void bookType::setPublisher(string publisher) | |
{ | |
this->publisher = publisher; | |
} | |
void bookType::setISBN(string ISBN) | |
{ | |
if (ISBN.find_first_not_of("0123456789-") == string::npos) | |
{ | |
this->ISBN = ISBN; | |
} | |
else | |
{ | |
cout << "Invalid ISBN" << endl; | |
} | |
} | |
void bookType::setPrice(double price) | |
{ | |
this->price = price; | |
} | |
void bookType::setYear(int year) | |
{ | |
if (year > 1800 && year < 2020) | |
{ | |
this->year = year; | |
} | |
else | |
{ | |
cout << "Invalid year" << endl; | |
} | |
} | |
void bookType::setStockCount(int count) | |
{ | |
if (count >= 0) | |
{ | |
this->stockCount = count; | |
} | |
} | |
// MEMBER ACCESSORS | |
string bookType::getTitle() | |
{ | |
return title; | |
} | |
string bookType::getAuthors() | |
{ | |
string firstName; | |
string lastName; | |
int position; | |
string output; | |
for (string author : authors) | |
{ | |
position = author.find(','); | |
firstName = author.substr(position + 2); | |
lastName = author.substr(0, position); | |
output += firstName + " " + lastName; | |
} | |
output = output + "\n"; | |
return output; | |
} | |
string bookType::getPublisher() | |
{ | |
return publisher; | |
} | |
string bookType::getISBN() | |
{ | |
return ISBN; | |
} | |
double bookType::getPrice() | |
{ | |
return price; | |
} | |
int bookType::getYear() | |
{ | |
return year; | |
} | |
int bookType::getStockCount() | |
{ | |
return stockCount; | |
} | |
void bookType::print() | |
{ | |
cout << "Title: " << title << endl; | |
cout << "Author(s): " << getAuthors() << endl; | |
cout << "Year: " << year; | |
cout << "ISBN: " << ISBN << endl; | |
cout << "Publisher: " << publisher << endl; | |
cout << "Price: $" << setprecision(2) << fixed << price << endl; | |
cout << "Stock on hand: " << stockCount << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment