It's not bad. The biggest problem here is the code duplication between firstClassSeat() and economySeat(). If you find yourself copy-and-pasting, you're doing something wrong. You should combine these into one `bookSeat` method, taking a parameter to indicate class. You can use an `int` for the class of seat, but in real life it would be better to use an enumeration.
You could use a boolean flag, something like `seatAssigned` that starts as false, and gets set to true when you assign a seat. If it's still false at the end of the loop, you know the section was full. You can also use this flag in the for-loop condition, which allows you to avoid `break`
Luigi Plinge wrote:It's not bad. The biggest problem here is the code duplication between firstClassSeat() and economySeat(). If you find yourself copy-and-pasting, you're doing something wrong. You should combine these into one `bookSeat` method, taking a parameter to indicate class. You can use an `int` for the class of seat, but in real life it would be better to use an enumeration.
Your system for checking if each section is fully booked doesn't seem very watertight (although it might be OK in this toy example). You know it's fully booked if you've been through and not found a seat, not by checking if one particular seat is full. You could use a boolean flag, something like `seatAssigned` that starts as false, and gets set to true when you assign a seat. If it's still false at the end of the loop, you know the section was full. You can also use this flag in the for-loop condition, which allows you to avoid `break` (which is bad style, because it provides multiple exit points making code harder to follow).