001package horstmann.ch05_mailgui;
002import java.util.ArrayList;
003
004/**
005   A system of voice mail boxes.
006 */
007public class MailSystem
008{
009        /**
010      Constructs a mail system with a given number of mail boxes
011      @param mailboxCount the number of mailboxes
012         */
013        public MailSystem(int mailboxCount)
014        {
015                mailboxes = new ArrayList<Mailbox>(mailboxCount);
016
017                /*
018         Initialize mail boxes.
019                 */
020                for (int i = 0; i < mailboxCount; i++)
021                {
022                        String passcode = "" + (i + 1);
023                        String greeting = "You have reached mailbox " + (i + 1)
024                                        + ". \nPlease leave a message now.";
025                        mailboxes.add(i, new Mailbox(passcode, greeting));
026                }
027        }
028
029        /**
030      Locate a mailbox.
031      @param ext the extension number
032      @return the mailbox or null if not found
033         */
034        public Mailbox findMailbox(String ext)
035        {
036                int i = Integer.parseInt(ext);
037                if (1 <= i && i <= mailboxes.size())
038                        return  mailboxes.get(i - 1);
039                else return null;
040        }
041
042        private ArrayList<Mailbox> mailboxes;
043}