Event.java


Below is the syntax highlighted version of Event.java.


/***********************************************************************
  * Donna Gabai, dgabai P01
  * 
  * Fall12 practice programming exam 4
  *  
  * Event class for storing appointments, checking for conflicts
  * Dependencies: StdOut (for main)
  **********************************************************************/

public class Event {
    // instance variables
    private int start;        // start time
    private int end;          // end time
    private String info;      // information about event
    
    // constructor
    public Event(int s, int e, String i) {
        start = s;
        end   = e;
        info  = i;
    }
    
    // accessor method for start time
    public int getStart() {
        return start;
    }
    
    // accessor method for end time
    public int getEnd() {
        return end;
    }

    // accessor method for start time
    public String getInfo() {
        return info;
    }
    
    // Does this Event conflict with that Event?
    public boolean conflict(Event that) {
        // does it start after the other ends?
        if (this.start >= that.end) return false;  // no conflict
        if (that.start >= this.end) return false;  // no conflict
        // uh-oh.  overlap.
        return true;
    }
    
    // String representation for output
    public String toString() {
        return start + "-" + end + ": " + info;
    }
    
    // test main
    public static void main(String[] args) {
        // set up some events
        Event ev0 = new Event(900, 1000, "Breakfast");
        Event ev1 = new Event(1000, 1050, "COS126 lecture");
        Event ev2 = new Event(1230, 1320, "COS126 precept");
        Event ev3 = new Event(1200, 1300, "lunch with Bob");
        
        StdOut.println(ev0);
        StdOut.println(ev1);
        StdOut.println("Does " + ev0.getInfo() + " conflict with "
                           + ev1.getInfo() + "?");
        if (ev0.conflict(ev1)) StdOut.println("Yes.");
        else                   StdOut.println("No.");
        StdOut.println("Does " + ev2.getInfo() + " conflict with "
                           + ev3.getInfo() + "?");
        if (ev2.conflict(ev3)) StdOut.println("Yes.");
        else                   StdOut.println("No.");
    }
}