0% found this document useful (0 votes)
26 views7 pages

New Text Document

The document defines classes for passengers, crew members, flights, and an airline. It includes attributes like name, ID, and ticket details for passengers. Methods are included to add, remove, and retrieve passengers and crew for a flight. A menu driver is provided to test the classes.

Uploaded by

i233029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

New Text Document

The document defines classes for passengers, crew members, flights, and an airline. It includes attributes like name, ID, and ticket details for passengers. Methods are included to add, remove, and retrieve passengers and crew for a flight. A menu driver is provided to test the classes.

Uploaded by

i233029
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <iostream>

#include <string>

using namespace std;

const int MAX_FLIGHTS = 10;


const int MAX_PASSENGERS = 100;
const int MAX_CREW_MEMBERS = 20;

class Passenger {
private:
string name;
string ticketClass;
double ticketPrice;
int id;

public:
Passenger(const string& newName, const string& tClass, double tPrice, int
newID) : name(newName), ticketClass(tClass), ticketPrice(tPrice), id(newID) {}

string getName() const { return name; }


string getTicketClass() const { return ticketClass; }
double getTicketPrice() const { return ticketPrice; }
int getID() const { return id; }

void setTicketClass(const string& tClass) { ticketClass = tClass; }


void adjustTicketPrice(double newPrice) { ticketPrice = newPrice; }
};

class CrewMember {
private:
string name;
string role;
int id;

public:
CrewMember(const string& newName, const string& newRole, int newID) :
name(newName), role(newRole), id(newID) {}

string getName() const { return name; }


string getRole() const { return role; }
int getID() const { return id; }
};

class Flight {
private:
string flightNumber;
int capacity;
string flightTime;
Passenger** passengers;
CrewMember** crew;
int passengerCount;
int crewCount;

public:
Flight(const string& number, const string& time, int cap) :
flightNumber(number), flightTime(time), capacity(cap), passengerCount(0),
crewCount(0) {
passengers = new Passenger*[capacity];
crew = new CrewMember*[capacity / 5];
}

~Flight() {
cancelFlight();
}

string getFlightNumber() const { return flightNumber; }


string getFlightTime() const { return flightTime; }
int getCapacity() const { return capacity; }

void setFlightTime(const string& newTime) { flightTime = newTime; }

void addPassenger(Passenger* passenger) {


if (passengerCount < capacity) {
passengers[passengerCount++] = passenger;
}
}

void removePassenger(int passengerID) {


for (int i = 0; i < passengerCount; ++i) {
if (passengers[i]->getID() == passengerID) {
delete passengers[i];
for (int j = i; j < passengerCount - 1; ++j) {
passengers[j] = passengers[j + 1];
}
passengerCount--;
return;
}
}
}

void addCrewMember(CrewMember* crewMember) {


if (crewCount < capacity / 5) {
crew[crewCount++] = crewMember;
}
}

void removeCrewMember(int crewMemberID) {


for (int i = 0; i < crewCount; ++i) {
if (crew[i]->getID() == crewMemberID) {
delete crew[i];
for (int j = i; j < crewCount - 1; ++j) {
crew[j] = crew[j + 1];
}
crewCount--;
return;
}
}
}

void displayPassengers() {
for (int i = 0; i < passengerCount; ++i) {
cout << "Name: " << passengers[i]->getName() << ", Ticket Class: " <<
passengers[i]->getTicketClass() << ", Ticket Price: $" << passengers[i]-
>getTicketPrice() << endl;
}
}
void displayCrew() {
for (int i = 0; i < crewCount; ++i) {
cout << "Name: " << crew[i]->getName() << ", Role: " << crew[i]-
>getRole() << endl;
}
}

void cancelFlight() {
for (int i = 0; i < passengerCount; ++i) {
delete passengers[i];
}
for (int i = 0; i < crewCount; ++i) {
delete crew[i];
}
passengerCount = 0;
crewCount = 0;
delete[] passengers;
delete[] crew;
passengers = nullptr;
crew = nullptr;
}

void changePassengerClass(int passengerID, const string& newClass) {


for (int i = 0; i < passengerCount; ++i) {
if (passengers[i]->getID() == passengerID) {
passengers[i]->setTicketClass(newClass);
return;
}
}
}

void adjustTicketPrice(Passenger* passenger, double basePrice) {


double newPrice = basePrice;
string ticketClass = passenger->getTicketClass();
if (ticketClass == "vip") {
newPrice += 20;
} else if (ticketClass == "economy") {
newPrice += 10;
} else if (ticketClass == "business") {
newPrice += 15;
}
// Adjust price if booking time is between 0 and 6
// Assuming booking time is based on 24-hour format
int bookingHour = stoi(flightTime.substr(0, 2));
if (bookingHour >= 0 && bookingHour <= 6) {
newPrice -= 2;
}
passenger->adjustTicketPrice(newPrice);
}
};

