import java.util.Vector;

/** An instance contains a group of mailRecipients who all should be sent a
  * version of template emails, but with personalizations prepended
  * as specified by each mailRecip instance. */
public class MailGroup extends Vector<MailRecip> {
    
   
    /** send each MailRecip a (personalized) email message, where
      * each message has template as its last portion. */
    public void mailAll(String template) {
        
        // inv: the appropriate message was sent to recipient 0..i-1
        // assume MailRecip has an instance method sendMsg(String template)
        // that sends the recipient a personalized version of template. 
        /*
         * (A) 
           for (int i= 0; i < size(); i= i+1) {
              get(i).sendMsg(template);
             }
         * 
         * (B) 
           for (int i=1; i <= size(); i= i+1) {
                 get(i).sendMsg(template);
               }
         *      
           (C) for (int i= 0; i <= size(); i= i+1) {
              get(i).sendMsg(template);
             }
             
           (D) None of the above are correct.
                 
         * 
         *
         * 
         * 
         * 
         * 
         * 
         * 
         * 
         *
         * 
         * 
         * 
         * 
         * 
         * */
        for (int i= 0; i < size(); i= i+1) {
            get(i).sendMsg(template);
        }
        // post: all MailRecips in this group were sent a personalized email with 
        //   template
    }
    
    
     /** add MailRecip newmr to this group (unless they are already in).
      * Precondition: newmr is not null. */
    public boolean add(MailRecip newmr) {
        
        boolean alreadyIn=false; 
        // inv: alreadyIn= "newmr equals and element of this MailGroup[0..i-1]".
        for (int i= 0; i < size(); i= i+1) {
            if (get(i).equals(newmr)) {
                alreadyIn= true;
            }
        }
        // post: alreadyIn= "newmr equals and element of this MailGroup"
        
        if (!alreadyIn) {
            super.add(newmr);
        }
        
        return true; // this is what Vector add does
    }
    
    /** reset all personalized messages for this group to "" */
    public void resetPersonalizers() {
        // inv: the personal messages of MailRecips in MailGroup[0..i-1]
        //  have been set to "".
        for (int i= 0; i < size(); i= i+1) {
            get(i).setPersonal("");
        }
        // post: the personal messages of all MailRecips in this MailGroup 
        //   have been set to "".
    }
    
    
    
    
    /** demo method.
      * Concepts: 
      * (1) sending email: a useful application
      * (2) for loops
      * [(3) Vectors and equals for sub-objects (don't add duplicates):
      *   see also MailRecip
      * [(4) elementary file output: see MailRecip]
      * (5) "\n" (newline) in Strings
      * 
      */
    public static void demo() {
        MailRecip gries= new 
            MailRecip("David Gries", "djg17@cornell.edu", 
                      "Hi David,\nGood to know that CS1110 is still going well.");
        
        MailRecip llee= new 
            MailRecip("Lillian Lee", "llee@cs.cornell.edu", 
                      "Hey self, remember me? (Hope this joke isn't getting old)");
        
        MailRecip llee2= new
            MailRecip("Lillian Lee", "llee@cs.cornell.edu", 
                      "You imposter!");
        
        MailGroup staff= new MailGroup();
        staff.add(gries);
        staff.add(llee);
        staff.add(llee2);
        
        
        // this can be used to check that there are no double entries
        System.out.println("contents of staff:\n" + staff);
        
        
        staff.mailAll("Remember to schedule or cancel the consultant meeting.\n--LL");
        
        // uncomment to show that a second message can be sent to an individual
        // llee.sendMsg("This means you!");
        
    } 
    
    // could write a class-specic toString.
    
}