1
0

removed tabs

This commit is contained in:
Holger Boerchers 2018-12-07 21:21:29 +01:00
parent c635cd9221
commit 0558fb8fd7

View File

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