class Airline {
private:
string name;
Flight** flights;
int flightCount;

public:
Airline(const string& airlineName) : name(airlineName), flightCount(0) {
flights = new Flight*[MAX_FLIGHTS];
}

~Airline() {
for (int i = 0; i < flightCount; ++i) {
delete flights[i];
}
delete[] flights;
}

void addFlight(Flight* flight) {


if (flightCount < MAX_FLIGHTS) {
flights[flightCount++] = flight;
}
}

void displayFlights() {
for (int i = 0; i < flightCount; ++i) {
cout << "Flight " << i + 1 << ": " << flights[i]->getFlightNumber() <<
" | Time: " << flights[i]->getFlightTime() << endl;
}
}

Flight* findFlight(const string& flightNumber) {


for (int i = 0; i < flightCount; ++i) {
if (flights[i]->getFlightNumber() == flightNumber) {
return flights[i];
}
}
return nullptr;
}

void cancelFlight(const string& flightNumber) {


for (int i = 0; i < flightCount; ++i) {
if (flights[i]->getFlightNumber() == flightNumber) {
delete flights[i];
for (int j = i; j < flightCount - 1; ++j) {
flights[j] = flights[j + 1];
}
flightCount--;
return;
}
}
}
};

void displayMenu() {
cout << "\nMenu:\n";
cout << "1. Add Flight\n";
cout << "2. Cancel Flight\n";
cout << "3. Add Passenger to Flight\n";
cout << "4. Remove Passenger from Flight\n";
cout << "5. Add Crew Member to Flight\n";
cout << "6. Remove Crew Member from Flight\n";
cout << "7. Display Flights\n";
cout << "8. Display Passengers for Flight\n";
cout << "9. Display Crew for Flight\n";
cout << "10. Change Flight Time\n";
cout << "11. Change Passenger Class\n";
cout << "12. Exit\n";
cout << "Enter your choice: ";
}

int main() {
Airline airline("MyAirline");

int choice;
do {
displayMenu();
cin >> choice;

switch (choice) {
case 1: {
string flightNumber, flightTime;
int capacity;
cout << "Enter flight number: ";
cin >> flightNumber;
cout << "Enter flight time: ";
cin >> flightTime;
cout << "Enter capacity: ";
cin >> capacity;
Flight* flight = new Flight(flightNumber, flightTime, capacity);
airline.addFlight(flight);
cout << "Flight added successfully.\n";
break;
}
case 2: {
string flightNumber;
cout << "Enter flight number to cancel: ";
cin >> flightNumber;
airline.cancelFlight(flightNumber);
break;
}
case 3: {
string flightNumber, passengerName, ticketClass;
double ticketPrice;
int passengerID;
cout << "Enter flight number: ";
cin >> flightNumber;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
cout << "Enter passenger name: ";
cin >> passengerName;
cout << "Enter ticket class (business, economy, vip): ";
cin >> ticketClass;
cout << "Enter passenger ticket price: ";
cin >> ticketPrice;
cout << "Enter passenger ID: ";
cin >> passengerID;
Passenger* passenger = new Passenger(passengerName,
ticketClass, ticketPrice, passengerID);
flight->addPassenger(passenger);
}
break;
}
case 4: {
string flightNumber;
int passengerID;
cout << "Enter flight number: ";
cin >> flightNumber;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
cout << "Enter passenger ID: ";
cin >> passengerID;
flight->removePassenger(passengerID);
}
break;
}
case 5: {
string flightNumber, crewName, crewRole;
int crewID;
cout << "Enter flight number: ";
cin >> flightNumber;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
cout << "Enter crew member name: ";
cin >> crewName;
cout << "Enter crew member role (pilot, co-pilot, attendant):
";
cin >> crewRole;
cout << "Enter crew member ID: ";
cin >> crewID;
CrewMember* crewMember = new CrewMember(crewName, crewRole,
crewID);
flight->addCrewMember(crewMember);
}
break;
}
case 6: {
string flightNumber;
int crewID;
cout << "Enter flight number: ";
cin >> flightNumber;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
cout << "Enter crew member ID: ";
cin >> crewID;
flight->removeCrewMember(crewID);
}
break;
}
case 7: {
airline.displayFlights();
break;
}
case 8: {
string flightNumber;
cout << "Enter flight number: ";
cin >> flightNumber;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
flight->displayPassengers();
}
break;
}
case 9: {
string flightNumber;
cout << "Enter flight number: ";
cin >> flightNumber;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
flight->displayCrew();
}
break;
}
case 10: {
string flightNumber, newFlightTime;
cout << "Enter flight number: ";
cin >> flightNumber;
cout << "Enter new flight time: ";
cin >> newFlightTime;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
flight->setFlightTime(newFlightTime);
cout << "Flight time changed successfully.\n";
}
break;
}
case 11: {
string flightNumber;
int passengerID;
string newClass;
cout << "Enter flight number: ";
cin >> flightNumber;
cout << "Enter passenger ID: ";
cin >> passengerID;
cout << "Enter new ticket class (business, economy, vip): ";
cin >> newClass;
Flight* flight = airline.findFlight(flightNumber);
if (flight != nullptr) {
flight->changePassengerClass(passengerID, newClass);
cout << "Passenger class changed successfully.\n";
}
break;
}
case 12: {
cout << "Exiting program.\n";
break;
}
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 12);

return 0;
}

You might also like