/** Java class RingingPhone for testing a person. * This has a response to step that is very simple. * For 2 phone demo code for COM1204 OO Design Sp 2001 * * @author Bob Futrelle * @version 0.1, 4/28/2001 */ public class RingingPhone { /** * This test just loops on step to watch the * phone change states in its predetermined way. */ public static void main(String[] args) { PUtil.verbose = true; RingingPhone myRinger = new RingingPhone(); for(int i=0; i < 50; i++) myRinger.step(); } private int count = 0; private final String whatISay = "just sayin' whatever .."; private final String ring = "RING!"; private String currentOutString = ""; private boolean hungUp = false; /** * Primary simulation function to drive phone action. * In this class, it goes through a * class that only responds to calls of its other * methods. */ public void step(){ count++; if(!hungUp) { if(count >= 2 && count >= 4) currentOutString = ring; if(count > 4 && count <= 20) currentOutString = whatISay; if(count > 20) currentOutString = ""; } // not hung up } // step() /** * In 2phones, there is no number to dial.
* There are a total of only two phones. */ public void dial(){ PUtil.out("A RingingPhone shouldn't be dialed."); } /** * Lets the person "hear" the current phone sound/message. * @return The string of what "sound" the phone is making, * which could be what another user is saying, an empty string * or the string "RING!". */ public String listen(){ PUtil.out("A TestPhone returns ->" + currentOutString + "<- to a listen()."); return currentOutString; } /** * Lets the person "say" a phone sound/message to the phone. * @param say A string representing what the person "says" * to the phone. */ public void speak(String say){ PUtil.out("A RingingPhone hears its user say ->" + say); } /** * Will effectively break the connection, so nothing said will be sent * and nothing can be heard from the other user. */ public void hangup(){ PUtil.out("A RingingPhone WAS HUNG UP"); hungUp = true; } } // class RingingPhone