Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Transaction.h"
- #include <iomanip>
- #include <limits>
- #include <ctime>
- #define DESCRIPTION_WIDTH 20
- #define DATE_WIDTH 10
- #define AMOUNT_WIDTH 9
- /** Transaction class is fairly self explanatory
- * It holds a description of the transaction, the amount,
- * and date.
- * Overloads io operators as needed to read/write to/from stdio/file
- * Overloads comparison operators as needed to determine if a Transaction
- * is another Transaction
- **/
- Transaction::Transaction(const std::string& d, const double a,
- const unsigned da, const unsigned mo, const unsigned yr)
- : description(d), amount(a), day(da), month(mo), year(yr) { }
- std::istream& operator>>(std::istream& in, Transaction& t) {
- std::string des;
- double amt;
- unsigned day, month, year;
- char today;
- struct tm* date;
- std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
- std::cout << "Descriptio: ";
- std::getline(in, des, '\n');
- std::cout << "Quantus: ";
- in >> amt;
- std::cout << "Hodie (y/n): ";
- std::cin >> today;
- if(today == 'n') {
- std::cout << "Dies Mensa Annus: ";
- in >> day >> month >> year;
- }
- else {
- time_t t = time(0);
- date = localtime(&t);
- day = date->tm_mday;
- month = date->tm_mon + 1;
- year = date->tm_year + 1900;
- }
- t = Transaction(des, amt, day, month, year);
- in.get();
- return in;
- }
- std::ostream& operator<<(std::ostream& out, const Transaction& t) {
- out.width(DESCRIPTION_WIDTH); out << std::left << t.GetDescription();
- out.width(AMOUNT_WIDTH); out << std::left << t.GetAmount();
- out.width(DATE_WIDTH); out << std::right << t.GetDay() << '/'
- << t.GetMonth() << '/' << t.GetYear();
- return out;
- }
- std::ofstream& operator<<(std::ofstream& of, const Transaction& t) {
- of << t.GetDescription() << '\n'
- << t.GetAmount() << ' '
- << t.GetDay() << ' '
- << t.GetMonth() << ' '
- << t.GetYear() << '\n';
- return of;
- }
- std::ifstream& operator>>(std::ifstream& in, Transaction& t) {
- std::string des;
- double amt;
- unsigned day, month, year;
- std::getline(in, des, '\n');
- in >> amt >> day >> month >> year;
- t = Transaction(des, amt, day, month, year);
- in.get();
- return in;
- }
- bool operator==(const Transaction& left, const Transaction& right) {
- if(!left.GetDescription().compare(right.GetDescription())&&
- left.GetDay() == right.GetDay() &&
- left.GetMonth() == right.GetMonth() &&
- left.GetYear() == right.GetYear() ) {
- return true;
- }
- else {
- return false;
- }
- }
- bool operator<(const Transaction& left, const Transaction& right) {
- if(left.GetYear() > right.GetYear()) {
- return false;
- }
- else
- if(left.GetYear() < right.GetYear()) {
- return true;
- }
- else {
- if(left.GetMonth() > right.GetMonth()) {
- return false;
- }
- else
- if(left.GetMonth() < right.GetMonth()) {
- return true;
- }
- else {
- if(left.GetDay() > right.GetDay()) {
- return false;
- }
- else
- if(left.GetDay() < right.GetDay()) {
- return true;
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement