/** RungPerson is for testing with a "ring only" RingingPhone. * In this way the two classes are isolated so that only * the two need testing.
* For 2 phone demo code for COM1204 OO Design Sp 2001 * * @author Bob Futrelle * @version 0.1, 4/28/2001 */ public class RungPerson { /** * This is a very simple test, since it * only needs to create a RingingPhone and * a RungPerson and step them both. */ public static void main (String[] args) { RingingPhone myPhone = new RingingPhone(); RungPerson me = new RungPerson(); me.givePhone(myPhone); PUtil.verbose = true; for(int i=0; i < 20 ; i++) { myPhone.step(); me.step(); } // step() loop } // main() private boolean free = true; private String whatIHear; private String ring = "RING!"; private int countDownToHangUp; private RingingPhone myPhone; public void givePhone(RingingPhone phone) { myPhone = phone; } /** * This listens for a ring, echoes what it hears and hangs up. * It interacts only with at RingingPhone, for testing purposes. * The design was done by workikng out the operation of * step() and then designing the methods it used. */ public void step(){ if(free) { whatIHear = myPhone.listen(); if(whatIHear == ring){ free = false; PUtil.out("RungPerson heard a RING!"); countDownToHangUp = 6; } } // if free else { countDownToHangUp-- ; if (countDownToHangUp == 0) { free = true; PUtil.out("RUNGPERSON HANGS UP"); myPhone.hangup(); } // hangup else { PUtil.out("RungPerson heard ->" + myPhone.listen()); } // or keep talking } // not free, so talking or hanging up } // step() } // class RungPerson