working on validation

This commit is contained in:
Holger Börchers 2018-12-26 22:33:22 +01:00
parent 6c03201662
commit f7e90c0eab

View File

@ -17,191 +17,191 @@ int next_reservation_id;
template <typename T> template <typename T>
T ask_question(const string& question) T ask_question(const string& question)
{ {
cout << question << ": "; cout << question << ": ";
T value; T value;
cin >> value; cin >> value;
return value; return value;
} }
bool ask_question(const string& question) bool ask_question(const string& question)
{ {
while (true) while (true)
{ {
const auto answer = ask_question<char>(question + " (J/N)"); const auto answer = ask_question<char>(question + " (J/N)");
if (answer == 'j' || answer == 'J') if (answer == 'j' || answer == 'J')
{ {
return true; return true;
} }
if (answer == 'n' || answer == 'N') if (answer == 'n' || answer == 'N')
{ {
return false; return false;
} }
cout << "Ungueltige Eigabe. Bitte noch einmal versuchen!"; cout << "Ungueltige Eigabe. Bitte noch einmal versuchen!";
} }
} }
class date final class date final
{ {
private: private:
unsigned short day_; unsigned short day_;
unsigned short month_; unsigned short month_;
unsigned short year_; unsigned short year_;
public: public:
date(const unsigned short day, const unsigned short month, const unsigned short year) : day_(day), month_(month), date(const unsigned short day, const unsigned short month, const unsigned short year) : day_(day), month_(month),
year_(year) year_(year)
{ {
} }
int get_sum() const int get_sum() const
{ {
return year_ + month_ + day_; return year_ + month_ + day_;
} }
}; };
class address final class address final
{ {
private: private:
string street_; string street_;
string street_no_; string street_no_;
string postal_code_; string postal_code_;
string city_; string city_;
public: public:
address() = default; address() = default;
address(const string&& street, const string&& street_no, const string&& postal_code, const string&& city) : address(const string&& street, const string&& street_no, const string&& postal_code, const string&& city) :
street_(street), street_(street),
street_no_(street_no), street_no_(street_no),
postal_code_(postal_code), postal_code_(postal_code),
city_(city) city_(city)
{ {
} }
friend istream& operator>>(istream& istr, address& address) friend istream& operator>>(istream& istr, address& address)
{ {
cout << "Strasse: "; cout << "Strasse: ";
istr >> address.street_; istr >> address.street_;
cout << "Hausnummer: "; cout << "Hausnummer: ";
istr >> address.street_no_; istr >> address.street_no_;
cout << "Postleitzahl: "; cout << "Postleitzahl: ";
istr >> address.postal_code_; istr >> address.postal_code_;
cout << "Ort: "; cout << "Ort: ";
istr >> address.city_; istr >> address.city_;
return istr; return istr;
} }
friend ostream& operator<<(ostream& ostr, const address& customer) friend ostream& operator<<(ostream& ostr, const address& customer)
{ {
ostr << "Strasse: " << customer.street_ << endl; ostr << "Strasse: " << customer.street_ << endl;
ostr << "Hausnummer: " << customer.street_no_ << endl; ostr << "Hausnummer: " << customer.street_no_ << endl;
ostr << "Postleitzahl: " << customer.postal_code_ << endl; ostr << "Postleitzahl: " << customer.postal_code_ << endl;
ostr << "Ort: " << customer.city_ << endl; ostr << "Ort: " << customer.city_ << endl;
return ostr; return ostr;
} }
}; };
class customer final class customer final
{ {
private: private:
int id_; int id_;
string name_; string name_;
string first_name_; string first_name_;
address address_; address address_;
unsigned short year_of_birth_ = 1900; unsigned short year_of_birth_ = 1900;
string phone_no_; string phone_no_;
bool has_driving_license_ = false; bool has_driving_license_ = false;
public: public:
customer() : id_(++next_customer_id) customer() : id_(++next_customer_id)
{ {
} }
customer(string&& name, string&& first_name, address&& address, unsigned short&& year_of_birth, string&& phone_no, customer(string&& name, string&& first_name, address&& address, unsigned short&& year_of_birth, string&& phone_no,
bool&& has_driving_license) : id_(++next_customer_id), bool&& has_driving_license) : id_(++next_customer_id),
name_(name), name_(name),
first_name_(first_name), first_name_(first_name),
address_(address), address_(address),
year_of_birth_(year_of_birth), year_of_birth_(year_of_birth),
phone_no_(phone_no), phone_no_(phone_no),
has_driving_license_(has_driving_license) has_driving_license_(has_driving_license)
{ {
} }
friend istream& operator>>(istream& istr, customer*& customer) friend istream& operator>>(istream& istr, customer*& customer)
{ {
cout << "Name: "; cout << "Name: ";
istr >> customer->name_; istr >> customer->name_;
cout << "Vorname: "; cout << "Vorname: ";
istr >> customer->first_name_; istr >> customer->first_name_;
cout << "Geburtsjahr: "; cout << "Geburtsjahr: ";
istr >> customer->year_of_birth_; istr >> customer->year_of_birth_;
istr >> customer->address_; istr >> customer->address_;
cout << "Telefonnumer: "; cout << "Telefonnumer: ";
istr >> customer->phone_no_; istr >> customer->phone_no_;
customer->has_driving_license_ = ask_question("Fuehrerschein der Klasse A?"); customer->has_driving_license_ = ask_question("Fuehrerschein der Klasse A?");
return istr; return istr;
} }
friend ostream& operator<<(ostream& ostr, const customer& customer) friend ostream& operator<<(ostream& ostr, const customer& customer)
{ {
ostr << "Name: " << customer.name_ << endl; ostr << "Name: " << customer.name_ << endl;
ostr << "Vorname: " << customer.first_name_ << endl; ostr << "Vorname: " << customer.first_name_ << endl;
ostr << "Geburtsjahr: " << customer.year_of_birth_ << endl; ostr << "Geburtsjahr: " << customer.year_of_birth_ << endl;
ostr << customer.address_; ostr << customer.address_;
ostr << "Telefonnummer: " << customer.phone_no_ << endl; ostr << "Telefonnummer: " << customer.phone_no_ << endl;
ostr << "Fuehrerschein " << (customer.has_driving_license_ ? "" : "nicht ") << "vorhanden." << endl; ostr << "Fuehrerschein " << (customer.has_driving_license_ ? "" : "nicht ") << "vorhanden." << endl;
return ostr; return ostr;
} }
string get_name() const string get_name() const
{ {
return this->name_; return this->name_;
} }
string get_first_name() const string get_first_name() const
{ {
return this->first_name_; return this->first_name_;
} }
}; };
class reservation final class reservation final
{ {
private: private:
int id_; int id_;
date start_; date start_;
date end_; date end_;
short motorcycle_ = -1; // => -1 means nothing reserved short motorcycle_ = -1; // => -1 means nothing reserved
customer* customer_; customer* customer_;
public: public:
reservation(const date start, const date end, const short motorcycle, customer* customer) : reservation(const date start, const date end, const short motorcycle, customer* customer) :
id_(++next_reservation_id), id_(++next_reservation_id),
start_(start), start_(start),
end_(end), end_(end),
motorcycle_(motorcycle), motorcycle_(motorcycle),
customer_(customer) customer_(customer)
{ {
} }
date get_start_date() const date get_start_date() const
{ {
return start_; return start_;
} }
date get_end_date() const date get_end_date() const
{ {
return end_; return end_;
} }
short get_motorcycle() const short get_motorcycle() const
{ {
return motorcycle_; return motorcycle_;
} }
}; };
string motorcycles[] = string motorcycles[] =
{ {
"Suzuki Bandit", "Suzuki Bandit",
"Honda TransAlp", "Honda TransAlp",
"BMW F 650 GS", "BMW F 650 GS",
"Kawasaki ZZR1400" "Kawasaki ZZR1400"
}; };
list<customer*> customers; list<customer*> customers;
@ -213,194 +213,203 @@ void export_reservations();
int main() int main()
{ {
try try
{ {
while (true) while (true)
{ {
system("cls"); system("cls");
cout << "Motorradvermietung (" << reservations.size() << " Reservierungen; " << customers.size() << cout << "Motorradvermietung (" << reservations.size() << " Reservierungen; " << customers.size() <<
" Kunden)" " Kunden)"
<< endl << endl; << endl << endl;
cout << "Bitte w\x84hlen Sie eine Option:" << endl; cout << "Bitte w\x84hlen Sie eine Option:" << endl;
cout << "1: Neuen Kunden anlegen" << endl; cout << "1: Neuen Kunden anlegen" << endl;
cout << "2: Erstellen einer Reservierung" << endl; cout << "2: Erstellen einer Reservierung" << endl;
cout << "3: Motorrad herausgeben" << endl; cout << "3: Motorrad herausgeben" << endl;
cout << "4: Reservierungen exportieren" << endl; cout << "4: Reservierungen exportieren" << 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_customer(); create_customer();
break; break;
case 2: case 2:
create_reservation(); create_reservation();
break; break;
case 3: case 3:
rent_a_motorcycle(); rent_a_motorcycle();
break; break;
case 4: case 4:
export_reservations(); export_reservations();
break; break;
case 0: case 0:
return 0; return 0;
default: default:
cout << "Unerlaubte Eingabe" << endl; cout << "Unerlaubte Eingabe" << endl;
system("pause"); system("pause");
} }
} }
} }
catch (...) catch (...)
{ {
cout << "Etwas unvorhergesehendes ist passiert. Tut mir leid." << endl; cout << "Etwas unvorhergesehendes ist passiert. Tut mir leid." << endl;
system("pause"); system("pause");
} }
} }
void create_customer() void create_customer()
{ {
while (true) while (true)
{ {
system("cls"); system("cls");
cout << "Neuen Kunden anlegen" << endl; cout << "Neuen Kunden anlegen" << endl;
cout << "------------------------------------" << endl; cout << "------------------------------------" << endl;
auto c = new customer; auto c = new customer;
cin >> c; cin >> c;
cout << endl << endl; cout << endl << endl;
cout << *c; cout << *c;
if (ask_question("Angaben korrekt?")) if (ask_question("Angaben korrekt?"))
{ {
customers.push_back(c); customers.push_back(c);
return; return;
} }
} }
} }
vector<customer*> find_customers_by_name(const string& name) vector<customer*> find_customers_by_name(const string& name)
{ {
vector<customer*> result; vector<customer*> result;
for (auto customer : customers) for (auto customer : customers)
{ {
if (customer->get_name() == name) if (customer->get_name() == name)
{ {
result.push_back(customer); result.push_back(customer);
} }
} }
return result; return result;
} }
date get_validated_date(const string&& question) date get_validated_date(const string&& question)
{ {
while (true) while (true)
{ {
auto date_as_string = ask_question<string>(question + " (tt.mm.jjjj)"); auto date_as_string = ask_question<string>(question + " (tt.mm.jjjj)");
auto day = 0, month = 0, year = 0; auto day = 0, month = 0, year = 0;
const auto res = sscanf_s(date_as_string.c_str(), "%2d.%2d.%4d", const auto res = sscanf_s(date_as_string.c_str(), "%2d.%2d.%4d",
&day, &day,
&month, &month,
&year); &year);
if (res == 3 && (month >= 1 && month <= 12) && (day >= 1 && day <= 31) && (year >= 1900 && month <= 2200)) if (res == 3 && (month >= 1 && month <= 12) && (day >= 1 && day <= 31) && (year >= 1900 && month <= 2200))
{ {
return date{ return date{
static_cast<const unsigned short>(day), static_cast<const unsigned short>(day),
static_cast<const unsigned short>(month), static_cast<const unsigned short>(month),
static_cast<const unsigned short>(year) static_cast<const unsigned short>(year)
}; };
} }
cout << "Fehlerhafte Eingabe." << endl; cout << "Fehlerhafte Eingabe." << endl;
} }
} }
customer* find_customer_by_first_name(const vector<customer*>& filtered_customers, const string& first_name) customer* find_customer_by_first_name(const vector<customer*>& filtered_customers, const string& first_name)
{ {
for (auto c : filtered_customers) for (auto c : filtered_customers)
{ {
if (c->get_first_name() == first_name) if (c->get_first_name() == first_name)
{ {
return c; return c;
} }
} }
return nullptr; return nullptr;
} }
int select_motorcycle() int select_motorcycle()
{ {
while (true) while (true)
{ {
cout << "Bitte w\x84hlen sie ein Motorrad aus:" << endl; cout << "Bitte w\x84hlen sie ein Motorrad aus:" << endl;
auto index = 0; auto index = 0;
for (const auto& cycle : motorcycles) for (const auto& cycle : motorcycles)
{ {
cout << ++index << ": " << cycle << endl; cout << ++index << ": " << cycle << endl;
} }
const auto answer = ask_question<unsigned int>("Auswahl"); const auto answer = ask_question<unsigned int>("Auswahl");
const auto size = static_cast<int>(sizeof(motorcycles) / sizeof(*motorcycles)); const auto size = static_cast<int>(sizeof(motorcycles) / sizeof(*motorcycles));
if (answer > 0 && answer <= size) if (answer > 0 && answer <= size)
{ {
return answer - 1; return answer - 1;
} }
cout << "Ung\x81ltige Eingabe" << endl << endl; cout << "Ung\x81ltige Eingabe" << endl << endl;
} }
} }
bool validate_reservation(reservation* const reservation) bool validate_reservation(reservation* const reservation)
{ {
for (auto r : reservations) const auto start_sum = reservation->get_start_date().get_sum();
{ const auto end_sum = reservation->get_end_date().get_sum();
if (r->get_motorcycle() == reservation->get_motorcycle()) for (auto r : reservations)
{ {
if (reservation->get_start_date().get_sum() > r->get_end_date().get_sum()) if (r->get_motorcycle() == reservation->get_motorcycle())
{ {
//TODO if (start_sum < r->get_end_date().get_sum() && end_sum > r->get_start_date().get_sum()
} || end_sum > r->get_start_date().get_sum() && end_sum < r->get_end_date().get_sum())
} {
} return false;
return true; }
}
}
return true;
} }
void create_reservation() void create_reservation()
{ {
system("cls"); system("cls");
const auto name = ask_question<string>("Name des Kunden"); const auto name = ask_question<string>("Name des Kunden");
auto customers = find_customers_by_name(name); auto customers = find_customers_by_name(name);
customer* current; customer* current;
if (customers.size() > 1) if (customers.size() > 1)
{ {
const auto first_name = ask_question<string>("Vorname des Kunden"); const auto first_name = ask_question<string>("Vorname des Kunden");
current = find_customer_by_first_name(customers, first_name); current = find_customer_by_first_name(customers, first_name);
if (current == nullptr) if (current == nullptr)
{ {
cout << "Kunde nicht gefunden" << endl; cout << "Kunde nicht gefunden" << endl;
system("pause"); system("pause");
return; return;
} }
} }
else if (customers.empty()) else if (customers.empty())
{ {
cout << "Kunde nicht gefunden" << endl; cout << "Kunde nicht gefunden" << endl;
system("pause"); system("pause");
return; return;
} }
else else
{ {
current = customers.front(); current = customers.front();
} }
const auto start_date = get_validated_date("Startdatum"); const auto start_date = get_validated_date("Startdatum");
const auto end_date = get_validated_date("Enddatum"); const auto end_date = get_validated_date("Enddatum");
const auto motorcycle = select_motorcycle(); const auto motorcycle = select_motorcycle();
const auto r = new reservation(start_date, end_date, motorcycle, current); const auto r = new reservation(start_date, end_date, motorcycle, current);
if (validate_reservation(r)) if (validate_reservation(r))
{ {
reservations.push_back(r); reservations.push_back(r);
} }
else
{
cout << "Reservierung konnte nicht angelegt werden." << endl;
cout << "Die Zeitperiode ueberschneidet sich mit einer vorhandenen Reservierung" << endl;
system("pause");
}
} }
void rent_a_motorcycle() void rent_a_motorcycle()