From 40089ad861dec3fca5a58bf2c909fb0e3f8ea551 Mon Sep 17 00:00:00 2001 From: quentin Date: Sat, 17 Sep 2022 17:47:54 -0500 Subject: [PATCH] payment now stores a pointer to a negative value --- src/data/dateMoney.cpp | 4 ++-- src/main.cpp | 17 +++++++++++++---- src/money/account.cpp | 2 +- src/money/payment.cpp | 8 +++++++- src/money/payment.h | 4 ++++ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/data/dateMoney.cpp b/src/data/dateMoney.cpp index a085943..76dc855 100644 --- a/src/data/dateMoney.cpp +++ b/src/data/dateMoney.cpp @@ -24,8 +24,8 @@ bool DateMoney::operator>=(const DateMoney &rhs) const { } std::ostream &operator<<(std::ostream &os, const DateMoney &money) { - os << "value: " << *money.value << " date: " << money.date->tm_mon + 1 << "/" << money.date->tm_mday << "/" - << money.date->tm_year; + os << "Value: " << *money.value << " Date: " << money.date->tm_mon + 1 << "/" << money.date->tm_mday << "/" + << money.date->tm_year+1900; return os; } diff --git a/src/main.cpp b/src/main.cpp index 351d998..2642a69 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -74,11 +74,20 @@ int main(int argc, char *argv[]) { if (a != accounts.end()) { int value = a->second.getAccount()->getValue(); std::vector timeline = a->second.getAccount()->getTimeline(); - printf("Account value: %n\n", &value); - printf("Last 10 payments:\n"); - for (auto transaction : timeline) { - + printf("Account value: %d\n", value); + printf("Last 10 transactions:\n"); + for (int i = 0; i < timeline.size() && i < 10; i++) { + const int *amount = timeline[i].getValue(); + const tm *date = timeline[i].getDate(); + if (*amount <= 0) { + printf("Value: \033[31m%d\033[0m, ", *amount); + } + else { + printf("Value: \033[32m%d\033[0m, ", *amount); + } + printf("Date: %d/%d/%d ", date->tm_mon+1, date->tm_mday, date->tm_year+1900); } + std::cout << std::endl; } } if (accountOptHandler.getSetOpts()->del) { diff --git a/src/money/account.cpp b/src/money/account.cpp index 792a724..1b0e28f 100644 --- a/src/money/account.cpp +++ b/src/money/account.cpp @@ -29,7 +29,7 @@ int Account::getValue() { std::vector Account::getTimeline() { std::vector timeline; for (auto &payment : payments) { - timeline.emplace_back(&payment.value, payment.getDate()); + timeline.emplace_back(payment.getValue(), payment.getDate()); } for (auto &earning : earnings) { timeline.emplace_back(&earning.value, earning.getDate()); diff --git a/src/money/payment.cpp b/src/money/payment.cpp index f695464..cf84ead 100644 --- a/src/money/payment.cpp +++ b/src/money/payment.cpp @@ -6,8 +6,14 @@ #include -Payment::Payment(const int value, Receipt receipt, std::tm date) : Transaction(value, date), receipt(std::move(receipt)) {} +Payment::Payment(const int value, Receipt receipt, std::tm date) : Transaction(value, date), receipt(std::move(receipt)) { + negativeValue = -value; +} Receipt &Payment::getReceipt() { return receipt; } + +const int *Payment::getValue() { + return &negativeValue; +} diff --git a/src/money/payment.h b/src/money/payment.h index 27875cb..677675d 100644 --- a/src/money/payment.h +++ b/src/money/payment.h @@ -15,8 +15,12 @@ public: Receipt &getReceipt(); + const int *getValue(); + private: Receipt receipt; + + int negativeValue; };