Master C++ with Real-time Projects and Kickstart Your Career Start Now!!
Program 1
//Railway Ticket Booking System
#include <iostream>
#include <string>
using namespace std;
const int MAX_SEATS = 10; // Max seat is 10
class Ticket
{
private:
int ticketID;
string name;
int age;
string gender;
string from, to;
bool isBooked;
public:
// Constructor
Ticket() {
isBooked = false;
}
void bookTicket(int id) //1001
{
if (isBooked) {
cout << " Seat already booked.\n";
return;
}
ticketID = id;
cout << "Enter Name: ";
cin.ignore();
getline(cin, name);
cout << "Enter Age: ";
cin >> age;
cout << "Enter Gender: ";
cin >> gender;
cout << "From Station: ";
cin.ignore();
getline(cin, from);
cout << "To Station: ";
getline(cin, to);
isBooked = true;
cout << " Ticket Booked Successfully! Ticket ID: " << ticketID<<"\n";
}
void displayTicket() {
if (!isBooked) {
cout << " No ticket found for this seat.\n";
return;
}
cout << "\n --- Ticket Details ---";
cout << "\nTicket ID: " << ticketID;
cout << "\nName : " << name;
cout << "\nAge : " << age;
cout << "\nGender : " << gender;
cout << "\nFrom : " << from;
cout << "\nTo : " << to;
cout << "\nStatus : Booked\n";
}
void cancelTicket() {
if (!isBooked) {
cout << " No ticket to cancel.\n";
return;
}
isBooked = false;
cout << " Ticket with ID " << ticketID << " cancelled successfully.\n";
}
bool getStatus() {
return isBooked;
}
};
int main() {
system("cls");
Ticket seats[MAX_SEATS];
int choice, seatNo;
do {
cout << "\n -------------Railway Ticket Booking Menu------------";
cout << "\n1. Book Ticket";
cout << "\n2. View Ticket";
cout << "\n3. Cancel Ticket";
cout << "\n4. Show Available Seats";
cout << "\n5. Exit";
cout<<"\n-------------------------------------------------------------";
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter Seat Number to Book (0-" << MAX_SEATS - 1 << "): ";
cin >> seatNo;
if (seatNo >= 0 && seatNo < MAX_SEATS) {
seats[seatNo].bookTicket(seatNo + 1001);
} else {
cout << " Invalid seat number.\n";
}
break;
case 2:
cout << "Enter Seat Number to View Ticket: ";
cin >> seatNo;
if (seatNo >= 0 && seatNo < MAX_SEATS) {
seats[seatNo].displayTicket();
} else {
cout << " Invalid seat number.\n";
}
break;
case 3:
cout << "Enter Seat Number to Cancel Ticket: ";
cin >> seatNo; //0
if (seatNo >= 0 && seatNo < MAX_SEATS) {
seats[seatNo].cancelTicket();
} else {
cout << " Invalid seat number.\n";
}
break;
case 4:
cout << "\n Available Seats:\n";
for (int i = 0; i < MAX_SEATS; i++) {
cout << "Seat " << i << ": " << (seats[i].getStatus() ? "Booked" : "Available") << "\n";
}
break;
case 5:
cout << "\nThank you for using Railway Booking System!\n";
break;
default:
cout << " Invalid choice. Try again.\n";
}
} while (choice != 5);
return 0;
}