 2003 Prentice Hall, Inc. All rights reserved. Appendix E – Elevator Model Outline E.1 Introduction E.2 Class ElevatorSimulation E.3Classes Location and.

Slides:



Advertisements
Similar presentations
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Written by: Dr. JJ Shepherd
Instructore: Tasneem Darwish1 University of Palestine Faculty of Applied Engineering and Urban Planning Software Engineering Department Concurrent and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Appendix F – Elevator View Outline F.1Introduction F.2Class Objects F.3Class Constants F.4Class Constructor.
 2001 Prentice Hall, Inc. All rights reserved. 1 Appendix D – Elevator Events and Listener Interfaces Outline D.1 Introduction D.2 Events D.3Listeners.
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Copyright © 2003 ProsoftTraining. All rights reserved. Sun Certified Java Programmer Exam Preparation Guide.
1 Object Oriented Analysis and Design. 2 Object-Oriented Analysis  Statement of what our client wants Object-Oriented Design  How to provide it using.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 16.1 Test-Driving the Flag Quiz Application.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development More stuff on Threads Paul Krause.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Introduction to Java Prepared by: Ahmed Hefny. Outline Classes Access Levels Member Initialization Inheritance and Polymorphism Interfaces Inner Classes.
The Java Programming Language
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 5 Outline 5.1 Introduction 5.2 Collaborations 5.3 Creating Collaborations.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 7 Outline 7.1 Introduction 7.2 Overview of Simulation Implementation.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 4 Outline 4.1 Introduction 4.2 Class Operations 4.3 Creating Class.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 3 Outline 3.1 Introduction 3.2 Class Attributes 3.3 Statechart Diagrams.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 16 – Multithreading Outline 16.1 Introduction 16.2 Thread States: Life Cycle of a Thread 16.3 Thread.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
 2002 Prentice Hall, Inc. All rights reserved. Appendix H – Elevator Model Outline H.1 Introduction H.2 Class ElevatorModel H.3Classes Location and Floor.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 2 Outline 2.1 Introduction 2.2 Thinking About Objects: Identifying.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
 2002 Prentice Hall, Inc. All rights reserved. Appendix I – Elevator View Outline I.1Introduction I.2Class Objects I.3Class Constants I.4Class Constructor.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
 2006 Doan Van Ban, IOIT. All rights reserved. 1 Case Study: E levator System Simulation Program Goal –Software simulator application –N-floor elevator.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2002 Prentice Hall, Inc. All rights reserved. Outline HoldIntegerUnsyn chronized.java Line 4 Lines 7-13 Lines // Fig. 15.6: HoldIntegerUnsynchronized.java.
