Source of Mailbox.java


  1: /**
  2:    A mailbox contains messages that can be listed, kept or discarded.
  3: */
  4: public class Mailbox
  5: {
  6:    /**
  7:       Create Mailbox object.
  8:       @param aPasscode passcode number
  9:       @param aGreeting greeting string
 10:    */
 11:    public Mailbox(String aPasscode, String aGreeting)
 12:    {
 13:       passcode = aPasscode;
 14:       greeting = aGreeting;
 15:       newMessages = new MessageQueue();
 16:       keptMessages = new MessageQueue();
 17:    }

 19:    /**
 20:       Check if the entered supplied is correct.
 21:       @param aPasscode a passcode to check
 22:       @return true if the supplied passcode matches the mailbox passcode
 23:    */
 24:    public boolean checkPasscode(String aPasscode)
 25:    {
 26:       return aPasscode.equals(passcode);
 27:    }

 29:    /**
 30:       Add a message to the mailbox.
 31:       @param aMessage the message to be added
 32:    */
 33:    public void addMessage(Message aMessage)
 34:    {
 35:       newMessages.add(aMessage);
 36:    }

 38:    /**
 39:       Get the current message.
 40:       @return the current message
 41:    */
 42:    Message getCurrentMessage()
 43:    {
 44:       if (newMessages.getLength() > 0)
 45:          return newMessages.getHead();
 46:       else if (keptMessages.getLength() > 0)
 47:          return keptMessages.getHead();
 48:       else
 49:          return null;
 50:    }

 52:    /**
 53:       Remove the current message from the mailbox.
 54:       @return the message that has just been removed
 55:    */
 56:    public Message removeCurrentMessage()
 57:    {
 58:       if (newMessages.getLength() > 0)
 59:          return newMessages.remove();
 60:       else if (keptMessages.getLength() > 0)
 61:          return keptMessages.remove();
 62:       else
 63:          return null;
 64:    }

 66:    /**
 67:       Save the current message
 68:    */
 69:    public void saveCurrentMessage()
 70:    {
 71:       Message m = removeCurrentMessage();
 72:       if (m != null)
 73:          keptMessages.add(m);
 74:    }

 76:    /**
 77:       Change mailbox's greeting.
 78:       @param newGreeting the new greeting string
 79:    */
 80:    public void setGreeting(String newGreeting)
 81:    {
 82:       greeting = newGreeting;
 83:    }

 85:    /**
 86:       Change mailbox's passcode.
 87:       @param newPasscode the new passcode
 88:    */
 89:    public void setPasscode(String newPasscode)
 90:    {
 91:       passcode = newPasscode;
 92:    }

 94:    /**
 95:       Get the mailbox's greeting.
 96:       @return the greeting
 97:    */
 98:    String getGreeting()
 99:    {
100:       return greeting;
101:    }

103:    private MessageQueue newMessages;
104:    private MessageQueue keptMessages;
105:    private String greeting;
106:    private String passcode;
107: }