diff --git a/src/Ausgabenverwaltung.cpp b/src/Ausgabenverwaltung.cpp index 4597085..2ba124f 100644 --- a/src/Ausgabenverwaltung.cpp +++ b/src/Ausgabenverwaltung.cpp @@ -11,104 +11,104 @@ using namespace std; class invoice { private: - int number_ = 0; - double amount_; - unsigned short month_ = 0; - int category_ = 0; + int number_ = 0; + double amount_; + unsigned short month_ = 0; + int category_ = 0; public: - /** - * \brief collection of categories - */ - static const char* categories[]; - /** - * \brief collection of months - */ - static const char* months[]; + /** + * \brief collection of categories + */ + static const char* categories[]; + /** + * \brief collection of months + */ + static const char* months[]; - /** - * \brief Creates a new instance of invoice - */ - invoice(const int number, const double amount, const unsigned short month, const int category) : number_(number), - amount_(amount), - month_(month), - category_(category) - { - } + /** + * \brief Creates a new instance of invoice + */ + invoice(const int number, const double amount, const unsigned short month, const int category) : number_(number), + amount_(amount), + month_(month), + category_(category) + { + } - /** - * \brief Get number of invoice. - * \return current number - */ - int get_number() const - { - return number_; - } + /** + * \brief Get number of invoice. + * \return current number + */ + int get_number() const + { + return number_; + } - /** - * \brief get month of invoice - * \return month as index - */ - unsigned short get_month() const - { - return month_; - } + /** + * \brief get month of invoice + * \return month as index + */ + unsigned short get_month() const + { + return month_; + } - /** - * \brief get category of invoice - * \return category as index - */ - int get_category() const - { - return category_; - } + /** + * \brief get category of invoice + * \return category as index + */ + int get_category() const + { + return category_; + } - /** - * \brief get amount of invoice - * \return amount in euro - */ - double get_amount() const - { - return amount_; - } + /** + * \brief get amount of invoice + * \return amount in euro + */ + double get_amount() const + { + return amount_; + } - /** - * \brief Implements the equality operator. - */ - bool operator ==(const invoice& e) const - { - return number_ == e.number_; - } + /** + * \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); } + /** + * \brief Implements the inequality operator. + */ + bool operator !=(const invoice& e) const { return !operator==(e); } }; const char* invoice::categories[] = { - "Tanken", - "Nahrungsmittel", - "Reinigungsmittel", - "Urlaub", - "Haustiere", - "Bekleidung", - "Wohnen", - "Versicherung" + "Tanken", + "Nahrungsmittel", + "Reinigungsmittel", + "Urlaub", + "Haustiere", + "Bekleidung", + "Wohnen", + "Versicherung" }; const char* invoice::months[] = { - "Januar", - "Februar", - "M\x84rz", - "April", - "Mai", - "Juni", - "Juli", - "August", - "September", - "Oktober", - "November", - "Dezember" + "Januar", + "Februar", + "M\x84rz", + "April", + "Mai", + "Juni", + "Juli", + "August", + "September", + "Oktober", + "November", + "Dezember" }; /** @@ -120,11 +120,11 @@ const char* invoice::months[] = { template T ask_question(const string& question) { - cout << question << endl; - cout << "> "; - T value; - cin >> value; - return value; + cout << question << endl; + cout << "> "; + T value; + cin >> value; + return value; } void create_invoice(); @@ -142,7 +142,7 @@ void sum_invoices_per_category(); */ bool compare_month_ascending(const invoice& first, const invoice& second) { - return first.get_month() < second.get_month(); + return first.get_month() < second.get_month(); } /** @@ -159,144 +159,159 @@ int invoice_serial_number; */ int main() { - while (true) - { - system("cls"); - cout << "Ausgabenverwaltung (" << invoices.size() << " Rechnungen)" << endl << endl; - cout << "Bitte w\x84hlen Sie eine Option:" << endl; - cout << "1: Erfassung einer Rechnung (inkl. Abfrage der Rechnungsdaten)" << endl; - 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; - const auto input = ask_question("Ihre Eingabe:"); + while (true) + { + system("cls"); + cout << "Ausgabenverwaltung (" << invoices.size() << " Rechnungen)" << endl << endl; + cout << "Bitte w\x84hlen Sie eine Option:" << endl; + cout << "1: Erfassung einer Rechnung (inkl. Abfrage der Rechnungsdaten)" << endl; + 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; + const auto input = ask_question("Ihre Eingabe:"); - switch (input) - { - case 1: - create_invoice(); - break; - case 2: - invoices_ordered_by_month(); - break; - case 3: - sum_invoices_per_category(); - break; - case 4: - delete_invoice(); - break; - case 0: - return 0; - default: - cout << "Unerlaubte Eingabe" << endl; - system("pause"); - } - } + switch (input) + { + case 1: + create_invoice(); + break; + case 2: + invoices_ordered_by_month(); + break; + case 3: + sum_invoices_per_category(); + break; + case 4: + delete_invoice(); + break; + case 0: + return 0; + default: + cout << "Unerlaubte Eingabe" << endl; + system("pause"); + } + } } void create_invoice() { - system("cls"); - cout << "Eine neue Rechnung erstellen" << endl; - cout << "------------------------------------" << endl; - const auto invoice_no = ++invoice_serial_number; - const auto amount = ask_question("Bitte geben Sie den Betrag in Euro ein."); - const auto month = get_month(); - const auto category = get_category(); - const auto i = invoice(invoice_no, amount, month, category); - invoices.push_back(i); + system("cls"); + cout << "Eine neue Rechnung erstellen" << endl; + cout << "------------------------------------" << endl; + const auto invoice_no = ++invoice_serial_number; + const auto amount = ask_question("Bitte geben Sie den Betrag in Euro ein."); + const auto month = get_month(); + const auto category = get_category(); + const auto i = invoice(invoice_no, amount, month, category); + invoices.push_back(i); } void invoices_ordered_by_month() { - system("cls"); - cout << "Alle Rechnungen nach Monat sortiert" << endl; - cout << "------------------------------------" << endl; - auto temp = list(invoices); - temp.sort(compare_month_ascending); - for (auto invoice : temp) - { - cout << " Rechnung: " << invoice.get_number() << endl; - cout << " Betrag: " << invoice.get_amount() << " Euro" << endl; - cout << " Monat: " << invoice::months[invoice.get_month()] << endl; - cout << " Kategorie: " << invoice::categories[invoice.get_category()] << endl; - cout << "------------------------------------" << endl; - } - system("pause"); + system("cls"); + cout << "Alle Rechnungen nach Monat sortiert" << endl; + cout << "------------------------------------" << endl; + //> + map> month_category_amount; + for (auto invoice : invoices) + { + month_category_amount[invoice.get_month()][invoice.get_category()] += invoice.get_amount(); + } + + for (auto month_pair : month_category_amount) + { + auto sum = 0.0; + cout << invoice::months[month_pair.first] << endl; + cout << "------------------------------------" << endl; + for (const auto category_amount : month_pair.second) + { + cout << invoice::categories[category_amount.first] << ": " << category_amount.second << " Euro" << endl; + sum += category_amount.second; + } + cout << endl; + cout << "Gesamt: " << sum << " Euro"; + if (sum > 500) cout << " Mehr ausgegeben als geplant!!!"; + cout << endl; + cout << "------------------------------------" << endl; + } + system("pause"); } void delete_invoice() { - system("cls"); - cout << "Rechnung l\x94schen" << endl; - cout << "------------------------------------" << endl; - const auto invoice_number = ask_question("Bitte geben Sie eine Rechnungsnummer ein"); - for (auto invoice : invoices) - { - if (invoice.get_number() == invoice_number) - { - invoices.remove(invoice); - return; - } - } - cerr << "Fehler: Rechnung nicht gefunden." << endl; - system("pause"); + system("cls"); + cout << "Rechnung l\x94schen" << endl; + cout << "------------------------------------" << endl; + const auto invoice_number = ask_question("Bitte geben Sie eine Rechnungsnummer ein"); + for (auto invoice : invoices) + { + if (invoice.get_number() == invoice_number) + { + invoices.remove(invoice); + return; + } + } + cerr << "Fehler: Rechnung nicht gefunden." << endl; + system("pause"); } void sum_invoices_per_category() { - system("cls"); - cout << "Summe der Ausgaben pro Kategorie ausgeben" << endl; - cout << "------------------------------------" << endl; - map sums; - for (auto invoice : invoices) - { - sums[invoice.get_category()] = sums[invoice.get_category()] += invoice.get_amount(); - } - for (const auto sum : sums) - { - cout << " Kategorie: " << invoice::categories[sum.first] << endl; - cout << " Summe: " << sum.second << " Euro" << endl; - cout << "------------------------------------" << endl; - } - system("pause"); + system("cls"); + cout << "Summe der Ausgaben pro Kategorie ausgeben" << endl; + cout << "------------------------------------" << endl; + map sums; + for (auto invoice : invoices) + { + sums[invoice.get_category()] = sums[invoice.get_category()] += invoice.get_amount(); + } + for (const auto sum : sums) + { + cout << " Kategorie: " << invoice::categories[sum.first] << endl; + cout << " Summe: " << sum.second << " Euro" << endl; + cout << "------------------------------------" << endl; + } + system("pause"); } int get_month() { - while (true) - { - cout << "Bitte w\x84hlen sie einen Monat aus:" << endl; - auto index = 0; - for (auto month : invoice::months) - { - cout << ++index << ": " << month << endl; - } - const auto answer = ask_question(""); - if (answer > 0 && answer <= 12) - { - return answer - 1; - } - cout << "Ung\x81ltige Eingabe" << endl << endl; - } + while (true) + { + cout << "Bitte w\x84hlen sie einen Monat aus:" << endl; + auto index = 0; + for (auto month : invoice::months) + { + cout << ++index << ": " << month << endl; + } + const auto answer = ask_question(""); + const auto size = static_cast(sizeof(invoice::months) / sizeof(*invoice::months)); + if (answer > 0 && answer <= size) + { + 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; - } -} + 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(""); + const auto size = static_cast(sizeof(invoice::categories) / sizeof(*invoice::categories)); + if (answer > 0 && answer <= size) + { + return answer - 1; + } + cout << "Ung\x81ltige Eingabe" << endl << endl; + } +} \ No newline at end of file