📄 light.java
字号:
// Light.java
// Light turns a light on or off
package com.deitel.jhtp5.elevator.model;
// Deitel packages
import com.deitel.jhtp5.elevator.event.*;
public class Light implements ElevatorMoveListener {
// Light state (on/off)
private boolean lightOn;
// time before Light turns off automatically (3 seconds)
public static final int AUTOMATIC_TURNOFF_DELAY = 3000;
// LightListener listens for when Light should turn on/off
private LightListener lightListener;
// location where Light turned on or off
private Location lightLocation;
// set LightListener
public void setLightListener( LightListener listener )
{
lightListener = listener;
}
// turn on Light
public void turnOnLight( Location location )
{
if ( !lightOn ) {
lightOn = true;
// send LightEvent to LightListener
lightListener.lightTurnedOn(
new LightEvent( this, location ) );
lightLocation = location;
// declare Thread that ensures automatic Light turn off
Thread thread = new Thread(
new Runnable() {
public void run()
{
// turn off Light if on for more than 3 seconds
try {
Thread.sleep( AUTOMATIC_TURNOFF_DELAY );
turnOffLight( lightLocation );
}
// handle exception if interrupted
catch ( InterruptedException exception ) {
exception.printStackTrace();
}
}
} // end anonymous inner class
);
thread.start();
}
} // end method turnOnLight
// turn off Light
public void turnOffLight( Location location )
{
if ( lightOn ) {
lightOn = false;
// send LightEvent to LightListener
lightListener.lightTurnedOff(
new LightEvent( this, location ) );
}
} // end method turnOffLight
// return whether Light is on or off
public boolean isLightOn()
{
return lightOn;
}
// invoked when Elevator has departed
public void elevatorDeparted(
ElevatorMoveEvent moveEvent )
{
turnOffLight( moveEvent.getLocation() );
}
// invoked when Elevator has arrived
public void elevatorArrived(
ElevatorMoveEvent moveEvent )
{
turnOnLight( moveEvent.getLocation() );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -