1
0

nearly done...

This commit is contained in:
Holger Boerchers 2018-12-01 22:36:11 +01:00
parent 34faba5bd9
commit ca5552e13c

View File

@ -4,28 +4,18 @@
using namespace std; using namespace std;
enum category class invoice
{
None = 0,
Tanken = 1,
Nahrungsmittel = 2,
Reinigungsmittel = 3,
Urlaub = 4,
Haustiere = 5,
Bekleidung = 6,
Wohnen = 7,
Versicherung = 8
};
class Invoice
{ {
private: private:
int number_ = 0; int number_ = 0;
double amount_; double amount_;
unsigned short month_ = 0; unsigned short month_ = 0;
category category_ = None; int category_ = 0;
public: public:
Invoice(int number, double amount, unsigned short month, category category) : number_(number), amount_(amount), 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) month_(month), category_(category)
{ {
} }
@ -40,7 +30,7 @@ public:
return month_; return month_;
} }
category get_category() const int get_category() const
{ {
return category_; return category_;
} }
@ -48,15 +38,15 @@ public:
void print() const void print() const
{ {
cout << " Rechnung: " << number_ << endl; cout << " Rechnung: " << number_ << endl;
cout << "Betrag: " << amount_ << endl; cout << " Betrag: " << amount_ << "" << endl;
cout << "Monat: " << month_ << endl; cout << " Monat: " << months[month_] << endl;
cout << "Kategorie: " << category_ << endl; cout << " Kategorie: " << categories[category_] << endl;
} }
/** /**
* \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_;
} }
@ -64,10 +54,12 @@ public:
/** /**
* \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[] = {"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 <typename T> template <typename T>
T ask_question(const string& question) T ask_question(const string& question)
{ {
@ -78,20 +70,15 @@ T ask_question(const string& question)
return value; return value;
} }
list<Invoice> invoices; list<invoice> invoices;
void create_invoice(); void create_invoice();
void output_invoices(list<Invoice> sorted_invoices); void output_invoices_order_by_month();
void delete_invoice(); void delete_invoice();
bool compare_category_ascending(const Invoice& first, const Invoice& second) bool compare_month_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(); return first.get_month() < second.get_month();
} }
@ -118,18 +105,10 @@ int main()
create_invoice(); create_invoice();
break; break;
case 2: case 2:
{ output_invoices_order_by_month();
auto temp = list<Invoice>(invoices);
temp.sort(compare_month_ascending);
output_invoices(temp);
}
break; break;
case 3: case 3:
{ //TODO
auto temp = list<Invoice>(invoices);
temp.sort(compare_category_ascending);
output_invoices(temp);
}
break; break;
case 4: case 4:
delete_invoice(); delete_invoice();
@ -145,33 +124,41 @@ int main()
return 0; return 0;
} }
category get_category(); int get_month();
int get_category();
void create_invoice() void create_invoice()
{ {
cout << "Eine neue Rechnung erstellen." << endl << endl; system("cls");
cout << "Eine neue Rechnung erstellen." << endl;
cout << "------------------------------------"<<endl;
const auto invoice_no = invoices.empty() ? 1 : invoices.back().get_number() + 1; const auto invoice_no = invoices.empty() ? 1 : invoices.back().get_number() + 1;
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 = ask_question<unsigned short>("Bitte geben Sie den Monat ein."); 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 output_invoices(const list<Invoice> sorted_invoices) void output_invoices_order_by_month()
{ {
for (auto invoice : sorted_invoices) system("cls");
cout << "Alle Rechnungen nach Monat sortiert:"<<endl;
cout << "------------------------------------"<<endl;
auto temp = list<invoice>(invoices);
temp.sort(compare_month_ascending);
for (auto invoice : temp)
{ {
invoice.print(); invoice.print();
cout << "--------------" << endl; cout << "------------------------------------" << endl;
} }
system("pause"); system("pause");
} }
void delete_invoice() void delete_invoice()
{ {
auto invoice_number = ask_question<int>("Bitte geben Sie die Rechnungsnummer ein"); const auto invoice_number = ask_question<int>("Bitte geben Sie die Rechnungsnummer ein");
for (auto invoice : invoices) for (auto invoice : invoices)
{ {
if (invoice.get_number() == invoice_number) if (invoice.get_number() == invoice_number)
@ -184,23 +171,39 @@ void delete_invoice()
system("pause"); system("pause");
} }
category get_category() int get_month()
{ {
while (true) while (true)
{ {
cout << "Bitte w\x84hlen sie eine Kategorie aus:" << endl; cout << "Bitte w\x84hlen sie einen Monat aus:" << endl;
cout << " 1: Tanken" << endl; auto index = 0;
cout << " 2: Nahrungsmittel" << endl; for (auto month : invoice::months)
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<int>("");
if (cat > 0 && cat <= 8)
{ {
return static_cast<category>(cat); cout << ++index << ": " << month << endl;
}
const auto answer = ask_question<unsigned int>("");
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<int>("");
if (answer > 0 && answer <= 8)
{
return answer - 1;
} }
cout << "Ung\x81ltige Eingabe" << endl << endl; cout << "Ung\x81ltige Eingabe" << endl << endl;
} }