/**Class Name: AppointmentCalendar--------------------------------Version: 0.2---------------------------------Written by: Aaron CrippsLicense: GPL v2---------------Purpose:--------This class is designed as an extension to the Appointment class. This classwill hold several Appointment objects, and perform operations on itself. Someof these operations will be adding appointments, removing appointments, returningAppointment Objects, and sorting appointments. This can be used as a back end to amore complex program that handles appointments, and appointment calendars. * */import java.util.ArrayList;public class AppointmentCalendar{ ArrayList<ArrayList<Appointment>> calendar; ArrayList<Appointment> day; public final int DEBUG = ,1; int one = 0, two = 0; //these will be used as placeholders in the searchAppointment() method, and will //also be used in cancelAppoiuntment() tocancela specific appointment; public AppointmentCalendar(){ //day is used as a placeholder for appointments that have a unique date not //yet located in calendar. day = new ArrayList<Appointment>(); //this holds all Appointments through multiple Lists of Appointments on //seperate days. calendar = new ArrayList<ArrayList<Appointment>>(); } public void newAppt(String a, String b, String c, String d){ //another placeholder. This holds a new Appointment object until we figure out //what to do with the Appointment. Appointment temp = new Appointment(a,b,c,d); //setting a test variable to help decide whether or not another day needs to be //added to the Calendar ArrayList. boolean test = false; //dirty hack. //if the calendar is empty, automatically make a new day, and put our appointment //(stored in temp) into that day. day.add(temp); if(calendar.size() == 0)calendar.add(day); //this iterates through Calendar to check if we need to add something new or not. for(int i=0; i<calendar.size();i++){ //if this element in Calendar already has a day that's the same as the date //in our newly made appointment, add the appointment to that day, and tell //our test that we tested true, and don't need anything new. if(calendar.get(i).get(0).getDate().compareTo(temp.getDate())==0){ calendar.get(i).add(temp); test = true; } //if we went through Calendar without finding our day, clear the day list, //and then use it to put our Appointment into a new day on the calendar. if(!test){ day.clear(); day.add(temp); calendar.add(day); } } } public void cancelAppointment(String a, String b, String c, String d){ //FIXME! //use searchAppointment for its side effects (setting one and two) //something that shouldn't be happening IS happening here. Basically, it seems that both variables // one and two are retaining their value of -1 ... this is wrong, and prevents us from cancelling appointments, // searchAppointment(a,b,c,d); if(DEBUG == 1)System.out.println("passed search method."); if(one == -1 || two == -1)return; if(DEBUG == 1)System.out.println("passed if statement"); //use one and two to remove the desired appointment. calendar.get(one).remove(two); System.out.println("passed remove."); System.out.println("Appointment " + a +" " + b + " " + c + " " + d + " cancelled."); } //this method searches for ONE appointment meeting the exact criteria specified. If one is not //found, it doesn't do anything. Extra functionality can be added to this later, like allowing //a search for multiple items, or even overloaded methods that search for any of the criterion. public Appointment searchAppointment(String a, String b, String c, String d){ one = -1; two = -1; for(int i=0; i<calendar.size();i++){ for(int j=0;j<calendar.get(i).size();j++){ if(DEBUG == 1)System.out.println("pre-if statement search appt."); //if the date passed to this method doesn't agree with the date in the //current day, then break to the outer loop ... otherwise, keep checking. if((calendar.get(i).get(j).getDate().compareTo(b)==0))break; if(DEBUG == 1)System.out.println("passed 1st if search appt."); //if the date matches, then we've found the correct day, thus, we don't //have to worry about i anymore. one = i; //these next three pretty much do the same thing as the first if statement, //save, they do it with the other arguments. If these fail, however, we only //skip to the next iteration instead of breaking out of the loop. //DEBUG: Currently execution of this method stops here. There's probably //a logic issue within the if() statements. a closer look and more time //would make provision for a working program. for now, output is broken. // these statements break the whole cancelAppointment() method. if((calendar.get(i).get(j).getDescription().compareTo(a)==0)){continue;} if(DEBUG == 1)System.out.println("passed second if statement"); if((calendar.get(i).get(j).getStartTime().compareTo(c)==0)){continue;} if(DEBUG == 1)System.out.println("passed third if statement "); if((calendar.get(i).get(j).getEndTime().compareTo(d)==0)){continue;} //If we've executed this far, it means that a perfect match has been found (a duck!) //therefore we assign the placeholder the proper value, and do what we have to do (burn //her!). two = j; } if(one != -1)break; //if one is no longer set to its inittial value, that means it's been set by //something else. If it's been set by something else, then we have the proper i, therefore, we //don't need to continue iterating through calendar. } //if two has been set, we set it, so we should probably employ one and two as intended so that the //proper value can be returned, otherwise, we just return null, and the user has to catch that //"reference to null pointer" exception if/when it comes along. if(two != -1)return calendar.get(one).get(two); return null; } //returns an element of calendar,namely, an ArrayList containing appointments for a given day. //With this, the user can decide what to do with that day's appointments. public ArrayList<Appointment> getDay(String b){ int i; for(i=0;i==calendar.size();i++){ //if i is the same size as calendar, break and do with i what you will. if(i == calendar.size())break; //if i is in the valid range, this will compare the date passed as a parameter, //and see if there are any days in calendar that match the parameter. If there is, //then we've found what we need, and can break out of the loop. if(calendar.get(i).get(0).getDate().compareTo(b) == 0)break; } //if i reaches this point, it means that there was no matching day found, so a null pointer //is returned, the user will be required to handle this. if(i == calendar.size())return null; //if the program makes it this far, everything has gone well, and we return the value we've found. return calendar.get(i); }}