0% found this document useful (0 votes)
0 views

Assignment_RevisionTurn-2

The document outlines a project to design a Train Ticket Reservation System that allows users to book AC and Sleeper tickets, incorporating custom exception handling for quota limits. It includes an abstract class for tickets, specific implementations for each ticket type, and a main menu for user interaction. The author concludes by reflecting on the learning experience regarding abstract classes, inheritance, and error handling.

Uploaded by

dhruvvbhalani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Assignment_RevisionTurn-2

The document outlines a project to design a Train Ticket Reservation System that allows users to book AC and Sleeper tickets, incorporating custom exception handling for quota limits. It includes an abstract class for tickets, specific implementations for each ticket type, and a main menu for user interaction. The author concludes by reflecting on the learning experience regarding abstract classes, inheritance, and error handling.

Uploaded by

dhruvvbhalani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Dhruv Vipul Bhalani

UID: 2024300019

Experiment No. Revision turn 2

AIM: Revision of previously learnt topics

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 an abstract class Ticket:

Abstract method: void bookTicket()

Fields: String passengerName, boolean isBooked

Create classes ACTicket and SleeperTicket that extend Ticket and implement
bookTicket().

Create a user-defined exception QuotaFullException (extends Exception).

Use a switch-case in the main menu:

Book AC Ticket

Book Sleeper Ticket

Exit

If the ticket is already booked, throw QuotaFullException.


Use try-catch-finally to handle the exception and ensure that a final message is always
printed.

Use throw to explicitly raise exceptions and throws where necessary.

In finally, print: "End of booking session."

PROGRAM: import java.util.Scanner;

abstract class Ticket {


String passengerName;
boolean isBooked = false;

abstract void bookTicket() throws QuotaFullException;


}

class ACTicket extends Ticket {


@Override
void bookTicket() throws QuotaFullException {
if (isBooked) {
throw new QuotaFullException("AC Quota is full.");
} else {
isBooked = true;
System.out.println("AC Ticket booked successfully for " + passengerName);
}
}
}

class SleeperTicket extends Ticket {


@Override
void bookTicket() throws QuotaFullException {
if (isBooked) {
throw new QuotaFullException("Sleeper Quota is full.");
} else {
isBooked = true;
System.out.println("Sleeper Ticket booked successfully for " + passengerName);
}
}
}
class QuotaFullException extends Exception {
public QuotaFullException(String message) {
super(message);
}
}

public class TrainTicketReservationSystem {


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ACTicket acTicket = new ACTicket();
SleeperTicket sleeperTicket = new SleeperTicket();

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.");
}

} while (choice != 3);

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.

You might also like