• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to manage a list representing seats on an airplane?  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm working on a mini-project. I am building an airplane booking system, where each plane has a limited number of seats (50).

Currently, when a booking is made I add it to the flight details using this method. (booking holds various information inc Number of seats booked as an Int)



My issue is that I can't figure out the best way to keep track and assign the seats for each booking. I.e. say someone books 6 seats, how can I allocated seats 0-5 (or 1-6)? So when the next user comes, they can only book seats 6 (or 7) onwards?

I considered using a LinkedList which would allow me to keep track of seats, but then say someone cancels the booking and seats 2,3,4 become available, these would now be 'null', but how do I manage this?

Is it best to create a new class like 'Seats', where my LinkedList would then store 50x Seats object which holds the seat number and a boolean value (whether it's occupied or not) and then go from there? Or does anyone else have a better idea? Thanks!

*Assumptions:
-Not fussed about where they sit
-All airplanes have 50 seats
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using a 2D array of booleans. Seats on a plane are like a matrix (unless first class has less seats per row).

Since the number of seats doesn't change, you don't need the benefits of a LinkedListArrayList that it grows dynamically
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:How about using a 2D array of booleans. Seats on a plane are like a matrix

I know it kind of naturally fits how you visualize seats on an airplane seating chart but I don't see that the data structure needs to be a 2D array nor do I see that it needs to have boolean elements.  Booking[] or Reservation[] seems like it would do the job just as well. Another option might be a Map<Seat, Reservation>
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at this again with refactoring lenses, the first thing I notice is that the method parameter booking is not used at all and that the actual argument, c, comes out of nowhere. I can only assume your actual code is:
If you read the code out loud, a casual observer might think you had a bit of a stammer. Go ahead, try to read it out loud and listen to what you're saying. Yeah, that's what an API smell sounds like.

Benjamin Scabbia wrote:Currently, when a booking is made I add it to the flight details using this method. (booking holds various information inc Number of seats booked as an Int)

My issue is that I can't figure out the best way to keep track and assign the seats for each booking. I.e. say someone books 6 seats, how can I allocated seats 0-5 (or 1-6)? So when the next user comes, they can only book seats 6 (or 7) onwards?

Still with refactoring lenses on, I realized that my approach wouldn't be to dig deeper into the depths of implementation trying to find a suitable data structure. Instead, I would step back to a higher, more abstract plane of thinking and consider my API design. I would ask questions like:

1. If I were to change it, what would the code to add a booking look like?
2. Then what would the code to delete a booking look like?
3. Does the API for checking seat availability fit naturally with the booking API? What would that code look like?

To answer these questions, I'd have to write some tests to try different things, like:

This is what unit test code would end up looking like after several iterations of TDD. The comments normally wouldn't be there but I added them to give you hints about the intent/purpose of different parts of the test code. When I have a reasonably good looking and coherent API (try that "reading out loud to see if I sound like a blubbering mess" experiment with the test code above), the main code usually starts to scream out what internal data structures it wants to use and I just follow its lead.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!