Program 1
// Movie Ticket Booking System( Based on 2D Array)
import java.util.Scanner;
class Seat
{
boolean booked;
String name;
public Seat()
{
booked = false;
name = "";
}
}
public class MovieTheater
{
static final int ROWS =5;
static final int COLS = 10;
static Seat[][] seats = new Seat[ROWS][COLS];
public static void initializeSeats()
{
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
seats[i][j] = new Seat();
}
}
}
public static void displaySeats()
{
System.out.println("\nSeating Arrangement (1 = Booked, 0 = Available):");
for (int i = 0; i < ROWS; i++)
{
System.out.print("Row " + (i + 1) + ": ");
for (int j = 0; j < COLS; j++)
{
System.out.print(seats[i][j].booked ? "1 " : "0 ");
}
System.out.println();
}
}
public static void bookSeat(Scanner sc)
{
System.out.print("Enter row (1-" + ROWS + "): ");
int row = sc.nextInt();
System.out.print("Enter seat number (1-" + COLS + "): ");
int col = sc.nextInt();
sc.nextLine(); // consume newline
if (row < 1 || row > ROWS || col < 1 || col > COLS)
{
System.out.println("Invalid seat selection.");
return;
}
if (seats[row - 1][col - 1].booked)
{
System.out.println("Seat already booked.");
} else
{
System.out.print("Enter your name: ");
String name = sc.nextLine();
seats[row - 1][col - 1].booked = true;
seats[row - 1][col - 1].name = name;
System.out.println("Seat (" + row + ", " + col + ") booked successfully for " + name + ".");
}
}
public static void cancelSeat(Scanner sc)
{
System.out.print("Enter row to cancel (1-" + ROWS + "): ");
int row = sc.nextInt(); //1
System.out.print("Enter seat number to cancel (1-" + COLS + "): ");
int col = sc.nextInt(); //1
if (row < 1 || row > ROWS || col < 1 || col > COLS)
{
System.out.println("Invalid seat selection.");
return;
}
if (seats[row - 1][col - 1].booked==true) //seats[0][0]
{
System.out.println("Booking for " + seats[row - 1][col - 1].name + " canceled.");
seats[row - 1][col - 1].booked = false; //seats[0][0]
seats[row - 1][col - 1].name = "";
} else {
System.out.println("Seat is already available.");
}
}
public static void showBookings()
{
System.out.println("\nBooked Seats:");
boolean found = false;
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (seats[i][j].booked==true)
{
System.out.println("Seat (" + (i + 1) + ", " + (j + 1) + ") - " + seats[i][j].name);
found = true;
}
}
}
if (!found)
{
System.out.println("No bookings yet.");
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
initializeSeats();
int choice;
while (true)
{
System.out.println("\n----------------- Movie Ticket Booking System -----------------");
System.out.println("1. Show Seat Layout");
System.out.println("2. Book a Seat");
System.out.println("3. Cancel a Booking");
System.out.println("4. Show All Bookings");
System.out.println("5. Exit");
System.out.println("---------------------------------------------------------------------------");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice)
{
case 1:
displaySeats();
break;
case 2:
bookSeat(sc);
break;
case 3:
cancelSeat(sc);
break;
case 4:
showBookings();
break;
case 5:
System.out.println("Exiting system. Goodbye!");
sc.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
}