DEV Community

Cover image for πŸš† Build a Train Station Management App in C++ (Beginner Friendly πŸš€)
Asutosh Kataruka
Asutosh Kataruka

Posted on

πŸš† Build a Train Station Management App in C++ (Beginner Friendly πŸš€)

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

  1. Clone the repo
git clone https://github.com/Akataruka/station-management-app.git
cd station-management-app
Enter fullscreen mode Exit fullscreen mode
  1. Compile the code
g++ -std=c++17 main.cpp -o station-management-app
Enter fullscreen mode Exit fullscreen mode
  1. Run it
./station-management-app
Enter fullscreen mode Exit fullscreen mode

πŸ“Έ 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
Enter fullscreen mode Exit fullscreen mode

✨ 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)