001package horstmann.ch02_mail; 002import java.util.Scanner; 003 004/** 005 A telephone that takes simulated keystrokes and voice input 006 from the user and simulates spoken text. 007 */ 008public class Telephone 009{ 010 /** 011 Construct phone object. 012 @param aScanner that reads text from a character-input stream 013 */ 014 public Telephone(Scanner aScanner) 015 { 016 scanner = aScanner; 017 } 018 019 /** 020 Speak a message to System.out. 021 @param output the text that will be "spoken" 022 */ 023 public void speak(String output) 024 { 025 System.out.println(output); 026 } 027 028 /** 029 Loops reading user input and passes the input to the 030 Connection object's methods dial, record or hangup. 031 @param c the connection that connects this phone to the 032 voice mail system 033 */ 034 public void run(Connection c) 035 { 036 boolean more = true; 037 while (more) 038 { 039 String input = scanner.nextLine(); 040 if (input == null) return; 041 if (input.equalsIgnoreCase("H")) 042 c.hangup(); 043 else if (input.equalsIgnoreCase("Q")) 044 more = false; 045 else if (input.length() == 1 046 && "1234567890#".indexOf(input) >= 0) 047 c.dial(input); 048 else 049 c.record(input); 050 } 051 } 052 053 private Scanner scanner; 054}