Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #ifndef SICT_PASSENGER_H
  2. #define SICT_PASSENGER_H
  3.  
  4. namespace sict
  5. {
  6.     class Passenger
  7.     {
  8.         char name[32];
  9.         char destination[32];
  10.     public:
  11.         Passenger();
  12.         Passenger(char* nameString, char* destinationString);
  13.         bool isEmpty() const;
  14.         void display() const;
  15.         void setEmpty();
  16.     };
  17. }
  18.  
  19. #endif //SICT_PASSENGER_H
  20.  
  21. namespace sict
  22. {
  23.     Passenger::Passenger()
  24.     {
  25.         setEmpty();
  26.     }
  27.     Passenger::Passenger(char* nameString, char* destinationString)
  28.     {
  29.         if ("" == nameString || "" == destinationString || '\0' == nameString || '\0' == destinationString)
  30.         {
  31.             setEmpty();
  32.         }
  33.         else
  34.         {
  35.             for (int i = 0; i < strlen(name); i++)
  36.             {
  37.                 name[i] = nameString[i];
  38.             }
  39.             for (int i = 0; i < strlen(destination); i++)
  40.             {
  41.                 destination[i] = destinationString[i];
  42.             }
  43.         }
  44.     }
  45.     void Passenger::setEmpty()
  46.     {
  47.         name[0] = '\0';
  48.         destination[0] = '\0';
  49.     }
  50.     bool Passenger::isEmpty() const
  51.     {
  52.         if ('\0' == name[0] && '\0' == destination[0])
  53.         {
  54.             return true;
  55.         }
  56.         else
  57.         {
  58.             return false;
  59.         }
  60.     }
  61.     void Passenger::display() const
  62.     {
  63.         if (!isEmpty())
  64.         {
  65.             for (int i = 0; i < strlen(name); i++)
  66.             {
  67.                 cout << name[i];
  68.             }
  69.             cout << " - ";
  70.             for (int i = 0; i < strlen(destination); i++)
  71.             {
  72.                 cout << destination[i];
  73.             }
  74.             cout << endl;
  75.         }
  76.         else
  77.         {
  78.             cout << "No Passenger!" << endl;
  79.         }
  80.     }
  81. }
  82.  
  83. #include <cstring>
  84. #include <iostream>
  85. using namespace std;
  86. using namespace sict;
  87. int main()
  88. {
  89.     Passenger travellers[] = {
  90.         Passenger(nullptr, "Toronto"),
  91.         Passenger("", "Toronto"),
  92.         Passenger("John Smith", nullptr),
  93.         Passenger("John Smith", ""),
  94.         Passenger("John Smith", "Toronto"), // valid
  95.         Passenger(nullptr, nullptr),
  96.         Passenger()
  97.     };
  98.     cout << "----------------------------------------" << endl;
  99.     cout << "Testing the validation logic" << endl;
  100.     cout << "(only passenger 5 should be valid)" << endl;
  101.     cout << "----------------------------------------" << endl;
  102.     for (int i = 0; i < 7; ++i)
  103.     {
  104.         cout << "Passenger " << i + 1 << ": " << (travellers[i].isEmpty() ? "not valid" : "valid") << endl;
  105.     }
  106.     cout << "----------------------------------------" << endl << endl;
  107.  
  108.     Passenger vanessa("Vanessa", "Paris"),
  109.               mike("Mike", "Tokyo"),
  110.               alice("Alice", "Paris");
  111.     cout << "----------------------------------------" << endl;
  112.     cout << "Testing the display function" << endl;
  113.     cout << "----------------------------------------" << endl;
  114.     vanessa.display();
  115.     mike.display();
  116.     alice.display();
  117.     travellers[0].display();
  118.     cout << "----------------------------------------" << endl << endl;
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement