Skip to content

Vikash565-dot/Advance-train

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Advanced Rail Ticket Booking System

A comprehensive console-based rail ticket booking system built with Core Java 8 featuring advanced OOP concepts, functional programming, and robust error handling.

🚂 Features

User Management

  • ✅ User registration with validation
  • ✅ Secure user login/logout
  • ✅ Profile management
  • ✅ Booking history tracking

Train Management

  • ✅ Comprehensive train database with 15+ sample trains
  • ✅ Search trains by:
    • Route (source → destination)
    • Train number
    • Source station
    • Destination station
    • Train name
  • ✅ Sort trains by departure time and fare
  • ✅ Real-time seat availability

Booking System

  • ✅ Interactive ticket booking with passenger details
  • ✅ Dynamic fare calculation with discounts:
    • 50% for children (age < 12)
    • 10% for senior citizens (age ≥ 60)
  • ✅ Automatic seat assignment
  • ✅ PNR generation and management
  • ✅ Booking confirmation with detailed ticket

Cancellation & Refund

  • ✅ Ticket cancellation with PNR
  • ✅ Smart refund calculation:
    • 90% refund (≥24 hours before journey)
    • 75% refund (≥12 hours before journey)
    • 50% refund (≥2 hours before journey)
    • No refund (<2 hours before journey)

Advanced Features

  • ✅ PNR status checking
  • ✅ Comprehensive booking history
  • ✅ Input validation and error handling
  • ✅ Thread-safe booking operations
  • ✅ Functional programming with Java 8

🛠 Technical Stack

  • Language: Core Java 8
  • Features Used:
    • Streams & Filters
    • Lambda Expressions
    • Functional Interfaces
    • Optional for null-safe operations
    • LocalDate/LocalDateTime for date handling
  • Data Storage: In-memory collections (HashMap, ArrayList)
  • Design Patterns: Service layer architecture
  • Thread Safety: Synchronized booking operations

📁 Project Structure

src/
├── models/
│   ├── User.java              # User entity
│   ├── Train.java             # Train entity
│   ├── Passenger.java         # Passenger entity
│   ├── Ticket.java            # Ticket entity
│   └── PNR.java               # PNR utility
├── services/
│   ├── AuthenticationService.java    # User auth & management
│   ├── TrainDatabase.java           # Train data operations
│   └── BookingSystem.java           # Booking & cancellation logic
├── utils/
│   ├── DisplayUtils.java            # UI formatting utilities
│   └── ValidationUtils.java         # Input validation utilities
├── exceptions/
│   └── BookingException.java        # Custom exception handling
└── MainApp.java                     # Main application entry point

🚀 How to Run

Prerequisites

  • Java 8+ installed
  • Any Java IDE (VS Code, IntelliJ, Eclipse) or command line

Running the Application

  1. Using Command Line:

    # Navigate to project directory
    cd "c:\Users\harsh\Desktop\Train"
    
    # Compile all Java files
    javac -d bin src/**/*.java src/*.java
    
    # Run the application
    java -cp bin MainApp
  2. Using VS Code:

    • Open the project folder in VS Code
    • Use Ctrl+F5 to run without debugging
    • Or use the Run button in the editor
  3. Using IDE:

    • Import the project
    • Run the MainApp.java file

📋 Usage Guide

1. Registration & Login

  • Register with username, password, email, and phone
  • Login with credentials to access the booking system

2. Search Trains

  • Search by route, train number, or station
  • View detailed train information
  • Check real-time seat availability

3. Book Tickets

  • Select train and journey date
  • Enter passenger details (up to 6 passengers)
  • Get automatic fare calculation with discounts
  • Receive PNR and seat assignments

4. Manage Bookings

  • Check PNR status anytime
  • View complete booking history
  • Cancel tickets with refund calculation

🧪 Sample Data

The system comes pre-loaded with 15 sample trains covering major Indian routes:

  • Rajdhani Express (Delhi-Mumbai, Delhi-Howrah, Delhi-Bangalore)
  • Shatabdi Express (Delhi-Bhopal, Delhi-Kalka, Mumbai-Ahmedabad)
  • Various other express trains connecting major cities

🎯 Key Java 8 Features Demonstrated

Streams & Lambda Expressions

// Filter trains by route
List<Train> trains = trainMap.values().stream()
    .filter(train -> train.getSource().equalsIgnoreCase(source))
    .filter(train -> train.getDestination().equalsIgnoreCase(destination))
    .collect(Collectors.toList());

// Sort trains by departure time
List<Train> sortedTrains = trains.stream()
    .sorted(Comparator.comparing(Train::getDepartureTime))
    .collect(Collectors.toList());

Functional Interfaces

@FunctionalInterface
public interface RefundCalculator {
    double calculateRefund(Ticket ticket, long hoursBeforeJourney);
}

// Usage with lambda
RefundCalculator refundCalculator = (ticket, hours) -> {
    if (hours >= 24) return ticket.getTotalFare() * 0.9;
    else if (hours >= 12) return ticket.getTotalFare() * 0.75;
    // ... more conditions
};

Optional Usage

// Null-safe operations
Optional<Train> trainOpt = trainDatabase.findTrainByNumber(trainNumber);
trainOpt.ifPresentOrElse(
    this::displayTrainDetails,
    () -> System.out.println("Train not found!")
);

🔒 Security Features

  • Input validation for all user inputs
  • Password protection (stored as plain text for demo)
  • User authorization for ticket operations
  • PNR format validation
  • Thread-safe booking operations

🐛 Error Handling

  • Comprehensive exception handling
  • Custom exceptions for booking scenarios
  • Input validation with user-friendly messages
  • Graceful error recovery

📊 System Statistics

The application tracks:

  • Total registered users
  • Total trains available
  • Total bookings made
  • Revenue generated
  • Seat utilization

🚀 Future Enhancements

  • Database integration (MySQL/PostgreSQL)
  • Web interface (Spring Boot)
  • Payment gateway integration
  • SMS/Email notifications
  • Admin panel for train management
  • Waitlist and RAC implementation
  • Multi-class booking (AC, Sleeper, etc.)

👨‍💻 Development Notes

This project demonstrates:

  • Clean Code Principles: Well-structured, readable code
  • SOLID Principles: Single responsibility, dependency injection
  • Design Patterns: Service layer, Factory pattern for PNR generation
  • Java 8 Features: Extensive use of modern Java features
  • Error Handling: Robust exception management
  • Thread Safety: Synchronized critical sections

🤝 Contributing

This is a learning project demonstrating Core Java and Java 8 features. Feel free to:

  • Add new features
  • Improve error handling
  • Enhance UI/UX
  • Add unit tests
  • Optimize performance

Built with ❤️ using Core Java 8

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages