From ca5552e13c935cc5860ba6258804d44565f0bc13 Mon Sep 17 00:00:00 2001 From: Holger Boerchers Date: Sat, 1 Dec 2018 22:36:11 +0100 Subject: [PATCH] nearly done... --- src/Ausgabenverwaltung.cpp | 139 +++++++++++++++++++------------------ 1 file changed, 71 insertions(+), 68 deletions(-) diff --git a/src/Ausgabenverwaltung.cpp b/src/Ausgabenverwaltung.cpp index 7ee5d68..6a37395 100644 --- a/src/Ausgabenverwaltung.cpp +++ b/src/Ausgabenverwaltung.cpp @@ -4,29 +4,19 @@ using namespace std; -enum category -{ - None = 0, - Tanken = 1, - Nahrungsmittel = 2, - Reinigungsmittel = 3, - Urlaub = 4, - Haustiere = 5, - Bekleidung = 6, - Wohnen = 7, - Versicherung = 8 -}; - -class Invoice +class invoice { private: int number_ = 0; double amount_; unsigned short month_ = 0; - category category_ = None; + int category_ = 0; public: - Invoice(int number, double amount, unsigned short month, category category) : number_(number), amount_(amount), - month_(month), category_(category) + static const char* categories[]; + static const char* months[]; + + invoice(int number, double amount, unsigned short month, int category) : number_(number), amount_(amount), + month_(month), category_(category) { } @@ -40,23 +30,23 @@ public: return month_; } - category get_category() const + int get_category() const { return category_; } void print() const { - cout << "Rechnung: " << number_ << endl; - cout << "Betrag: " << amount_ << endl; - cout << "Monat: " << month_ << endl; - cout << "Kategorie: " << category_ << endl; + cout << " Rechnung: " << number_ << endl; + cout << " Betrag: " << amount_ << "€" << endl; + cout << " Monat: " << months[month_] << endl; + cout << " Kategorie: " << categories[category_] << endl; } - /** - * \brief Implements the equality operator. - */ - bool operator ==(const Invoice& e) const + /** + * \brief Implements the equality operator. + */ + bool operator ==(const invoice& e) const { return number_ == e.number_; } @@ -64,10 +54,12 @@ public: /** * \brief Implements the inequality operator. */ - bool operator !=(const Invoice& e) const { return !operator==(e); } - + bool operator !=(const invoice& e) const { return !operator==(e); } }; +const char* invoice::categories[] = {"Tanken", "Nahrungsmittel", "Reinigungsmittel", "Urlaub", "Haustiere", "Bekleidung", "Wohnen", "Versicherung"}; +const char* invoice::months[] = {"Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}; + template T ask_question(const string& question) { @@ -78,20 +70,15 @@ T ask_question(const string& question) return value; } -list invoices; +list invoices; void create_invoice(); -void output_invoices(list sorted_invoices); +void output_invoices_order_by_month(); 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) +bool compare_month_ascending(const invoice& first, const invoice& second) { return first.get_month() < second.get_month(); } @@ -118,18 +105,10 @@ int main() create_invoice(); break; case 2: - { - auto temp = list(invoices); - temp.sort(compare_month_ascending); - output_invoices(temp); - } + output_invoices_order_by_month(); break; case 3: - { - auto temp = list(invoices); - temp.sort(compare_category_ascending); - output_invoices(temp); - } + //TODO break; case 4: delete_invoice(); @@ -145,36 +124,44 @@ int main() return 0; } -category get_category(); +int get_month(); +int get_category(); void create_invoice() { - cout << "Eine neue Rechnung erstellen." << endl << endl; + system("cls"); + cout << "Eine neue Rechnung erstellen." << endl; + cout << "------------------------------------"<("Bitte geben Sie den Betrag in Euro ein."); - const auto month = ask_question("Bitte geben Sie den Monat ein."); + const auto month = get_month(); const auto category = get_category(); - const auto i = Invoice(invoice_no, amount, month, category); + const auto i = invoice(invoice_no, amount, month, category); invoices.push_back(i); } -void output_invoices(const list sorted_invoices) +void output_invoices_order_by_month() { - for (auto invoice : sorted_invoices) + system("cls"); + cout << "Alle Rechnungen nach Monat sortiert:"<(invoices); + temp.sort(compare_month_ascending); + for (auto invoice : temp) { invoice.print(); - cout << "--------------" << endl; + cout << "------------------------------------" << endl; } system("pause"); } void delete_invoice() { - auto invoice_number = ask_question("Bitte geben Sie die Rechnungsnummer ein"); + const auto invoice_number = ask_question("Bitte geben Sie die Rechnungsnummer ein"); for (auto invoice : invoices) { - if(invoice.get_number() == invoice_number) + if (invoice.get_number() == invoice_number) { invoices.remove(invoice); return; @@ -184,23 +171,39 @@ void delete_invoice() system("pause"); } -category get_category() +int get_month() { while (true) { - cout << "Bitte w\x84hlen sie eine Kategorie aus:" << endl; - cout << " 1: Tanken" << endl; - cout << " 2: Nahrungsmittel" << endl; - cout << " 3: Reinigungsmittel" << endl; - cout << " 4: Urlaub" << endl; - cout << " 5: Haustiere" << endl; - cout << " 6: Bekleidung" << endl; - cout << " 7: Wohnen" << endl; - cout << " 8: Versicherung" << endl; - auto cat = ask_question(""); - if (cat > 0 && cat <= 8) + cout << "Bitte w\x84hlen sie einen Monat aus:" << endl; + auto index = 0; + for (auto month : invoice::months) { - return static_cast(cat); + cout << ++index << ": " << month << endl; + } + const auto answer = ask_question(""); + if (answer > 0 && answer <= 12) + { + return answer - 1; + } + cout << "Ung\x81ltige Eingabe" << endl << endl; + } +} + +int get_category() +{ + while (true) + { + cout << "Bitte w\x84hlen sie eine Kategorie aus:" << endl; + auto index = 0; + for (auto category : invoice::categories) + { + cout << ++index << ": " << category << endl; + } + const auto answer = ask_question(""); + if (answer > 0 && answer <= 8) + { + return answer - 1; } cout << "Ung\x81ltige Eingabe" << endl << endl; }