01: import java.io.BufferedReader;
02: import java.io.IOException;
03: 
04: /**
05:    A telephone that takes simulated keystrokes and voice input
06:    from the user and simulates spoken text.
07: */
08: public class Telephone
09: {
10:    /**
11:       Construct phone object.
12:       @param aReader that reads text from a character-input stream
13:    */
14:    public Telephone(BufferedReader aReader)
15:    {
16:       reader = aReader;
17:    }
18: 
19:    /**
20:       Speak a message to System.out.
21:       @param output the text that will be "spoken"
22:    */
23:    public void speak(String output)
24:    {
25:       System.out.println(output);
26:    }
27: 
28:    /**
29:       Loops reading user input and passes the input to the 
30:       Connection object's methods dial, record or hangup.
31:       @param c the connection that connects this phone to the 
32:       voice mail system
33:    */
34:    public void run(Connection c) throws IOException
35:    {
36:       boolean more = true;
37:       while (more)
38:       {
39:          String input = reader.readLine();
40:          if (input == null) return;
41:          if (input.equalsIgnoreCase("H"))
42:             c.hangup();
43:          else if (input.equalsIgnoreCase("Q"))
44:             more = false;
45:          else if (input.length() == 1 
46:             && "1234567890#".indexOf(input) >= 0)
47:             c.dial(input);
48:          else            
49:             c.record(input);
50:       }
51:    }
52: 
53:    private BufferedReader reader;
54: }