Skip to content

A Java application that manages train routes, merging, and station management. The system can create trains, manage their routes, and handle train merging at designated stations.

Notifications You must be signed in to change notification settings

Anant-Saini/ME_GEEKTRUST_BACKEND

Repository files navigation

Train Management System

A Java application that manages train routes, merging, and station management. The system can create trains, manage their routes, and handle train merging at designated stations.

Features

  • 🚂 Train creation and route management
  • 🛤️ Dynamic route loading from CSV files
  • 🔄 Train merging at designated stations
  • 🚉 Station distance and path calculations
  • ⚡ Command-based architecture
  • 🧪 Comprehensive test coverage

Technology Stack

  • Java 11
  • Gradle
  • JUnit 5
  • Mockito

Project Structure

src/
├── main/
│   ├── java/com/geektrust/backend/
│   │   ├── commands/          # Command pattern implementations
│   │   ├── entities/          # Domain models
│   │   ├── repositories/      # Data access layer
│   │   ├── services/         # Business logic
│   │   └── exceptions/       # Custom exceptions
│   └── resources/
│       └── *.csv             # Route configuration files
└── test/
    └── java/com/geektrust/backend/
        ├── commands/          # Command tests
        ├── entities/          # Entity tests
        ├── services/         # Service layer tests
        └── resources/        # Test data files

Getting Started

Prerequisites

  • JDK 11 or higher
  • Gradle 7.x or higher

Building the Project

# Clone the repository
git clone <repository-url>
cd anantsaini-india-ME_GEEKTRUST_BACKEND

# Build the project
./gradlew build

# Run tests
./gradlew test

Running the Application

# Run with input file
java -jar build/libs/geektrust.jar <path-to-input-file>

Input Format

The application accepts a text file with train configurations. Each line should contain:

TRAIN_NAME ENGINE STATION1 STATION2 ... STATIONN

Example:

TRAIN_A ENGINE NDL NDL GHY NJP NGP
TRAIN_B ENGINE NJP GHY AGA BPL PTA

Output Format

The application will output train arrivals and departures in the format:

ARRIVAL TRAIN_NAME ENGINE STATION1 STATION2 ... STATIONN
DEPARTURE TRAIN_NAME ENGINE1 ENGINE2 STATION1 STATION2 ... STATIONN

Architecture

Key Components

  1. Commands

    • LoadRouteCommand: Loads route data from CSV files
    • CreateTrainCommand: Creates new train instances
    • RunTrainTillMergingStationCommand: Handles train movement
    • MergeTrainCommand: Manages train merging operations
  2. Services

    • TrainService: Core business logic for train operations
    • RouteService: Route management and calculations
  3. Repositories

    • Implements CRUD operations for entities
    • Uses in-memory storage with ICrudRepository interface
  4. Entities

    • Train: Represents a train with its units
    • Route: Defines a train route with stations
    • Station: Represents a station with distance info
    • Bogie & Engine: Train unit types

Testing

The project includes comprehensive unit tests for all components:

# Run all tests
./gradlew test

# Run specific test class
./gradlew test --tests "com.geektrust.backend.commands.CreateTrainCommandTest"

# Generate test coverage report
./gradlew jacocoTestReport

Test reports can be found at build/reports/tests/test/index.html.

Design Patterns

  • Command Pattern: For executing different train operations
  • Repository Pattern: For data access abstraction
  • Dependency Injection: For loose coupling between components
  • Builder Pattern: For constructing complex objects

Error Handling

The application includes custom exceptions:

  • NoEngineFoundException
  • RouteNotFoundException
  • NoMergingOrDivergingStationFoundOnRouteException

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.


Built with ❤️ for GeekTrust Backend Challenge

About

A Java application that manages train routes, merging, and station management. The system can create trains, manage their routes, and handle train merging at designated stations.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages