From 0bcc4216784d3cfc55295792be892e491682f5a2 Mon Sep 17 00:00:00 2001 From: Holger Boerchers Date: Tue, 25 Dec 2018 15:09:44 +0100 Subject: [PATCH] working on #5 --- src/Motorradvermietung.cpp | 162 ++++++++++++++++++++++++++++++------- 1 file changed, 135 insertions(+), 27 deletions(-) diff --git a/src/Motorradvermietung.cpp b/src/Motorradvermietung.cpp index 338ea86..c42f798 100644 --- a/src/Motorradvermietung.cpp +++ b/src/Motorradvermietung.cpp @@ -1,10 +1,15 @@ #include #include #include +#include +#include #include using namespace std; +int next_customer_id; +int next_reservation_id; + /** * \brief ask a question and get a answer. * \tparam T return parameter @@ -78,20 +83,10 @@ public: } }; -class reservation final -{ -private: - tm start_ = {}; - tm end_ = {}; - short motorcycle_ = -1; // => -1 means nothing reserved - string customer_name_; - string costumer_first_name_; -public: -}; - class customer final { private: + int id_; string name_; string first_name_; address address_; @@ -99,10 +94,13 @@ private: string phone_no_; bool has_driving_license_ = false; public: - customer() = default; + customer() : id_(++next_customer_id) + { + } customer(string&& name, string&& first_name, address&& address, unsigned short&& year_of_birth, string&& phone_no, - bool&& has_driving_license) : name_(name), + bool&& has_driving_license) : id_(++next_customer_id), + name_(name), first_name_(first_name), address_(address), year_of_birth_(year_of_birth), @@ -111,18 +109,18 @@ public: { } - friend istream& operator>>(istream& istr, customer& customer) + friend istream& operator>>(istream& istr, customer*& customer) { cout << "Name: "; - istr >> customer.name_; + istr >> customer->name_; cout << "Vorname: "; - istr >> customer.first_name_; + istr >> customer->first_name_; cout << "Geburtsjahr: "; - istr >> customer.year_of_birth_; - istr >> customer.address_; + istr >> customer->year_of_birth_; + istr >> customer->address_; cout << "Telefonnumer: "; - istr >> customer.phone_no_; - customer.has_driving_license_ = ask_question("Fuehrerschein der Klasse A?"); + istr >> customer->phone_no_; + customer->has_driving_license_ = ask_question("Fuehrerschein der Klasse A?"); return istr; } @@ -136,6 +134,35 @@ public: ostr << "Fuehrerschein " << (customer.has_driving_license_ ? "" : "nicht ") << "vorhanden." << endl; return ostr; } + + string get_name() const + { + return this->name_; + } + + string get_first_name() const + { + return this->first_name_; + } +}; + +class reservation final +{ +private: + int id_; + string start_ = {}; + string end_ = {}; + short motorcycle_ = -1; // => -1 means nothing reserved + customer* customer_; +public: + reservation(string start, string end, const short motorcycle, customer* customer) : id_(++next_reservation_id), + start_( + std::move(start)), end_( + std::move(end)), + motorcycle_(motorcycle), + customer_(customer) + { + } }; string motorcycles[] = { @@ -145,6 +172,8 @@ string motorcycles[] = { "Kawasaki ZZR1400" }; +list customers; +list reservations; int size_of_motorcycles = 4; void create_customer(); @@ -152,9 +181,6 @@ void create_reservation(); void rent_a_motorcycle(); void export_reservations(); -list customers; -list reservations; - int main() { try @@ -211,20 +237,102 @@ void create_customer() cout << "Neuen Kunden anlegen" << endl; cout << "------------------------------------" << endl; - customer customer; - cin >> customer; + auto c = new customer; + cin >> c; cout << endl << endl; - cout << customer; + cout << *c; if (ask_question("Angaben korrekt?")) { - customers.push_back(customer); + customers.push_back(c); return; } } } + +vector find_customers_by_name(const string& name) +{ + vector result; + for (auto customer : customers) + { + if (customer->get_name() == name) + { + result.push_back(customer); + } + } + return result; +} + +bool validate_date(string& date) +{ + auto day = 0, month = 0, year = 0; + const auto res = sscanf_s(date.c_str(), "%2d.%2d.%4d", + &day, + &month, + &year); + if (res != 3) return false; + if (month < 1 || month > 12) return false; + if (day < 1 || day > 31) return false; + if (year < 1900 || month > 2200) return false; + return true; +} + +string get_validated_date(const string&& question) +{ + while (true) + { + auto date = ask_question(question + " (tt.mm.jjjj)"); + if (validate_date(date)) + { + return date; + } + cout << "Fehlerhafte Eingabe." << endl; + } +} + +customer* find_customer_by_first_name(const vector& filtered_customers, const string& first_name) +{ + for (auto c : filtered_customers) + { + if (c->get_first_name() == first_name) + { + return c; + } + } + return nullptr; +} + void create_reservation() { + system("cls"); + const auto name = ask_question("Name des Kunden"); + auto customers = find_customers_by_name(name); + customer* current; + if (customers.size() > 1) + { + const auto first_name = ask_question("Vorname des Kunden"); + current = find_customer_by_first_name(customers, first_name); + if (current == nullptr) + { + cout << "Kunde nicht gefunden" << endl; + system("pause"); + return; + } + } + else if (customers.empty()) + { + cout << "Kunde nicht gefunden" << endl; + system("pause"); + return; + } + else + { + current = customers.front(); + } + + const auto start_date = get_validated_date("Startdatum"); + const auto end_date = get_validated_date("Enddatum"); + auto r = reservation(start_date, end_date, 2, current); } void rent_a_motorcycle()