1  import java.util.ArrayList;
  2  
  3  /**
  4     A system of voice mail boxes.
  5  */
  6  public class MailSystem
  7  {
  8     /**
  9        Constructs a mail system with a given number of mailboxes
 10        @param mailboxCount the number of mailboxes
 11     */
 12     public MailSystem(int mailboxCount)
 13     {
 14        mailboxes = new ArrayList<>();
 15  
 16        // Initialize mail boxes.
 17  
 18        for (int i = 0; i < mailboxCount; i++)
 19        {
 20           String passcode = "" + (i + 1);
 21           String greeting = "You have reached mailbox " + (i + 1)
 22                 + ". \nPlease leave a message now.";
 23           mailboxes.add(new Mailbox(passcode, greeting));
 24        }
 25     }
 26  
 27     /**
 28        Locate a mailbox.
 29        @param ext the extension number
 30        @return the mailbox or null if not found
 31     */
 32     public Mailbox findMailbox(String ext)
 33     {
 34        int i = Integer.parseInt(ext);
 35        if (1 <= i && i <= mailboxes.size())
 36           return  mailboxes.get(i - 1);
 37        else return null;
 38     }
 39  
 40     private ArrayList<Mailbox> mailboxes;
 41  }