Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Created by hexa on 20.11.21.
//
#include <iostream>
#include "facility.h"
using std::cout;
using std::cerr;
using std::endl;
using std::cin;
void menu_parken(ParkingFacility& parkingFacility) try {
int pos{parkingFacility.getNextFreeSlot()};
if (parkingFacility.at(pos).park()) {
cout << "Parkplatz " << pos << " ist nun besetzt." << endl;
};
}
catch (std::runtime_error& ex) {
cerr << ex.what() << endl;
}
const int POSITION_ERROR{-1};
int menu_ask_number() {
int pos{};
cin >> pos;
if (!cin) {
cerr << "Ungültige Angabe." << endl;
cin.clear();
return POSITION_ERROR;
}
return pos;
}
void menu_ausparken(ParkingFacility& parkingFacility) try {
cout << "Welcher Parkplatz soll ausparken? ";
int pos{menu_ask_number()};
if (pos == POSITION_ERROR) {
return;
}
if (parkingFacility.parkout(pos)) {
cout << "Parkplatz " << pos << " ist nun frei." << endl;
}
}
catch (std::out_of_range& ex) {
cerr << "Parkplatznummer ungültig." << endl;
}
void menu_leasen(ParkingFacility& parkingFacility) {
cout << "Welcher Parkplatz soll vermietet werden? ";
int pos{menu_ask_number()};
if (pos == POSITION_ERROR) {
return;
}
cout << "Wie lautet der Name des Mieters? ";
std::string name;
cin >> name;
if (!cin) {
cerr << "Ungültige Angabe." << endl;
cin.clear();
return;
}
if (parkingFacility.lease(pos, name)) {
cout << "Parkplatz " << pos << " ist nun an " << name << " vermietet." << endl;
};
}
void menu_kuendigen(ParkingFacility& parkingFacility) {
cout << "Welcher Parkplatz soll gekündigt werden? ";
int pos{menu_ask_number()};
if (pos == POSITION_ERROR) {
return;
}
if (parkingFacility.release(pos)) {
cout << "Parkplatz " << pos << " ist nun an gekündigt." << endl;
};
}
void menu(ParkingFacility& parkingFacility) {
while (true) {
cout << endl << "Hauptmenü:" << endl;
cout << " 1. Parkplätze ansehen" << endl;
cout << " 2. Parken" << endl;
cout << " 3. Ausparken" << endl;
cout << " 4. Dauerplatz mieten" << endl;
cout << " 5. Dauerplatz kündigen" << endl;
cout << " 9. Beenden" << endl << endl;
cout << "Auswahl: ";
int sel{menu_ask_number()};
cout << endl;
switch (sel) {
case 1:
parkingFacility.print();
continue;
case 2:
menu_parken(parkingFacility);
continue;
case 3:
menu_ausparken(parkingFacility);
continue;
case 4:
menu_leasen(parkingFacility);
continue;
case 5:
menu_kuendigen(parkingFacility);
continue;
case 9:
return;
default:
cerr << "Ungültige Eingabe." << endl;
continue;
}
};
}
int main() try {
ParkingFacility parkingFacility{};
cout << "Parkhaus-Simulator 2021" << endl;
cout << "Gesamtkapazität: " << parkingFacility.size() << " (" << parkingFacility.lettable() << " für Dauerparker reserviert)" << endl;
menu(parkingFacility);
return 0;
}
catch (...) {
cerr << "Unerwarteter Fehler im Parkhaussimulator!";
return -1;
}