Advertisement
Hikigaya8man

bookstore

Apr 16th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iomanip>
  5. #include <cstdlib>
  6. #include <sstream>
  7.  
  8. using namespace std;
  9.  
  10. class CSales {
  11. public:
  12.     CSales () {}
  13.     CSales (string i, int p, int c) {
  14.         this->m_isbn = i;
  15.         this->m_price = p;
  16.         this->m_count = c;
  17.         this->m_revenue = p*c;
  18.     }
  19.     friend ostream & operator << ( ostream &os, const CSales &x ); //const;
  20.     friend istream & operator >> ( istream &is, const CSales &x );
  21.  
  22.     string m_isbn;
  23.     int m_price;
  24.     int m_count;
  25.     int m_revenue;
  26.  
  27. };
  28. ostream & operator << ( ostream &os, const CSales &x ) {
  29.     os << "isbn: " << x.m_isbn << endl;
  30.     os << "price: " << x.m_price << endl;
  31.     os << "sold: " << x.m_count << "x." << endl;
  32.     os << "____________" << endl;
  33.  
  34.     return os;
  35. }
  36. istream & operator >> ( istream &is, const CSales &x ) {
  37.     if ( is >> x.m_isbn >> x.m_price >> x.m_count ) {
  38.         x.m_revenue = x.m_count*x.m_price;
  39.     }
  40.  
  41.     return is;
  42. }
  43.  
  44. bool cmp_isbn(const CSales *a, const CSales *b) {
  45.     return (a->m_isbn < b->m_isbn);
  46. }
  47.  
  48. int main() {
  49.     vector<CSales*> dbs1;
  50.     CSales *candidate = new CSales();
  51.  
  52.     cout << "start entering transactions (isbn, price, quantity):" << endl;
  53.  
  54.     while ( cin >> *candidate ) {
  55.         //sorted by isbn
  56.         auto iter = lower_bound(dbs1.begin(), dbs1.end(), candidate, cmp_isbn);
  57.         //found book in database
  58.         if ( iter != dbs1.end() && (*iter)->m_isbn == candidate->m_isbn ) {
  59.             (*iter)->m_count += candidate->m_count;
  60.             (*iter)->m_revenue += candidate->m_count * candidate->m_price;
  61.         } else {
  62.             //add book to database
  63.             dbs1.insert(iter, candidate);
  64.         }
  65.     }
  66.  
  67.     for (auto it : dbs1) {
  68.         cout << it;
  69.     }
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement