Search code examples
javaarraysobjectdatamember

Should I make an array a data member of an object? Or is there any other way to declare a member of an object multiple times?


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?


Solution

  • 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.