Calendar.java


Below is the syntax highlighted version of Calendar.java.


/******************************************************************************
  * Donna Gabai, dgabai, P01
  * Fall 12 practice programming exam 4
  * 
  * Calendar class for constructing a symbol table of Days and Events.
  * Dependencies: Event, Day, ST, StdOut
  ***********************************************************************/

public class Calendar {
    // instance variable: symbol table for the day's events
    // key Integer for the date yyyymmdd 
    // value Day is linked list of Events
    private ST<Integer, Day> st;
    
    // empty constructor
    public Calendar() {
        st = new ST<Integer, Day>();         // no appointments yet
    }
    
    // schedule an Event on this Date
    public void schedule(int date, Event ev) {
        // Is this date on the calendar?
        if (!st.contains(date)) {
            // new entry
            Day d = new Day();
            d.addEvent(ev);
            st.put(date, d);
            return;
        }
        
        // date already on st
        Day d = st.get(date);
        d.addEvent(ev);
        st.put(date, d);
    }
    
    // print all dates and their events
    public void show() {
        for(Integer date: st) {
            StdOut.println(date);
            st.get(date).show();
            StdOut.println();
        }
    }
    
    // test main for Calendar
    public static void main(String[] args) {
        // set up some events
        Event ev1 = new Event(1000, 1050, "COS126 exam");
        Event ev2 = new Event(1930, 2100, "COS126 programming exam");
        Event ev3 = new Event(1200, 1300, "lunch with Bob");
        Event ev4 = new Event(1000, 1050, "COS126 lecture");
        Event ev5 = new Event(1230, 1320, "COS126 precept");
        
        // set up some dates
        int d1 = 20120501;
        int d2 = 20120503;
        
        Calendar c = new Calendar();
        c.schedule(d1, ev2);
        c.schedule(d1, ev1);
        c.schedule(d2, ev3);
        c.schedule(d2, ev4);
        c.schedule(d2, ev5);

        // Output the events for each date
        c.show();
    }
}