//----------------------------------------------------------------------
//  IMPLEMENTATION FILE (pkglot.cpp)
//  This module exports an ADT for a parking lot with two alleys
//----------------------------------------------------------------------
#include "pkglot.h"
#include "bool.h"
#include <iomanip.h>    // For setw()

// Private members of class:
//    IntStack alleyA;
//    IntStack alleyB;
//    int      ticket;
//
// CLASSINV:  alleyA contains ticket numbers in LIFO order
//         && alleyB is empty

PkgLotType::PkgLotType()
    //..................................................................
    // Constructor
    // POST: alleyA and alleyB are empty (by implicit calls to their
    //       class constructors)
    //    && ticket == 0
    //..................................................................
{
    ticket = 0;
}

void PkgLotType::Park()
    //..................................................................
    // POST: (Alley A full) -->
    //              Error message displayed
    //           && ticket == ticket<entry>
    //    && (NOT Alley A full) -->
    //              ticket == ticket<entry> + 1
    //           && Value of ticket displayed to user
    //           && ticket pushed onto alleyA stack
    //..................................................................
{
    if (alleyA.IsFull())
        cout << "PARKING LOT FULL\n";
    else {
        ticket++;
        cout << "Ticket no. = " << ticket << '\n';
        alleyA.Push(ticket);
    }
}
void PkgLotType::Retrieve( /* in */ int ticketStub )
    //..................................................................
    // PRE:  Assigned(ticketStub)
    // POST: Top of alleyA popped and pushed onto alleyB until
    //       ticketStub found or alleyA is empty
    //    && Error message displayed if ticketStub was not in alleyA
    //    && All tickets transferred back to alleyA from alleyB
    //..................................................................
{
    int     topTicket;
    Boolean found = FALSE;

    while ( !alleyA.IsEmpty() && !found ) {
        topTicket = alleyA.Top();
        alleyA.Pop();
        if (topTicket == ticketStub)
            found = TRUE;
        else
            alleyB.Push(topTicket);
    }
    if ( !found )
        cout << "CAR NOT PARKED IN MY LOT\n";
    while ( !alleyB.IsEmpty() ) {
        alleyA.Push(alleyB.Top());
        alleyB.Pop();
    }
}

void PkgLotType::Display()
    //..................................................................
    // POST: Contents of alleyA have been output in
    //       order from top to bottom
    //..................................................................
{
    IntStack tempStk = alleyA;  // Copy contents of alleyA into
                                // temporary stack we can destroy
    cout << "Alley A: ";
    while ( !tempStk.IsEmpty() ) {
        cout << setw(4) << tempStk.Top();
        tempStk.Pop();
    }
    cout << '\n';
}


