In: Computer Science
In C++,
Designe a ticketing system for an airline in which there will be multiple flights, all containing passengers. Our airline prides itself on the fact that pets may fly in seats if the customer purchases a full ticket for the pet (note, it is not necessary for the customer to accompany the pet on the flight). Thus, any seat may be occupied by a pet or by a person either of which is considered a passenger. Please design the following classes as well as any other classes which you deem necessary:
a. Flight. The flight will hold a vector (this must be a single vector, not two) of passengers, and should have a function for adding passengers as well as a function to calculate the total revenue for the entire flight. The revenue is the sum of all ticket prices on the flight.
b. Person. This class will have a function “getTicketCost” which will return how much the person paid for the ticket. The cost will be determined when the ticket is created and will be different for each person.
c. Pet. This class will have a function “getTicketCost” which will return how much the pet paid for the ticket. The cost will be determined when the ticket is created and will be different for each pet.
Code:
#include <iostream>
using namespace std;
class Passenger
{
protected:
string m_name;
string m_id;
int m_age;
int milesTravelling;
public:
Passenger(string name, int age, string id, int
miles)
{
m_name = name;
m_age = age;
m_id = id;
milesTravelling = miles;
}
virtual int getTicketCost() = 0;
};
class Person : public Passenger
{
public:
Person(string name,int age,string id,int
miles):Passenger(name
,age,id,miles)
{
}
int getTicketCost() override
{
if (m_age < 20)
return
milesTravelling * 120;
return milesTravelling * 150;
}
};
class Pet : public Passenger
{
public:
Pet(string name, int age, string id, int miles)
:Passenger(name
, age, id, miles)
{
}
int getTicketCost() override
{
if (m_age < 5)
return
milesTravelling * 80;
return milesTravelling * 120;
}
};
class Flight
{
vector<Passenger*> passengers;
public:
void addPassenger(Passenger* p)
{
passengers.push_back(p);
}
int calculateTotalRevenue()
{
int totalRevenue = 0;
for(auto& p : passengers)
{
totalRevenue +=
p->getTicketCost();
}
return totalRevenue;
}
};
int main()
{
Flight f;
Person person("A", 23, "12", 125);
Pet apet("B", 15, "13", 140);
Passenger& p1 = person;
Passenger& p2 = apet;
f.addPassenger(&p1);
f.addPassenger(&p2);
cout << "Total Revenue : " <<
f.calculateTotalRevenue();
return 0;
}
OUTPUT: