package calendar;
import java.util.ArrayList;
import java.util.Calendar;
import javax.naming.directory.InvalidAttributesException;
m_data = new ArrayList<Event>();
}
ArrayList<Event> events = new ArrayList<Event>();
for(int i = 0; i < m_data.size(); i++) {
Event e = m_data.get(i);
Calendar edate = e.GetStart_date();
if(edate.get(Calendar.DAY_OF_MONTH) ==
date.get(Calendar.DAY_OF_MONTH)
&& edate.get(Calendar.MONTH) == date.get(Calendar.MONTH)
&& edate.get(Calendar.YEAR) == date.get(Calendar.YEAR)
)
events.add(e);
else if(date.compareTo(edate) >= 0) {
if(e.GetRepetition() == Event.REPEATING_DAILY) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_WORKING_DAYS &&
date.get(Calendar.DAY_OF_WEEK) >= Calendar.MONDAY &&
date.get(Calendar.DAY_OF_WEEK) <= Calendar.FRIDAY) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_WEEKENDS &&
(date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_WEEKLY &&
date.get(Calendar.DAY_OF_WEEK) ==
edate.get(Calendar.DAY_OF_WEEK)) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_TWO_WEEKS &&
date.get(Calendar.DAY_OF_WEEK) ==
edate.get(Calendar.DAY_OF_WEEK) &&
date.get(Calendar.WEEK_OF_YEAR)%2 ==
edate.get(Calendar.WEEK_OF_YEAR)%2) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_FOUR_WEEKS &&
date.get(Calendar.DAY_OF_WEEK) ==
edate.get(Calendar.DAY_OF_WEEK) &&
date.get(Calendar.WEEK_OF_YEAR)%4 ==
edate.get(Calendar.WEEK_OF_YEAR)%4) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_MONTHLY &&
date.get(Calendar.DAY_OF_MONTH) ==
edate.get(Calendar.DAY_OF_MONTH)) {
events.add(e);
}
else if(e.GetRepetition() == Event.REPEATING_YEARLY &&
date.get(Calendar.DAY_OF_MONTH) ==
edate.get(Calendar.DAY_OF_MONTH) &&
date.get(Calendar.MONTH) ==
edate.get(Calendar.MONTH)) {
events.add(e);
}
}
}
return events;
}
for(int i = 0; i < m_data.size(); i++) {
if(m_data.get(i).GetId() == id) {
return i;
}
}
throw new InvalidAttributesException(
"The requested EventID does not exists");
}
public Event
GetEventById(
int id)
throws InvalidAttributesException {
for(int i = 0; i < m_data.size(); i++) {
if(m_data.get(i).GetId() == id) {
return m_data.get(i);
}
}
throw new InvalidAttributesException("Event not found");
}
public static void main(String args[]) {
EventsCollection collection = new EventsCollection();
EventsCollection emptyCollection = new EventsCollection();
Calendar calStart = Calendar.getInstance();
Calendar calEnd = Calendar.getInstance();
Calendar calNoEvents = Calendar.getInstance();
calStart.set(2012, 12, 10, 12, 00);
calEnd.set(2012, 12, 11, 12, 00);
calNoEvents.set(2012, 12, 12, 12, 00);
try {
collection.Add(new Event(1, "My Birthday", calStart, calEnd,
"The day of my birthday", 0));
} catch (Exception e) {
System.out.println(e.getMessage());
}
ArrayList<Event> eventArray = collection.GetEventsByDay(calStart);
if (eventArray.size()==1) {
System.out.println("Pass: Event successfully returned " +
"for set date");
} else {
System.out.println("Fail: Event could not be returned " +
"for set date");
}
ArrayList<Event> noEventArray = emptyCollection.GetEventsByDay
(calNoEvents);
if (noEventArray.size()==0) {
System.out.println("Pass: No events found for set day");
} else {
System.out.println("Fail: Some events were found when no " +
"events were set");
}
try {
collection.GetEventIndexById(1);
System.out.println("Pass: Event returned with matching ID '1'");
} catch (InvalidAttributesException e) {
System.out.println("Failed: The event ID '1' does exist but " +
"has not been returned");
}
try {
collection.GetEventIndexById(2);
System.out.println("Fail: No event has the ID '2' but an event " +
"has been returned");
} catch (InvalidAttributesException e) {
System.out.println("Pass: "+e.getMessage());
}
try {
collection.GetEventById(1);
System.out.println("Pass: Event with valid ID found");
} catch (InvalidAttributesException e) {
System.out.println("Fail: "+e.getMessage());
}
try {
collection.GetEventById(2);
System.out.println("Fail: No event has the ID '2' but an event " +
"has been returned");
} catch (InvalidAttributesException e) {
System.out.println("Pass: "+e.getMessage());
}
}
}