
JPanel keyPanel = new JPanel();
keyPanel.setLayout(new GridLayout(4, 3));
for (int i = 0; i < 12; i++)
{
JButton keyButton = new JButton(...);
keyPanel.add(keyButton);
keyButton.addActionListener(...);
}
JPanel speakerPanel = new JPanel();
speakerPanel.setLayout(new BorderLayout());
speakerPanel.add(new JLabel("Speaker:"), BorderLayout.NORTH);
speakerField = new JTextArea(10, 25);
speakerPanel.add(speakerField, BorderLayout.CENTER);
See:
Connection.java/**
Connect a phone to the mail system.
*/
public class Connection
{
/**
Construct a Connection object.
@param s a MailSystem object
@param p a Telephone object
*/
public Connection(MailSystem s, Telephone p)
{
system = s;
thePhone = p;
resetConnection();
}
/**
Respond to the user's pressing a key on the phone touchpad
@param key the phone key pressed by the user
*/
public void dial(String key)
{
if (state == CONNECTED)
connect(key);
else if (state == RECORDING)
login(key);
else if (state == CHANGE_PASSCODE)
changePasscode(key);
else if (state == CHANGE_GREETING)
changeGreeting(key);
else if (state == MAILBOX_MENU)
mailboxMenu(key);
else if (state == MESSAGE_MENU)
messageMenu(key);
}
/**
Record voice.
@param voice voice spoken by the user
*/
public void record(String voice)
{
currentRecording += voice;
}
/**
The user hangs up the phone.
*/
public void hangup()
{
if (state == RECORDING)
currentMailbox.addMessage(new Message(currentRecording));
resetConnection();
}
/**
Reset the connection to the initial state and prompt
for mailbox number
*/
private void resetConnection()
{
currentRecording = "";
accumulatedKeys = "";
state = CONNECTED;
thePhone.speak(initialPrompt);
}
/**
Try to connect the user with the specified mail box.
@param key the phone key pressed by the user
*/
private void connect(String key)
{
if (key.equals("#"))
{
currentMailbox = system.findMailbox(accumulatedKeys);
if (currentMailbox != null)
{
state = RECORDING;
thePhone.speak(currentMailbox.getGreeting());
}
else
thePhone.speak("Incorrect mailbox number. Try again!");
accumulatedKeys = "";
}
else
accumulatedKeys += key;
}
/**
Try to log in the user.
@param key the phone key pressed by the user
*/
private void login(String key)
{
if (key.equals("#"))
{
if (currentMailbox.checkPasscode(accumulatedKeys))
{
state = MAILBOX_MENU;
thePhone.speak(mailboxMenu);
}
else
thePhone.speak("Incorrect passcode. Try again!");
accumulatedKeys = "";
}
else
accumulatedKeys += key;
}
/**
Change passcode.
@param key the phone key pressed by the user
*/
private void changePasscode(String key)
{
if (key.equals("#"))
{
currentMailbox.setPasscode(accumulatedKeys);
state = MAILBOX_MENU;
thePhone.speak(mailboxMenu);
accumulatedKeys = "";
}
else
accumulatedKeys += key;
}
/**
Change greeting.
@param key the phone key pressed by the user
*/
private void changeGreeting(String key)
{
if (key.equals("#"))
{
currentMailbox.setGreeting(currentRecording);
currentRecording = "";
state = MAILBOX_MENU;
thePhone.speak(mailboxMenu);
}
}
/**
Respond to the user's selection from mailbox menu.
@param key the phone key pressed by the user
*/
private void mailboxMenu(String key)
{
if (key.equals("1"))
{
state = MESSAGE_MENU;
thePhone.speak(messageMenu);
}
else if (key.equals("2"))
{
state = CHANGE_PASSCODE;
thePhone.speak("Enter new passcode followed by the # key");
}
else if (key.equals("3"))
{
state = CHANGE_GREETING;
thePhone.speak("Record your greeting, then press the # key");
}
}
/**
Respond to the user's selection from message menu.
@param key the phone key pressed by the user
*/
private void messageMenu(String key)
{
if (key.equals("1"))
{
String output = "";
Message m = currentMailbox.getCurrentMessage();
if (m == null) output += "No messages." + "\n";
else output += m.getText() + "\n";
output += messageMenu;
thePhone.speak(output);
}
else if (key.equals("2"))
{
currentMailbox.saveCurrentMessage();
thePhone.speak(messageMenu);
}
else if (key.equals("3"))
{
currentMailbox.removeCurrentMessage();
thePhone.speak(messageMenu);
}
else if (key.equals("4"))
{
state = MAILBOX_MENU;
thePhone.speak(mailboxMenu);
}
}
private MailSystem system;
private Mailbox currentMailbox;
private String currentRecording;
private String accumulatedKeys;
private Telephone thePhone;
private int state;
private static final int DISCONNECTED = 0;
private static final int CONNECTED = 1;
private static final int RECORDING = 2;
private static final int MAILBOX_MENU = 3;
private static final int MESSAGE_MENU = 4;
private static final int CHANGE_PASSCODE = 5;
private static final int CHANGE_GREETING = 6;
private static final String initialPrompt =
"Please enter mailbox number followed by #";
private static final String mailboxMenu =
"Enter 1 to listen to your messages\n"
+ "Enter 2 to change your passcode\n"
+ "Enter 3 to change your greeting";
private static final String messageMenu =
"Enter 1 to listen to the current message\n"
+ "Enter 2 to save the current message\n"
+ "Enter 3 to delete the current message\n"
+ "Enter 4 to return to the main menu";
}
Mailbox.java/**
A mailbox contains messages that can be listed, kept or discarded.
*/
public class Mailbox
{
/**
Create Mailbox object.
@param aPasscode passcode number
@param aGreeting greeting string
*/
public Mailbox(String aPasscode, String aGreeting)
{
passcode = aPasscode;
greeting = aGreeting;
newMessages = new MessageQueue();
keptMessages = new MessageQueue();
}
/**
Check if the entered supplied is correct.
@param aPasscode a passcode to check
@return true if the supplied passcode matches the mailbox passcode
*/
public boolean checkPasscode(String aPasscode)
{
return aPasscode.equals(passcode);
}
/**
Add a message to the mailbox.
@param aMessage the message to be added
*/
public void addMessage(Message aMessage)
{
newMessages.add(aMessage);
}
/**
Get the current message.
@return the current message
*/
Message getCurrentMessage()
{
if (newMessages.getLength() > 0)
return newMessages.getHead();
else if (keptMessages.getLength() > 0)
return keptMessages.getHead();
else
return null;
}
/**
Remove the current message from the mailbox.
@return the message that has just been removed
*/
public Message removeCurrentMessage()
{
if (newMessages.getLength() > 0)
return newMessages.remove();
else if (keptMessages.getLength() > 0)
return keptMessages.remove();
else
return null;
}
/**
Save the current message
*/
public void saveCurrentMessage()
{
Message m = removeCurrentMessage();
if (m != null)
keptMessages.add(m);
}
/**
Change mailbox's greeting.
@param newGreeting the new greeting string
*/
public void setGreeting(String newGreeting)
{
greeting = newGreeting;
}
/**
Change mailbox's passcode.
@param newPasscode the new passcode
*/
public void setPasscode(String newPasscode)
{
passcode = newPasscode;
}
/**
Get the mailbox's greeting.
@return the greeting
*/
String getGreeting()
{
return greeting;
}
private MessageQueue newMessages;
private MessageQueue keptMessages;
private String greeting;
private String passcode;
}
Message.java/**
A message left by the caller.
*/
public class Message
{
/**
Construct a Message object.
@param messageText the message text
*/
public Message(String messageText)
{
text = messageText;
}
/**
Get the message text.
@return message text
*/
public String getText()
{
return text;
}
private String text;
}
Telephone.javaimport java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
Presents a phone GUI for the voice mail system.
*/
public class Telephone
{
/**
Constructs a telephone with a speaker, keypad,
and microphone.
*/
public Telephone()
{
JPanel speakerPanel = new JPanel();
speakerPanel.setLayout(new BorderLayout());
speakerPanel.add(new JLabel("Speaker:"),
BorderLayout.NORTH);
speakerField = new JTextArea(10, 25);
speakerPanel.add(speakerField,
BorderLayout.CENTER);
String keyLabels = "123456789*0#";
JPanel keyPanel = new JPanel();
keyPanel.setLayout(new GridLayout(4, 3));
for (int i = 0; i < keyLabels.length(); i++)
{
final String label = keyLabels.substring(i, i + 1);
JButton keyButton = new JButton(label);
keyPanel.add(keyButton);
keyButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
connect.dial(label);
}
});
}
final JTextArea microphoneField = new JTextArea(10, 25);
JButton speechButton = new JButton("Send speech");
speechButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
connect.record(microphoneField.getText());
microphoneField.setText("");
}
});
JButton hangupButton = new JButton("Hangup");
hangupButton.addActionListener(new
ActionListener()
{
public void actionPerformed(ActionEvent event)
{
connect.hangup();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(speechButton);
buttonPanel.add(hangupButton);
JPanel microphonePanel = new JPanel();
microphonePanel.setLayout(new BorderLayout());
microphonePanel.add(new JLabel("Microphone:"),
BorderLayout.NORTH);
microphonePanel.add(microphoneField,
BorderLayout.CENTER);
microphonePanel.add(buttonPanel,
BorderLayout.SOUTH);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.add(speakerPanel,
BorderLayout.NORTH);
contentPane.add(keyPanel,
BorderLayout.CENTER);
contentPane.add(microphonePanel,
BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
/**
Give instructions to the mail system user.
*/
public void speak(String output)
{
speakerField.setText(output);
}
public void run(Connection c)
{
connect = c;
}
private JTextArea speakerField;
private Connection connect;
}
MailSystemTest.javapublic class MailSystemTest
{
public static void main(String[] args)
{
MailSystem system = new MailSystem(MAILBOX_COUNT);
Telephone p = new Telephone();
Connection c = new Connection(system, p);
p.run(c);
}
private static int MAILBOX_COUNT = 20;
}
Maintained by John Loomis, updated Sun Feb 25 20:58:04 2007