package calendar; import java.util.Calendar; //import java.util.Locale; import javax.swing.Icon; /** * This class is the main superclass of all event types * It will hold any necessary information for the classes as * well as any variables that will be used throughout all events * * @author James Alfei (631989) * @date 22/02/2012 * @brief This class will be extended by all event types */ public class Event extends CsvStructure{ /* * These variables set the repeat schedule for the Event */ public static final int NOT_REPEATING = 0; public static final int REPEATING_DAILY = 1; public static final int REPEATING_WEEKENDS = 2; public static final int REPEATING_WORKING_DAYS = 3; public static final int REPEATING_WEEKLY = 4; public static final int REPEATING_TWO_WEEKS = 5; public static final int REPEATING_FOUR_WEEKS = 6; public static final int REPEATING_MONTHLY = 7; public static final int REPEATING_YEARLY = 8; /*Define what event type the object is */ public static final int GENERAL_EVENT = 0; public static final int WORK_EVENT = 1; public static final int SOCIAL_EVENT = 2; public static final int BIRTHDAY_EVENT = 3; /*define what field the object has */ public static final int NO_FIELDS = 7; //IDX 0 is the type public static final int IDX_ID = 1; public static final int IDX_TITLE = 2; public static final int IDX_START_DATE = 3; public static final int IDX_END_DATE = 4; public static final int IDX_DESCRIPTION = 5; public static final int IDX_REPETITION = 6; /*define the variables which the object will store */ protected int m_type; protected int m_id; protected String m_title; protected Calendar m_start_date; protected Calendar m_end_date; protected String m_description; protected int m_repetition; protected Icon m_icon; /* * Getters and Setters */ /** * Gets global description of the event * @return description of the event as a string */ public String GetDescription() { return m_description; } /** * Set global event description * @param description The desciption to set for this event */ public boolean SetDescription(String description) { m_description = description; return true; } /** * Get ending date of event * @return end date in type Calendar */ public Calendar GetEnd_date() { return m_end_date; } /** * set global end date for event * @param end_date */ public boolean SetEnd_date(Calendar end_date) { m_end_date = end_date; return true; } /** * get the ID for the event * @return ID for the event */ public int GetId() { return m_id; } /** * Set global variable ID for the event * @param id new id for the event */ public boolean SetId(int id) { m_id = id; return true; } /** * Return the repetition for the event * @return repetition identifier */ public int GetRepetition() { return m_repetition; } /** * set repetition for the event * @param repetition repetition identifier as integer */ public boolean SetRepetition(int repetition) { m_repetition = repetition; return true; } /** * get start date for event * @return startdate in type Calendar */ public Calendar GetStart_date() { return m_start_date; } /** * set the Event start date * @param start_date new start date for event in type Calendar */ public boolean SetStart_date(Calendar start_date) { this.m_start_date = start_date; return true; } /** * return the Event title * @return title of the Event as string */ public String GetTitle() { return m_title; } /** * Set the event title * @param title The title for the event as a String * @throws Exception Throws exception if invalid title is entered */ public boolean SetTitle(String title) throws Exception { if (ValidTitle(title)==true){ m_title = title; } return true; } /** * Creating a new Event with no values (dummy). * Mainly used for testing purposes. */ public Event() { m_id = Data.AllocateEventId(); m_title = "Temp Event"; m_start_date = Calendar.getInstance(); m_end_date = Calendar.getInstance(); m_end_date.add(Calendar.HOUR_OF_DAY, 1); m_description = "Dummy event"; m_repetition = Event.NOT_REPEATING; m_type = Event.GENERAL_EVENT; } /** * Creating a new Event with given values. * @param id The id of the event as an int * @param title The title of the event as a String * @param start_date The starting date of the event, as a Calendar object * @param end_date The ending date of the event, as a Calendar object * @param description The description of the event, as a String * @param repetition The repetition mode, as an int * @throws Exception Any exceptions caught in validation */ public Event(int id, String title, Calendar start_date, Calendar end_date, String description, int repetition) throws Exception { m_id = id; if(ValidTitle(title) == true){ m_title = title; } m_start_date = start_date; if(validEnd(end_date) == true){ m_end_date = end_date; } if (ValidDescription(description) == true){ m_description = description; } m_repetition = repetition; m_type = Event.GENERAL_EVENT; } /** * Computes the Event as a row in the CSV file * @return The CSV line of the input, as a String */ public String ToCSV() { String csv = m_type+","+m_id+","+"\""+m_title+"\",\""+ Data.FromDate(m_start_date)+"\",\""+Data.FromDate(m_end_date)+"\",\"" +m_description+"\","+m_repetition; return csv; } /** * Check to see whether the Title is valid * @param title the title to be checked as a string * @return true if valid, false if not * @throws Exception Throw exception if Title is invalid */ public boolean ValidTitle(String title) throws Exception{ if (title.equals("") || title.length() > 50){ throw new Exception("Please enter a valid title " + "(Between 1 and 50 characters)"); } return true; } /** * Check whether the end date is valid and after start date * @param end the event end date * @return true if valid and after start, false if not * @throws Exception Throw exception if end is before start */ public boolean validEnd(Calendar end) throws Exception{ if(end.after(GetStart_date()) || end.equals(GetStart_date())){ return true; } throw new Exception("Invalid end date: Please make sure" + " the end is after the beginning of the event"); } /** * check whether the description is valid * @param description the description to be checked * @return True if description is valid, false if not * @throws Exception Throw exception if description is invalid */ public boolean ValidDescription(String description) throws Exception{ if (description.equals("") || description.length() > 250){ throw new Exception("Please enter a valid description " + "(more than 1 and less than 250 characters)"); } return true; } /* * This class is used purely for testing purposes and will be commented * out with the final submission * * @param args command line arguments if needed * @throws Exception Any exceptions throws from validation of input */ /* public static void main (String [] args) throws Exception{ Calendar cal1 = Calendar.getInstance(); Calendar cal2 = Calendar.getInstance(); cal2.add(Calendar.HOUR, 2); Event e = new Event(); @SuppressWarnings("unused") Event f = new Event(Data.AllocateEventId(), "E", cal1, cal2, "Test", NOT_REPEATING); e.SetDescription("Changed 1"); cal2.add(Calendar.HOUR, 4); e.SetEnd_date(cal2); e.SetId(999); e.SetRepetition(2); e.SetTitle("CHANGE TITLE"); System.out.println(e.GetDescription()); System.out.println(e.GetEnd_date()); System.out.println(e.GetId()); System.out.println(e.GetRepetition()); System.out.println(e.GetStart_date()); System.out.println(e.GetTitle()); //TESTING FOR INCORRECT EVENT TYPE (THIS IS SUPPOSED TO FAIL!) @SuppressWarnings("unused") Event g = new Event(Data.AllocateEventId(), "", cal1, cal1, "Test", 8); } */ }