Skip to main content
added 85 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I have several suggestions:

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

  2. 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,enums 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.

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

I have several suggestions:

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

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

I have several suggestions:

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

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

edited body
Source Link
Thomas Junk
  • 2k
  • 14
  • 18

I have several suggestions:

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

  2. 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)?0firstClassStart:5;economyStart; int stop=start+capacity; for(int number=start;number<stop;number+=1){ if(seats[number]==falseseats[number]==free){ seats[number]=true;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.

I have several suggestions:

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

  2. 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)?0:5; int stop=start+capacity; for(int number=start;number<stop;number+=1){ if(seats[number]==false){ seats[number]=true; 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.

I have several suggestions:

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

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

Source Link
Thomas Junk
  • 2k
  • 14
  • 18

I have several suggestions:

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

  2. 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)?0:5; int stop=start+capacity; for(int number=start;number<stop;number+=1){ if(seats[number]==false){ seats[number]=true; 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.