Skip to content
Snippets Groups Projects
main.cpp 3.35 KiB
Newer Older
  • Learn to ignore specific revisions
  • Martin Weinelt's avatar
    Martin Weinelt committed
    //
    // 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;
    }