Example #1
0
  /**
   * Assign passenger p who has a booking but no seat to the specified seat.
   *
   * @param p the passenger to be given a seat
   * @param seat the seat for the passenger
   * @precond p != null && isSeatAvailable(seat) && p.hasBookingOn(this) && !p.hasSeatOn(this)
   */
  public void assignSeat(Passenger p, String seat) {
    if (p == null) throw new RuntimeException("Cannot assign a seat for a null passenger.");
    if (!isSeatAvailable(seat))
      throw new RuntimeException(
          "Seat " + seat + " is invalid or already occupied " + "so cannot book it.");
    if (p.hasSeatOn(this))
      throw new RuntimeException(
          p.getName()
              + " already has a seat on flight "
              + getNumber()
              + " so cannot book another one.");

    Iterator<Booking> waitingIter = waitingSeatList.iterator();
    while (waitingIter.hasNext()) {
      Booking curBooking = waitingIter.next();
      if (curBooking.getPerson() == p) {
        waitingIter.remove();
        curBooking.setSeat(seat);
        setSeat(curBooking);
        return;
      }
    }
    throw new RuntimeException("Cannot assign a seat if not already booked and waiting.");
  }
  /**
   * This method reports that a passenger is taking stairs (no elevator is available on the
   * appropriate floor).
   *
   * @param newPass Passenger object for this passenger
   */
  private static void takingStairs(Passenger newPass) {
    System.out.print(
        "Passenger " + newPass.getName() + " arrives on floor " + newPass.getFloor() + ".");

    System.out.println(" Taking stairs.");
  } //  end takingStairs
  /**
   * Read and process each item in the simulation file. It reads each input item in the file. If a
   * PASSENGER entry, a passenger object is created, and the elevators are checked to see if one is
   * available to service this passenger. If so, the passenger is logically added to the elevator.
   * If an elevator is not available, the passenger must take the stairs.
   *
   * <p>If the input item is a TICK, it indicates that one time unit has passed. At EOF, the method
   * is invoked to shut the elevators down and perform the end of day processing.
   *
   * <p>If an error is detected at any time, this method returns the proper boolean to the calling
   * method.
   *
   * @return True or False to indicate an error
   */
  private static boolean processSimulationFile() {

    // local constants used by this method

    final String PASSENGER = "passenger";
    final String TICK = "tick";
    final String SIMULATION_END = "done";

    boolean done = false; // set when done
    boolean error = false; // set if error detected
    boolean addedOK = false; // set if no elevator is available
    Passenger newPass = null; // new passenger object
    Elevator availableEl = null; // elevator object

    // Read next token from file and process

    try {
      while (!done && !error && in.nextToken() != in.TT_EOF) {

        if (in.sval.equals(PASSENGER)) {
          //  if a new passenger, create a new object, look
          //  for an elevator and add to elevator.  If elevator
          //  is not available, passenger takes the stairs.

          newPass = newPassenger();
          availableEl = findElevator(newPass);
          if (availableEl == null) takingStairs(newPass);
          else {
            System.out.print(
                "Passenger: "
                    + newPass.getName()
                    + " arrives on floor "
                    + newPass.getFloor()
                    + ".");
            addedOK = availableEl.addPassenger(newPass);
            if (!addedOK) {
              error = true;
            }
          }
        } else if (in.sval.equals(TICK)) {
          // token read from file is a time indicator
          recordTick();

        } else if (in.sval.equals(SIMULATION_END)) {
          // end of file == end of workday
          done = true;
        } else {
          error = true;
        }
      } // end while

      if (in.nextToken() == in.TT_EOF) {
        //  perform end of day reporting
        endOfDay();
      }

      // catch for file exception
    } catch (IOException e) {
      error = true;
    } // end try

    return error;
  } //  end processSimulationFile