Essentials of Counter-Controlled Repetition Counter-controlled repetition requires: Control variable (loop counter) Initial value of the control variable.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
 2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 6 Outline 6.1 Introduction 6.2 Implementation: Visibility 6.3 Implementation:
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Multithreading / Concurrency
Multi Threading.
Optional Case Study - Chapter 4
Threads Chate Patanothai.
null, true, and false are also reserved.
Multithreaded Programming
Chapter 4 - Control Structures: Part 1
Object Oriented Programming in java
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

 2003 Prentice Hall, Inc. All rights reserved. Appendix E – Elevator Model Outline E.1 Introduction E.2 Class ElevatorSimulation E.3Classes Location and Floor E.4Class Door and ElevatorDoor E.5Class Button E.6Class ElevatorShaft E.7Classes Light and Bell E.8Class Elevator E.9Class Person E.10Artifacts Revisited E.11Conclusion

 2003 Prentice Hall, Inc. All rights reserved. E.1 Introduction Classes implement the MVC model

 2003 Prentice Hall, Inc. All rights reserved. E.2 Class ElevatorSimulation ElevatorSimulation –“Ties together” the objects that comprise the elevator simulation model –Sends events from MVC model to view –Instantiates Person object (as per user request) –Allows Floor to obtain reference to ElevatorShaft

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Line 12 Lines Lines // ElevatorSimulation.java 2 // Elevator simulation model with ElevatorShaft and two Floors 3 package com.deitel.jhtp5.elevator.model; 4 5 // Java core packages 6 import java.util.*; 7 8 // Deitel packages 9 import com.deitel.jhtp5.elevator.event.*; 10 import com.deitel.jhtp5.elevator.ElevatorConstants; public class ElevatorSimulation implements ElevatorSimulationListener, 13 ElevatorConstants { // declare two-Floor architecture in simulation 16 private Floor firstFloor; 17 private Floor secondFloor; // ElevatorShaft in simulation 20 private ElevatorShaft elevatorShaft; // objects listening for events from ElevatorModel 23 private Set personMoveListeners; 24 private DoorListener doorListener; 25 private ButtonListener buttonListener; 26 private LightListener lightListener; 27 private BellListener bellListener; 28 private ElevatorMoveListener elevatorMoveListener; ElevatorSimulation implements ElevatorSimulationListener, which inherits from all listener interfaces Use class diagram to determine associations with Floor and ElevatorShaft Declare listeners that receive events from model (and will send these events to ElevatorView )

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Line 31 Lines Lines // cumulative number of people in simulation 31 private int numberOfPeople = 0; // constructor instantiates ElevatorShaft and Floors 34 public ElevatorSimulation() 35 { 36 // instantiate firstFloor and secondFloor objects 37 firstFloor = new Floor( FIRST_FLOOR_NAME ); 38 secondFloor = new Floor( SECOND_FLOOR_NAME ); // instantiate ElevatorShaft object 41 elevatorShaft = 42 new ElevatorShaft( firstFloor, secondFloor ); // give elevatorShaft reference to first and second Floor 45 firstFloor.setElevatorShaft( elevatorShaft ); 46 secondFloor.setElevatorShaft( elevatorShaft ); // register for events from ElevatorShaft 49 elevatorShaft.setDoorListener( this ); 50 elevatorShaft.setButtonListener( this ); 51 elevatorShaft.addElevatorMoveListener( this ); 52 elevatorShaft.setLightListener( this ); 53 elevatorShaft.setBellListener( this ); // instantiate Set for ElevatorMoveListener objects 56 personMoveListeners = new HashSet( 1 ); } // end ElevatorModel constructor Instantiate Floor s and ElevatorShaft, then assign the ElevatorShaft reference to each Floor Register ElevatorSimulation for events from ElevatorShaft Use class diagram to determine attributes

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines // return Floor with given name 61 private Floor getFloor( String name ) 62 { 63 if ( name.equals( FIRST_FLOOR_NAME ) ) 64 return firstFloor; 65 else if ( name.equals( SECOND_FLOOR_NAME ) ) 68 return secondFloor; 69 else 70 return null; } // end method getFloor // add Person to Elevator Simulator 75 public void addPerson( String floorName ) 76 { 77 // instantiate new Person and place on Floor 78 Person person = 79 new Person( numberOfPeople, getFloor( floorName ) ); 80 person.setName( Integer.toString( numberOfPeople ) ); // register listener for Person events 83 person.setPersonMoveListener( this ); // start Person thread 86 person.start(); Method for adding Person to simulation model Instantiate Person, register ElevatorModel to receive events from that Person and start Person ’s thread

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines Lines // increment number of Person objects in simulation 89 numberOfPeople++; } // end method addPerson // invoked when Elevator has departed from Floor 94 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) 95 { 96 elevatorMoveListener.elevatorDeparted( moveEvent ); 97 } // invoked when Elevator has arrived at destination Floor 100 public void elevatorArrived( ElevatorMoveEvent moveEvent ) 101 { 102 elevatorMoveListener.elevatorArrived( moveEvent ); 103 } // send PersonMoveEvent to listener, depending on event type 106 private void sendPersonMoveEvent( 107 int eventType, PersonMoveEvent event ) 108 { 109 Iterator iterator = personMoveListeners.iterator(); while ( iterator.hasNext() ) { PersonMoveListener listener = 114 ( PersonMoveListener ) iterator.next(); When Elevator has arrived at Floor, notify listener ( ElevatorView ) When Person performs some action (event) in model, determine which event was sent, then forward the event to any listeners When Elevator has departed from Floor, notify listener (i.e., Elevator- View, in this simulation)

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines // send Event to this listener, depending on eventType 117 switch ( eventType ) { // Person has been created 120 case Person.PERSON_CREATED: 121 listener.personCreated( event ); 122 break; // Person arrived at Elevator 125 case Person.PERSON_ARRIVED: 126 listener.personArrived( event ); 127 break; // Person entered Elevator 130 case Person.PERSON_ENTERING_ELEVATOR: 131 listener.personEntered( event ); 132 break; // Person pressed Button object 135 case Person.PERSON_PRESSING_BUTTON: 136 listener.personPressedButton( event ); 137 break; // Person exited Elevator 140 case Person.PERSON_EXITING_ELEVATOR: 141 listener.personDeparted( event ); 142 break; When Person performs some action (event) in model, determine which event was sent, then forward the event to any listeners

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines Lines // Person exited simulation 145 case Person.PERSON_EXITED: 146 listener.personExited( event ); 147 break; default: 150 break; 151 } 152 } 153 } // end method sendPersonMoveEvent // invoked when Person has been created in model 156 public void personCreated( PersonMoveEvent moveEvent ) 157 { 158 sendPersonMoveEvent( Person.PERSON_CREATED, moveEvent ); 159 } // invoked when Person has arrived at Floor's Button 162 public void personArrived( PersonMoveEvent moveEvent ) 163 { 164 sendPersonMoveEvent( Person.PERSON_ARRIVED, moveEvent ); 165 } // invoked when Person has pressed Button 168 public void personPressedButton( PersonMoveEvent moveEvent ) 169 { 170 sendPersonMoveEvent( Person.PERSON_PRESSING_BUTTON, 171 moveEvent ); 172 } When Person has pressed Button, notify listeners When Person has been created, notify listeners When Person has arrived at Elevator, notify listeners

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines Lines Lines // invoked when Person has entered Elevator 175 public void personEntered( PersonMoveEvent moveEvent ) 176 { 177 sendPersonMoveEvent( Person.PERSON_ENTERING_ELEVATOR, 178 moveEvent ); 179 } // invoked when Person has departed from Elevator 182 public void personDeparted( PersonMoveEvent moveEvent ) 183 { 184 sendPersonMoveEvent( Person.PERSON_EXITING_ELEVATOR, 185 moveEvent ); 186 } // invoked when Person has exited Simulation 189 public void personExited( PersonMoveEvent moveEvent ) 190 { 191 sendPersonMoveEvent( Person.PERSON_EXITED, moveEvent ); 192 } // invoked when Door has opened 195 public void doorOpened( DoorEvent doorEvent ) 196 { 197 doorListener.doorOpened( doorEvent ); 198 } 199 When Person has entered Elevator, notify listeners When Person has left Elevator, notify listeners When Person has exited simulation, notify listeners When Door has opened, notify listeners

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines Lines Lines Lines // invoked when Door has closed 201 public void doorClosed( DoorEvent doorEvent ) 202 { 203 doorListener.doorClosed( doorEvent ); 204 } // invoked when Button has been pressed 207 public void buttonPressed( ButtonEvent buttonEvent ) 208 { 209 buttonListener.buttonPressed( buttonEvent ); 210 } // invoked when Button has been reset 213 public void buttonReset( ButtonEvent buttonEvent ) 214 { 215 buttonListener.buttonReset( buttonEvent ); 216 } // invoked when Bell has rung 219 public void bellRang( BellEvent bellEvent ) 220 { 221 bellListener.bellRang( bellEvent ); 222 } // invoked when Light has turned on 225 public void lightTurnedOn( LightEvent lightEvent ) 226 { 227 lightListener.lightTurnedOn( lightEvent ); 228 } When Door has closed, notify listeners When Button has been pressed, notify listeners When Button has been reset, notify listeners When Bell has rung, notify listeners When Light has been turned on, notify listeners

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines Lines // invoked when Light has turned off 231 public void lightTurnedOff( LightEvent lightEvent ) 232 { 233 lightListener.lightTurnedOff( lightEvent ); 234 } // set listener for ElevatorModelListener 237 public void setElevatorSimulationListener( 238 ElevatorSimulationListener listener ) 239 { 240 // ElevatorModelListener extends all interfaces below 241 addPersonMoveListener( listener ); 242 setElevatorMoveListener( listener ); 243 setDoorListener( listener ); 244 setButtonListener( listener ); 245 setLightListener( listener ); 246 setBellListener( listener ); 247 } // set listener for PersonMoveEvents 250 public void addPersonMoveListener( 251 PersonMoveListener listener ) 252 { 253 personMoveListeners.add( listener ); 254 } 255 Allow PersonListener to listen for PersonMoveEvent s from model Allow ElevatorSimulationListener to listen for all events from model When Light has been turned off, notify listeners

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorSimulat ion.java Class ElevatorSimulat ion represents the MVC model in our elevator simulation. Lines Lines Lines Lines Lines // set listener for DoorEvents 257 public void setDoorListener( DoorListener listener ) 258 { 259 doorListener = listener; 260 } // set listener for ButtonEvents 263 public void setButtonListener( ButtonListener listener ) 264 { 265 buttonListener = listener; 266 } // add listener for ElevatorMoveEvents 269 public void setElevatorMoveListener( 270 ElevatorMoveListener listener ) 271 { 272 elevatorMoveListener = listener; 273 } // set listener for LightEvents 276 public void setLightListener( LightListener listener ) 277 { 278 lightListener = listener; 279 } // set listener for BellEvents 282 public void setBellListener( BellListener listener ) 283 { 284 bellListener = listener; 285 } 286 } Allow ElevatorMoveListener to listen for ElevatorMoveEvent s from model Allow LightListener to listen for LightEvent s from model Allow BellListener to listen for BellEvent s from model Allow DoorListener to listen for DoorEvent s from model Allow ButtonListener to listen for ButtonEvent s from model

 2003 Prentice Hall, Inc. All rights reserved. Fig. E.2 Class diagram showing realizations in the elevator model (Part 1). ElevatorSimulation ElevatorSimulationListener Elevator ButtonListenerDoorListenerBellListener ElevatorShaft ButtonListener DoorListener LightListener ElevatorMove- Listener BellListener LightBellElevatorDoorButton ElevatorMoveListener

 2003 Prentice Hall, Inc. All rights reserved. Fig. E.3 Class diagram showing realizations in the elevator model (Part 2). ElevatorSimulation Button- Listener Door- Listener ElevatorMove- Listener PersonMove- Listener Bell- Listener Light- Listener ElevatorSimulationListener

 2003 Prentice Hall, Inc. All rights reserved. Fig. E.4 Classes and implemented listener interfaces from Fig. E.2.

 2003 Prentice Hall, Inc. All rights reserved. E.3Classes Location and Floor Location –Represents location in the simulation Person has reference to Location –We can know each Person ’s whereabouts –Abstract superclass –Subclasses: Floor and Elevator Floor represents first or second floor in model ( Elevator is discussed later)

 2003 Prentice Hall, Inc. All rights reserved. Outline Location.java Location superclass that represents a location in the simulation. Line 11 Lines // Location.java 2 // Abstract superclass representing location in simulation 3 package com.deitel.jhtp5.elevator.model; 4 5 // Deitel packages 6 import com.deitel.jhtp5.elevator.event.*; 7 8 public abstract class Location { 9 10 // name of Location 11 private String locationName; // set name of Location 14 protected void setLocationName( String name ) 15 { 16 locationName = name; 17 } // return name of Location 20 public String getLocationName() 21 { 22 return locationName; 23 } // return Button at Location 26 public abstract Button getButton(); // return Door object at Location 29 public abstract Door getDoor(); 30 } Name of Location can equal “ elevator,” “ first floor ” or “ second floor ” Classes Floor and Elevator implement these abstract methods to return appropriate objects

 2003 Prentice Hall, Inc. All rights reserved. Outline Floor.java Class Floor —a subclass of Location — represents a Floor across which a Person walks to the Elevator. 1 // Floor.java 2 // Represents a Floor located next to an ElevatorShaft 3 package com.deitel.jhtp5.elevator.model; 4 5 // Deitel packages 6 import com.deitel.jhtp5.elevator.ElevatorConstants; 7 8 public class Floor extends Location 9 implements ElevatorConstants { // reference to ElevatorShaft object 12 private ElevatorShaft elevatorShaft; // Floor constructor sets name of Floor 15 public Floor( String name ) 16 { 17 setLocationName( name ); 18 } 19

 2003 Prentice Hall, Inc. All rights reserved. Outline Floor.java Class Floor —a subclass of Location — represents a Floor across which a Person walks to the Elevator. Lines Lines // get first or second Floor Button, using Location name 21 public Button getButton() 22 { 23 if ( getLocationName().equals( FIRST_FLOOR_NAME ) ) 24 return getElevatorShaft().getFirstFloorButton(); 25 else if ( getLocationName().equals( SECOND_FLOOR_NAME ) ) 28 return getElevatorShaft().getSecondFloorButton(); 29 else return null; } // end method getButton // get first or second Floor Door, using Location name 36 public Door getDoor() 37 { 38 if ( getLocationName().equals( FIRST_FLOOR_NAME ) ) 39 return getElevatorShaft().getFirstFloorDoor(); 40 else if ( getLocationName().equals( SECOND_FLOOR_NAME ) ) 43 return getElevatorShaft().getSecondFloorDoor(); 44 else return null; } // end method getDoor Implement Location ’s abstract method getButton to return Button on either first or second Floor Implement Location ’s abstract method getDoor to return Door on either first or second Floor

 2003 Prentice Hall, Inc. All rights reserved. Outline Floor.java Class Floor —a subclass of Location — represents a Floor across which a Person walks to the Elevator // get ElevatorShaft reference 51 public ElevatorShaft getElevatorShaft() 52 { 53 return elevatorShaft; 54 } // set ElevatorShaft reference 57 public void setElevatorShaft( ElevatorShaft shaft ) 58 { 59 elevatorShaft = shaft; 60 } 61 }

 2003 Prentice Hall, Inc. All rights reserved. E.4Class Door and ElevatorDoor Door –Signals Person when to enter and exit Elevator –Subclass ElevatorDoor

 2003 Prentice Hall, Inc. All rights reserved. Outline Door.java Class Door, which represents a Door in the model, informs listeners when a Door has opened or closed. Line 11 Lines 20 and 28 1 // Door.java 2 // Sends DoorEvents to DoorListeners when opened or closed 3 package com.deitel.jhtp5.elevator.model; 4 5 // Java core packages 6 import java.util.*; 7 8 // Deitel packages 9 import com.deitel.jhtp5.elevator.event.*; public class Door { // represent whether Door is open or closed 14 private boolean open = false; // time before Door closes automatically 17 public static final int AUTOMATIC_CLOSE_DELAY = 3000; // Set of DoorListeners 20 private Set doorListeners; // location where Door opened or closed 23 private Location doorLocation; // Door constructor instantiates Set for DoorListeners 26 public Door() 27 { 28 doorListeners = new HashSet( 1 ); 29 } Door listens for ElevatorMoveEvent s; when Elevator arrives, Door opens HashSet stores listener for DoorEvent s (i.e., when Door opens and closes)

 2003 Prentice Hall, Inc. All rights reserved. Outline Door.java Class Door, which represents a Door in the model, informs listeners when a Door has opened or closed. Lines Line // add Door listener 32 public void addDoorListener( DoorListener listener ) 33 { 34 // prevent other objects from modifying doorListeners 35 synchronized( doorListeners ) 36 { 37 doorListeners.add( listener ); 38 } 39 } // remove Door listener 42 public void removeDoorListener( DoorListener listener ) 43 { 44 // prevent other objects from modifying doorListeners 45 synchronized( doorListeners ) 46 { 47 doorListeners.remove( listener ); 48 } 49 } // open Door and send all listeners DoorEvent objects 52 public synchronized void openDoor( Location location ) 53 { 54 if ( !open ) { open = true; 57 Allow DoorListener s to register and unregister to receive DoorEvent s Open Door if it is closed

 2003 Prentice Hall, Inc. All rights reserved. Outline Door.java Class Door, which represents a Door in the model, informs listeners when a Door has opened or closed. Lines Lines // obtain iterator from Set 59 Iterator iterator; 60 synchronized( doorListeners ) 61 { 62 iterator = new HashSet( doorListeners ).iterator(); 63 } // get next DoorListener 66 while ( iterator.hasNext() ) { 67 DoorListener doorListener = 68 ( DoorListener ) iterator.next(); // send doorOpened event to this DoorListener 71 doorListener.doorOpened( 72 new DoorEvent( this, location ) ); 73 } doorLocation = location; // declare Thread that ensures automatic Door closing 78 Thread closeThread = new Thread( 79 new Runnable() { public void run() 82 { 83 // close Door if open for more than 3 seconds 84 try { 85 Thread.sleep( AUTOMATIC_CLOSE_DELAY ); 86 closeDoor( doorLocation ); 87 } Notify DoorListener s that Door has opened Use Thread to enable Door to close itself automatically after three seconds

 2003 Prentice Hall, Inc. All rights reserved. Outline Door.java Class Door, which represents a Door in the model, informs listeners when a Door has opened or closed. Line // handle exception if interrupted 90 catch ( InterruptedException exception ) { 91 exception.printStackTrace(); 92 } 93 } 94 } // end anonymous inner class 95 ); closeThread.start(); 98 } // notify all waiting threads that the door has opened 101 notifyAll(); } // end method openDoor // close Door and send all listeners DoorEvent objects 106 public synchronized void closeDoor( Location location ) 107 { 108 if ( open ) { open = false; // obtain iterator from Set 113 Iterator iterator; 114 synchronized( doorListeners ) 115 { 116 iterator = new HashSet( doorListeners ).iterator(); 117 } Close Door if it is open

 2003 Prentice Hall, Inc. All rights reserved. Outline Door.java Class Door, which represents a Door in the model, informs listeners when a Door has opened or closed. Lines Lines // get next DoorListener 120 while ( iterator.hasNext() ) { 121 DoorListener doorListener = 122 ( DoorListener ) iterator.next(); // send doorClosed event to this DoorListener 125 doorListener.doorClosed( 126 new DoorEvent( this, location ) ); 127 } 128 } } // end method closeDoor // return whether Door is open or closed 133 public synchronized boolean isDoorOpen() 134 { 135 return open; 136 } 137 } When Elevator arrives, open Door Notify DoorListener s that Door has closed

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorDoor.ja va Class ElevatorDoor opens in response to elevatorArrived messages and opens and closes the Floor Doors. Line 11 Lines // ElevatorDoor.java 2 // Opens and closes floor Door when elevator arrives and departs. 3 package com.deitel.jhtp5.elevator.model; 4 5 // Java core packages 6 import java.util.*; 7 8 // Deitel packages 9 import com.deitel.jhtp5.elevator.event.*; public class ElevatorDoor extends Door implements ElevatorMoveListener { // open ElevatorDoor and corresponding Floor Door 14 public synchronized void openDoor( Location location ) 15 { 16 location.getDoor().openDoor( location ); super.openDoor( location ); } // end method openDoor 21 ElevatorDoor extends Door and implements ElevatorMoveListener Override method openDoor

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorDoor.ja va Class ElevatorDoor opens in response to elevatorArrived messages and opens and closes the Floor Doors. Lines // close ElevatorDoor and Corresponding Floor Door 23 public synchronized void closeDoor( Location location ) 24 { 25 location.getDoor().closeDoor( location ); super.closeDoor( location ); } // end method closeDoor // invoked when Elevator has departed 32 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {} // invoked when Elevator has arrived 35 public void elevatorArrived( ElevatorMoveEvent moveEvent ) 36 { 37 openDoor( moveEvent.getLocation() ); 38 } 39 } Override method closeDoor

 2003 Prentice Hall, Inc. All rights reserved. E.5Class Button Button –Signals Elevator to move between Floor s

 2003 Prentice Hall, Inc. All rights reserved. Outline Button.java Class Button, which represents a Button in the model, informs listeners when a Button has been pressed or reset. Line 8 Lines 11 and Lines // Button.java 2 // Sends ButtonEvents to ButtonListeners when accessed 3 package com.deitel.jhtp5.elevator.model; 4 5 // Deitel packages 6 import com.deitel.jhtp5.elevator.event.*; 7 8 public class Button implements ElevatorMoveListener { 9 10 // ButtonListener 11 private ButtonListener buttonListener = null; // represent whether Button is pressed 14 private boolean pressed = false; // set listener 17 public void setButtonListener( ButtonListener listener ) 18 { 19 buttonListener = listener; 20 } // press Button and send ButtonEvent 23 public void pressButton( Location location ) 24 { 25 pressed = true; buttonListener.buttonPressed( 28 new ButtonEvent( this, location ) ); 29 } Button listens for ElevatorMoveEvent s; when Elevator arrives, Button resets Allow ButtonListener to listen for ButtonEvent s Press Button and notify ButtonListener that Button has been pressed

 2003 Prentice Hall, Inc. All rights reserved. Outline Button.java Class Button, which represents a Button in the model, informs listeners when a Button has been pressed or reset. Lines Lines // reset Button and send ButtonEvent 32 public void resetButton( Location location ) 33 { 34 pressed = false; buttonListener.buttonReset( 37 new ButtonEvent( this, location ) ); 38 } // return whether button is pressed 41 public boolean isButtonPressed() 42 { 43 return pressed; 44 } // invoked when Elevator has departed 47 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {} // invoked when Elevator has arrived 50 public void elevatorArrived( ElevatorMoveEvent moveEvent ) 51 { 52 resetButton( moveEvent.getLocation() ); 53 } 54 } Reset Button and notify ButtonListener that Button has been reset When Elevator arrives, reset Button

 2003 Prentice Hall, Inc. All rights reserved. E.6Class ElevatorShaft ElevatorShaft –Represents elevator shaft in which Elevator travels –Receives events from Elevator –“Bubbles up” events to ElevatorModel

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines // ElevatorShaft.java 2 // Represents elevator shaft, which contains elevator 3 package com.deitel.jhtp5.elevator.model; 4 5 // Java core packages 6 import java.util.*; 7 8 // Deitel packages 9 import com.deitel.jhtp5.elevator.event.*; public class ElevatorShaft implements ElevatorMoveListener, 12 LightListener, BellListener { // Elevator 15 private Elevator elevator; // Buttons on Floors 18 private Button firstFloorButton; 19 private Button secondFloorButton; // Doors on Floors 22 private Door firstFloorDoor; 23 private Door secondFloorDoor; // Lights on Floors 26 private Light firstFloorLight; 27 private Light secondFloorLight; 28 ElevatorShaft listens for ElevatorMoveEvent s LightEvent s and BellEvent s Use class diagram to determine associations of ElevatorShaft

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines Lines // listeners 30 private DoorListener doorListener; 31 private ButtonListener buttonListener; 32 private LightListener lightListener; 33 private BellListener bellListener; 34 private Set elevatorMoveListeners; // constructor initializes aggregated components 37 public ElevatorShaft( Floor firstFloor, Floor secondFloor ) 38 { 39 // instantiate Set for ElevatorMoveListeners 40 elevatorMoveListeners = new HashSet( 1 ); // anonymous inner class listens for ButtonEvents 43 ButtonListener floorButtonListener = 44 new ButtonListener() { // called when Floor Button has been pressed 47 public void buttonPressed( ButtonEvent buttonEvent ) 48 { 49 // request elevator move to location 50 Location location = buttonEvent.getLocation(); 51 buttonListener.buttonPressed( buttonEvent ); 52 elevator.requestElevator( location ); 53 } 54 Instantiate anonymous inner class to listen for ButtonEvent s from Button s on floors When Button on floor is pressed, request Elevator to move to Floor of request Declare listeners that receive events from model (and will send these events to ElevatorModel )

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines Lines // called when Floor Button has been reset 56 public void buttonReset( ButtonEvent buttonEvent ) 57 { 58 buttonListener.buttonReset( buttonEvent ); 59 } 60 }; // end anonymous inner class // instantiate Floor Buttons 63 firstFloorButton = new Button(); 64 secondFloorButton = new Button(); // register anonymous ButtonListener with Floor Buttons 67 firstFloorButton.setButtonListener( 68 floorButtonListener ); 69 secondFloorButton.setButtonListener( 70 floorButtonListener ); // Floor Buttons listen for ElevatorMoveEvents 73 addElevatorMoveListener( firstFloorButton ); 74 addElevatorMoveListener( secondFloorButton ); // anonymous inner class listens for DoorEvents 77 DoorListener floorDoorListener = new DoorListener() { // called when Floor Door has opened 80 public void doorOpened( DoorEvent doorEvent ) 81 { 82 // forward event to doorListener 83 doorListener.doorOpened( doorEvent ); 84 } Instantiate anonymous inner class to listen for DoorEvent s from Door s on floors When Door on floor is opened or closed, forward DoorEvent to DoorListener When Button on floor is reset, reset Button

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines // called when Floor Door has closed 87 public void doorClosed( DoorEvent doorEvent ) 88 { 89 // forward event to doorListener 90 doorListener.doorClosed( doorEvent ); 91 } 92 }; // end anonymous inner class // instantiate Floor Doors 95 firstFloorDoor = new Door(); 96 secondFloorDoor = new Door(); // register anonymous DoorListener with Floor Doors 99 firstFloorDoor.addDoorListener( floorDoorListener ); 100 secondFloorDoor.addDoorListener( floorDoorListener ); // instantiate Lights, then listen for LightEvents 103 firstFloorLight = new Light(); 104 addElevatorMoveListener( firstFloorLight ); 105 firstFloorLight.setLightListener( this ); secondFloorLight = new Light(); 108 addElevatorMoveListener( secondFloorLight ); 109 secondFloorLight.setLightListener( this ); // instantiate Elevator object 112 elevator = new Elevator( firstFloor, secondFloor ); 113 Instantiate Light s and listen for LightEvent s Instantiate Elevator and listen for ElevatorMoveEvent s

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Line 118 Lines Lines // register for ElevatorMoveEvents from elevator 115 elevator.addElevatorMoveListener( this ); // listen for BellEvents from elevator 118 elevator.setBellListener( this ); // anonymous inner class listens for ButtonEvents from 121 // elevator 122 elevator.setButtonListener( 123 new ButtonListener() { // invoked when button has been pressed 126 public void buttonPressed( ButtonEvent buttonEvent ) 127 { 128 // send event to listener 129 buttonListener.buttonPressed( buttonEvent ); 130 } // invoked when button has been reset 133 public void buttonReset( ButtonEvent buttonEvent ) 134 { 135 // send event to listener 136 buttonListener.buttonReset( 137 new ButtonEvent( this, elevator ) ); 138 } 139 } // end anonymous inner class 140 ); 141 Instantiate anonymous inner class to listen for ButtonEvent s from Elevator ’s Button When Elevator ’s Button is pressed or reset, forward ButtonEvent to ButtonListener Listen for BellEvent s from elevator

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines Line 164 Lines // anonymous inner class listens for DoorEvents from 143 // elevator 144 elevator.setDoorListener( 145 new DoorListener() { // invoked when door has opened 148 public void doorOpened( DoorEvent doorEvent ) 149 { 150 // send event to listener 151 doorListener.doorOpened( doorEvent ); 152 } // invoked when door has closed 155 public void doorClosed( DoorEvent doorEvent ) 156 { 157 // send event to listener 158 doorListener.doorClosed( doorEvent ); 159 } 160 } // end anonymous inner class 161 ); // start Elevator Thread 164 elevator.start(); } // end ElevatorShaft constructor // get Elevator 169 public Elevator getElevator() 170 { 171 return elevator; 172 } Instantiate anonymous inner class to listen for DoorEvent s from Elevator ’s Door When Elevator ’s Door is pressed or reset, forward DoorEvent to DoorListener Start Elevator ’s Thread Get method for Elevator

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines // get Door on first Floor 175 public Door getFirstFloorDoor() 176 { 177 return firstFloorDoor; 178 } // get Door on second Floor 181 public Door getSecondFloorDoor() 182 { 183 return secondFloorDoor; 184 } // get Button on first Floor 187 public Button getFirstFloorButton() 188 { 189 return firstFloorButton; 190 } // get Button on second Floor 193 public Button getSecondFloorButton() 194 { 195 return secondFloorButton; 196 } // get Light on first Floor 199 public Light getFirstFloorLight() 200 { 201 return firstFloorLight; 202 } Get methods for Door s, Button s and Light s

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines Lines // get Light on second Floor 205 public Light getSecondFloorLight() 206 { 207 return secondFloorLight; 208 } // invoked when Bell rings 211 public void bellRang( BellEvent bellEvent ) 212 { 213 bellListener.bellRang( bellEvent ); 214 } // invoked when Light turns on 217 public void lightTurnedOn( LightEvent lightEvent ) 218 { 219 lightListener.lightTurnedOn( lightEvent ); 220 } // invoked when Light turns off 223 public void lightTurnedOff( LightEvent lightEvent ) 224 { 225 lightListener.lightTurnedOff( lightEvent ); 226 } // invoked when Elevator departs 229 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) 230 { 231 Iterator iterator = elevatorMoveListeners.iterator(); 232 When Bell has rung, forward BellEvent to BellListener When Light s turn on or off, forward LightEvent to LightListener When Elevator has departed, notify ElevatorMoveListener s

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines // iterate Set of ElevatorMoveEvent listeners 234 while ( iterator.hasNext() ) { // get respective ElevatorMoveListener from Set 237 ElevatorMoveListener listener = 238 ( ElevatorMoveListener ) iterator.next(); // send ElevatorMoveEvent to this listener 241 listener.elevatorDeparted( moveEvent ); 242 } 243 } // end method elevatorDeparted // invoked when Elevator arrives 246 public void elevatorArrived( ElevatorMoveEvent moveEvent ) 247 { 248 // obtain iterator from Set 249 Iterator iterator = elevatorMoveListeners.iterator(); // get next DoorListener 252 while ( iterator.hasNext() ) { // get next ElevatorMoveListener from Set 255 ElevatorMoveListener listener = 256 ( ElevatorMoveListener ) iterator.next(); // send ElevatorMoveEvent to this listener 259 listener.elevatorArrived( moveEvent ); } // end while loop 262 } // end method elevatorArrived When Elevator has arrived, notify ElevatorMoveListener s

 2003 Prentice Hall, Inc. All rights reserved. Outline ElevatorShaft.j ava Class ElevatorShaft, which represents the ElevatorShaft, which sends events from the Elevator to the ElevatorSimulat ion. Lines Lines Lines Lines Lines // set listener to DoorEvents 265 public void setDoorListener( DoorListener listener ) 266 { 267 doorListener = listener; 268 } // set listener to ButtonEvents 271 public void setButtonListener( ButtonListener listener ) 272 { 273 buttonListener = listener; 274 } // add listener to ElevatorMoveEvents 277 public void addElevatorMoveListener( 278 ElevatorMoveListener listener ) 279 { 280 elevatorMoveListeners.add( listener ); 281 } // set listener to LightEvents 284 public void setLightListener( LightListener listener ) 285 { 286 lightListener = listener; 287 } // set listener to BellEvents 290 public void setBellListener( BellListener listener ) 291 { 292 bellListener = listener; 293 } 294 } Enable ElevatorMoveListener s to receive ElevatorMoveEvent s from ElevatorShaft Enable LightListener to receive LightEvent s from ElevatorShaft Enable BellListener to receive BellEvent s from ElevatorShaft Enable DoorListener to receive DoorEvent s from ElevatorShaft Enable ButtonListener to receive ButtonEvent s from ElevatorShaft

 2003 Prentice Hall, Inc. All rights reserved. E.7Classes Light and Bell Light and Bell –Help decorate the view Send events to ElevatorView via ElevatorShaft and ElevatorSimulation

 2003 Prentice Hall, Inc. All rights reserved. Outline Light.java Class Light represents a Light on the Floor in the model. Line 14 Lines // Light.java 2 // Light turns a light on or off 3 package com.deitel.jhtp5.elevator.model; 4 5 // Deitel packages 6 import com.deitel.jhtp5.elevator.event.*; 7 8 public class Light implements ElevatorMoveListener { 9 10 // Light state (on/off) 11 private boolean lightOn; // time before Light turns off automatically (3 seconds) 14 public static final int AUTOMATIC_TURNOFF_DELAY = 3000; // LightListener listens for when Light should turn on/off 17 private LightListener lightListener; // location where Light turned on or off 20 private Location lightLocation; // set LightListener 23 public void setLightListener( LightListener listener ) 24 { 25 lightListener = listener; 26 } Light turns itself off automatically after three seconds Enable LightListener to receive LightEvent s

 2003 Prentice Hall, Inc. All rights reserved. Outline Light.java Class Light represents a Light on the Floor in the model. Lines Lines Lines // turn on Light 29 public void turnOnLight( Location location ) 30 { 31 if ( !lightOn ) { lightOn = true; // send LightEvent to LightListener 36 lightListener.lightTurnedOn( 37 new LightEvent( this, location ) ); lightLocation = location; // declare Thread that ensures automatic Light turn off 42 Thread thread = new Thread( 43 new Runnable() { public void run() 46 { 47 // turn off Light if on for more than 3 seconds 48 try { 49 Thread.sleep( AUTOMATIC_TURNOFF_DELAY ); 50 turnOffLight( lightLocation ); 51 } 52 Method to turn on Light Send LightEvent to LightListener ( ElevatorShaft ) when Light turns on Instantiate and start Thread that turns Light off automatically after three seconds

 2003 Prentice Hall, Inc. All rights reserved. Outline Light.java Class Light represents a Light on the Floor in the model. Lines Lines // handle exception if interrupted 54 catch ( InterruptedException exception ) { 55 exception.printStackTrace(); 56 } 57 } 58 } // end anonymous inner class 59 ); thread.start(); 62 } 63 } // end method turnOnLight // turn off Light 66 public void turnOffLight( Location location ) 67 { 68 if ( lightOn ) { lightOn = false; // send LightEvent to LightListener 73 lightListener.lightTurnedOff( 74 new LightEvent( this, location ) ); 75 } 76 } // end method turnOffLight Method to turn off Light Send LightEvent to LightListener ( ElevatorShaft ) when Light turns off

 2003 Prentice Hall, Inc. All rights reserved. Outline Light.java Class Light represents a Light on the Floor in the model. Lines Lines // return whether Light is on or off 79 public boolean isLightOn() 80 { 81 return lightOn; 82 } // invoked when Elevator has departed 85 public void elevatorDeparted( 86 ElevatorMoveEvent moveEvent ) 87 { 88 turnOffLight( moveEvent.getLocation() ); 89 } // invoked when Elevator has arrived 92 public void elevatorArrived( 93 ElevatorMoveEvent moveEvent ) 94 { 95 turnOnLight( moveEvent.getLocation() ); 96 } 97 } When Elevator departs from Floor, turn off Light When Elevator arrives at Floor, turn off Light

 2003 Prentice Hall, Inc. All rights reserved. Outline Bell.java Class Bell represents the Bell in the model. Lines Lines // Bell.java 2 // Represents Bell in simulation 3 package com.deitel.jhtp5.elevator.model; 4 5 // Deitel packages 6 import com.deitel.jhtp5.elevator.event.*; 7 8 public class Bell implements ElevatorMoveListener { 9 10 // BellListener listens for BellEvent object 11 private BellListener bellListener; // ring bell and send BellEvent object to listener 14 private void ringBell( Location location ) 15 { 16 if ( bellListener != null ) 17 bellListener.bellRang( 18 new BellEvent( this, location ) ); 19 } // set BellListener 22 public void setBellListener( BellListener listener ) 23 { 24 bellListener = listener; 25 } Send BellEvent to BellListener ( ElevatorShaft ) when Bell has rung Enable BellListener to receive BellEvent s

 2003 Prentice Hall, Inc. All rights reserved. Outline Bell.java Class Bell represents the Bell in the model. Lines // invoked when Elevator has departed 28 public void elevatorDeparted( ElevatorMoveEvent moveEvent ) {} // invoked when Elevator has arrived 31 public void elevatorArrived( ElevatorMoveEvent moveEvent ) 32 { 33 ringBell( moveEvent.getLocation() ); 34 } 35 } When Elevator arrives at Floor, ring Bell

 2003 Prentice Hall, Inc. All rights reserved. E.8Class Elevator Elevator –Travels in ElevatorShaft between Floor s –Carries Person –“Is a” Location

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Line 12 Lines // Elevator.java 2 // Travels between Floors in the ElevatorShaft 3 package com.deitel.jhtp5.elevator.model; 4 5 // Java core packages 6 import java.util.*; 7 8 // Deitel packages 9 import com.deitel.jhtp5.elevator.event.*; 10 import com.deitel.jhtp5.elevator.ElevatorConstants; public class Elevator extends Location implements Runnable, 13 BellListener, ElevatorConstants { // manages Elevator thread 16 private boolean elevatorRunning = false; // describes Elevator state (idle or moving) 19 private boolean moving = false; // current Floor 22 private Location currentFloorLocation; // destination Floor 25 private Location destinationFloorLocation; // Elevator needs to service other Floor 28 private boolean summoned; 29 Use class diagram to determine associations and attributes of Elevator Class Elevator “is a” Location

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines 37-39, 44 Lines // listener objects 31 private Set elevatorMoveListeners; 32 private BellListener bellListener; 33 private ButtonListener elevatorButtonListener; 34 private DoorListener elevatorDoorListener; // ElevatorDoor, Button and Bell on Elevator 37 private ElevatorDoor elevatorDoor; 38 private Button elevatorButton; 39 private Bell bell; public static final int ONE_SECOND = 1000; // time needed to travel between Floors (5 seconds) 44 private static final int TRAVEL_TIME = 5 * ONE_SECOND; // Elevator's thread to handle asynchronous movement 47 private Thread thread; // constructor creates variables; registers for ButtonEvents 50 public Elevator( Floor firstFloor, Floor secondFloor ) 51 { 52 setLocationName( ELEVATOR_NAME ); // instantiate Elevator's Door, Button and Bell 55 elevatorDoor = new ElevatorDoor(); 56 elevatorButton = new Button(); 57 bell = new Bell(); Use class diagram to determine associations and attributes of Elevator Instantiate ElevatorDoor, Button and Bell inside Elevator Declare listeners that receive events from model (and will send these events to ElevatorShaft )

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Line 60 Lines // register Elevator for BellEvents 60 bell.setBellListener( this ); // instantiate listener Set 63 elevatorMoveListeners = new HashSet( 1 ); // start Elevator on first Floor 66 currentFloorLocation = firstFloor; 67 destinationFloorLocation = secondFloor; // register elevatorButton for ElevatorMoveEvents 70 addElevatorMoveListener( elevatorButton ); // register elevatorDoor for ElevatorMoveEvents 73 addElevatorMoveListener( elevatorDoor ); // register bell for ElevatorMoveEvents 76 addElevatorMoveListener( bell ); // anonymous inner class listens for ButtonEvents from 79 // elevatorButton 80 elevatorButton.setButtonListener( 81 new ButtonListener() { 82 Instantiate anonymous inner class to listen for ButtonEvent s from Elevator ’s Button Listen for BellEvent s

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines Lines // invoked when elevatorButton has been pressed 84 public void buttonPressed( ButtonEvent buttonEvent ) 85 { 86 // send ButtonEvent to listener 87 elevatorButtonListener.buttonPressed( 88 buttonEvent ); // start moving Elevator to destination Floor 91 setMoving( true ); 92 } // invoked when elevatorButton has been reset 95 public void buttonReset( ButtonEvent buttonEvent ) 96 { 97 // send ButtonEvent to listener 98 elevatorButtonListener.buttonReset( 99 buttonEvent ); 100 } 101 } // end anonymous inner class 102 ); // anonymous inner class listens for DoorEvents from 105 // elevatorDoor 106 elevatorDoor.addDoorListener( 107 new DoorListener() { 108 When Elevator ’s Button is pressed, forward ButtonEvent to ButtonListener and signal Elevator to move to other Floor When Elevator ’s Button is reset, forward ButtonEvent to ButtonListener Instantiate anonymous inner class to listen for DoorEvent s from Elevator ’s Door

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines // invoked when elevatorDoor has opened 110 public void doorOpened( DoorEvent doorEvent ) 111 { 112 // send DoorEvent to listener 113 elevatorDoorListener.doorOpened( new DoorEvent( 114 doorEvent.getSource(), Elevator.this )); 115 } // invoked when elevatorDoor has closed 118 public void doorClosed( DoorEvent doorEvent ) 119 { 120 // send DoorEvent to listener 121 elevatorDoorListener.doorClosed( new DoorEvent( 122 doorEvent.getSource(), Elevator.this )); 123 } 124 } // end anonymous inner class 125 ); 126 } // end Elevator constructor // swaps current Floor Location with opposite Floor Location 129 private void changeFloors() 130 { 131 Location location = currentFloorLocation; 132 currentFloorLocation = destinationFloorLocation; 133 destinationFloorLocation = location;‘ 134 } When Elevator ’s Door is opened or closed, open or close Door on Floor ( Location ), and send DoorEvent to DoorListener private method for changing destination Location

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines Lines Lines // start Elevator thread 137 public void start() 138 { 139 if ( thread == null ) 140 thread = new Thread( this ); elevatorRunning = true; 143 thread.start(); 144 } // stop Elevator thread; method run terminates 147 public void stopElevator() 148 { 149 elevatorRunning = false; 150 } // Elevator thread's run method 153 public void run() 154 { 155 while ( isElevatorRunning() ) { // remain idle until awoken 158 while ( !isMoving() ) 159 pauseThread( 10 ); // pause while passenger exits (if one exists) 162 pauseThread( ONE_SECOND ); Start Elevator ’s Thread Stop Elevator ’s Thread Invoked after ElevatorShaft invokes Elevator ’s Start method When Elevator maintains “waiting state,” the Elevator should not move

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Line 165 Line 171 Lines Line 180 Line // close elevatorDoor 165 getDoor().closeDoor( currentFloorLocation ); // closing Door takes one second 168 pauseThread( ONE_SECOND ); // issue elevatorDeparted Event 171 sendDepartureEvent( currentFloorLocation ); // Elevator needs 5 seconds to travel 174 pauseThread( TRAVEL_TIME ); // stop Elevator 177 setMoving( false ); // swap Floor Locations 180 changeFloors(); // issue elevatorArrived Event 183 sendArrivalEvent( currentFloorLocation ); } // end while loop } // end method run 188 When Elevator maintains “moving state,” close Elevator Door Notify listeners that Elevator has departed Travel five seconds, then stop Change destination Floor Notify listeners that Elevator has arrived

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines Lines // pause concurrent thread for number of milliseconds 190 private void pauseThread( int milliseconds ) 191 { 192 try { 193 Thread.sleep( milliseconds ); 194 } // handle if interrupted while sleeping 197 catch ( InterruptedException exception ) { 198 exception.printStackTrace(); 199 } 200 } // end method pauseThread // return Button on Elevator 203 public Button getButton() 204 { 205 return elevatorButton; 206 } // return Door on Elevator 209 public Door getDoor() 210 { 211 return elevatorDoor; 212 } // set if Elevator should move 215 private void setMoving( boolean elevatorMoving ) 216 { 217 moving = elevatorMoving; 218 } Private method for putting Elevator ’s Thread to sleep Implement Location method getButton to return Elevator ’s Button Implement Location method getDoor to return Elevator ’s Door

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines // is Elevator moving? 221 public boolean isMoving() 222 { 223 return moving; 224 } // is Elevator thread running? 227 private boolean isElevatorRunning() 228 { 229 return elevatorRunning; 230 } // register ElevatorMoveListener for ElevatorMoveEvents 233 public void addElevatorMoveListener( 234 ElevatorMoveListener listener ) 235 { 236 elevatorMoveListeners.add( listener ); 237 } // register BellListener fpr BellEvents 240 public void setBellListener( BellListener listener ) 241 { 242 bellListener = listener; 243 } 244 Enable ElevatorMoveListener s to receive ElevatorMoveEvent s Enable BellListener to receive BellEvent s

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines Lines // register ButtonListener for ButtonEvents 246 public void setButtonListener( ButtonListener listener ) 247 { 248 elevatorButtonListener = listener; 249 } // register DoorListener for DoorEvents 252 public void setDoorListener( DoorListener listener ) 253 { 254 elevatorDoorListener = listener; 255 } // notify all ElevatorMoveListeners of arrival 258 private void sendArrivalEvent( Location location ) 259 { 260 // obtain iterator from Set 261 Iterator iterator = elevatorMoveListeners.iterator(); // get next DoorListener 264 while ( iterator.hasNext() ) { // get next ElevatorMoveListener from Set 267 ElevatorMoveListener listener = 268 ( ElevatorMoveListener ) iterator.next(); // send event to listener 271 listener.elevatorArrived( new 272 ElevatorMoveEvent( this, location ) ); } // end while loop Enable DoorListener to receive DoorEvent s Private method for notifying all ElevatorMoveListener s that Elevator has arrived Enable ButtonListener to receive ButtonEvent s

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Lines // service queued request, if one exists 277 if ( summoned ) { 278 setMoving( true ); // start moving Elevator 279 } summoned = false; // request has been serviced } // end method sendArrivalEvent // notify all ElevatorMoveListeners of departure 286 private void sendDepartureEvent( Location location ) 287 { 288 // obtain iterator from Set 289 Iterator iterator = elevatorMoveListeners.iterator(); // get next DoorListener 292 while ( iterator.hasNext() ) { // get next ElevatorMoveListener from Set 295 ElevatorMoveListener listener = 296 ( ElevatorMoveListener ) iterator.next(); // send ElevatorMoveEvent to this listener 299 listener.elevatorDeparted( new ElevatorMoveEvent( 300 this, currentFloorLocation ) ); } // end while loop 303 } // end method sendDepartureEvent If a queued request exists, service that request (i.e., move to other Floor ) Private method for notifying all ElevatorMoveListener s that Elevator has departed

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines Line 315 Line 319 Line // request Elevator 306 public void requestElevator( Location location ) 307 { 308 // if Elevator is idle 309 if ( !isMoving() ) { // if Elevator is on same Floor of request 312 if ( location == currentFloorLocation ) // Elevator has already arrived; send arrival event 315 sendArrivalEvent( currentFloorLocation ); // if Elevator is on opposite Floor of request 318 else { 319 setMoving( true ); // move to other Floor 320 } 321 } 322 else // if Elevator is moving // if Elevator departed from same Floor as request 325 if ( location == currentFloorLocation ) 326 summoned = true; // if Elevator is traveling to Floor of request, 329 // simply continue traveling } // end method requestElevator If Person requests Elevator just after Elevator has left the Floor on which the Person is waiting, Elevator must “remember” to return to that Floor If Elevator is idle and servicing the Floor of the request, send elevatorArrived event If Elevator is idle and servicing opposite Floor of the request, move to other Floor Public service to request Elevator (used by Button s)

 2003 Prentice Hall, Inc. All rights reserved. Outline Elevator.java Class Elevator represents the Elevator traveling between two Floor s, operating asynchronously with other objects. Lines // invoked when bell has rung 334 public void bellRang( BellEvent bellEvent ) 335 { 336 // send event to bellLirdstener 337 if ( bellListener != null ) 338 bellListener.bellRang( bellEvent ); 339 } // get the currentFloorLocation of the Elevator 342 public Location getCurrentFloor() 343 { 344 return currentFloorLocation; 345 } 346 } Notify BellListener that Bell has rung

 2003 Prentice Hall, Inc. All rights reserved. E.9Class Person Person –Walks across Floor to Elevator –Rides Elevator –“Has a” Location –Operates asynchronously with other objects Extends class Thread

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Lines Line 23 Lines // Person.java 2 // Person riding the elevator 3 package com.deitel.jhtp5.elevator.model; 4 5 // Java core packages 6 import java.util.*; 7 8 // Deitel packages 9 import com.deitel.jhtp5.elevator.event.*; public class Person extends Thread { // identification number 14 private int ID = -1; // represents whether Person is moving or waiting 17 private boolean moving; // reference to Location (either on Floor or in Elevator) 20 private Location location; // listener object for PersonMoveEvents 23 private PersonMoveListener personMoveListener; // time in milliseconds to walk to Button on Floor 26 private static final int TIME_TO_WALK = 3000; 27 Declare listener that receives PersonMoveEvent Use class diagram to determine associations and attributes of Person Define constants that indicate each type of event that a Person may send

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Lines Lines Lines // types of messages Person may send 29 public static final int PERSON_CREATED = 1; 30 public static final int PERSON_ARRIVED = 2; 31 public static final int PERSON_ENTERING_ELEVATOR = 3; 32 public static final int PERSON_PRESSING_BUTTON = 4; 33 public static final int PERSON_EXITING_ELEVATOR = 5; 34 public static final int PERSON_EXITED = 6; // Person constructor set initial location 37 public Person( int identifier, Location initialLocation ) 38 { 39 super(); ID = identifier; // assign unique identifier 42 location = initialLocation; // set Floor Location 43 moving = true; // start moving toward Button on Floor 44 } // set listener for PersonMoveEvents 47 public void setPersonMoveListener( 48 PersonMoveListener listener ) 49 { 50 personMoveListener = listener; 51 } // set Person Location 54 private void setLocation( Location newLocation ) 55 { 56 location = newLocation; 57 } Person constructor assigns unique identifier and sets initial Floor on which Person is located Enable PersonMoveListener to receive PersonMoveEvent s When Door opens, set Person ’s Location to that of where the Door opened

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Lines Line // get current Location 60 private Location getLocation() 61 { 62 return location; 63 } // get identifier 66 public int getID() 67 { 68 return ID; 69 } // set if Person should move 72 public void setMoving( boolean personMoving ) 73 { 74 moving = personMoving; 75 } // get if Person should move 78 public boolean isMoving() 79 { 80 return moving; 81 } // Person either rides or waits for Elevator 84 public void run() 85 { 86 // indicate that Person thread was created 87 sendPersonMoveEvent( PERSON_CREATED ); Invoked when Person ’s Thread is started Notify listeners when Person is created

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Line 90 Line 96 Lines // walk to Elevator 90 pauseThread( TIME_TO_WALK ); // stop walking at Elevator 93 setMoving( false ); // Person arrived at Elevator 96 sendPersonMoveEvent( PERSON_ARRIVED ); // get Door on current Floor 99 Door currentFloorDoor = location.getDoor(); // get Elevator 102 Elevator elevator = 103 ( (Floor) getLocation() ).getElevatorShaft().getElevator(); // begin exclusive access to currentFloorDoor 106 synchronized ( currentFloorDoor ) { // check whether Floor Door is open 109 if ( !currentFloorDoor.isDoorOpen() ) { sendPersonMoveEvent( PERSON_PRESSING_BUTTON ); 112 pauseThread( 1000 ); // press Floor's Button to request Elevator 115 Button floorButton = getLocation().getButton(); 116 floorButton.pressButton( getLocation() ); 117 } Put Person Thread to sleep for three seconds, simulating a three second walk to the Elevator Notify listeners when Person arrived at Elevator If Door is closed, press Button on Floor and wait for Elevator to arrive

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Lines Lines Line 138 Line // wait for Floor door to open 120 try { while ( !currentFloorDoor.isDoorOpen() ) 123 currentFloorDoor.wait(); 124 } // handle exception waiting for Floor door to open 127 catch ( InterruptedException interruptedException ) { 128 interruptedException.printStackTrace(); 129 } // Floor Door takes one second to open 132 pauseThread( 1000 ); // implicitly wait for exclusive access to elevator 135 synchronized ( elevator ) { // Person enters Elevator 138 sendPersonMoveEvent( PERSON_ENTERING_ELEVATOR ); // set Person Location to Elevator 141 setLocation( elevator ); // Person takes one second to enter Elevator 144 pauseThread( 1000 ); Wait for the currentFloorDoor to open Notify listeners when Person entered Elevator Only one Person is allowed to occupy the Elevator at one time Set the Person ’s location to the Elevator

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Line 147 Line 154 Lines // pressing Elevator Button takes one second 147 sendPersonMoveEvent( PERSON_PRESSING_BUTTON ); 148 pauseThread( 1000 ); // get Elevator's Button 151 Button elevatorButton = getLocation().getButton(); // press Elevator's Button 154 elevatorButton.pressButton( location ); // Door closing takes one second 157 pauseThread( 1000 ); 158 } } // give up exclusive access to Floor door // get exclusive access to Elevator 163 synchronized( elevator ) { // get Elevator door 166 Door elevatorDoor = getLocation().getDoor(); // wait for Elevator door to open 169 synchronized( elevatorDoor ) { try { while ( !elevatorDoor.isDoorOpen() ) 174 elevatorDoor.wait(); 175 } Invoke method wait on the elevatorDoor Press the Elevator ’s button to instruct the Elevator to begin traveling Notify listeners when Person pressed Button

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Line 186 Line 189 Line // handle exception waiting for Elevator door to open 178 catch ( InterruptedException interruptedException ) { 179 interruptedException.printStackTrace(); 180 } // waiting for Elevator's Door to open takes a second 183 pauseThread( 1000 ); // move Person onto Floor 186 setLocation( elevator.getCurrentFloor() ); // walk away from Elevator 189 setMoving( true ); // Person exiting Elevator 192 sendPersonMoveEvent( PERSON_EXITING_ELEVATOR ); } // release elevatorDoor lock, allowing door to close } // release elevator lock, allowing waiting Person to enter // walking from elevator takes five seconds 199 pauseThread( 2 * TIME_TO_WALK ); // Person exits simulation 202 sendPersonMoveEvent( PERSON_EXITED ); } // end method run Set Person ’s new location to FloorPerson walks away from the Elevator Person left the simulation

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects. Lines Lines // pause thread for desired number of milliseconds 207 private void pauseThread( int milliseconds ) 208 { 209 try { 210 sleep( milliseconds ); 211 } // handle exception if interrupted when paused 214 catch ( InterruptedException interruptedException ) { 215 interruptedException.printStackTrace(); 216 } 217 } // end method pauseThread // send PersonMoveEvent to listener, depending on event type 220 private void sendPersonMoveEvent( int eventType ) 221 { 222 // create new event 223 PersonMoveEvent event = 224 new PersonMoveEvent( this, getLocation(), getID() ); // send Event to this listener, depending on eventType 227 switch ( eventType ) { // Person has been created 230 case PERSON_CREATED: 231 personMoveListener.personCreated( event ); 232 break; Utility method for putting Person ’s Thread to sleep Utility method for determining which PersonMoveEvent to send to listener, then sending that event

 2003 Prentice Hall, Inc. All rights reserved. Outline Person.java Class Person represents the Person that rides the Elevator. The Person operates asynchronously with other objects // Person arrived at Elevator 235 case PERSON_ARRIVED: 236 personMoveListener.personArrived( event ); 237 break; // Person entered Elevator 240 case PERSON_ENTERING_ELEVATOR: 241 personMoveListener.personEntered( event ); 242 break; // Person pressed Button object 245 case PERSON_PRESSING_BUTTON: 246 personMoveListener.personPressedButton( event ); 247 break; // Person exited Elevator 250 case PERSON_EXITING_ELEVATOR: 251 personMoveListener.personDeparted( event ); 252 break; // Person exited simulation 255 case PERSON_EXITED: 256 personMoveListener.personExited( event ); 257 break; default: 260 break; 261 } 262 } // end method sendPersonMoveEvent 263 }

 2003 Prentice Hall, Inc. All rights reserved. E.10Artifacts Revisited Artifacts for package model –Each class in model imports package model –Each component in package model maps to distinct file Therefore, each component maps to distinct class –Package model aggregates package event

 2003 Prentice Hall, Inc. All rights reserved. Fig. E.15 Artifacts for package model. Bell.java > model Button.java > Door.java > Elevator.java > ElevatorSimulation.java > ElevatorShaft.java > Floor.java > Light.java > Location.java > Person.java > event > ElevatorDoor.java > Bell.java

 2003 Prentice Hall, Inc. All rights reserved. E.11 Conclusion Object-oriented fundamentals and Java-specific topics –Event handling –Multithreading Appendix F –Implements the ElevatorView