-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathElevatorConstants.java
71 lines (68 loc) · 2.92 KB
/
ElevatorConstants.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// ElevatorController.java
// Controller for Elevator Simulation
package com.deitel.jhtp5.elevator.controller;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.deitel.jhtp5.elevator.model.*;
import com.deitel.jhtp5.elevator.event.*;
import com.deitel.jhtp5.elevator.ElevatorConstants;
public class ElevatorControllerextendsJPanel
implements ElevatorConstants {
// controller contains two JButtons
private JButton firstControllerButton;
private JButton secondControllerButton;
private ElevatorSimulation elevatorSimulation;
public ElevatorConstants( ElevatorSimulation simulation ) {
elevatorSimulation = simulation;
setBackground( Color.WHITE );
// add first button to controller
firstControllerButton = new JButton( "First Floor" ); add(firstControllerButton);
// add second button to controller
secondControllerButton = new JButton( "Second Floor" ); add(secondControllerButton);
// anonymous inner class registers to receive ActionEvents // from first Controller JButton firstControllerButton.addActionListener(
new ActionListener() {
// invoked when a JButton has been pressed
public void actionPerformed( ActionEvent event ) {
// place Person on first Floor
elevatorSimulation.addPerson(FIRST_FLOOR_NAME);
// disable user input
firstControllerButton.setEnabled( false ); }
} // end anonymous inner class
);
new ActionListener() {
public void actionPerformed( ActionEvent event ) {
elevatorSimulation.addPerson(SECOND_FLOOR_NAME );
secondControllerButton.setEnabled( false ); }
}
);
elevatorSimulation.addPersonMoveListener(
new PersonMoveListener() {
public void personEntered(PersonMoveEvent event )
{
String location = event.getLocation().getLocationName();
if ( location.equals( FIRST_FLOOR_NAME ) )
firstControllerButton.setEnabled( true );
else
secondControllerButton.setEnabled( true );
} // end method personEntered 96
// other methods implementing PersonMoveListener
public void personCreated(PersonMoveEvent event ){}
public void personArrived(
PersonMoveEvent event ) {}
public void personExited(
PersonMoveEvent event ) {}
public void personDeparted(
PersonMoveEvent event ) {}
public void personPressedButton(
PersonMoveEvent event ) {}
} // end anonymous inner class
);
} // end ElevatorController constructor
}
// ElevatorConstants.java
// Constants used between ElevatorModel and ElevatorView package com.deitel.jhtp5.elevator;
public interface ElevatorConstants {
public static final String FIRST_FLOOR_NAME = "firstFloor";
public static final String SECOND_FLOOR_NAME = "secondFloor";
public static final String ELEVATOR_NAME = "elevator";