samedi 9 novembre 2019

Queue not adding objects - C++ 11

I have two custom classes, Passenger and Elevator:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

class Passenger
{
    public:

    explicit Passenger(int startTime, int startFloor, int endFloor, int waitTime){
        this->startTime = startTime;
        this->startFloor = startFloor;    
        this->endFloor = endFloor;
        this->waitTime = waitTime;
    }//end constructor

    void increaseWait(int increase){
        this->waitTime += increase;
    }

    int getWait(){
        return this->waitTime;
    }

    int startTime, startFloor, endFloor, waitTime;

}; //end passenger


class Elevator
{
    public:

    explicit Elevator(string the){
        status = "STOPPED";
        passengerCount = 0;
        name = the;        
    }//end constructor

    int addtoQueue(Passenger person){
        if (this->passengerCount < 8){
            this->pickupQueue.push(person);
            return 0;
        }else{
            cout << "Error -- full" << endl;
            return 1;
        } //end

    } //end addtoQueue

    void changePassengerCount(int change){
        passengerCount += change;
    }

    string getStatus(){
        return status;
    }

    void setStatus(string status){
        status = status;
    }

    void addStop(int location){
        stops.push(location);
    }

    int getStops(){
        return stops.size();
    }

    string getName(){
        return name;
    }

    int getRoute(){
        return stops.front();
    }

    bool checkCount(){
        if (passengerCount > 8){
            return false;
        }else{
            return true;
        }
    }

    int printCount(){
        return passengerCount;
    }

    void printQueue(){
        queue<int> copy = stops;

        while(!copy.empty()){
            cout << copy.front() << " ";
            copy.pop();
        }

        cout << endl;
    }

    private:
    string status;
    queue<Passenger> pickupQueue;
    int passengerCount;
    queue<int> stops;
    string name;

}; //end passenger

I am reading a csv file into a vector of Passenger objects, and then what I want to do is fill some queue objects in each Elevator object with those Passenger objects. However, it seems as though none of the objects in the Elevator class are actually updating. For example, passengerCount and Queue Size never go above one. I cannot figure out why.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include "Passenger.h"
using namespace std;

int time = 0;

int main(){
    vector<Passenger> passengers;
    Elevator elevatorOne = Elevator("elevatorOne");
    Elevator elevatorTwo = Elevator("elevatorTwo");
    Elevator elevatorThree = Elevator("elevatorThree");
    Elevator elevatorFour = Elevator("elevatorFour");
    vector<Elevator> elevators;
    elevators.push_back(elevatorOne);
    elevators.push_back(elevatorTwo);
    elevators.push_back(elevatorThree);
    elevators.push_back(elevatorFour);

    ifstream ifile("filepath"); 
    string line; // we read the full line here
    while (getline(ifile, line)){ // read the current line
        istringstream iss{line}; // construct a string stream from line
        // read the tokens from current line separated by comma
        vector<string> tokens; // here we store the tokens
        string token; // current token
        while (getline(iss, token, ',')){
            tokens.push_back(token); // add the token to the vector
        }
        // map the tokens into our variables, this applies to your scenario
        int startTime = stoi(tokens[0]); // first is a string, no need for further processing
        int startFloor = stoi(tokens[1]); // second is an int, convert it
        int endFloor = stoi(tokens[2]); // same for third

        Passenger curr = Passenger(startTime, startFloor, endFloor, 0);
        passengers.push_back(curr);

    } //end while reading the file

    for(unsigned int i = 0; i < passengers.size(); i++){
        //cout << "Currently evaluating passenger: " << i << endl;
        for (Elevator currElevator : elevators){
            // If the elevator hasn't moved
            if(currElevator.getStatus().compare("STOPPED") == 0 && currElevator.getStops() == 0 && currElevator.checkCount() == true){                
                // Add the passenger's end floor to the current elevator's queue
                cout << "adding passenger " << i << " to elevator: " << currElevator.getName() << " because of condition 1 " << endl;
                currElevator.addStop(passengers[i].endFloor);
                currElevator.changePassengerCount(1);
                cout << "Queue Size: " << currElevator.getStops() << endl;
                cout << "Passenger Count: " << currElevator.printCount() << endl;
                if(passengers[i].endFloor > currElevator.getRoute()){
                    currElevator.setStatus("MOVING UP");
                } else{
                    currElevator.setStatus("MOVING DOWN");
                }

                break;                     
            } //end if

            // If the elevator is stopped
            else if (currElevator.getStatus() == "STOPPED"){
                // Add the passenger's end floor to the current elevator's queue
                cout << "adding passenger " << i << " to elevator: " << currElevator.getName() << " because of condition 2 " << endl;
                currElevator.addStop(passengers[i].endFloor);
                if(passengers[i].endFloor > currElevator.getRoute()){
                    currElevator.setStatus("MOVING UP");
                } else{
                    currElevator.setStatus("MOVING DOWN");
                }

                break;                
            } //end else if

            // If the current elevator is going to pass this passengers start floor
            else if (currElevator.getRoute() > passengers[i].startFloor){
                // Add the passenger's end floor to the current elevator's queue
                cout << "adding passenger " << i << " to elevator: " << currElevator.getName() << " because of condition 3 " << endl;
                currElevator.addStop(passengers[i].endFloor);
                if(passengers[i].endFloor > currElevator.getRoute()){
                    currElevator.setStatus("MOVING UP");
                } else{
                    currElevator.setStatus("MOVING DOWN");
                }

                break;
            } //end else if

            else{
                // If none of these conditions match, move on to the next elevator
                continue;
            } //end else
        } //end looping through elevators
    } //end looping through passengers
} //end main

I am getting results like:

adding passenger 0 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 1 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 2 to elevator: elevatorOne because of condition 1
Queue Size: 1

But would expect to see:

adding passenger 0 to elevator: elevatorOne because of condition 1
Queue Size: 1
Passenger Count: 1
adding passenger 1 to elevator: elevatorOne because of condition 1
Queue Size: 2
Passenger Count: 2
adding passenger 2 to elevator: elevatorOne because of condition 1
Queue Size: 3
Passenger Count: 3

etc

Aucun commentaire:

Enregistrer un commentaire