0

I am making a transportation project. As there will be different types of buses with different number of seats. The seats will be either booked or not. I don't know whether I should make a boolean array of seats as data member of a class or should I just make only one boolean data member as a seat of the buses and later on do something with it, i.e after initialization.

How to overcome such cases?

Awais Korai
  • 27
  • 1
  • 6
  • Learn about [Collections Framework](https://docs.oracle.com/javase/tutorial/collections/intro/index.html). – PM 77-1 Nov 08 '17 at 22:28

1 Answers1

0

An array of booleans would not give you much more information than just a number of reserved seats. It depends on what you need. If you need to actually track which seats are reserved, then you will probably want an ArrayList of BusSeats where the BusSeat object identifies the seat and whether it is reserved or not. If you just need a tally of how many seats are reserved and how many are left, then the Bus object can just have an integer for the total available seats, and another for the total reserved seats.

  • I have to track it! I was using an array because there would of course be no bus with more than 100 seats? The max an array allows – Awais Korai Nov 08 '17 at 22:33
  • Your object has to have what it needs to track, so if your bus object needs to track which seats in the bus are reserved, then it needs to have that. An array is fine if you have a hard max and it is small, like this. (Arrays allow a lot more than 100 items - a bit over 2 billion items). –  Nov 08 '17 at 22:38
  • Oh, our Professor told us otherwise, I should have done a bit of a research. Yes, I was limiting it to a number of seats that does have a 'hard max'. It would be known how many seats a bus has and then an administrator reserves it. So an array or an arraylist(if real time) is fine, right? – Awais Korai Nov 08 '17 at 22:46
  • yes, either will work. Because the number of seats for a bus is fixed, an array is better. It is more efficient than an arraylist, although you will not notice the difference. –  Nov 08 '17 at 22:52