/** * ColTest is a sample class that include a main that * tests a PIMCollection object by adding a number of * various PIMEntity objects to the PIMCollection, * then test some of the PIMCollection methods. * * This is not an exhaustive test, just some sample * code that might be helpful! */ import java.util.*; class ColTest { /** * main creates some PIMEntity objects and adds them to * a PIMCollection. Then it extracts Collections of the * various types of PIMEntitys to make sure the PIMCollection * works right */ static public void main(String args[]) { PIMCollection pc = new PIMCollection(); PIMNote note; PIMAppointment appt; PIMTodo todo; PIMContact contact; // add 5 PIMNotes to the collection for (int i=1;i<6;i++) { note = new PIMNote(); note.setPriority("Note "+ i); pc.add(note); } // add 4 appointments for (int i=1;i<5;i++) { appt = new PIMAppointment(); appt.setPriority("Appointment "+ i); pc.add(appt); } // add 3 todos for (int i=1;i<4;i++) { todo = new PIMTodo(); todo.setPriority("Todo "+ i); pc.add(todo); } // add 2 Contacts for (int i=1;i<3;i++) { contact = new PIMContact(); contact.setPriority("Contact "+ i); pc.add(contact); } // check getNotes if (!checkCollection(pc.getNotes(),"PIMNote","Note",5)) { System.out.println("Error - notes don't check out"); } else { System.out.println("Notes looks good"); } // check getAppointments if (!checkCollection(pc.getAppointments(),"PIMAppointment","Appointment",4)) { System.out.println("Error - Appointments don't check out"); } else { System.out.println("Appointments looks good"); } // check getTodos if (!checkCollection(pc.getTodos(),"PIMTodo","Todo",3)) { System.out.println("Error - todos don't check out"); } else { System.out.println("Todos looks good"); } // check getContacts // changed to call getContact() since that is what the hw // assignment says the method name should be. // during grading will will try both (so don't worry // about which name you use). if (!checkCollection(pc.getContact(),"PIMContact","Contact",2)) { System.out.println("Error - Contacts don't check out"); } else { System.out.println("Contacts looks good"); } // make sure getItemsForDate does something (this should compile!) Collection cd = pc.getItemsForDate(new Date()); } /** checkCollection is used to check a collection. This method makes * sure that the collection c contains items of the class ClassName, * and that each has a valid priority string (one created above) and * that the number of items in the Collection is as expected. */ static boolean checkCollection( Collection c, String ClassName, String PriorityPrefix, int expectednumber) { Iterator i = c.iterator(); // check each object in the Collection while (i.hasNext()) { Object x = i.next(); // make sure the object in the collection is the right class if (! x.getClass().getName().equals(ClassName)) { // not the right type! System.out.println("Invalid object type: expected " + ClassName + " but found " + x.getClass().getName()); return false; } // now check the priority and see if it looks right String prio = ((PIMEntity) x).getPriority(); if ( ! prio.startsWith(PriorityPrefix)) { System.out.println("Invalid priority string " + prio); return false; } } // all the objects check out - make sure the number is right return(expectednumber == c.size()); } }