Here you can find job related posts so you can end your search for the perfect job for you or your friends.

Flight Booking Ticket Program in C

1. Structures and Data Types: Define structures to represent flights, passengers, and bookings.

struct Flight {
int flightNumber;
char origin[50];
char destination[50];
float price;
int seatsAvailable;
};

struct Passenger {
char name[50];
int age;
char gender;
};

struct Booking {
int bookingId;
struct Flight flight;
struct Passenger passenger;
int seatNumber;
}; 

Apply Now Here:

2. Function Prototypes: Declare Functions To Handle Various Operations Like Booking, Canceling, And Displaying Flights.

 
void displayMenu();
void displayAvailableFlights(struct Flight flights[], int numFlights);
int bookFlight(struct Flight flights[], int numFlights, struct Booking bookings[], int *nextBookingId);
void displayBookings(struct Booking bookings[], int numBookings);
int cancelBooking(struct Booking bookings[], int numBookings); 
 

3. Main Function: Implement the main logic of the program where users can interact with the system.

 
 

int main() {
// Declare variables and initialize data
struct Flight flights[] = {
{1, “New York”, “Los Angeles”, 250.0, 100},
{2, “Chicago”, “San Francisco”, 300.0, 80},
{3, “Boston”, “Miami”, 200.0, 120}
};
struct Booking bookings[100];
int nextBookingId = 1;
int numBookings = 0;
int choice;

do {
// Display menu and get user choice
displayMenu();
printf(“Enter your choice: “);
scanf(“%d”, &choice);

switch (choice) {
case 1:
displayAvailableFlights(flights, sizeof(flights) / sizeof(flights[0]));
break;
case 2:
numBookings += bookFlight(flights, sizeof(flights) / sizeof(flights[0]), bookings, &nextBookingId);
break;
case 3:
displayBookings(bookings, numBookings);
break;
case 4:
numBookings -= cancelBooking(bookings, numBookings);
break;
case 5:
printf(“Exiting program.\n”);
break;
default:
printf(“Invalid choice! Please enter a valid option.\n”);
}
} while (choice != 5);

return 0;
}

4. Function Implementations: Implement the functions declared earlier.

void displayMenu() {
printf(“\n===== Flight Booking System Menu =====\n”);
printf(“1. Display available flights\n”);
printf(“2. Book a flight\n”);
printf(“3. Display bookings\n”);
printf(“4. Cancel a booking\n”);
printf(“5. Exit\n”);
}

void displayAvailableFlights(struct Flight flights[], int numFlights) {
printf(“\nAvailable Flights:\n”);
printf(“————————————————–\n”);
printf(“Flight No.\tFrom\t\tTo\t\tPrice\n”);
printf(“————————————————–\n”);
for (int i = 0; i < numFlights; ++i) {
printf(“%d\t\t%s\t\t%s\t\t%.2f\n”, flights[i].flightNumber, flights[i].origin, flights[i].destination, flights[i].price);
}
printf(“————————————————–\n”);
}

int bookFlight(struct Flight flights[], int numFlights, struct Booking bookings[], int *nextBookingId) {
int flightNumber;
printf(“\nEnter flight number to book: “);
scanf(“%d”, &flightNumber);

// Find the selected flight
for (int i = 0; i < numFlights; ++i) {
if (flights[i].flightNumber == flightNumber) {
if (flights[i].seatsAvailable > 0) {
// Book the flight
bookings[*nextBookingId – 1].bookingId = *nextBookingId;
bookings[*nextBookingId – 1].flight = flights[i];
printf(“Enter passenger name: “);
scanf(“%s”, bookings[*nextBookingId – 1].passenger.name);
printf(“Enter passenger age: “);
scanf(“%d”, &bookings[*nextBookingId – 1].passenger.age);
printf(“Enter passenger gender (M/F): “);
scanf(” %c”, &bookings[*nextBookingId – 1].passenger.gender);
printf(“Enter seat number: “);
scanf(“%d”, &bookings[*nextBookingId – 1].seatNumber);
flights[i].seatsAvailable–;
(*nextBookingId)++;
printf(“Booking successful!\n”);
return 1;
} else {
printf(“Sorry, no seats available for this flight.\n”);
return 0;
}
}
}
printf(“Flight not found!\n”);
return 0;
}

void displayBookings(struct Booking bookings[], int numBookings) {
if (numBookings > 0) {
printf(“\nYour Bookings:\n”);
printf(“—————————————————————————————————\n”);
printf(“Booking ID\tFlight No.\tFrom\t\tTo\t\tPassenger Name\tAge\tGender\tSeat No.\n”);
printf(“—————————————————————————————————\n”);
for (int i = 0; i < numBookings; ++i) {
printf(“%d\t\t%d\t\t%s\t\t%s\t\t%s\t\t%d\t%c\t%d\n”, bookings[i].bookingId, bookings[i].flight.flightNumber,
bookings[i].flight.origin, bookings[i].flight.destination, bookings[i].passenger.name,
bookings[i].passenger.age, bookings[i].passenger.gender, bookings[i].seatNumber);
}
printf(“—————————————————————————————————\n”);
} else {
printf(“\nNo bookings found.\n”);
}
}

int cancelBooking(struct Booking bookings[], int numBookings) {
int bookingId;
printf(“\nEnter booking ID to cancel: “);
scanf(“%d”, &bookingId);

// Find the booking
for (int i = 0; i < numBookings; ++i) {
if (bookings[i].bookingId == bookingId) {
// Cancel the booking
printf(“Booking ID %d canceled.\n”, bookings[i].bookingId);
// Shift bookings to cover the canceled one
for (int j = i; j < numBookings – 1; ++j) {
bookings[j] = bookings[j + 1];
}
return 1;
}
}
printf(“Booking ID not found!\n”);
return 0;
}

 

Leave a Comment