/**
   * Load game state from file
   *
   * @param path Path to file
   */
  public void deserialize(String path) {
    try {
      for (int i = 0; i < getVehicles().size(); i++) {
        getVehicles().get(i).stopThread();
      }

      for (int i = 0; i < getIntersections().size(); i++) {
        getIntersections().get(i).stopThread();
      }

      Passenger p = passengers.poll();
      while (p != null) {
        p.stopThread();
        p = passengers.poll();
      }

      vehicles = null;
      intersections = null;
      passengers = null;
      //            DrawPanel.getInstance() = null;
      //            VehicleInspector.getInstance() = null;
      _instance = null;

      // Just to make sure things are right
      Thread.sleep(500);

      ObjectInputStream in =
          new ObjectInputStream(new BufferedInputStream(new FileInputStream(path)));

      _instance = (WorldManager) in.readObject();
      in.close();
      Thread.sleep(500);

      announceWorldChange();
      System.out.println("Deserialized!");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 2
0
  /**
   * 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
Ejemplo n.º 3
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