import java.io.*;

/** An instance has a name, email address, and
  * "personalized" message that prefaces any email sent to them.
  * 
  * Note to class: you might find the file-output stuff here useful. */
public class MailRecip{
    
    private String name; // name of person (can be ""); used for equals testing
    private String addr; // email address, assumed to be well-formatted or ""
    private int msgctr= 0;  // number of messages sent to this person
    
    /* used to make template email seem "personal".
     * Example, "Hi David, boy, developing A1 sure was fun!"
     * (to which a template email "Reminder: we have a staff meeting tomorrow"
     * would be appended).  Can be "" */
    private String personalNote= ""; 
    
    /** Constructor: a new instance with name name, email address addr,
      * and an empty personal message ("") 
      * Precondition: email address is well-formatted (no blanks/whitespace) */
    public MailRecip(String name, String addr) {
        this.name= name;
        this.addr= addr;
    }
    
    /** Constructor: a new instance with name name, email address addr, 
      * and personalized message pm.
      * Precondition: email address is well-formatted (no blanks/whitespace, etc.)*/
    public MailRecip(String name, String addr, String pm) {
        this(name,addr);
        this.personalNote= pm;
    }
    
    /** = "o is a MailRecip with the same name and email address as this MailRecip".
      * (Note: we don't compare the personalized message or other fields,
      * since the intent for having this method is to prevent an extra MailRecip
      * from being added to a MailRecip group (so the same person doesn't get multiple
      * copies of our emails), and these other fields might not be known 
      * at the time of the attempted add.) */
    public boolean equals(Object o) {
        if (!(o instanceof MailRecip)) {
            return false;
        }
        MailRecip mr= (MailRecip)o; 

        return name.equals(mr.getName())  &&  addr.equals(mr.getAddr());
    }
    
    /** = name of this person */
    public String getName() {
        return name; 
    }
    
    /** = email address of this instance */
    public String getAddr() {
        return addr;
    }
    
    /** = personal message for this instance */
    public String getPersonalMessage() {
        return personalNote;
    }
    
    /** Set the personal part of messages to this person to pm.  
      * pm can be "", in which case no personalization is added. */
    public void setPersonal(String pm) {
        personalNote= pm;
    }
    
    /** Send an email to this person with the personal note in this instance
      * and general message s   --Actually, write a file with name
      * email-msgctr.txt, where msgctr indicates the number of messages previously
      * written to this recipient (for record-keeping purposes),
      * containing the message to send, which looks like this:
      *     personal note (stored in this instance)
      *     template (message supplied to this method).
      * This method does not check for file name conflicts or handle
      * other I/O matters very carefully, for demo purposes. 
      * 
      * Those interested in rewriting sendMsg to actually send email
      * may wish to look into the documentation on JavaMail 
      * (a web search will bring you to the right place).
      */
    public void sendMsg(String s) {
        String outname= addr + "-" + msgctr + ".txt";
        
        try {
            //output initialization stuff
            PrintStream p= new PrintStream(new FileOutputStream(outname));
            
            p.println(personalNote);
            p.println();
            p.println(s);
            p.close();
            
            msgctr= msgctr + 1;
        }
        
        // more file-handling stuff
        catch (Exception e) {
            System.err.println("Error in writing to file");
        }
    }
    
    /** = a representation of this instance, including info about 
      * the MailRecip's name, email address, specific note used to "personalize"
      * template email to them, and the number of messages sent to this person 
      * via this Java program */
    public String toString() {
        return name + ": " + addr + "; personalizer: " + personalNote 
            + "; num of msgs sent: " + msgctr;
    }
}