I have several suggestions:
For readability I would suggest to introduce Enums like
public enum SeatType { FIRSTCLASS, ECONOMY } If you have to make several decisions depending on the seats type SeatType.FIRSTCLASS reads better, than perhaps seatnumber<5.
You are doing twice the same thing: Iterating through a partial array. It would be more streamlined, if you write a method for both cases,
enum
s like:private Integer tryReservation(SeatType s) { Integer reservation=null; int start=(s==SeatType.FIRSTCLASS)?firstClassStart:economyStart; int stop=start+capacity; for(int number=start;number<stop;number+=1){ if(seats[number]==free){ seats[number]=!free; reservation=number; break; } } return reservation; } This makes the following possible:
private Integer makeReservation(SeatType s) { SeatType alternative=getAlternative(s); Integer reservedSeat=tryReservation(s); if(reservedSeat==null && !isClassFull(alternative)){ if(alternativeWanted){ reservedSeat=tryReservation(alternative); } } return reservedSeat; }
So your code becomes much smaller. And the number of if-else-whatelse decreases a lot.public enum SeatType { FIRSTCLASS, ECONOMY }
If you have to make several decisions depending on the seats type SeatType.FIRSTCLASS
reads better, than perhaps seatnumber<5
.
- You are doing twice the same thing: Iterating through a partial array.
It would be more streamlined if you write a method for both cases, like:
private Integer tryReservation(SeatType s) {
Integer reservation=null;
int start=(s==SeatType.FIRSTCLASS)?firstClassStart:economyStart;
int stop=start+capacity;
for(int number=start;number<stop;number+=1){
if(seats[number]==free){
seats[number]=!free;
reservation=number;
break;
}
}
return reservation;
}
This makes the following possible:
private Integer makeReservation(SeatType s) {
SeatType alternative=getAlternative(s);
Integer reservedSeat=tryReservation(s);
if(reservedSeat==null && !isClassFull(alternative)){
if(alternativeWanted){
reservedSeat=tryReservation(alternative);
}
}
return reservedSeat;
}
So your code becomes much smaller. And the number of if-else-whatelse decreases a lot.