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

Java Mini Project report

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)
5 views

Java Mini Project report

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
You are on page 1/ 7

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 java.io.*;
import java.util.*;

// 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) {


this.row = row;
this.seatNumber = seatNumber;
this.ticketPrice = ticketPrice;
this.occupied = 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) {


this.occupied = 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);
this.comfortLevel = comfortLevel;
}

@Override
public void displaySeatDetails() {
System.out.println("Seat (" + getRow() + "," + getSeatNumber() + ") - Regular");
System.out.println("Price: $" + getTicketPrice());
System.out.println("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);
this.complimentaryDrinks = complimentaryDrinks;
}

@Override
public void displaySeatDetails() {
System.out.println("Seat (" + getRow() + "," + getSeatNumber() + ") - VIP");
System.out.println("Price: $" + getTicketPrice());
System.out.println("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) {
row.add(new VIPSeat(i, j, price, true));
} else {
row.add(new RegularSeat(i, j, price, "Standard"));
}
}
seats.add(row);
}
}

public void displaySeatingLayout() {


System.out.println("Seating Layout:");
for (int i = 0; i < seats.size(); i++) {
System.out.print("Row " + (i + 1) + ": ");
for (Seat seat : seats.get(i)) {
System.out.print((seat.isOccupied() ? "[X]" : "[ ]") + " ");
}
System.out.println();
}
}

public void displaySeatingDetails() {


System.out.println("\nDetailed Seating Information:");
for (int i = 0; i < seats.size(); i++) {
System.out.println("Row " + (i + 1) + ":");
for (Seat seat : seats.get(i)) {
System.out.print(" Seat " + seat.getSeatNumber() + " - ");
if (seat instanceof VIPSeat) {
System.out.println("VIP, Price: $" + seat.getTicketPrice() +
", Complimentary Drinks: " + (((VIPSeat) seat).complimentaryDrinks ? "Yes" :
"No") +
", Occupied: " + (seat.isOccupied() ? "Yes" : "No"));
} else if (seat instanceof RegularSeat) {
System.out.println("Regular, Price: $" + seat.getTicketPrice() +
", Comfort Level: " + ((RegularSeat) seat).comfortLevel +
", Occupied: " + (seat.isOccupied() ? "Yes" : "No"));
}
}
}
}

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


SeatAlreadyOccupiedException {
if (row < 0 || row >= seats.size() || seatNumber < 0 || seatNumber >= seats.get(row).size()) {
throw new InvalidSeatException("Invalid seat selection!");
}
Seat seat = seats.get(row).get(seatNumber);
if (seat.isOccupied()) {
throw new SeatAlreadyOccupiedException("Seat is already occupied!");
}
seat.setOccupied(true);
System.out.println("Seat booked successfully!");
}

public void saveSeatingData(String fileName) throws IOException {


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

@SuppressWarnings("unchecked")
public void loadSeatingData(String fileName) throws IOException, ClassNotFoundException
{
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName))) {
Object obj = ois.readObject();
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(System.in);

while (true) {
System.out.println("\n1. View Seating Layout");
System.out.println("2. View Detailed Seat Information");
System.out.println("3. Book a Seat");
System.out.println("4. Save Seating Data");
System.out.println("5. Load Seating Data");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
arrangement.displaySeatingLayout();
break;
case 2:
arrangement.displaySeatingDetails();
break;
case 3:
System.out.print("Enter row number (1-5): ");
int row = scanner.nextInt() - 1;
System.out.print("Enter seat number (1-5): ");
int seatNumber = scanner.nextInt() - 1;
try {
arrangement.bookSeat(row, seatNumber);
} catch (InvalidSeatException | SeatAlreadyOccupiedException e) {
System.out.println("Error: " + e.getMessage());
}
break;
case 4:
try {
arrangement.saveSeatingData("seatingData.txt");
System.out.println("Seating data saved.");
} catch (IOException e) {
System.out.println("Error saving data: " + e.getMessage());
}
break;
case 5:
try {
arrangement.loadSeatingData("seatingData.txt");
System.out.println("Seating data loaded.");
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading data: " + e.getMessage());
}
break;
case 6:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("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)

You might also like