-4

I need to create a Airline Reservation App but I don't know how to make a map of the seat numbers / positions using 2D arrays. Anyone can help?

2 Answers2

0

Have a look at multidimensional arrays. I'm not sure if using a 2D array is the way to go but here's a way to do it:

boolean[][] seats = new boolean[NUMBER_OF_ROWS][NUMBER_OF_SEATS_PER_ROW];

// checking if a seat is reserved:

boolean reserved = seats[rowIndex][seatIndex];

// marking a seat as reserved:

seats[rowIndex][seatIndex] = true;
Community
  • 1
  • 1
PETA
  • 31
  • 4
0

So in general there are multiple ways to solve this problem.

I remember being on a plane and I think the seats was just a unique number going from 1 and upwards. So if we keep that in mind and also that seats are placed not exactly right in front or next to each other as the plane structure would not always allow that, and that some areas have only 4 seats per row and other 6 how would we know the position.

What I'd recommend you when would be to actually just have a simple list of the seat object which would look like this:

public class PlaceSeat {

    public final int number;
    public final double x, y;

    public PlaceSeat(int number, double x, double y) {
        this.number = number;
        this.x = x;
        this.y = y;
    }
}

Once you have this you can use whatever graphic tool and run through the list and draw the seats on the app screen.

Additional information like what image to use for a seat, the width/height and rotation might also be relevant. Also maybe some kind of action Listener if a user click on some seat.

Wisienkas
  • 1,602
  • 2
  • 17
  • 22