Source of Telephone.java


  1: import java.util.Scanner;

  3: /**
  4:    A telephone that takes simulated keystrokes and voice input
  5:    from the user and simulates spoken text.
  6: */
  7: public class Telephone
  8: {
  9:    /**
 10:       Construct phone object.
 11:       @param aScanner that reads text from a character-input stream
 12:    */
 13:    public Telephone(Scanner aScanner)
 14:    {
 15:       scanner = aScanner;
 16:    }

 18:    /**
 19:       Speak a message to System.out.
 20:       @param output the text that will be "spoken"
 21:    */
 22:    public void speak(String output)
 23:    {
 24:       System.out.println(output);
 25:    }

 27:    /**
 28:       Loops reading user input and passes the input to the
 29:       Connection object's methods dial, record or hangup.
 30:       @param c the connection that connects this phone to the
 31:       voice mail system
 32:    */
 33:    public void run(Connection c)
 34:    {
 35:       boolean more = true;
 36:       while (more)
 37:       {
 38:          String input = scanner.nextLine();
 39:          if (input == null) return;
 40:          if (input.equalsIgnoreCase("H"))
 41:             c.hangup();
 42:          else if (input.equalsIgnoreCase("Q"))
 43:             more = false;
 44:          else if (input.length() == 1
 45:             && "1234567890#".indexOf(input) >= 0)
 46:             c.dial(input);
 47:          else
 48:             c.record(input);
 49:       }
 50:    }

 52:    private Scanner scanner;
 53: }