jeudi 30 mars 2017

Assigning values to vectors of vectors WITH DOUBLE FOR LOOPs

I want to create a loop that will add the PASSENGER's name to a SCHEDULE if the PASSENGER's destination is the same as the VEHICLE's and the timing difference between the PASSENGER and VEHICLE must be <= 30mins.

*Every VEHICLE has its own SCHEDULE.

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

#include "Vehicle.h"
#include "Passenger.h"
#include "Scheduler.h"

int main()
{
    //Passenger Creation
    Passenger pass1("US123", "Donald Trump", "A", 23, 00);
    Passenger pass2("S432", "Jon Doe", "B", 20, 35);
    Passenger pass3("W069", "General Aladeen", "A", 22, 46);
    Passenger pass4("UK13", "Mr Bean", "C", 8, 01);

    vector<Passenger> listOfPassengers;             //Vector of PASSENGERS
    listOfPassengers.push_back(pass1);
    listOfPassengers.push_back(pass2);
    listOfPassengers.push_back(pass3);
    listOfPassengers.push_back(pass4);

    //Vehicle Creation
    Vehicle veh1("Audi", 4, "A", 22, 45);
    Vehicle veh2("Subaru", 4, "B", 20, 35);
    Vehicle veh3("Mini-cooper", 4, "C", 20, 35);

    vector<Vehicle> listOfVehicles;                 //Vector of VEHICLES
    listOfVehicles.push_back(veh1);
    listOfVehicles.push_back(veh2);
    listOfVehicles.push_back(veh3);

    //Scheduler VECTOR aka C++ dynamic array

    vector<vector<string> > schedule;                               //schedule vector OF vector

    int vehVectorNum = listOfVehicles.size();           // How many vehicles
    int pasVectorNum = listOfPassengers.size();         // How many passengers
    int count;
    int result;

    cout << "How many vehicles: " << vehVectorNum << endl;

    for (int i = 0; i < vehVectorNum; i++)
    {

        vector<string> temp;                    // [i + 1] vehicle's vector
        count = listOfVehicles[i].getVSize();

        for (int j = 0; j < pasVectorNum; j++)
        {
            if (count > 0)
            {
                if (listOfVehicles[i].getVDestination() == listOfPassengers[j].getpDestination())
                {
                    result = listOfPassengers[j].getpTimeDuration() - listOfVehicles[i].getvTimeDuration();
                    if ((result <= 30) && (result >= 0))
                    {
                        temp.push_back(listOfPassengers[j].getpName());     // assigns PASSENGER to VEHICLE
                        schedule.push_back(temp);       // assigns VEHICLE to SCHEDULE     

                        count--;
                    }
                }
                else
                {
                    continue;
                }
            }


        }
        cout << "Remaining Capacity for Vehicle" << i + 1 << ": " << count << endl;         //remaining count after each vehicle
        listOfVehicles[i].setcurrentVCap(count);        // assign count to current vehicle capacity
    }

    for (int i = 0; i < listOfVehicles.size(); i++)         //check which vehicle is not full
    {
        if (listOfVehicles[i].getcurrentVCap() != listOfVehicles[i].getVSize())
        {
            cout << listOfVehicles[i].getVName() << endl;
        }
    }
    cout << endl;


    unsigned int i;
    for (i = 0; i < schedule.size(); i++)                           // check what is in SCHEDULE
    {
        cout << "Schedule #" << i << endl;

        unsigned int j;
        for (j = 0; j < schedule[i].size(); j++)
        {
            cout << schedule[i][j] << endl;
        }
        cout << endl;
    }

    return 0;
}

However, there is a logic error as the passengers were assigned wrongly: Output

Based on my conditions, only 3 PASSENGERS should be assigned to 2 schedules - Trump and Aladeen to Schedule #0 (which belongs to Vehicle A) and Jon Doe to Schedule #1 (which belongs to Vehicle B):

Schedule #0
Donald Trump
General Aladeen

Schedule #1
Jon Doe

May I know if I am missing out something? Thx in advance!

Aucun commentaire:

Enregistrer un commentaire