0

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?

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Suhail Moh
  • 45
  • 1
  • 1
  • 11
  • what is the expected output? – Govinda Sakhare Mar 01 '20 at 06:58
  • Seat No : wb1 Booked Seat No : wb2 Booked Seat No : wb3 Booked Seat No : wb4 Available Seat No : wb5 Available Seat No : wb6 Booked Seat No : wb7 Available Seat No : wb8 Available Seat No : wb9 Available Seat No : wb10 Booked – Suhail Moh Mar 01 '20 at 07:54

2 Answers2

0

I did researches. System.out.println() is Thread-safe but System.err.println() is not. So Changing the System.err to System.out solve the problem.

And System.out.println("\u001B[32m"+"Available"); here "\u001B[32m" is used to display colored fonts using the ANSI.

"\u001B[32m" - Denotes Green Color

Thanks for the supports

Suhail Moh
  • 45
  • 1
  • 1
  • 11
-1

The first comment is good enough,

but I would suggest you enter "debug mode" of Eclipse to see what happened.

Step 1: Try to set a break point by clicking at left border of your "showReservationDetails" method.

Step 2: Try to run your software with "Debug", the button is placed next to the normal "Run" button.

Step 3: try to use "F6" to step through your program

Step 4: use your mouse to view values of variables during debug by pointing to them.

This is basic skill you need to see if anything wrong in your software. Hope this help.

KhanhLe
  • 1
  • 3