001package horstmann.ch05_mailgui;
002/**
003   A mailbox contains messages that can be listed, kept or discarded.
004 */
005public class Mailbox
006{
007        /**
008      Create Mailbox object.
009      @param aPasscode passcode number
010      @param aGreeting greeting string
011         */
012        public Mailbox(String aPasscode, String aGreeting)
013        {
014                passcode = aPasscode;
015                greeting = aGreeting;
016                newMessages = new MessageQueue();
017                keptMessages = new MessageQueue();
018        }
019
020        /**
021      Check if the entered supplied is correct.
022      @param aPasscode a passcode to check
023      @return true if the supplied passcode matches the mailbox passcode
024         */
025        public boolean checkPasscode(String aPasscode)
026        {
027                return aPasscode.equals(passcode);
028        }
029
030        /**
031      Add a message to the mailbox.
032      @param aMessage the message to be added
033         */
034        public void addMessage(Message aMessage)
035        {
036                newMessages.add(aMessage);
037        }
038
039        /**
040      Get the current message.
041      @return the current message
042         */
043        Message getCurrentMessage()
044        {
045                if (newMessages.getLength() > 0)
046                        return newMessages.getHead();
047                else if (keptMessages.getLength() > 0)
048                        return keptMessages.getHead();
049                else
050                        return null;
051        }
052
053        /**
054      Remove the current message from the mailbox.
055      @return the message that has just been removed
056         */
057        public Message removeCurrentMessage()
058        {
059                if (newMessages.getLength() > 0)
060                        return newMessages.remove();
061                else if (keptMessages.getLength() > 0)
062                        return keptMessages.remove();
063                else
064                        return null;
065        }
066
067        /**
068      Save the current message
069         */
070        public void saveCurrentMessage()
071        {
072                Message m = removeCurrentMessage();
073                if (m != null)
074                        keptMessages.add(m);
075        }
076
077        /**
078      Change mailbox's greeting.
079      @param newGreeting the new greeting string
080         */
081        public void setGreeting(String newGreeting)
082        {
083                greeting = newGreeting;
084        }
085
086        /**
087      Change mailbox's passcode.
088      @param newPasscode the new passcode
089         */
090        public void setPasscode(String newPasscode)
091        {
092                passcode = newPasscode;
093        }
094
095        /**
096      Get the mailbox's greeting.
097      @return the greeting
098         */
099        String getGreeting()
100        {
101                return greeting;
102        }
103
104        private MessageQueue newMessages;
105        private MessageQueue keptMessages;
106        private String greeting;
107        private String passcode;
108}