    001: /**
    002:    Connect a phone to the mail system.
    003: */
    004: public class Connection
    005: {
    006:    /**
    007:       Construct a Connection object.
    008:       @param s a MailSystem object
    009:       @param p a Telephone object
    010:    */
    011:    public Connection(MailSystem s, Telephone p)
    012:    {
    013:       system = s;
    014:       thePhone = p;
    015:       resetConnection();
    016:    }
    017:
    018:    /**
    019:       Respond to the user's pressing a key on the phone touchpad
    020:       @param key the phone key pressed by the user
    021:    */
    022:    public void dial(String key)
    023:    {
    024:       if (state == CONNECTED)
    025:          connect(key);
    026:       else if (state == RECORDING)
    027:          login(key);
    028:       else if (state == CHANGE_PASSCODE)
    029:          changePasscode(key);
    030:       else if (state == CHANGE_GREETING)
    031:          changeGreeting(key);
    032:       else if (state == MAILBOX_MENU)
    033:          mailboxMenu(key);
    034:       else if (state == MESSAGE_MENU)
    035:          messageMenu(key);
    036:    }
    037:
    038:    /**
    039:       Record voice.
    040:       @param voice voice spoken by the user
    041:    */
    042:    public void record(String voice)
    043:    {
    044:       currentRecording += voice;
    045:    }
    046:
    047:    /**
    048:       The user hangs up the phone.
    049:    */
    050:    public void hangup()
    051:    {
    052:       if (state == RECORDING)
    053:          currentMailbox.addMessage(new Message(currentRecording));
    054:       resetConnection();
    055:    }
    056:
    057:    /**
    058:       Reset the connection to the initial state and prompt
    059:       for mailbox number
    060:    */
    061:    private void resetConnection()
    062:    {
    063:       currentRecording = "";
    064:       accumulatedKeys = "";
    065:       state = CONNECTED;
    066:       thePhone.speak(initialPrompt);
    067:    }
    068:
    069:    /**
    070:       Try to connect the user with the specified mail box.
    071:       @param key the phone key pressed by the user
    072:    */
    073:    private void connect(String key)
    074:    {
    075:       if (key.equals("#"))
    076:       {
    077:          currentMailbox = system.findMailbox(accumulatedKeys);
    078:          if (currentMailbox != null)
    079:          {
    080:             state = RECORDING;
    081:             thePhone.speak(currentMailbox.getGreeting());
    082:          }
    083:          else
    084:             thePhone.speak("Incorrect mailbox number. Try again!");
    085:          accumulatedKeys = "";
    086:       }
    087:       else
    088:          accumulatedKeys += key;
    089:    }
    090:
    091:    /**
    092:       Try to log in the user.
    093:       @param key the phone key pressed by the user
    094:    */
    095:    private void login(String key)
    096:    {
    097:       if (key.equals("#"))
    098:       {
    099:          if (currentMailbox.checkPasscode(accumulatedKeys))
    100:          {
    101:             state = MAILBOX_MENU;
    102:             thePhone.speak(mailboxMenu);
    103:          }
    104:          else
    105:             thePhone.speak("Incorrect passcode. Try again!");
    106:          accumulatedKeys = "";
    107:       }
    108:       else
    109:          accumulatedKeys += key;
    110:    }
    111:
    112:    /**
    113:       Change passcode.
    114:       @param key the phone key pressed by the user
    115:    */
    116:    private void changePasscode(String key)
    117:    {
    118:       if (key.equals("#"))
    119:       {
    120:          currentMailbox.setPasscode(accumulatedKeys);
    121:          state = MAILBOX_MENU;
    122:          thePhone.speak(mailboxMenu);
    123:          accumulatedKeys = "";
    124:       }
    125:       else
    126:          accumulatedKeys += key;
    127:    }
    128:
    129:    /**
    130:       Change greeting.
    131:       @param key the phone key pressed by the user
    132:    */
    133:    private void changeGreeting(String key)
    134:    {
    135:       if (key.equals("#"))
    136:       {
    137:          currentMailbox.setGreeting(currentRecording);
    138:          currentRecording = "";
    139:          state = MAILBOX_MENU;
    140:          thePhone.speak(mailboxMenu);
    141:       }
    142:    }
    143:
    144:    /**
    145:       Respond to the user's selection from mailbox menu.
    146:       @param key the phone key pressed by the user
    147:    */
    148:    private void mailboxMenu(String key)
    149:    {
    150:       if (key.equals("1"))
    151:       {
    152:          state = MESSAGE_MENU;
    153:          thePhone.speak(messageMenu);
    154:       }
    155:       else if (key.equals("2"))
    156:       {
    157:          state = CHANGE_PASSCODE;
    158:          thePhone.speak("Enter new passcode followed by the # key");
    159:       }
    160:       else if (key.equals("3"))
    161:       {
    162:          state = CHANGE_GREETING;
    163:          thePhone.speak("Record your greeting, then press the # key");
    164:       }
    165:    }
    166:
    167:    /**
    168:       Respond to the user's selection from message menu.
    169:       @param key the phone key pressed by the user
    170:    */
    171:    private void messageMenu(String key)
    172:    {
    173:       if (key.equals("1"))
    174:       {
    175:          String output = "";
    176:          Message m = currentMailbox.getCurrentMessage();
    177:          if (m == null) output += "No messages." + "\n";
    178:          else output += m.getText() + "\n";
    179:          output += messageMenu;
    180:          thePhone.speak(output);
    181:       }
    182:       else if (key.equals("2"))
    183:       {
    184:          currentMailbox.saveCurrentMessage();
    185:          thePhone.speak(messageMenu);
    186:       }
    187:       else if (key.equals("3"))
    188:       {
    189:          currentMailbox.removeCurrentMessage();
    190:          thePhone.speak(messageMenu);
    191:       }
    192:       else if (key.equals("4"))
    193:       {
    194:          state = MAILBOX_MENU;
    195:          thePhone.speak(mailboxMenu);
    196:       }
    197:    }
    198:
    199:    private MailSystem system;
    200:    private Mailbox currentMailbox;
    201:    private String currentRecording;
    202:    private String accumulatedKeys;
    203:    private Telephone thePhone;
    204:    private int state;
    205:
    206:    private static final int DISCONNECTED = 0;
    207:    private static final int CONNECTED = 1;
    208:    private static final int RECORDING = 2;
    209:    private static final int MAILBOX_MENU = 3;
    210:    private static final int MESSAGE_MENU = 4;
    211:    private static final int CHANGE_PASSCODE = 5;
    212:    private static final int CHANGE_GREETING = 6;
    213:
    214:    private static final String initialPrompt =
    215:       "Please enter mailbox number followed by #";
    216:    private static final String mailboxMenu =
    217:       "Enter 1 to listen to your messages\n"
    218:       + "Enter 2 to change your passcode\n"
    219:       + "Enter 3 to change your greeting";
    220:    private static final String messageMenu =
    221:       "Enter 1 to listen to the current message\n"
    222:       + "Enter 2 to save the current message\n"
    223:       + "Enter 3 to delete the current message\n"
    224:       + "Enter 4 to return to the main menu";
    225: }
    226:
    227:
    228:
    229:
    230:
    231:
    232:
    233:
    234:
    235:
    236: