Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: C++  |  size: 2.62 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Lab 14
  2. // Programmer: Arthur Zvenigorodsky
  3. // Editor used: Eclipse IDE for C/C++ Linux Developers
  4. // Compiler used: GCC C++ Compiler
  5.  
  6. #include <iostream>
  7. using std::ostream;
  8.  
  9. #include <string>
  10. using std::string;
  11.  
  12. #include <vector>
  13. using std::vector;
  14.  
  15. #include "Rider.h"
  16. #include "Floor.h"
  17. #include "Elevator.h"
  18.  
  19. Floor::Floor(const char* name, const int location)
  20. : NAME(name), location(location)
  21. {
  22. }
  23.  
  24. // non-inline functions
  25. bool Floor::isPreferredDirectionUp() const // based on Rider with smallest ID
  26. {
  27.         // if there are no downRiders. return true
  28.         if (!(hasDownRiders()))
  29.         {
  30.                 return 1;
  31.         }
  32.  
  33.         else if (!(hasUpRiders()))
  34.         {
  35.                 return 0;
  36.         }
  37.  
  38.         // if the ID of the first upRider (upRider[0]) is less than that of the first downRider
  39.         else if (upRiders[0] < downRiders[0])
  40.         {
  41.                 return 1;
  42.         }
  43.  
  44.         else
  45.         {
  46.                 return 0;
  47.         }
  48. }
  49.  
  50. void Floor::addNewRider(const Rider& rider) // add to up- or down-vector
  51. {
  52.         // if added rider's destination is greater than the floor's location
  53.     if (rider.getDestination().getLocation() > this->getLocation())
  54.     {
  55.         // add rider to the upRiders vector
  56.         upRiders.push_back(rider);
  57.     }
  58.  
  59.         // else add rider to the downRiders vector
  60.     {
  61.         downRiders.push_back(rider);
  62.     }
  63. }
  64.  
  65. // removeUpRiders -- int is max #of riders...
  66. vector<Rider> Floor::removeUpRiders(int max)
  67. {
  68.         vector<Rider> removedRiders;
  69.  
  70.     if (hasUpRiders())
  71.     {
  72.         vector<Rider> remainingRiders;
  73.         for (int i = 0; i < upRiders.size(); i++)
  74.         {
  75.                 if (max > removedRiders.size())
  76.             {
  77.                         removedRiders.push_back(upRiders[i]);
  78.             }
  79.  
  80.                 else
  81.             {
  82.                         remainingRiders.push_back(upRiders[i]);
  83.             }
  84.         }
  85.  
  86.         upRiders = remainingRiders;
  87.     }
  88.  
  89.     return removedRiders;
  90. }
  91.  
  92. // removeDownRiders -- ...to move onto elevator
  93. vector<Rider> Floor::removeDownRiders(int max)
  94. {
  95.         vector<Rider> removedRiders;
  96.  
  97.         if (hasDownRiders())
  98.         {
  99.                 vector<Rider> remainingRiders;
  100.  
  101.         for (int i = 0; i < downRiders.size(); i++)
  102.         {
  103.                 if (max > removedRiders.size())
  104.             {
  105.                         removedRiders.push_back(downRiders[i]);
  106.             }
  107.  
  108.                 else
  109.             {
  110.                         remainingRiders.push_back(downRiders[i]);
  111.             }
  112.         }
  113.  
  114.         downRiders = remainingRiders;
  115.     }
  116.  
  117.         return removedRiders;
  118. }
  119.  
  120.  
  121. // friend function
  122. ostream& operator<<(ostream& out, const Floor& f)
  123. {
  124.         out << "Name: " << f.getName() << "\nLocation: " << f.getLocation() << "\nNumber of Up Riders: " << f.getUpRiderCount() << "\nNumber of Down Riders: " << f.getDownRiderCount();
  125.         return out;
  126. }