0% found this document useful (0 votes)
12 views7 pages

Java Theater Seating System Project

Uploaded by

testpentest0704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views7 pages

Java Theater Seating System Project

Uploaded by

testpentest0704
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

EXPERIMENT -12

DEVELOP A MINI PROJECT FOR ANY APPLICATION USING JAVA CONCEPTS

Theater Seating Arrangement System


This system will involve designing a theater seating arrangement system in Java, using object-
oriented
principles such as inheritance, polymorphism, and encapsulation, along with core Java features
like
exception handling, file handling, and custom data structures. The system will allow users to
view
seating layouts, book seats, and process ticket payments.
● Seat Class:
○ Define a base class Seat to represent a seat in the theater. This class will include: row
(int): The row number of the seat, seatNumber (int): The seat number within the row,
isOccupied (boolean): Whether the seat is occupied or available, ticketPrice (double):
The price of the ticket for the seat.
○ Implement Constructors for initializing seat details, Accessors and mutators for each
attribute, A method checkAvailability() to verify seat availability, A method
displaySeatDetails() to show details of the seat.
● Inheritance:
○ Create derived classes RegularSeat and VIPSeat that inherit from Seat.
○ Each derived class should have specific attributes: RegularSeat: Additional attribute
comfortLevel (String) to describe the comfort level, VIPSeat: Additional attribute
complimentaryDrinks (boolean) for free drinks.
○ Override displaySeatDetails() in each derived class to include seat-specific details.
● Seating Arrangement System:
○ Design a SeatingArrangement class to manage the seating layout and booking process.
This class will: Use a 2D array or ArrayList of Seat objects to represent seating in the
theater.
○ Implement methods to: Display seating layout and show availability, Book a seat by
marking it as occupied, Process payment and confirm ticket purchase.
○ Provide a user-friendly interface for selecting seats, booking, and payment
confirmation.
● Exception Handling:
○ Add exception handling to handle potential errors: InvalidSeatException for invalid
row/seat numbers. SeatAlreadyOccupiedException for booking an already occupied
seat.
○ Use try-catch blocks for user inputs and ensure graceful error messaging.
● File Handling:
○ Implement methods in SeatingArrangement to save and load seating information:
saveSeatingData() to save the current seating arrangement to a file. loadSeatingData()
to load previous seating data from a file for session persistence.
○ This will allow users to resume bookings or check availability in future sessions.
You can add your own additional class, methods wherever needed.

AIM:

To develop a mini project for any application using java concepts.


Program:

import [Link].*;
import [Link].*;

// Abstract class Seat


abstract class Seat implements Serializable {
private int row;
private int seatNumber;
private double ticketPrice;
private boolean occupied;

public Seat(int row, int seatNumber, double ticketPrice) {


[Link] = row;
[Link] = seatNumber;
[Link] = ticketPrice;
[Link] = false;
}

public int getRow() {


return row;
}

public int getSeatNumber() {


return seatNumber;
}

public double getTicketPrice() {


return ticketPrice;
}

public boolean isOccupied() {


return occupied;
}

public void setOccupied(boolean occupied) {


[Link] = occupied;
}

public abstract void displaySeatDetails();


}

// RegularSeat class
class RegularSeat extends Seat {
String comfortLevel;

public RegularSeat(int row, int seatNumber, double ticketPrice, String comfortLevel) {


super(row, seatNumber, ticketPrice);
[Link] = comfortLevel;
}

@Override
public void displaySeatDetails() {
[Link]("Seat (" + getRow() + "," + getSeatNumber() + ") - Regular");
[Link]("Price: $" + getTicketPrice());
[Link]("Comfort Level: " + comfortLevel);
}
}

// VIPSeat class
class VIPSeat extends Seat {
boolean complimentaryDrinks;

public VIPSeat(int row, int seatNumber, double ticketPrice, boolean complimentaryDrinks) {


super(row, seatNumber, ticketPrice);
[Link] = complimentaryDrinks;
}

@Override
public void displaySeatDetails() {
[Link]("Seat (" + getRow() + "," + getSeatNumber() + ") - VIP");
[Link]("Price: $" + getTicketPrice());
[Link]("Complimentary Drinks: " + (complimentaryDrinks ? "Yes" : "No"));
}
}

// Custom Exception for invalid seats


class InvalidSeatException extends Exception {
public InvalidSeatException(String message) {
super(message);
}
}

// Custom Exception for occupied seats


class SeatAlreadyOccupiedException extends Exception {
public SeatAlreadyOccupiedException(String message) {
super(message);
}
}

