Advertisement
babbles

Transaction.h

Sep 15th, 2014
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include "Transaction.h"
  2.  
  3. #include <iomanip>
  4. #include <limits>
  5. #include <ctime>
  6.  
  7. #define DESCRIPTION_WIDTH 20
  8. #define DATE_WIDTH        10
  9. #define AMOUNT_WIDTH       9
  10.  
  11. /** Transaction class is fairly self explanatory
  12.  * It holds a description of the transaction, the amount,
  13.  * and date.
  14.  *  Overloads io operators as needed to read/write to/from stdio/file
  15.  *  Overloads comparison operators as needed to determine if a Transaction
  16.  * is another Transaction
  17. **/
  18.  
  19. Transaction::Transaction(const std::string& d, const double a,
  20.                          const unsigned da, const unsigned mo, const unsigned yr)
  21.     : description(d), amount(a), day(da), month(mo), year(yr) { }
  22.  
  23. std::istream& operator>>(std::istream& in, Transaction& t) {
  24.     std::string des;
  25.     double amt;
  26.     unsigned day, month, year;
  27.     char today;
  28.     struct tm* date;
  29.  
  30.     std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  31.     std::cout << "Descriptio: ";
  32.     std::getline(in, des, '\n');
  33.     std::cout << "Quantus: ";
  34.     in >> amt;
  35.     std::cout << "Hodie (y/n): ";
  36.     std::cin  >> today;
  37.     if(today == 'n') {
  38.         std::cout << "Dies Mensa Annus: ";
  39.         in >> day >> month >> year;
  40.     }
  41.     else {
  42.         time_t t = time(0);
  43.         date  = localtime(&t);
  44.         day   = date->tm_mday;
  45.         month = date->tm_mon + 1;
  46.         year  = date->tm_year + 1900;
  47.     }
  48.  
  49.     t = Transaction(des, amt, day, month, year);
  50.  
  51.     in.get();
  52.  
  53.     return in;
  54. }
  55.  
  56. std::ostream& operator<<(std::ostream& out, const Transaction& t) {
  57.     out.width(DESCRIPTION_WIDTH); out << std::left    << t.GetDescription();
  58.     out.width(AMOUNT_WIDTH);      out << std::left    << t.GetAmount();
  59.     out.width(DATE_WIDTH);        out << std::right   << t.GetDay() << '/'
  60.                                       << t.GetMonth() << '/' << t.GetYear();
  61.     return out;
  62. }
  63.  
  64. std::ofstream& operator<<(std::ofstream& of, const Transaction& t) {
  65.     of << t.GetDescription() << '\n'
  66.        << t.GetAmount()      << ' '
  67.        << t.GetDay()         << ' '
  68.        << t.GetMonth()       << ' '
  69.        << t.GetYear()        << '\n';
  70.  
  71.     return of;
  72. }
  73.  
  74. std::ifstream& operator>>(std::ifstream& in, Transaction& t) {
  75.     std::string des;
  76.     double amt;
  77.     unsigned day, month, year;
  78.  
  79.     std::getline(in, des, '\n');
  80.     in >> amt >> day >> month >> year;
  81.  
  82.     t = Transaction(des, amt, day, month, year);
  83.  
  84.     in.get();
  85.  
  86.     return in;
  87. }
  88.  
  89. bool operator==(const Transaction& left, const Transaction& right) {
  90.     if(!left.GetDescription().compare(right.GetDescription())&&
  91.        left.GetDay()         == right.GetDay()               &&
  92.        left.GetMonth()       == right.GetMonth()             &&
  93.        left.GetYear()        == right.GetYear()               ) {
  94.  
  95.         return true;
  96.     }
  97.     else {
  98.         return false;
  99.     }
  100. }
  101.  
  102. bool operator<(const Transaction& left, const Transaction& right) {
  103.     if(left.GetYear() > right.GetYear()) {
  104.         return false;
  105.     }
  106.     else
  107.     if(left.GetYear() < right.GetYear()) {
  108.         return true;
  109.     }
  110.     else {
  111.         if(left.GetMonth() > right.GetMonth()) {
  112.             return false;
  113.         }
  114.         else
  115.         if(left.GetMonth() < right.GetMonth()) {
  116.             return true;
  117.         }
  118.         else {
  119.             if(left.GetDay() > right.GetDay()) {
  120.                 return false;
  121.             }
  122.             else
  123.             if(left.GetDay() < right.GetDay()) {
  124.                 return true;
  125.             }
  126.         }
  127.     }
  128.     return false;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement