A C++ based train booking system that implements object-oriented programming concepts and file I/O operations for persistent data storage. The system manages train schedules, passenger bookings, and seat reservations using modern C++17 features and template-based generic programming.
- Book Tickets: Create bookings for trains and vehicles
- View Bookings: Display detailed booking information
- Cancel Bookings: Remove bookings and free up seats
- Generic Templates: Works with both Train and Vehicle types
- Persistent Storage: JSON-based file storage for all bookings
- Multiple Passengers: Support for multiple passengers per booking
- C++ compiler with C++17 support (g++)
- Make build system
- Linux, Mac, or Windows with WSL
make./booking_systemcd tests
make runSee tests/README.md for detailed testing instructions.
train-booking-cpp/
├── entities/ # Data models
│ ├── user.hpp # User/Passenger struct
│ ├── train.hpp # Train struct
│ └── vehicle.hpp # Vehicle struct
├── services/ # Business logic
│ ├── bookingService.hpp # Booking service (header)
│ ├── bookingService.cpp # Booking service (implementation)
│ ├── fileIO.hpp # File I/O service (header)
│ └── fileIO.cpp # File I/O service (implementation)
├── external/ # Third-party libraries
│ └── json.hpp # nlohmann/json library
├── tests/ # Test suite
│ ├── test_booking.cpp # Test file
│ ├── Makefile # Test build configuration
│ └── README.md # Testing guide
├── main.cpp # Application entry point
├── Makefile # Build configuration
└── README.MD # This file
- Templates & Generics: Template specialization for Vehicle and Train types
- File I/O: Reading and writing booking data to JSON files
- Standard Template Library (STL): Vectors for dynamic arrays, time_t for timestamps
- Structs: Lightweight data structures for entities
- External Libraries: nlohmann/json for JSON serialization/deserialization
- Modern C++17: Standard compliance for robust code
User (entities/user.hpp)
struct User {
string userId;
string name;
string aadharCard;
vector<Vehicle*> vehicles;
};Train (entities/train.hpp)
struct Train {
string trainId;
string name;
string source;
string destination;
time_t time;
vector<vector<User>> seats; // 2D seat matrix
};Vehicle (entities/vehicle.hpp)
struct Vehicle {
string vehicleId;
string name;
string source;
string destination;
time_t time;
vector<vector<User>> seats; // 2D seat matrix
};Location: services/bookingService.hpp and services/bookingService.cpp
Public Methods:
book(string entityId, User user, string name, string source, string destination)- Creates a new bookingcancelBooking(string entityId, string userId)- Cancels an existing bookingprintBooking(string entityId, string userId)- Displays booking details
Template Specializations:
BookingService<Vehicle>- For vehicle bookingsBookingService<Train>- For train bookings
Example Usage:
// Create user
User user;
user.userId = "U001";
user.name = "John Doe";
user.aadharCard = "1234-5678-9012";
// Book a vehicle
BookingService<Vehicle> bookingService;
bookingService.book("V001", user, "Express Train", "Mumbai", "Delhi");
// Print booking
bookingService.printBooking("V001", "U001");
// Cancel booking
bookingService.cancelBooking("V001", "U001");Location: services/fileIO.hpp and services/fileIO.cpp
Public Methods:
saveToFile(T entity)- Appends an entity to the JSON filereadFromFile()- Reads all entities of type T from the JSON fileupdateFile(vector<T> entities)- Overwrites the JSON file with provided entities
Features:
- Automatic JSON serialization/deserialization
- Separate handling for Vehicle and Train types
- Persistent storage in
db.json
Creates a new booking and saves it to the database.
Parameters:
entityId- Unique identifier for the vehicle/trainuser- User object with passenger detailsname- Name of the vehicle/trainsource- Starting stationdestination- Ending station
Returns: void
Cancels a booking by removing the user from the seat allocation.
Parameters:
entityId- ID of the vehicle/trainuserId- ID of the user to remove
Returns: void
Output: Prints confirmation message or error if booking not found
Displays detailed information about a booking.
Parameters:
entityId- ID of the vehicle/trainuserId- ID of the user
Returns: void
Output: Formatted booking details including route, time, and passenger info
Bookings are stored in db.json as a JSON array. Each booking contains:
Vehicle Booking:
{
"vehicleId": "V001",
"name": "Express Train",
"source": "Mumbai",
"destination": "Delhi",
"time": 1699363200,
"seats": [
[
{
"userId": "U001",
"name": "John Doe",
"aadharCard": "1234-5678-9012"
}
]
]
}Train Booking:
{
"trainId": "T001",
"name": "Rajdhani Express",
"source": "Chennai",
"destination": "Kolkata",
"time": 1699363200,
"seats": [
[
{
"userId": "U002",
"name": "Jane Smith",
"aadharCard": "9876-5432-1098"
}
]
]
}# Clean previous builds
make clean
# Build the project
make
# Run the application
./booking_system# Navigate to tests directory
cd tests
# Build and run tests
make run
# Or build separately
make
./test_booking
# Clean test artifacts
make cleanEnter User ID: U001
Enter Name: John Doe
Enter Aadhar Card Number: 123456789012
Enter the option:
Enter 1 to book the train
1
Enter Train ID: T001
Enter Train Name: Rajdhani Express
Enter Source Station: Mumbai
Enter Destination Station: Delhi
Train booked successfully!
The project includes a comprehensive test suite that validates all booking operations.
Tests Include:
- Vehicle booking creation
- Booking retrieval and display
- Multiple concurrent bookings
- Booking cancellation
- Cancellation verification
- Train booking (template verification)
- Invalid booking lookups
Run Tests:
cd tests
make runFor detailed testing instructions, see tests/README.md.
The system handles various error conditions:
- File Not Found: Returns empty list when
db.jsondoesn't exist - Invalid Entity: Skips incompatible records (e.g., Vehicle records when reading Trains)
- Booking Not Found: Displays appropriate error message
- User Not Found: Notifies when attempting to cancel non-existent booking
Potential improvements for the system:
- User authentication and login system
- Seat selection interface (row/column selection)
- Search functionality for available trains
- Date/time based booking validation
- Booking history per user
- Multiple seat booking in single transaction
- Payment integration
- Email/SMS notifications