I'm developing a console application for the Theatre Reservation system with saving the records in a file. When I'm trying to display seat details, it shows in different orders. I'm using static block to initialize the data.
public class WestBelcony extends Seat {
public final static double price = 40;
private static List<Seat> listSeat;
static {
System.out.println("Here");
listSeat = new ArrayList<Seat>();
for (int i = 1; i <= 10; i++) {
listSeat.add(new Seat().setId("wb" + i));
}
}
public List<Seat> getListSeat() {
return listSeat;
}
public void updateSeatBooking(String seatNo, boolean status ) {
for (int i = 0; i < listSeat.size(); i++) {
if (listSeat.get(i).getId().contains(seatNo)) {
listSeat.get(i).setBooked(status);
}
}
}
public void getSeatDetails(String seatNo) {
for (Seat seat : listSeat) {
if (seat.getId().contains(seatNo)) {
System.out.println("Seat Id : " + seat.getId() + " IsBooked : " + seat.isBooked());
}
}
}
public void showReservationDetails() {
for (Seat seat : listSeat) {
System.out.print("Seat No : " + seat.getId() + " ");
if(!seat.isBooked()) {
System.out.println("\u001B[32m"+"Available");
System.out.print("\u001B[0m");
} else {
System.err.println("Booked");
}
}
}
public static Seat getSeat(String id) {
for (Seat seat : listSeat) {
if(seat.getId().contains(id))
return seat;
}
return null;
}
}
And this is how I'm calling the method
WestBelcony eb = new WestBelcony();
wb.showReservationDetails();
It displays like this
Seat No : wb1 Booked
BookedSeat No : wb2 Seat No : wb3 Seat No : wb4 Available
Seat No : wb5 Available
Seat No : wb6 Seat No : wb7 Available
Seat No : wb8 Available
Seat No : wb9 Available
Seat No : wb10 How many seats do you want to book?
Booked
Booked
Booked
What is wrong with this code?