concept Flight in category java

appears as: Flight, Flight
JUnit in Action

This is an excerpt from Manning's book JUnit in Action.

One project under development at Tested Data Systems is a flight-management application. The application creates and sets up flights; it also adds and removes passengers. The engineers want to test the integration between two classes: Passenger and Flight. The test will check whether each passenger can be added to or removed from a flight correctly, as well as whether the number of passengers exceeds the number of seats. (Nobody wants to get on a flight without having a seat!) The following two listings show the Passenger and Flight classes, as well as their logic.

Let's look at the initial design for this application (figure 20.3). It has a field called flightType in the Flight class. Its value determines the behavior of the addPassenger and removePassenger methods. The developers need to focus on decision-making at the level of the code for these two methods.

Figure 20.3 The class diagram for the flight-management application: the flight type is kept as a field in the Flight class.

The next listing shows the Flight class.

Listing 20.2 Flight class
public class Flight {
 
   private String id;                                                    #1
   private List<Passenger> passengers = new ArrayList<Passenger>();      #2
   private String flightType;                                            #3
 
   public Flight(String id, String flightType) {                         #4
      this.id = id;                                                      #4
      this.flightType = flightType;                                      #4
   }                                                                     #4
 
   public String getId() {                                               #5
      return id;                                                         #5
   }                                                                     #5
 
   public List<Passenger> getPassengersList() {                          #6
      return Collections.unmodifiableList(passengers);                   #6
   }                                                                     #6
 
   public String getFlightType() {                                       #7
      return flightType;                                                 #7
   }                                                                     #7
 
 
   public boolean addPassenger(Passenger passenger) {
      switch (flightType) {                                              #8
         case "Economy":                                                 #9
            return passengers.add(passenger);                            #9
         case "Business":                                               #10
            if (passenger.isVip()) {                                    #10
               return passengers.add(passenger);                        #10
            }                                                           #10
            return false;                                               #10
         default:                                                       #11
            throw new RuntimeException("Unknown type: " + flightType);  #11
      }                                                                
 
   }
 
   public boolean removePassenger(Passenger passenger) {
      switch (flightType) {                                             #12
         case "Economy":                                                #13
            if (!passenger.isVip()) {                                   #13
               return passengers.remove(passenger);                     #13
            }                                                           #13
            return false;                                               #13
         case "Business":                                               #14
            return false;                                               #14
         default:                                                       #15
            throw new RuntimeException("Unknown type: " + flightType);  #15
         }
   }
 
}