🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

A train reservation system is essential for managing train schedules, seat bookings, and cancellations efficiently. This project demonstrates a simple train reservation system using C++, leveraging structs, vectors, file handling, and functions for data management and persistence.
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
struct Train {
int trainNumber;
string trainName;
string source;
string destination;
int availableSeats;
void display() {
cout << "Train No: " << trainNumber
<< " | Name: " << trainName
<< " | From: " << source
<< " | To: " << destination
<< " | Available Seats: " << availableSeats << endl;
}
};
struct Booking {
int trainNumber;
string passengerName;
int seatNumber;
void display() {
cout << "Train No: " << trainNumber
<< " | Passenger: " << passengerName
<< " | Seat No: " << seatNumber << endl;
}
};
vector<Train> trains;
vector<Booking> bookings;
void loadTrains() {
ifstream file("trains.txt");
if (file.is_open()) {
Train t;
while (file >> t.trainNumber >> t.trainName >> t.source >> t.destination >> t.availableSeats) {
trains.push_back(t);
}
file.close();
}
}
void saveTrains() {
ofstream file("trains.txt");
for (auto &t : trains) {
file << t.trainNumber << " " << t.trainName << " "
<< t.source << " " << t.destination << " "
<< t.availableSeats << endl;
}
file.close();
}
void addTrain() {
Train t;
cout << "Enter Train Number: ";
cin >> t.trainNumber;
cout << "Enter Train Name: ";
cin >> t.trainName;
cout << "Enter Source: ";
cin >> t.source;
cout << "Enter Destination: ";
cin >> t.destination;
cout << "Enter Available Seats: ";
cin >> t.availableSeats;
trains.push_back(t);
saveTrains();
cout << "Train added successfully!\n";
}
void viewTrains() {
if (trains.empty()) {
cout << "No trains available.\n";
return;
}
for (auto &t : trains) {
t.display();
}
}
void bookTicket() {
int trainNum;
string name;
cout << "Enter Train Number: ";
cin >> trainNum;
for (auto &t : trains) {
if (t.trainNumber == trainNum && t.availableSeats > 0) {
cout << "Enter Passenger Name: ";
cin >> name;
t.availableSeats--;
saveTrains();
Booking b = {trainNum, name, t.availableSeats + 1};
bookings.push_back(b);
cout << "Ticket booked successfully!\n";
return;
}
}
cout << "Train not found or no seats available.\n";
}
void viewBookings() {
if (bookings.empty()) {
cout << "No bookings yet.\n";
return;
}
for (auto &b : bookings) {
b.display();
}
}
void cancelTicket() {
int trainNum;
string name;
cout << "Enter Train Number: ";
cin >> trainNum;
cout << "Enter Passenger Name: ";
cin >> name;
for (auto it = bookings.begin(); it != bookings.end(); ++it) {
if (it->trainNumber == trainNum && it->passengerName == name) {
bookings.erase(it);
for (auto &t : trains) {
if (t.trainNumber == trainNum) {
t.availableSeats++;
saveTrains();
break;
}
}
cout << "Ticket cancelled successfully!\n";
return;
}
}
cout << "Booking not found.\n";
}
void menu() {
int choice;
do {
cout << "1. Add Train\n";
cout << "2. View Trains\n";
cout << "3. Book Ticket\n";
cout << "4. View Bookings\n";
cout << "5. Cancel Ticket\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: addTrain(); break;
case 2: viewTrains(); break;
case 3: bookTicket(); break;
case 4: viewBookings(); break;
case 5: cancelTicket(); break;
case 6: cout << "Exiting...\n"; break;
default: cout << "Invalid choice! Please try again.\n";
}
} while (choice != 6);
}
Compile the Program:
g++ train_reservation.cpp -o train_reservation
Run the Program:
./train_reservation
Follow the on-screen menu to add trains, book tickets, view reservations, and cancel bookings.
Example 1: Regional Commuter Rail Reservation
Local commuter rail systems in multiple regions use similar systems for managing daily train schedules and booking seats. For instance, a regional transport authority might use an automatic system for updating train availability in real time. This ensures that seats can be quickly reserved during peak hours, reducing administrative costs and errors.
Example 2: Tourist Train Service
Scenic rail trips often have limited seating and require efficient management of reservations. A smaller reservation system, like the one demonstrated here, enables tourist train services to handle bookings, cancellations, and schedule updates seamlessly. This efficiency results in improved customer service and optimal resource utilization.
Case Study 1: Seasonal Workforce Management in a Holiday Resort
Scenario:
A holiday resort experienced significant occupancy fluctuations during peak season and had to hire 50+ temporary staff to manage increased demand.
Deployment:
The resort deployed a train reservation system in C++ that was enhanced with features like overtime calculation for extra working hours and HR tools for filtering employees by employment type (full-time, temporary, seasonal, part-time).
Advantages:
Case Study 2: Multi‑State Tax Compliance for a Transport Company
Scenario:
A transport company operating in multiple states needed to manage various tax rules for over 200 employees and several train services.
Deployment:
The system was modified to include state codes within the product (or employee) details, along with a function to calculate taxes according to state-specific rates:
double getStateTaxRate(string stateCode) {
if (stateCode == "TX") return 0.13;
else if (stateCode == "FL") return 0.10;
// Additional state logic...
else return 0.10; // default rate
}
Advantages:
Approach 1: Ensuring Data Persistence During Power Failures
Challenge:
A startup company lost its bookings data following an electricity outage.
Solution:
Implement file I/O methods to save current train and booking data before shutdown and reload the data during system startup:
void saveTrains() {
ofstream file("trains.txt");
for (const auto& t : trains) {
file << t.trainNumber << "," << t.trainName << ","
<< t.source << "," << t.destination << ","
<< t.availableSeats << "\n";
}
file.close();
}
Outcome:
This approach ensures that data remains safe across sessions and provides a mechanism for recovery after unexpected power outages.
Approach 2: Input Validation to Prevent Invalid Data Entry
Challenge:
HR staff sometimes enter unrealistic or negative values for available seats, causing errors in reservation calculations.
Solution:
Incorporate validation loops in input functions to ensure data integrity before acceptance:
void addTrain() {
Train t;
cout << "Enter Train Number: ";
cin >> t.trainNumber;
cout << "Enter Train Name: ";
cin >> t.trainName;
cout << "Enter Source: ";
cin >> t.source;
cout << "Enter Destination: ";
cin >> t.destination;
do {
cout << "Enter Available Seats (>=0): ";
cin >> t.availableSeats;
} while(t.availableSeats < 0);
trains.push_back(t);
saveTrains();
cout << "Train added successfully!\n";
}
Outcome:
This prevents invalid entries, ensuring accurate calculations and data integrity.
Approach 3: Robust Testing and Debugging for Reliability
Challenge:
Logical errors, such as incorrect seat counts after multiple bookings and cancellations, can occur in a complex system.
Solution:
This train reservation system demonstrates how structs, vectors, file handling, and functions can be effectively used in C++ to manage train schedules and reservations. By exploring real‑world examples—from regional commuter rail systems to tourist train services—and in‑depth case studies addressing seasonal workforce management and multi‑state tax compliance, you gain insight into the practical impact of such systems.
Moreover, the problem solving approaches presented—ensuring data persistence during outages, validating inputs, and employing robust testing and debugging techniques—provide you with practical strategies to build a reliable and scalable reservation system.
Would you like to add additional features, such as searching for trains by destination or modifying existing train details? Let us know in the comments!
Comments
Post a Comment