From 34faba5bd91b207c48d772f288dab3d952705d03 Mon Sep 17 00:00:00 2001 From: Holger Boerchers Date: Fri, 30 Nov 2018 21:52:31 +0100 Subject: [PATCH] more useful functionality --- src/Ausgabenverwaltung.cpp | 100 +++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 14 deletions(-) diff --git a/src/Ausgabenverwaltung.cpp b/src/Ausgabenverwaltung.cpp index 1e2b482..7ee5d68 100644 --- a/src/Ausgabenverwaltung.cpp +++ b/src/Ausgabenverwaltung.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include @@ -26,7 +26,7 @@ private: category category_ = None; public: Invoice(int number, double amount, unsigned short month, category category) : number_(number), amount_(amount), - month_(month), category_(category) + month_(month), category_(category) { } @@ -34,6 +34,38 @@ public: { return number_; } + + unsigned short get_month() const + { + return month_; + } + + category get_category() const + { + return category_; + } + + void print() const + { + cout << "Rechnung: " << number_ << endl; + cout << "Betrag: " << amount_ << endl; + cout << "Monat: " << month_ << endl; + cout << "Kategorie: " << category_ << endl; + } + + /** + * \brief Implements the equality operator. + */ + bool operator ==(const Invoice& e) const + { + return number_ == e.number_; + } + + /** + * \brief Implements the inequality operator. + */ + bool operator !=(const Invoice& e) const { return !operator==(e); } + }; template @@ -50,9 +82,19 @@ list invoices; void create_invoice(); -void output_invoice_per_month(); +void output_invoices(list sorted_invoices); -void output_invoice_per_category(); +void delete_invoice(); + +bool compare_category_ascending(const Invoice& first, const Invoice& second) +{ + return first.get_category() < second.get_category(); +} + +bool compare_month_ascending(const Invoice& first, const Invoice& second) +{ + return first.get_month() < second.get_month(); +} int main() { @@ -66,8 +108,9 @@ int main() cout << "2: Alle Ausgaben, sortiert nach Monat, ausgeben" << endl; cout << "3: Summe der Ausgaben pro Kategorie ausgeben" << endl; cout << "4: Rechnung l\x94schen" << endl; + cout << endl; cout << "0: Programm beenden" << endl; - auto input = ask_question("Ihre Eingabe:"); + const auto input = ask_question("Ihre Eingabe:"); switch (input) { @@ -75,10 +118,21 @@ int main() create_invoice(); break; case 2: - output_invoice_per_month(); + { + auto temp = list(invoices); + temp.sort(compare_month_ascending); + output_invoices(temp); + } break; case 3: - output_invoice_per_category(); + { + auto temp = list(invoices); + temp.sort(compare_category_ascending); + output_invoices(temp); + } + break; + case 4: + delete_invoice(); break; case 0: end_program = true; @@ -96,20 +150,38 @@ category get_category(); void create_invoice() { cout << "Eine neue Rechnung erstellen." << endl << endl; - auto invoice_no = invoices.empty() ? 1 : invoices.back().get_number() + 1; - auto amount = ask_question("Bitte geben Sie den Betrag in Euro ein."); - auto month = ask_question("Bitte geben Sie den Monat ein."); + const auto invoice_no = invoices.empty() ? 1 : invoices.back().get_number() + 1; + const auto amount = ask_question("Bitte geben Sie den Betrag in Euro ein."); + const auto month = ask_question("Bitte geben Sie den Monat ein."); const auto category = get_category(); const auto i = Invoice(invoice_no, amount, month, category); invoices.push_back(i); } -void output_invoice_per_month() + +void output_invoices(const list sorted_invoices) { + for (auto invoice : sorted_invoices) + { + invoice.print(); + cout << "--------------" << endl; + } + system("pause"); } -void output_invoice_per_category() +void delete_invoice() { + auto invoice_number = ask_question("Bitte geben Sie die Rechnungsnummer ein"); + for (auto invoice : invoices) + { + if(invoice.get_number() == invoice_number) + { + invoices.remove(invoice); + return; + } + } + cout << "Rechnung nicht gefunden." << endl; + system("pause"); } category get_category() @@ -130,6 +202,6 @@ category get_category() { return static_cast(cat); } - cout << "Ung\x81ltige Eingabe"; + cout << "Ung\x81ltige Eingabe" << endl << endl; } -} \ No newline at end of file +}