// SeatingArrangement class
class SeatingArrangement {
private ArrayList<ArrayList<Seat>> seats;

public SeatingArrangement(int rows, int seatsPerRow) {


seats = new ArrayList<>();
for (int i = 0; i < rows; i++) {
ArrayList<Seat> row = new ArrayList<>();
for (int j = 0; j < seatsPerRow; j++) {
double price = (i < 2) ? 500 : 300; // VIP for first two rows
if (i < 2) {
[Link](new VIPSeat(i, j, price, true));
} else {
[Link](new RegularSeat(i, j, price, "Standard"));
}
}
[Link](row);
}
}

public void displaySeatingLayout() {


[Link]("Seating Layout:");
for (int i = 0; i < [Link](); i++) {
[Link]("Row " + (i + 1) + ": ");
for (Seat seat : [Link](i)) {
[Link](([Link]() ? "[X]" : "[ ]") + " ");
}
[Link]();
}
}

public void displaySeatingDetails() {


[Link]("\nDetailed Seating Information:");
for (int i = 0; i < [Link](); i++) {
[Link]("Row " + (i + 1) + ":");
for (Seat seat : [Link](i)) {
[Link](" Seat " + [Link]() + " - ");
if (seat instanceof VIPSeat) {
[Link]("VIP, Price: $" + [Link]() +
", Complimentary Drinks: " + (((VIPSeat) seat).complimentaryDrinks ? "Yes" :
"No") +
", Occupied: " + ([Link]() ? "Yes" : "No"));
} else if (seat instanceof RegularSeat) {
[Link]("Regular, Price: $" + [Link]() +
", Comfort Level: " + ((RegularSeat) seat).comfortLevel +
", Occupied: " + ([Link]() ? "Yes" : "No"));
}
}
}
}

public void bookSeat(int row, int seatNumber) throws InvalidSeatException,


SeatAlreadyOccupiedException {
if (row < 0 || row >= [Link]() || seatNumber < 0 || seatNumber >= [Link](row).size()) {
throw new InvalidSeatException("Invalid seat selection!");
}
Seat seat = [Link](row).get(seatNumber);
if ([Link]()) {
throw new SeatAlreadyOccupiedException("Seat is already occupied!");
}
[Link](true);
[Link]("Seat booked successfully!");
}

public void saveSeatingData(String fileName) throws IOException {


try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
for (int i = 0; i < [Link](); i++) {
[Link]("Row " + (i + 1) + ":\n");
for (Seat seat : [Link](i)) {
if (seat instanceof VIPSeat) {
[Link](" Seat " + [Link]() + ": VIP, Price: $" +
[Link]() +
", Complimentary Drinks: " + (((VIPSeat) seat).complimentaryDrinks ?
"Yes" : "No") +
", Occupied: " + ([Link]() ? "Yes" : "No") + "\n");
} else if (seat instanceof RegularSeat) {
[Link](" Seat " + [Link]() + ": Regular, Price: $" +
[Link]() +
", Comfort Level: " + ((RegularSeat) seat).comfortLevel +
", Occupied: " + ([Link]() ? "Yes" : "No") + "\n");
}
}
[Link]("\n");
}
}
}

@SuppressWarnings("unchecked")
public void loadSeatingData(String fileName) throws IOException, ClassNotFoundException
{
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
Object obj = [Link]();
if (obj instanceof ArrayList) {
seats = (ArrayList<ArrayList<Seat>>) obj;
} else {
throw new IOException("Invalid data format in file.");
}
}
}
}

