Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef SICT_PASSENGER_H
- #define SICT_PASSENGER_H
- namespace sict
- {
- class Passenger
- {
- char name[32];
- char destination[32];
- public:
- Passenger();
- Passenger(char* nameString, char* destinationString);
- bool isEmpty() const;
- void display() const;
- void setEmpty();
- };
- }
- #endif //SICT_PASSENGER_H
- namespace sict
- {
- Passenger::Passenger()
- {
- setEmpty();
- }
- Passenger::Passenger(char* nameString, char* destinationString)
- {
- if ("" == nameString || "" == destinationString || '\0' == nameString || '\0' == destinationString)
- {
- setEmpty();
- }
- else
- {
- for (int i = 0; i < strlen(name); i++)
- {
- name[i] = nameString[i];
- }
- for (int i = 0; i < strlen(destination); i++)
- {
- destination[i] = destinationString[i];
- }
- }
- }
- void Passenger::setEmpty()
- {
- name[0] = '\0';
- destination[0] = '\0';
- }
- bool Passenger::isEmpty() const
- {
- if ('\0' == name[0] && '\0' == destination[0])
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- void Passenger::display() const
- {
- if (!isEmpty())
- {
- for (int i = 0; i < strlen(name); i++)
- {
- cout << name[i];
- }
- cout << " - ";
- for (int i = 0; i < strlen(destination); i++)
- {
- cout << destination[i];
- }
- cout << endl;
- }
- else
- {
- cout << "No Passenger!" << endl;
- }
- }
- }
- #include <cstring>
- #include <iostream>
- using namespace std;
- using namespace sict;
- int main()
- {
- Passenger travellers[] = {
- Passenger(nullptr, "Toronto"),
- Passenger("", "Toronto"),
- Passenger("John Smith", nullptr),
- Passenger("John Smith", ""),
- Passenger("John Smith", "Toronto"), // valid
- Passenger(nullptr, nullptr),
- Passenger()
- };
- cout << "----------------------------------------" << endl;
- cout << "Testing the validation logic" << endl;
- cout << "(only passenger 5 should be valid)" << endl;
- cout << "----------------------------------------" << endl;
- for (int i = 0; i < 7; ++i)
- {
- cout << "Passenger " << i + 1 << ": " << (travellers[i].isEmpty() ? "not valid" : "valid") << endl;
- }
- cout << "----------------------------------------" << endl << endl;
- Passenger vanessa("Vanessa", "Paris"),
- mike("Mike", "Tokyo"),
- alice("Alice", "Paris");
- cout << "----------------------------------------" << endl;
- cout << "Testing the display function" << endl;
- cout << "----------------------------------------" << endl;
- vanessa.display();
- mike.display();
- alice.display();
- travellers[0].display();
- cout << "----------------------------------------" << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement