-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTheaterApp.java
More file actions
103 lines (83 loc) · 2.83 KB
/
TheaterApp.java
File metadata and controls
103 lines (83 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package gr.aueb.cf.ch10.projects;
import java.util.Scanner;
public class TheaterApp {
public static final int NUM_ROWS = 30;
public static final int NUM_SEATS_PER_ROW = 12;
public static char[][] seats = new char[NUM_ROWS][NUM_SEATS_PER_ROW];
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
initSeats();
while (true) {
System.out.println("Enter command (book, check, cancel, availability, exit): ");
String command = scan.next();
if (command.equals("exit")) {
break;
}
if (command.equals("book")) {
bookSeat();
} else if (command.equals("check")) {
checkSeat();
} else if (command.equals("cancel")) {
cancelSeat();
} else if (command.equals("availability")) {
checkAvailability();
} else {
System.out.println("Invalid command.");
}
}
}
public static void initSeats() {
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_SEATS_PER_ROW; j++) {
seats[i][j] = '.';
}
}
}
// Method to book a seat
public static void bookSeat() {
System.out.println("Enter seat row and column: ");
int row = scan.nextInt();
int col = scan.nextInt();
if (seats[row][col] != '.') {
System.out.println("Seat is already booked.");
return;
}
seats[row][col] = 'B';
System.out.println("Seat booked successfully.");
}
// Method to check a seat
public static void checkSeat() {
System.out.println("Enter seat row and column: ");
int row = scan.nextInt();
int col = scan.nextInt();
if (seats[row][col] == '.') {
System.out.println("Seat is available.");
} else {
System.out.println("Seat is booked.");
}
}
// Method to cancel a seat
public static void cancelSeat() {
System.out.println("Enter seat row and column: ");
int row = scan.nextInt();
int col = scan.nextInt();
if (seats[row][col] == '.') {
System.out.println("Seat is already available.");
return;
}
seats[row][col] = '.';
System.out.println("Booking cancelled successfully.");
}
// Method to check availability
public static void checkAvailability() {
int availableSeats = 0;
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_SEATS_PER_ROW; j++) {
if (seats[i][j] == '.') {
availableSeats++;
}
}
}
System.out.println("Number of available seats: " + availableSeats);
}
}