-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraysExample.java
More file actions
39 lines (31 loc) · 1.12 KB
/
ArraysExample.java
File metadata and controls
39 lines (31 loc) · 1.12 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
class TicketBooking {
public void bookSeat(int[] seats, int seatNumber) {
if (seatNumber < 1 || seatNumber > seats.length) {
System.out.println("Invalid seat number");
return;
}
if (seats[seatNumber - 1] == 0) {
seats[seatNumber - 1] = 1;
System.out.println("-> Seat " + seatNumber + " booked successfully");
} else {
System.out.println("-> Seat " + seatNumber + " is already booked");
}
}
public void displaySeats(int[] seats) {
for (int i = 0; i < seats.length; i++) {
System.out.println("seat " + (i + 1) + ": " + (seats[i] == 0 ? "Available" : "Booked"));
}
}
}
public class ArraysExample {
public static void main(String[] args) {
int[] seats = new int[10];
TicketBooking ticketBooking = new TicketBooking();
System.out.println("Before Booking");
ticketBooking.displaySeats(seats);
System.out.println();
System.out.println("After booking");
ticketBooking.bookSeat(seats, 5);
ticketBooking.displaySeats(seats);
}
}