A comprehensive console-based rail ticket booking system built with Core Java 8 featuring advanced OOP concepts, functional programming, and robust error handling.
- β User registration with validation
- β Secure user login/logout
- β Profile management
- β Booking history tracking
- β 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
- β 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
- β 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)
- β PNR status checking
- β Comprehensive booking history
- β Input validation and error handling
- β Thread-safe booking operations
- β Functional programming with Java 8
- 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
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
- Java 8+ installed
- Any Java IDE (VS Code, IntelliJ, Eclipse) or command line
-
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
-
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
-
Using IDE:
- Import the project
- Run the
MainApp.javafile
- Register with username, password, email, and phone
- Login with credentials to access the booking system
- Search by route, train number, or station
- View detailed train information
- Check real-time seat availability
- Select train and journey date
- Enter passenger details (up to 6 passengers)
- Get automatic fare calculation with discounts
- Receive PNR and seat assignments
- Check PNR status anytime
- View complete booking history
- Cancel tickets with refund calculation
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
// 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());@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
};// Null-safe operations
Optional<Train> trainOpt = trainDatabase.findTrainByNumber(trainNumber);
trainOpt.ifPresentOrElse(
this::displayTrainDetails,
() -> System.out.println("Train not found!")
);- 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
- Comprehensive exception handling
- Custom exceptions for booking scenarios
- Input validation with user-friendly messages
- Graceful error recovery
The application tracks:
- Total registered users
- Total trains available
- Total bookings made
- Revenue generated
- Seat utilization
- 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.)
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
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