001package horstmann.ch02_mail;
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 mailboxes
011      @param mailboxCount the number of mailboxes
012         */
013        public MailSystem(int mailboxCount)
014        {
015                mailboxes = new ArrayList<Mailbox>();
016
017                // Initialize mail boxes.
018
019                for (int i = 0; i < mailboxCount; i++)
020                {
021                        String passcode = "" + (i + 1);
022                        String greeting = "You have reached mailbox " + (i + 1)
023                                        + ". \nPlease leave a message now.";
024                        mailboxes.add(new Mailbox(passcode, greeting));
025                }
026        }
027
028        /**
029      Locate a mailbox.
030      @param ext the extension number
031      @return the mailbox or null if not found
032         */
033        public Mailbox findMailbox(String ext)
034        {
035                int i = Integer.parseInt(ext);
036                if (1 <= i && i <= mailboxes.size())
037                        return  mailboxes.get(i - 1);
038                else return null;
039        }
040
041        private ArrayList<Mailbox> mailboxes;
042}