// class Elevator.java //:Assignment 4: //:Student Name: Duo Zhou //:Studnet Number: 722452 /**FileName: Elevator.java * Date: July, 27, 2000 * Purpose: Learn how to use JFrame, JPanel, Synchronized method, creating * multithread, the basic idea of creating GUI * Name of Files: This assignment include Building.java, ConnectedWall.java, * Elevator.java, ElevatorFrame.java, ElevatorPanel.java, * Floor.java, MailDeliveryApp.java, MailPiece.java, * Position.java, Robot.java * Brief Discription: Nothing is changed from the original sample code, except that I add a * method to get the current floor number, for creating elevator GUI use. */ public class Elevator { private Building building; // which building is this elevator part-of private int currentFloor; // where is the elevator stationed now private static final int SECS_PER_FLOOR = 5; // 5 seconds to move to next floor //private Image robotImage; Elevator(Building building){ // Construct elevator for the given building this.building = building; currentFloor = Building.BASEMENT; // elevator starts at the basement or 0 } // This method is invoked by Robot only if inside elevator to move to a floor boolean moveRobTo(int floorNum){ if( building.getFloor( building.getRobot().getFloorNum() ). //we have a floor inElevator( building.getRobot().getPosition()) ){ if(currentFloor == floorNum) return true; // we are already at the requested floor try{ Thread.sleep( Math.abs(currentFloor - floorNum) * Elevator.SECS_PER_FLOOR * 1000); } catch(InterruptedException e){} currentFloor = floorNum; // now both elevator and Robot are at new floor building.getRobot().setFloor(floorNum); return true; }else // Robot not inside elevator return false; } boolean callFrom(int floorNum){ if( !( building.getFloor( building.getRobot().getFloorNum() ). inElevator( building.getRobot().getPosition()) ) ){ if(currentFloor == floorNum) return true; // we are already at the requested floor try{ Thread.sleep( Math.abs(currentFloor - floorNum) * Elevator.SECS_PER_FLOOR * 1000); } catch(InterruptedException e){ } currentFloor = floorNum; // now both elevator and Robot are at new floor return true; }else // Robot is inside elevator cannot override his priority in commanding // elevator return false; } public int getFloorNum() { return currentFloor; } }// end of class Elevator.java