Working on #3

This commit is contained in:
Holger Boerchers 2018-12-27 17:59:11 +01:00
parent f7e90c0eab
commit 91b63b022f

View File

@ -2,6 +2,7 @@
#include <string>
#include <list>
#include <vector>
#include <fstream>
using namespace std;
@ -56,6 +57,12 @@ public:
{
return year_ + month_ + day_;
}
friend ostream& operator<<(ostream& ostr, const date& date)
{
ostr << date.day_ << "." << date.month_ << "." << date.year_;
return ostr;
}
};
class address final
@ -170,13 +177,15 @@ private:
date end_;
short motorcycle_ = -1; // => -1 means nothing reserved
customer* customer_;
bool canceled_;
public:
reservation(const date start, const date end, const short motorcycle, customer* customer) :
id_(++next_reservation_id),
start_(start),
end_(end),
motorcycle_(motorcycle),
customer_(customer)
customer_(customer),
canceled_(false)
{
}
@ -194,6 +203,21 @@ public:
{
return motorcycle_;
}
customer* get_customer() const
{
return customer_;
}
void cancel_reservation()
{
canceled_ = true;
}
int get_id() const
{
return id_;
}
};
string motorcycles[] =
@ -368,34 +392,32 @@ bool validate_reservation(reservation* const reservation)
return true;
}
void create_reservation()
customer* get_customer()
{
system("cls");
const auto name = ask_question<string>("Name des Kunden");
auto customers = find_customers_by_name(name);
customer* current;
if (customers.size() > 1)
{
const auto first_name = ask_question<string>("Vorname des Kunden");
current = find_customer_by_first_name(customers, first_name);
return find_customer_by_first_name(customers, first_name);
}
if (customers.empty())
{
return nullptr;
}
return customers.front();
}
void create_reservation()
{
system("cls");
const auto current = get_customer();
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");
const auto motorcycle = select_motorcycle();
@ -412,10 +434,62 @@ void create_reservation()
}
}
reservation* get_reservation_for_customer(customer* const current_customer)
{
auto index = 0;
vector<reservation*> reservations_of_customer;
for (auto reservation : reservations)
{
if (reservation->get_customer() == current_customer)
{
cout << ++index << ". " << reservation->get_start_date() << " - " << reservation->get_end_date() << ": " <<
motorcycles[reservation->get_motorcycle()] << endl;
reservations_of_customer.push_back(reservation);
}
}
index = ask_question<int>("Waehlen Sie eine Reservierung aus");
return reservations_of_customer[index - 1];
}
void rent_a_motorcycle()
{
system("cls");
const auto current = get_customer();
if (current == nullptr)
{
cout << "Kunde nicht gefunden" << endl;
system("pause");
return;
}
auto r = get_reservation_for_customer(current);
const auto cancel_reservation = ask_question("Motorrad ausgeben?");
cout << endl;
if (cancel_reservation)
{
r->cancel_reservation();
cout << "Motorrad reserviert!" << endl;
}
else
{
cout << "Reservierung abgebrochen" << endl;
}
system("pause");
}
void export_reservations()
{
ofstream f;
f.open("reservierungen.txt", ios::app);
f << "Export" << endl;
for (auto reservation : reservations)
{
f << reservation->get_id()<< ". ";
f << motorcycles[reservation->get_motorcycle()]<< " ";
f << reservation->get_start_date()<< " - "<< reservation->get_end_date()<< " ";
f << reservation->get_customer()->get_first_name() << " " << reservation->get_customer()->get_name();
f <<endl;
}
f << "------------" << endl;
f.close();
}