Advertisement
Guest User

Train Sim w/ Bug

a guest
Nov 25th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.69 KB | None | 0 0
  1. #include<String>
  2. #include<iostream>
  3. #include<math.h>
  4. #include<iomanip>
  5. #include<ctime>
  6.  
  7. #include "Passenger.hpp"
  8. #include "PassengerQueue.hpp"
  9. #include "Platform.hpp"
  10. #include "Train.hpp"
  11. #include "CTrainLine.hpp"
  12. #include "PassengerList.hpp"
  13.  
  14. using namespace std;
  15.  
  16. void wait(int sec);
  17.  
  18. int main(){
  19.    
  20.     Platform a1UptownRed("a1","up","red"),
  21.              a2UptownRed("a2","up","red"),
  22.              a3UptownRed("a3","up","red"),
  23.              a3DowntownRed("a3","down","red"),
  24.              a2DowntownRed("a2","down","red"),
  25.              a1DowntownRed("a1","down","red");
  26.              
  27.     CTrainLine redLine;
  28.    
  29.     redLine.add(a1UptownRed);            
  30.     redLine.add(a2UptownRed);
  31.     redLine.add(a3UptownRed);
  32.     redLine.add(a3DowntownRed);
  33.     redLine.add(a2DowntownRed);
  34.     redLine.add(a1DowntownRed);  
  35.    
  36.     redLine.getNext(a1UptownRed);
  37.     redLine.getNext(a2UptownRed);    
  38.     redLine.getNext(a3UptownRed);
  39.     redLine.getNext(a3DowntownRed);    
  40.     redLine.getNext(a2DowntownRed);
  41.     redLine.getNext(a1DowntownRed);    
  42.  
  43.    
  44.     //TRAIN TEST
  45.     Train pelham(redLine, a1UptownRed);
  46.     for(int n = 0; n < 60; n++){
  47.         pelham.displayTrainInfo();  
  48.         pelham.update();
  49.         wait(1);
  50.     }
  51.    
  52.    
  53.     /* seeking the seg fault
  54.  
  55.     Platform *a1u, *a2u, *a3u, *a1d, *a2d, *a3d;
  56.    
  57.     a1u = &a1UptownRed;
  58.     a2u = &a2UptownRed;
  59.     a3u = &a3UptownRed;
  60.     a3d = &a3DowntownRed;
  61.     a2d = &a2DowntownRed;
  62.     a1d = &a1DowntownRed;
  63.    
  64.     a1d = 0;
  65.    
  66.     Platform gru;
  67.    
  68.     gru = redLine.getNext(*a1u);
  69.     gru = redLine.getNext(*a2u);    
  70.     gru = redLine.getNext(*a3u);
  71.     gru = redLine.getNext(*a3d);    
  72.     gru = redLine.getNext(*a2d);
  73.     cout << "HERE?" << endl;
  74.     gru = redLine.getNext(*a1d);
  75.    
  76.     cout << "!!!!" << endl;
  77.     */
  78.  
  79.    
  80. /* Passenger.onSameLine() test
  81.    
  82.     Passenger anon;
  83.     cout << "a3 and b3 on same line?" << endl;
  84.     cout << anon.onSameLine("a3","b3") << endl;
  85.     cout << "a1 and a4 on same line?" << endl;
  86.     cout << anon.onSameLine("a1","a4") << endl;
  87.     cout << "b1 and a4 on same line?" << endl;
  88.     cout << anon.onSameLine("b1","a4") << endl;
  89. /*
  90.    
  91. /*   PASSENGERLIST TEST
  92.     Passenger john("John"), paul("Paul"), george("George"), ringo("Ringo"), anon;
  93.     PassengerList pList;
  94.     pList.add(john);
  95.     pList.add(paul);
  96.     pList.add(george);
  97.     pList.add(ringo);
  98.     pList.add(anon);
  99.     pList.displayList();
  100. */
  101.      
  102.     /*  TRAINLINE TEST
  103.     redLine.displayList();
  104.    
  105.     cout << "\n\nThere are " << redLine.getNumStops(a1UptownRed,a3UptownRed) << " stops between A1 & A3 going uptown" << endl;
  106.     cout << "\n\nThere are " << redLine.getNumStops(a1DowntownRed,a3DowntownRed) << " stops between A1 & A3 going downtown" << endl;
  107.     */
  108.    
  109.     /*  //PLATFORM LINE TEST
  110.     Passenger * pDoe;
  111.     for(int n = 0; n < 50; n++){
  112.         pDoe = new Passenger;
  113.         a1UptownRed.placeOnLine(*pDoe);
  114.     }
  115.     a1UptownRed.displayWaitLines();
  116.     for(int n = 0; n < 25; n++){
  117.         try{
  118.             a1UptownRed.getNextPassenger(n%3+1);
  119.         } catch(...){}
  120.     }
  121.     a1UptownRed.displayWaitLines();
  122.     */  
  123.        
  124.     system("PAUSE");    
  125.     return 0;
  126. }
  127.  
  128. void wait(int sec){
  129.         clock_t endwait;
  130.         endwait = clock() + sec * CLK_TCK;
  131.         while (clock() < endwait) {}
  132. }
  133.  
  134.  
  135. #ifndef CTrainLine_class
  136. #define CTrainLine_class
  137.  
  138. #include "Platform.hpp"
  139. #include <iostream>
  140.  
  141. using namespace std;
  142.  
  143. class CTrainLine{
  144.     public:
  145.         struct ListNode{
  146.             Platform data;
  147.             ListNode * next;
  148.         };
  149.         CTrainLine();
  150.         ~CTrainLine();                 //Destructor deletes all nodes to prevent memory leak
  151.         bool isEmpty();
  152.         void add(Platform P);
  153.         ListNode * find(int pos);              //Returns a pointer to a node by position number        
  154.         ListNode * find(Platform plat);
  155.         int getSize();          
  156.         Platform remove(int pos);             //Removes & deletes a node
  157.         Platform retrieve(int pos);           //Retrieves the data of node
  158.         Platform getNext(Platform plat);
  159.         void displayList();
  160.         int getNumStops(Platform p1, Platform p2);
  161.  
  162.     private:
  163.         ListNode * head;
  164.         ListNode * last;
  165.         int size;  
  166. };
  167.  
  168. CTrainLine::CTrainLine() {
  169.     size = 0;
  170.     head = new ListNode;
  171. }
  172.  
  173. bool CTrainLine::isEmpty(){
  174.     if(!size)
  175.         return true;
  176.     else return false;    
  177. }
  178.  
  179. void CTrainLine::add(Platform item){
  180.     ListNode * temp = new ListNode;
  181.     temp->data = item;
  182.     if(isEmpty()){
  183.         head->next = temp;
  184.         temp->next = temp;
  185.     }
  186.     else {
  187.         temp->next = head->next;  
  188.         last->next = temp;
  189.     }
  190.     last = temp;
  191.     size++;
  192. }
  193.  
  194. int CTrainLine::getSize(){
  195.     return size+1;
  196. }
  197.  
  198. struct CTrainLine::ListNode * CTrainLine::find(int pos){
  199.     if(pos < 1 || pos > size)
  200.         throw 1;
  201.     else{
  202.         ListNode * temp = head;
  203.         for(int n = 0; n < pos; n++)
  204.             temp = temp->next;
  205.         return temp;
  206.     }    
  207. }  
  208.  
  209. struct CTrainLine::ListNode * CTrainLine::find(Platform plat){
  210.     ListNode * curr = head;
  211.     for(int n = 0; n < size; n++){
  212.         curr = curr->next;
  213.         if(curr->data == plat)
  214.             return curr;
  215.     }
  216.     cout << "ERROR: ";
  217.     plat.displayInfo();
  218.     cout << "\n ... not found on line" << endl;
  219. }
  220.  
  221. Platform CTrainLine::remove(int pos){
  222.    try{
  223.         if(pos < 1 || pos > size)
  224.             throw 5;
  225.         else{
  226.             Platform data;
  227.             ListNode * curr,
  228.                      * prev;
  229.             if(pos == 1)
  230.                 prev = head;
  231.             else
  232.                 prev = find(pos-1);
  233.             curr = prev->next;
  234.             prev->next = curr->next;
  235.             data = curr->data;
  236.             if(curr == last)
  237.                 last = prev;
  238.             delete curr;
  239.             size--;
  240.             return data;
  241.         }
  242.     } catch(...){
  243.         cout << "ERROR: CTrainLine trying to remove nonexistant item" << endl;
  244.     }
  245. }
  246.  
  247.  
  248. Platform CTrainLine::retrieve(int pos){
  249.     ListNode * curr = find(pos);
  250.     return curr->data;
  251. }
  252.  
  253. Platform CTrainLine::getNext(Platform plat){
  254.     ListNode * curr = find(plat);
  255.     return ((curr->next)->data);  
  256. }
  257.  
  258.  
  259. void CTrainLine::displayList(){
  260.     ListNode * curr = head;
  261.     cout << "Total size of list: " << size << endl;
  262.     if(!isEmpty()){
  263.         for(int n = 0; n < size; n++){
  264.             curr = curr -> next;
  265.             curr->data.displayInfo();
  266.             if(n < size-1)
  267.                 cout << "... links to ..." << endl;        
  268.         }  
  269.         curr = curr -> next;
  270.         cout << "... which links back to ..." <<  endl;
  271.         curr->data.displayInfo();
  272.     }
  273. }
  274.  
  275. int CTrainLine::getNumStops(Platform p1, Platform p2){
  276.     ListNode * curr = find(p1);
  277.     for(int n = 0; n < size; n++){
  278.         if (curr->data == p2)
  279.             return n;
  280.         else
  281.             curr = curr->next;
  282.     }
  283.     cout << "ERROR: ";
  284.     p1.displayInfo();
  285.     cout << "... and ... " << endl;
  286.     p2.displayInfo();
  287.     cout << "Don't appear on the same track." << endl;
  288.     return -1;
  289. }
  290.  
  291. CTrainLine::~CTrainLine(){
  292.     for(int n = size; size > 0; n--)
  293.         remove(n);    
  294. }
  295.  
  296. #endif
  297.  
  298. #ifndef platform_class
  299. #define platform_class
  300.  
  301. #include "Passenger.hpp"
  302. #include "PassengerQueue.hpp"
  303.  
  304. using namespace std;
  305.  
  306. class Platform{
  307.     public:
  308.         Platform();
  309.         Platform(string station, string direction, string tLine);
  310.         string direction,
  311.                station,
  312.                line;
  313.         PassengerQueue waitLines[12];
  314.              
  315.         void placeOnLine(Passenger pass);
  316.         int getTotalWaiting();
  317.         string getStation();
  318.         string getDirection();
  319.         string getLine();
  320.         bool isEmpty();
  321.         Passenger getNextPassenger(int carSection);
  322.         void displayInfo();
  323.         bool operator==(Platform &p2);
  324.         bool hasTrain();
  325.         void setTrain(bool T);
  326.         void displayWaitLines();
  327.                
  328.     private:
  329.         bool train;
  330.         int totalWaiting,
  331.             lastQueueUsed;
  332. };
  333.  
  334. Platform::Platform(){
  335.     station = "DEFAULT STATION";
  336.     direction = "DEFAULT DIRECTION";
  337.     line = "DEFAULT LINE";
  338.     totalWaiting = 0;
  339.     lastQueueUsed = 0;
  340.     train = false;
  341. }
  342.  
  343. Platform::Platform(string stat, string dir, string tLine){
  344.     station = stat;
  345.     direction = dir;
  346.     line = tLine;
  347.     totalWaiting = 0;
  348.     lastQueueUsed = 0;
  349.     train = false;  
  350. }
  351.  
  352. Passenger Platform::getNextPassenger(int carSection){
  353.     try{
  354.         int line = ((carSection - 1)*4)+lastQueueUsed%4; //randomly unloads a line within a car range 1:0-3, 2:4-7,3:8:11
  355.         lastQueueUsed++;
  356.         Passenger result = (waitLines[line]).dequeue();
  357.         result.setWaiting(false);
  358.         totalWaiting--;
  359.         return result;
  360.     } catch(...){}
  361. }
  362.  
  363. string Platform::getStation(){
  364.     return station;    
  365. }
  366.  
  367. string Platform::getDirection(){
  368.     return direction;    
  369. }
  370.  
  371. string Platform::getLine(){
  372.     return line;    
  373. }
  374.  
  375. void Platform::placeOnLine(Passenger pass){
  376.     (waitLines[lastQueueUsed]).enqueue(pass);
  377.     pass.setWaiting(true);
  378.     totalWaiting++;
  379.     lastQueueUsed++;
  380.     lastQueueUsed %= 12;
  381. }
  382.  
  383. void Platform::displayInfo(){
  384.     cout << getLine() << "\t" << getDirection() << "\t" << getStation() << "\t" << "Waiting: " << getTotalWaiting() << endl;
  385. }
  386.  
  387. int Platform::getTotalWaiting(){
  388.     return totalWaiting;    
  389. }
  390.  
  391. bool Platform::isEmpty(){
  392.     return (totalWaiting == 0);    
  393. }
  394.  
  395. bool Platform::operator==(Platform &p2){
  396.     return (getLine() == p2.getLine() &&
  397.             getDirection() == p2.getDirection() &&
  398.             getStation() == p2.getStation());    
  399. }
  400.  
  401. bool Platform::hasTrain(){
  402.     return train;    
  403. }
  404.  
  405. void Platform::setTrain(bool t){
  406.     train = t;    
  407. }
  408.  
  409. void Platform::displayWaitLines(){
  410.     int tLine;
  411.     cout << "Waitlines for " << direction << " platform on " << line << " line at " << station << " station..." << endl;
  412.     for(int j = 0; j < 3; j++){  //1 to 3 cars
  413.         cout << "Car " << (j+1) << " : ";
  414.         for(int k = 0; k < 4; k++){  // 0 to 3 lines
  415.             if(k) cout << "\t";
  416.             tLine = j*4 + k;
  417.             cout << "Line " << (k+1) << " ... " << waitLines[tLine].getSize() << endl;
  418.         }
  419.     }    
  420. }
  421.  
  422. #endif
  423.  
  424. #ifndef train_class
  425. #define train_class
  426.  
  427. #include "Passenger.hpp"
  428. #include "PassengerQueue.hpp"
  429. #include "PassengerList.hpp"
  430. #include "Platform.hpp"
  431. #include "CTrainLine.hpp"
  432.  
  433. #define CAR_CAPACITY 64
  434. #define TRIP_LENGTH 3
  435.  
  436. class Train{
  437.     public:
  438.         Train(CTrainLine tLine, Platform start);
  439.         Train(CTrainLine tLine, Platform prev, int travelTime);
  440.         void update();
  441.         void travel();
  442.         void board();
  443.         CTrainLine getLine();
  444.         Platform getNextPlat();
  445.         Platform getPrevPlat();
  446.         Platform getCurrPlat();
  447.         string getStatus();
  448.         void setStatus(string newstat);
  449.         int getPop();
  450.         int getTripTime();
  451.         void displayTrainInfo();
  452.        
  453.     private:
  454.         PassengerList cars[3];
  455.         CTrainLine trainline;
  456.         string status,
  457.                location;
  458.         Platform * nextPlat, * prevPlat, * currPlat;
  459.         int tripTime;
  460. };
  461.  
  462. Train::Train(CTrainLine tLine, Platform start){
  463.     location = start.getStation();
  464.     trainline = tLine;
  465.      
  466.     currPlat = new Platform;
  467.     prevPlat = new Platform;
  468.     nextPlat = new Platform;
  469.    
  470.     *currPlat = start;    
  471.     *nextPlat = tLine.getNext(start);
  472.     prevPlat = 0;
  473.     tripTime = 0;
  474.     status = "traveling";
  475. }
  476.  
  477. Train::Train(CTrainLine tLine, Platform prev, int travelTime){
  478.     trainline = tLine;
  479.     *nextPlat = trainline.getNext(prev);
  480.     currPlat = 0;
  481.     *prevPlat = prev;
  482.     tripTime = travelTime;
  483.     location = "Between " + prev.getStation() + " and " + nextPlat->getStation();      
  484.     status = "traveling";
  485. }
  486.  
  487. void Train::update(){
  488.     if(status != "boarding"){
  489.         tripTime++;  
  490.         if(tripTime >= TRIP_LENGTH){
  491.             if(getNextPlat().hasTrain()){
  492.                 status = "waiting";    
  493.             } else {
  494.                 board();    
  495.             }
  496.         }
  497.     } else
  498.         travel();
  499. }
  500.  
  501. void Train::travel(){
  502.     currPlat->setTrain(false);
  503.     status = "traveling";
  504.     prevPlat = currPlat;
  505.     currPlat = 0;
  506.     tripTime = 0;
  507.     location = "Between " + prevPlat->getStation() + " and " + nextPlat->getStation();
  508. }
  509.  
  510. //INCOMPLETE
  511. void Train::board(){
  512.     tripTime = 0;
  513.     currPlat = nextPlat;
  514.  
  515.     nextPlat = new Platform;
  516.  /*  
  517.     cout << "3" <<endl;
  518.     nextPlat->displayInfo();
  519.     currPlat->displayInfo();
  520. */    
  521.     Platform currP = *currPlat;
  522. //    currP.displayInfo();
  523. //    cout << "3.25" << endl;
  524.     Platform next = trainline.getNext(currP);
  525. //    cout << "3.5" << endl;
  526.     *nextPlat = next;
  527. //    cout << "4" << endl;
  528.  /*
  529.     if(nextPlat && currPlat)
  530.     11(*nextPlat) = trainline.getNext(*currPlat);
  531.  */
  532.  //   cout << "5" << endl;
  533.     currPlat->setTrain(true);
  534.     status = "boarding";
  535.     Passenger thisP;
  536.     for(int n = 0; n < 3; n++){         //Let off passengers
  537.         int total = cars[n].getSize()+1;
  538.         for(int k = 1; k < total; k++){  //For every passenger in every car
  539.             thisP = cars[n].retrieve(k);          
  540.             if(thisP.getNextStation() == currPlat->getStation()){ //If this is his station...
  541.               cars[n].remove(k);           // he leaves the car
  542.               thisP.popStation();          // he crosses this station off his path
  543.               if(thisP.isAtDestination()){         // If this is his final destination...
  544.         //          passMan.removePassenger(thisP); //He should be removed from the simulation
  545.               } else{                              //Otherwise...
  546.         //          Platform nextPlat = passMan.findRoute(*currPlat.getStation(), thisP.getNextStation()); //He finds the platform to go to the next station  
  547.                   nextPlat->placeOnLine(thisP); //And he gets on line
  548.               }
  549.             }
  550.        
  551.         }
  552.     }
  553. }
  554.  
  555. CTrainLine Train::getLine(){
  556.     return trainline;
  557. }
  558.  
  559. Platform Train::getNextPlat(){
  560.     return *nextPlat;    
  561. }
  562.  
  563. Platform Train::getPrevPlat(){
  564.     return *prevPlat;    
  565. }
  566.  
  567. Platform Train::getCurrPlat(){
  568.     return *currPlat;
  569. }
  570.  
  571. string Train::getStatus(){
  572.     return status;    
  573. }
  574.  
  575. void Train::setStatus(string newstat){
  576.         status = newstat;
  577. }
  578.  
  579. int Train::getPop(){
  580.     return (cars[0].getSize() + cars[1].getSize() + cars[2].getSize());
  581. }
  582.  
  583. int Train::getTripTime(){
  584.     return tripTime;    
  585. }
  586.  
  587. void Train::displayTrainInfo(){
  588.     cout << "Train is at " << location << " -- " << status << " -- " << getPop() << " passengers" << endl;
  589. }
  590.  
  591. #endif
  592.  
  593. #ifndef PassengerQueue_class
  594. #define PassengerQueue_class
  595.  
  596. #include "Passenger.hpp"
  597.  
  598. using namespace std;
  599.  
  600. class PassengerQueue{
  601.     public:
  602.         struct qNode{
  603.             Passenger data;
  604.             qNode * next;
  605.         };
  606.         PassengerQueue();
  607.         bool isEmpty();
  608.         int getSize();
  609.         void enqueue(Passenger pass);
  610.         Passenger dequeue();
  611.         void displayQueue();
  612.         ~PassengerQueue();
  613.        
  614.     private:    
  615.        qNode * front;
  616.        qNode * back;
  617.        int size;  
  618.  
  619. };
  620.  
  621. PassengerQueue::PassengerQueue(){
  622.     size = 0;  
  623.     front = 0;
  624.     back = 0;  
  625. }
  626.  
  627.  
  628. bool PassengerQueue::isEmpty(){
  629.     return (!front);    
  630. }
  631.  
  632.  
  633. int PassengerQueue::getSize(){
  634.     return size;
  635. }
  636.  
  637. void PassengerQueue::enqueue(Passenger item){
  638.     qNode * node = new qNode;
  639.     node -> data = item;
  640.     node -> next = 0;
  641.     if(!front)
  642.         front = node;
  643.     else
  644.         back -> next = node;
  645.     back = node;    
  646.     size++;
  647. }
  648.  
  649. Passenger PassengerQueue::dequeue(){
  650.     try{
  651.         if(isEmpty())
  652.             throw 1;
  653.         else{
  654.             qNode * temp = front;
  655.             front = front -> next;
  656.             if(!front)
  657.                 back = 0;
  658.             Passenger result = temp -> data;
  659.             delete temp;
  660.             size--;
  661.             return result;    
  662.         }
  663.     }
  664.     catch(...){
  665.        // cout << "ERROR: Attempt to dequeue empty queue" << endl;
  666.     }
  667. }
  668.  
  669. void PassengerQueue::displayQueue(){
  670.     cout << "Queue Size: " << getSize() << endl;
  671.     qNode * temp;
  672.     for(int n = 0; n < size; n++){
  673.         if(!n)
  674.             temp = front;
  675.         else
  676.             temp = temp -> next;
  677.         cout << "Item " << n << ": " << (temp->data).getName() << endl;
  678.     }    
  679. }
  680.  
  681.  
  682. PassengerQueue::~PassengerQueue(){
  683.     qNode * curr;
  684.     qNode * next;
  685.     for(int n = 0; n < size; n++)
  686.         dequeue();  
  687. }
  688.  
  689.  
  690.  
  691. #endif
  692.  
  693. #ifndef passengerlist_class
  694. #define passengerlist_class
  695.  
  696. #include <iostream>
  697. #include "Passenger.hpp"
  698.  
  699. using namespace std;
  700.  
  701. class PassengerList{
  702.     public:
  703.         struct pNode{
  704.             Passenger data;
  705.             pNode * next;
  706.         };
  707.         PassengerList();
  708.         ~PassengerList();                 //Destructor deletes all nodes to prevent memory leak
  709.         bool isEmpty();
  710.         void add(Passenger P);
  711.         pNode * find(int pos);              //Returns a pointer to a node by position number        
  712.         int getSize();          
  713.         Passenger remove(int pos);             //Removes & deletes a node
  714.         Passenger retrieve(int pos);           //Retrieves the data of node
  715.         void displayList();
  716.     private:
  717.         pNode * head;
  718.         int size;
  719. };
  720.  
  721. PassengerList::PassengerList() {
  722.     size = 0;
  723.     head = new pNode;
  724. }
  725.  
  726. bool PassengerList::isEmpty(){
  727.     if(!size)
  728.         return true;
  729.     else return false;    
  730. }
  731.  
  732. void PassengerList::add(Passenger item){
  733.     pNode * temp = new pNode;
  734.     temp->data = item;
  735.     if(isEmpty())
  736.         head->next = temp;
  737.     else
  738.         find(size)->next = temp;
  739.     size++;
  740. }
  741.  
  742. int PassengerList::getSize(){
  743.     return size;
  744. }
  745.  
  746. struct PassengerList::pNode * PassengerList::find(int pos){
  747.     if(pos < 1 || pos > size)
  748.         throw 1;
  749.     else{
  750.         pNode * temp = head;
  751.         for(int n = 0; n < pos; n++)
  752.             temp = temp->next;
  753.         return temp;
  754.     }    
  755. }
  756.  
  757. Passenger PassengerList::remove(int pos){
  758.    try{
  759.         if(pos < 1 || pos > size || isEmpty())
  760.             throw 5;
  761.         else{
  762.             Passenger data;
  763.             pNode * curr,
  764.                      * prev;
  765.             if(pos == 1)
  766.                 prev = head;
  767.             else
  768.                 prev = find(pos-1);
  769.             curr = prev->next;
  770.             prev->next = curr->next;
  771.             data = curr->data;
  772.             delete curr;
  773.             size--;
  774.             return data;
  775.         }
  776.     } catch(...){
  777.         cout << "ERROR: PassengerList trying to remove nonexistant item" << endl;
  778.     }
  779. }
  780.  
  781. Passenger PassengerList::retrieve(int pos){
  782.     pNode * curr = find(pos);
  783.     return curr->data;
  784. }
  785.  
  786. void PassengerList::displayList(){
  787.     pNode * curr = head;
  788.     cout << "List size: " << size << endl;
  789.     for(int n = 0; n < size; n++){
  790.         curr = curr->next;
  791.         cout << "Entry " << (n+1) << ": " << curr->data.getName() << endl;
  792.     }    
  793. }
  794.  
  795. PassengerList::~PassengerList(){
  796.     for(int n = size; size > 0; n--)
  797.         remove(n);    
  798. }
  799.  
  800. #endif
  801.  
  802. #ifndef passenger_class
  803. #define passenger_class
  804.  
  805. #include <String>
  806.  
  807. #define LATE_THRESH 45
  808.  
  809. using namespace std;
  810.  
  811. class Passenger{
  812.     public:
  813.         Passenger();
  814.         Passenger(string pName, string entry, string dest);
  815.         void update();
  816.         void update(int min);
  817.         int getWaitTime();
  818.         int getTotalTime();
  819.         string getName();
  820.         string getPOE();
  821.         string getDestination();
  822.         bool isAtDestination();
  823.         void popStation();
  824.         string getNextStation();
  825.         void setWaiting(bool w);
  826.         bool isWaiting();
  827.         bool isLate();
  828.         bool mustTransfer();
  829.     private:
  830.         bool onSameLine(string entry,string dest);
  831.         string name,
  832.                path[3];
  833.         int destPointer,
  834.             waitTime,
  835.             totalTime;
  836.         bool transfer,
  837.              waiting,
  838.              late;
  839. };
  840.  
  841. Passenger::Passenger(){
  842.     name = "John Doe";
  843.     waitTime = 0;
  844.     totalTime = 0;
  845.     waiting = false;
  846.     late = false;    
  847. }
  848.  
  849. Passenger::Passenger(string pName, string entry = "DEFAULT", string dest = "DEFAULT"){
  850.     name = pName;
  851.     path[0] = entry;
  852.     destPointer = 0;
  853.     if(onSameLine(entry,dest)){
  854.         path[1] = dest;
  855.         transfer = false;
  856.     }
  857.     else{
  858.         path[1] = "b3";
  859.         path[2] = dest;
  860.         transfer = true;
  861.     }
  862.     waitTime = 0;
  863.     totalTime = 0;
  864.     waiting = false;
  865.     late = false;        
  866. }
  867.  
  868. void Passenger::update(){
  869.     update(1);    
  870. }
  871.  
  872. void Passenger::update(int min){
  873.     waitTime += min;
  874.     totalTime += min;
  875.     if((!late) && (totalTime > LATE_THRESH))
  876.         late = true;
  877. }
  878.  
  879. int Passenger::getWaitTime(){
  880.     return waitTime;
  881. }
  882.  
  883. int Passenger::getTotalTime(){
  884.     return totalTime;    
  885. }
  886.  
  887. string Passenger::getName(){
  888.     return name;    
  889. }
  890.  
  891. string Passenger::getPOE(){
  892.     return path[0];    
  893. }
  894.  
  895. string Passenger::getDestination(){
  896.     if(transfer)
  897.         return path[2];
  898.     else
  899.         return path[1];    
  900. }
  901.  
  902.  
  903. bool Passenger::isAtDestination(){
  904.     if((transfer && destPointer == 3) ||
  905.         (!transfer && destPointer == 4))
  906.         return true;
  907.     else
  908.         return false;
  909. }
  910.  
  911. void Passenger::setWaiting(bool w){
  912.     waiting = w;    
  913. }
  914.  
  915. bool Passenger::isLate(){
  916.     return late;
  917. }
  918.  
  919. bool Passenger::isWaiting(){
  920.     return waiting;    
  921. }
  922.  
  923. bool Passenger::mustTransfer(){
  924.     return transfer;    
  925. }
  926.  
  927. string Passenger::getNextStation(){
  928.     return path[destPointer];    
  929. }
  930.  
  931. void Passenger::popStation(){
  932.     destPointer++;    
  933. }
  934.  
  935. bool Passenger::onSameLine(string entry, string dest){
  936.     bool result = true;
  937.     if(entry != "b3" || dest != "b3"){         //if either begins or ends at B3, we know its on the same line
  938.         string redLine[6] = {"a1", "a2", "a3", "a4", "b4", "b5"};  
  939.         string blueLine[3] = {"b1", "b2", "a5"};
  940.         for(int n = 0; n < 6; n++){
  941.             if(result){      //prevents search from continuing if answer found
  942.                 for(int j = 0; j < 3; j++){       //For each combo of red line and blue line
  943.                     if((redLine[n] == entry && blueLine[j] == dest) ||    //If the entry is red and the dest is blue or vice versa
  944.                         (redLine[n] == dest && blueLine[j] == entry))
  945.                         result = false;               //answer is that they're not on the same line
  946.                 }
  947.             }
  948.         }
  949.     }  
  950.     return result;
  951.    
  952. }
  953.  
  954. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement