Have you ever wondered how trains are managed at a busy station? What happens when there are only a few tracks and multiple trains arriving and leaving? In this blog post, Iβll walk you through a fun console-based project I built using C++ to simulate train management at a station!
π§ What Is It?
This is a Train Station Management System written in C++. It lets you:
- Add or delete trains π
- Automatically assign tracks based on arrival times and availability π―
- Handle delays and reschedule track allocation βοΈ
- View upcoming trains (with filters!) π
π§ What You Need
- Basic knowledge of C++ (OOP, functions, vectors)
- A C++ compiler like
g++
- A terminal to run the app
ποΈ How It Works
β Adding Trains
You can add new trains by providing:
- Train name
- Arrival time (e.g.,
1430
means 2:30 PM) - Departure time
- Source and Destination
The system will automatically assign the best available track.
π Delays? No Problem!
If a train is delayed, the system will reallocate tracks and update the schedule.
π View Next Trains
You can see which trains are coming next based on:
- Time
- Number of trains
- Specific track
ποΈ All Trains
Want to see all train details? You can sort them by:
- Arrival time
- Departure time
- Track number
π¨βπ» How to Run
- Clone the repo
git clone https://github.com/Akataruka/station-management-app.git
cd station-management-app
- Compile the code
g++ -std=c++17 main.cpp -o station-management-app
- Run it
./station-management-app
πΈ Demo
WELCOME TO STATION MANAGEMENT APP
1. Create a new Station
Enter station Name: GrandCentral
Enter number of tracks: 2
Station Created β
1. Add/Delete trains
2. View Next trains
3. Add delay
4. View all trains
5. Get station details
β¨ What I Learned
Priority Queue Interval Processing - Implemented real-time track allocation using min-heaps to efficiently manage overlapping arrival-departure intervals and ensure optimal scheduling.
Timestamp-Based Binary Search - Used binary search on sorted train schedules to quickly identify the next available train based on a user-defined time query.
Function Overloading - Leveraged overloaded functions to support multiple formats of train queries, including time-based, track-based, and count-based filters.
Dynamic Memory Allocation - Created and managed
Train
objects dynamically using heap allocation, with proper cleanup handled in destructors to prevent memory leaks.Encapsulation - Encapsulated object properties and behaviors within dedicated classes (
Train
,Station
) to maintain modularity and object-oriented integrity.Command-Line Interface Support - Designed an interactive CLI for querying and managing station operations, providing a smooth user experience for scheduling and reporting.
π Want to Try It?
π GitHub Repository
π Final Thoughts
This was a fun and rewarding project. It's simple, educational, and can be extended in many ways like:
- Saving data to files
- Real-time simulation with random train traffic
If you're learning C++ and want to build something cool β give this a try!
Feel free to ask questions or suggest features. π
Happy coding! π¨βπ»π©βπ»
More about Author
π Asutoshk - Linktree
Top comments (0)