// Main Class
public class Main {
public static void main(String[] args) {
SeatingArrangement arrangement = new SeatingArrangement(5, 5); // 5 rows, 5 seats per
row
Scanner scanner = new Scanner([Link]);

while (true) {
[Link]("\n1. View Seating Layout");
[Link]("2. View Detailed Seat Information");
[Link]("3. Book a Seat");
[Link]("4. Save Seating Data");
[Link]("5. Load Seating Data");
[Link]("6. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();

switch (choice) {
case 1:
[Link]();
break;
case 2:
[Link]();
break;
case 3:
[Link]("Enter row number (1-5): ");
int row = [Link]() - 1;
[Link]("Enter seat number (1-5): ");
int seatNumber = [Link]() - 1;
try {
[Link](row, seatNumber);
} catch (InvalidSeatException | SeatAlreadyOccupiedException e) {
[Link]("Error: " + [Link]());
}
break;
case 4:
try {
[Link]("[Link]");
[Link]("Seating data saved.");
} catch (IOException e) {
[Link]("Error saving data: " + [Link]());
}
break;
case 5:
try {
[Link]("[Link]");
[Link]("Seating data loaded.");
} catch (IOException | ClassNotFoundException e) {
[Link]("Error loading data: " + [Link]());
}
break;
case 6:
[Link]("Exiting...");
[Link]();
return;
default:
[Link]("Invalid choice!");
}
}
}
}
Output:
• Compile all the code in the command prompt

• View the seating layout in the Theatre

• View detailed seating layout in the Theatre

• Book a seat

• View the seating


• Saving and Loading the Data

• A new text document is created

RESULT:
Thus , a mini project for an application using java concepts is developed successfully.
PERFORMANCE (25)
VIVA VOCE (10)
RECORD (15)
TOTAL (50)

Common questions

Powered by AI

Exception handling contributes to the robustness of the theater seating system by managing potential errors gracefully. It uses custom exceptions like InvalidSeatException and SeatAlreadyOccupiedException to specifically address issues related to invalid seat selections and attempts to book occupied seats. By incorporating try-catch blocks, the system can provide informative error messages without crashing, ensuring a smooth user experience even when invalid operations are attempted. This design aids in maintaining the integrity of the seating data and prevents the application from entering an inconsistent state .

The system design supports user interaction and interface usability through a simple, text-based menu system that guides users step-by-step, offering choices to view seating layouts, obtain detailed seat information, book seats, and save/load data. This interaction is facilitated by methods such as displaySeatingLayout() and bookSeat() that process user input and update seat statuses. The intuitive layout of options and prompt feedback via console output make it accessible and easy to use, even though it's a basic command-line interface. This design is efficient for a small-scale application model and can be expanded with graphical elements for a GUI if needed .

Polymorphism in the theater seating arrangement system is manifested through the ability to treat objects of RegularSeat and VIPSeat as instances of their common superclass, Seat. This allows the SeatingArrangement class to aggregate these diverse seat types into a uniform data structure like an ArrayList, and interact with them using the uniform methods defined in Seat such as displaySeatDetails(), which are overridden in each subclass to provide specific details. Practical benefits include code that is more flexible and easier to maintain, as operations can be defined in terms of the base class type, reducing the need for complex type-checking logic during seat operations .

Using a 2D ArrayList to represent theater seating provides flexibility and dynamic management of seat rows and seats per row, enabling efficient access and manipulation of theater layout. Each array list within the top-level array represents a row of seats, allowing easy iteration over rows and seats for operations like booking or displaying the layout. The dynamic nature of ArrayList is particularly advantageous for resizing and managing real-time data when seats are booked or their status changes. This model supports both fixed and variable seat configurations per row, accommodating complex theater designs .

The use of abstract classes and methods in the theater seating project addresses challenges related to providing a common template for different types of seats while allowing specific implementation details to be defined in subclasses. The abstract class Seat defines core attributes and methods like displaySeatDetails() but leaves the implementation of these methods to concrete subclasses (RegularSeat and VIPSeat), which provide details relevant to their contexts. This structure allows the program to enforce a consistent interface for all seats, ensuring that any seat type created in the future adheres to the defined contract, thus simplifying the management and extension of the system .

File handling in the Java project ensures data persistence by saving the current state of the seating arrangement to a file and loading it back in future sessions, preserving user data across application restarts. The system uses methods like saveSeatingData() and loadSeatingData() which write and read the seating configuration to and from a file system, maintaining data between program executions. Data integrity is maintained by using structured formats for storing seat attributes and statuses, and through exception handling techniques that safeguard against file I/O errors, such as catching IOException and ensuring valid data formats before processing .

Inheritance enhances the functionality of the theater seating arrangement system by allowing the creation of specific seat types like RegularSeat and VIPSeat, which inherit from a common base class, Seat. This commonality ensures that all seats share the same fundamental characteristics (such as row, seat number, and ticket price), while still allowing specialization through additional attributes specific to each subclass: RegularSeat includes comfortLevel, and VIPSeat includes complimentaryDrinks. This approach promotes code reuse and simplicity, as changes to shared behavior are centralized in the parent class, and it allows for polymorphic behavior when interacting with groups of seats .

Custom exceptions like InvalidSeatException and SeatAlreadyOccupiedException enhance user experience by providing precise feedback and error resolution. When users interact with the system, attempting operations like selecting an invalid seat or booking an already occupied seat, these exceptions are triggered, capturing the specific nature of the error. Coupled with meaningful error messages, they guide users towards correct actions without exposing the underlying complexity of the program logic. This targeted handling reduces frustration by ensuring users receive clear, actionable information on errors .

Constructors in the derived classes RegularSeat and VIPSeat differ in their initialization of subclass-specific attributes: RegularSeat’s constructor includes an additional parameter for comfortLevel, while VIPSeat’s constructor handles the complimentaryDrinks flag. These differences are significant because they enable each class to encapsulate seat-specific features, reflecting their real-world distinctions and impacting how they are displayed and processed within the system. This design allows seat details to be tailored specifically and prominently features these attributes during user interactions and data persistence .

The Serializable interface in Java allows objects of classes such as Seat, RegularSeat, and VIPSeat to be converted into byte streams for efficient storage and retrieval, providing a straightforward means to persist complex object states. Advantages include simplicity in saving/restoring entire object graphs and reducing the need for manually writing parsing logic. However, limitations include potential issues with version control if class structures change over time, as well as the overhead of maintaining serialization compatibility. The approach also requires careful handling of transient fields to ensure security and consistency .

You might also like