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