01
02
03
04
05
06
07
08
09
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
package horstmann.ch02_mail;
import java.util.Scanner;

/**
   A telephone that takes simulated keystrokes and voice input
   from the user and simulates spoken text.
 */
public class Telephone
{
  /**
      Construct phone object.
      @param aScanner that reads text from a character-input stream
   */
  public Telephone(Scanner aScanner)
  {
    scanner = aScanner;
  }

  /**
      Speak a message to System.out.
      @param output the text that will be "spoken"
   */
  public void speak(String output)
  {
    System.out.println(output);
  }

  /**
      Loops reading user input and passes the input to the
      Connection object's methods dial, record or hangup.
      @param c the connection that connects this phone to the
      voice mail system
   */
  public void run(Connection c)
  {
    boolean more = true;
    while (more)
    {
      String input = scanner.nextLine();
      if (input == null) return;
      if (input.equalsIgnoreCase("H"))
        c.hangup();
      else if (input.equalsIgnoreCase("Q"))
        more = false;
      else if (input.length() == 1
          && "1234567890#".indexOf(input) >= 0)
        c.dial(input);
      else
        c.record(input);
    }
  }

  private Scanner scanner;
}