Assignment_RevisionTurn-2
Assignment_RevisionTurn-2
UID: 2024300019
Program 1
PROBLEM Design a Train Ticket Reservation System that allows users to book tickets. A user can
STATEMENT : choose between AC and Sleeper classes. If the quota is full, the system should throw a
custom exception QuotaFullException.
Requirements:
Create classes ACTicket and SleeperTicket that extend Ticket and implement
bookTicket().
Book AC Ticket
Exit
int choice;
do {
System.out.println("\nTrain Ticket Reservation System");
System.out.println("1. Book AC Ticket");
System.out.println("2. Book Sleeper Ticket");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
sc.nextLine();
try {
switch (choice) {
case 1:
System.out.print("Enter Passenger Name: ");
acTicket.passengerName = sc.nextLine();
acTicket.bookTicket();
break;
case 2:
System.out.print("Enter Passenger Name: ");
sleeperTicket.passengerName = sc.nextLine();
sleeperTicket.bookTicket();
break;
case 3:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice.");
}
} catch (QuotaFullException e) {
System.out.println("Booking failed: " + e.getMessage());
} finally {
System.out.println("End of booking session.");
}
sc.close();
}
}
RESULT:
CONCLUSION: I have learnt how to apply the concepts of abstract classes, inheritance, error handling,
and overriding and how to implement all together.