Ejemplo n.º 1
0
  /**
   * This method checks both elevators to determine if one is on the floor where this passenger is
   * attempting to board.
   *
   * <p>It returns a reference to the elevator or a null if no elevator is available.
   *
   * @return available elevator or a null if no elevator is available.
   */
  private static Elevator findElevator(Passenger passenger) {
    Elevator foundElevator = null;

    if (elevator1.isElevatorHere(passenger)) {
      foundElevator = elevator1;
    } else if (elevator2.isElevatorHere(passenger)) {
      foundElevator = elevator2;
    }

    return foundElevator;
  } //  end findElevator
Ejemplo n.º 2
0
  /**
   * This method performs reporting at the end of the workday. It indicates that the elevators have
   * stopped service and reports passenger counts.
   */
  private static void endOfDay() {

    System.out.println();
    System.out.println("End of workday. Elevators stopping service.");
    System.out.println(
        "Stopping elevator 1 with "
            + elevator1.getTotalPassengers()
            + " onboard."); // changed method from getPassengerCount() to getTotalPassengers()
                            // edited by MAGNUM
    System.out.println(
        "Stopping elevator 2 with "
            + elevator2.getTotalPassengers()
            + " onboard."); // changed method from getPassengerCount() to getTotalPassengers()
                            // edited by MAGNUM
  } //  end endOfDay
Ejemplo n.º 3
0
 /** This method tells both elevators that one time unit has passed. */
 private static void recordTick() {
   //  tell each elevator that one unit of time has passed
   System.out.println("Tick.");
   elevator1.tick();
   elevator2.tick();
 } //  end recordTick
Ejemplo n.º 4
0
  /**
   * 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