Created
October 28, 2015 18:28
-
-
Save krysseltillada/494565fa59f4a8bcd7f0 to your computer and use it in GitHub Desktop.
SearchBook class
This file contains hidden or 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 "SearchBook.hpp" | |
int main () | |
{ | |
std::vector <Sales_data> tmpData; | |
std::vector <std::vector <Sales_data> > files; | |
std::string book; | |
double revenue; | |
int units_sold; | |
char key; | |
while (std::cin >> book) { | |
std::cin >> revenue; | |
std::cin >> units_sold; | |
tmpData.push_back (Sales_data (book, units_sold, revenue)); | |
files.insert (files.begin (), tmpData); | |
std::cout << "exit? y or n" << std::endl; | |
std::cin >> key; | |
if (key == 'y') break; | |
tmpData.clear (); | |
} | |
book.clear(); | |
while (std::cin >> book) { | |
SearchBook search (files, book); | |
search.report(); | |
} | |
return 0; | |
} |
This file contains hidden or 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 "SearchBook.hpp" | |
double Sales_data::avg_price () const { | |
return units_sold / revenue; | |
} | |
Sales_data &Sales_data::operator += (const Sales_data &lhs) { | |
units_sold += lhs.units_sold; | |
revenue += lhs.revenue; | |
return *this; | |
} | |
void Sales_data::operator -= (const Sales_data &lhs) { | |
this->units_sold -= lhs.units_sold; | |
this->revenue -= lhs.revenue; | |
} | |
void Sales_data::operator *= (const Sales_data &lhs) { | |
this->units_sold *= lhs.units_sold; | |
this->revenue *= lhs.revenue; | |
} | |
void Sales_data::operator /= (const Sales_data &lhs) { | |
this->units_sold /= lhs.units_sold; | |
this->revenue /= lhs.revenue; | |
} | |
Sales_data operator + (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data sum = lhs; | |
sum += rhs; | |
return sum; | |
} | |
Sales_data operator - (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data diff = lhs; | |
diff -= rhs; | |
return diff; | |
} | |
Sales_data operator * (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data product = lhs; | |
product *= rhs; | |
return product; | |
} | |
Sales_data operator / (const Sales_data &lhs, const Sales_data &rhs) { | |
Sales_data quotient = lhs; | |
quotient /= lhs; | |
return quotient; | |
} | |
Sales_data &Sales_data::operator = (const Sales_data &c_obj) { | |
this->bookNo = c_obj.bookNo; | |
this->units_sold = c_obj.units_sold; | |
this->revenue = c_obj.revenue; | |
return *this; | |
} | |
std::ostream &operator << (std::ostream &out, const Sales_data &rho) { | |
out << rho.isbn () << " " << rho.units_sold << " " | |
<< rho.revenue << " " << rho.avg_price(); | |
return out; | |
} | |
std::istream &operator >> (std::istream &in, Sales_data &rho) { | |
double t_price; | |
in >> rho.bookNo >> rho.units_sold >> t_price; | |
if (in) | |
rho.revenue = rho.units_sold * t_price; | |
else | |
rho = Sales_data (); | |
return in; | |
} | |
This file contains hidden or 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
#ifndef SALES_DATA_H | |
#define SALES_DATA_H | |
#include <string> | |
#include <iostream> | |
class Sales_data { | |
friend class std::hash <Sales_data>; | |
friend Sales_data add(const Sales_data&, const Sales_data&); | |
friend std::ostream &print(std::ostream&, const Sales_data&); | |
friend std::istream &read(std::istream&, Sales_data&); | |
friend std::ostream &operator << (std::ostream &, const Sales_data &); | |
friend std::istream &operator >> (std::istream &, Sales_data &); | |
friend Sales_data operator + (const Sales_data &, const Sales_data &); | |
friend Sales_data operator - (const Sales_data &, const Sales_data &); | |
friend Sales_data operator * (const Sales_data &, const Sales_data &); | |
friend Sales_data operator / (const Sales_data &, const Sales_data &); | |
public: | |
Sales_data() : | |
bookNo (std::string ("")), units_sold (0), revenue (0) { } | |
Sales_data(const std::string &s): | |
bookNo(s) { | |
} | |
Sales_data(const std::string &s, unsigned n, double p): | |
bookNo(s), units_sold(n), revenue(p*n) { | |
} | |
Sales_data(std::istream &); | |
std::string isbn() const { | |
return bookNo; | |
} | |
Sales_data &operator = (const Sales_data &); | |
Sales_data &operator += (const Sales_data &); | |
void operator -= (const Sales_data &); | |
void operator *= (const Sales_data &); | |
void operator /= (const Sales_data &); | |
Sales_data& combine(const Sales_data&); | |
double avg_price() const; | |
private: | |
std::string bookNo; | |
unsigned units_sold = 0; | |
double revenue = 0.0; | |
}; | |
#endif |
This file contains hidden or 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 "SearchBook.hpp" | |
SearchBook::SearchBook (const std::vector <std::vector <Sales_data> > &files, const std::string &book) : | |
TransactionFiles (files), bookName (book) { | |
this->DataTransaction = findBook (); | |
} | |
SearchBook::SearchBook (const std::vector <std::vector <Sales_data> > &files) : | |
TransactionFiles (files) { | |
this->DataTransaction = findBook (files); | |
} | |
SearchBook::SearchBook (const std::string & book) : | |
bookName (book) { | |
this->DataTransaction = findBook (this->TransactionFiles); | |
} | |
SearchBook::SearchBook (const SearchBook &getObj) : | |
DataTransaction (getObj.DataTransaction), TransactionFiles (getObj.TransactionFiles), bookName (getObj.bookName) { } | |
SearchBook::SearchBook (SearchBook &&mObj) : | |
DataTransaction (mObj.DataTransaction), TransactionFiles (mObj.TransactionFiles), bookName (mObj.bookName) { } | |
SearchBook::tupleInfo SearchBook::findBook () { | |
tupleInfo tmp; | |
for (std::vector <std::vector <Sales_data> >::const_iterator it = TransactionFiles.cbegin (); | |
it != TransactionFiles.cend (); ++it) | |
{ | |
std::pair <std::vector <Sales_data>::const_iterator, | |
std::vector <Sales_data>::const_iterator> foundBook = std::equal_range (it->cbegin (), it->cend (), this->bookName, compareIsbn); | |
if (foundBook.first != foundBook.second) { | |
tmp.push_back (std::make_tuple (it - TransactionFiles.cbegin (), foundBook.first, foundBook.second)); | |
} | |
} | |
return tmp; | |
} | |
SearchBook::tupleInfo SearchBook::findBook (const std::vector <std::vector <Sales_data> > &files) { | |
if (!files.empty ()) | |
this->TransactionFiles = files; | |
tupleInfo tmp; | |
for (std::vector <std::vector <Sales_data> >::const_iterator it = TransactionFiles.cbegin (); | |
it != TransactionFiles.cend (); ++it) | |
{ | |
std::pair <std::vector <Sales_data>::const_iterator, | |
std::vector <Sales_data>::const_iterator> foundBook = std::equal_range (it->cbegin (), it->cend (), this->bookName, compareIsbn); | |
if (foundBook.first != foundBook.second) { | |
tmp.push_back (std::make_tuple (it - TransactionFiles.cbegin (), foundBook.first, foundBook.second)); | |
} | |
} | |
return tmp; | |
} | |
SearchBook::tupleInfo SearchBook::findBook (const std::string &book) { | |
if (!book.empty ()) | |
this->bookName = book; | |
tupleInfo tmp; | |
for (std::vector <std::vector <Sales_data> >::const_iterator it = TransactionFiles.cbegin (); | |
it != TransactionFiles.cend (); ++it) | |
{ | |
std::pair <std::vector <Sales_data>::const_iterator, | |
std::vector <Sales_data>::const_iterator> foundBook = std::equal_range (it->cbegin (), it->cend (), this->bookName, compareIsbn); | |
if (foundBook.first != foundBook.second) { | |
tmp.push_back (std::make_tuple (it - TransactionFiles.cbegin (), foundBook.first, foundBook.second)); | |
} | |
} | |
return tmp; | |
} | |
SearchBook &SearchBook::report () const { | |
if (DataTransaction.empty()) { | |
std::cout << "book: " << this->bookName << "does not found in any stores" << std::endl; | |
return *const_cast <SearchBook *> (this); | |
} | |
for (tupleElem e : DataTransaction) { | |
std::cout << "book: " << std::get <0> (e) << std::endl | |
<< "sales: " << std::accumulate (std::get <1> (e), | |
std::get <2> (e), Sales_data (this->bookName)) << std::endl; | |
} | |
return *const_cast <SearchBook *> (this); | |
} | |
This file contains hidden or 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
/** | |
@Author: Kryssel Tillada | |
@Date : 10/29/15 | |
@exercise section 17.1.2 | |
*/ | |
#ifndef SEARCH_BOOK_HEADER | |
#define SEARCH_BOOK_HEADER | |
#include <tuple> | |
#include <vector> | |
#include <algorithm> | |
#include <utility> | |
#include "Sales_data.hpp" | |
class SearchBook { | |
public: | |
typedef std::vector <std::tuple <size_t, | |
std::vector <Sales_data>::const_iterator, | |
std::vector <Sales_data>::const_iterator> > tupleInfo; | |
typedef std::tuple <size_t, | |
std::vector <Sales_data>::const_iterator, | |
std::vector <Sales_data>::const_iterator> tupleElem; | |
public: | |
SearchBook () = default; | |
SearchBook (const std::vector <std::vector <Sales_data> > &, const std::string &); | |
SearchBook (const std::vector <std::vector <Sales_data> > &); | |
SearchBook (const std::string &); | |
SearchBook (const SearchBook &); | |
SearchBook (SearchBook &&); | |
~SearchBook () = default; | |
SearchBook &report () const; | |
private: | |
tupleInfo findBook (); | |
tupleInfo findBook (const std::vector <std::vector <Sales_data> > &); | |
tupleInfo findBook (const std::string &); | |
private: | |
tupleInfo DataTransaction; | |
std::vector <std::vector <Sales_data> > TransactionFiles; | |
std::string bookName; | |
}; | |
inline | |
bool compareIsbn(const Sales_data &lhs, const Sales_data &rhs) | |
{ | |
return lhs.isbn() < rhs.isbn(); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment