package lab3; import goldman.collection.Collection; import java.io.*; public class DriverForFinalOutput { public static void main(String args[]) throws FileNotFoundException{ HistoricalEventApplication eventApp = new HistoricalEventApplication(); BufferedReader r; // Create a reader for the file allEvents. For debugging you may want to instead // use 20Events try { InputStream is = new FileInputStream("allEvents"); r = new BufferedReader(new InputStreamReader(is)); } catch (IOException e) { System.out.println("IOException while opening " + e); return; } // Accumulate each line of the file (minus surrounding whitespace) // sequentially in a string buffer. try { boolean stop = false; while (!stop){ String nextline = r.readLine(); if (nextline == null) //end of file stop = true; else { int tabLoc = nextline.indexOf('\t'); //input file is tab separated String tempDate = nextline.substring(0,tabLoc); int date = Integer.parseInt(tempDate); String description = nextline.substring(tabLoc+1,nextline.length()); eventApp.add(new HistoricalEvent(date,description)); } } } catch (IOException e) { System.out.println("IOException while reading " + e); } //test date search System.out.println("Date search for 1771."); outputEventsInCollection(eventApp.dateSearch(1771)); System.out.println("\nDate search for 1945."); outputEventsInCollection(eventApp.dateSearch(1945)); System.out.println("\nDate search for 1984."); outputEventsInCollection(eventApp.dateSearch(1984)); //test range search System.out.println("\nRange search for [-5000,-2000]."); outputEventsInRange(eventApp.rangeSearch(-5000,-2000)); System.out.println("\nRange search for [0,100]."); outputEventsInRange(eventApp.rangeSearch(0,100)); System.out.println("\nRange search for [1700,1779]."); outputEventsInRange(eventApp.rangeSearch(1770,1779)); System.out.println("\nRange search for [1990,2000]."); outputEventsInRange(eventApp.rangeSearch(1990,2000)); //test word search System.out.println("\nWord search for last."); outputEventsInCollection(eventApp.wordSearch("last")); System.out.println("\nWord search for virus."); outputEventsInCollection(eventApp.wordSearch("virus")); System.out.println("\nWord search for nebulae."); outputEventsInCollection(eventApp.wordSearch("nebulae")); System.out.println("\nWord search for pi."); outputEventsInCollection(eventApp.wordSearch("pi")); //test remove date System.out.println("\nRemoving following events with date 1771."); outputEventsInCollection(eventApp.removeDate(1771)); System.out.println("\nRemoving following events with date 1848."); outputEventsInCollection(eventApp.removeDate(1848)); System.out.println("\nRemoving a specific event that is there."); HistoricalEvent event = new HistoricalEvent(); //test remove event event.date = 1984; event.description = "Alec Jeffreys devises a DNA fingerprinting method"; if (eventApp.removeEvent(event)) System.out.println("The event was found and removed."); else System.out.println("There was no such event"); System.out.println("\nRemoving a specific event that is not there."); event.date = 1984; event.description = "Ken and Sally Goldman were married."; if (eventApp.removeEvent(event)) System.out.println("The event was found and removed."); else System.out.println("There was no such event."); eventApp.add(new HistoricalEvent(1999,"Takahashi Kanada computed pi to 206158430000 digits")); eventApp.add(new HistoricalEvent(1947,"Wrench Ferguson computed pi to 808 digits using a desk calculator.")); //check deletes and adds are properly reflected System.out.println("\nRepeating range search for [1770-1779]."); outputEventsInRange(eventApp.rangeSearch(1770,1779)); System.out.println("\nRepeating word search for nebulae."); outputEventsInCollection(eventApp.wordSearch("nebulae")); System.out.println("\nRepeating word search for pi."); outputEventsInCollection(eventApp.wordSearch("pi")); //additional tests System.out.println("\nDate search for 1050."); outputEventsInCollection(eventApp.dateSearch(1050)); event.date = 1050; event.description = "Crossbows are invented in France"; if (eventApp.removeEvent(event)) System.out.println("The event was found and removed."); else System.out.println("There was no such event"); if (eventApp.contains(1050)) System.out.println("There is an event on 1050"); else System.out.println("There are no events on 1050"); System.out.println("\nWord search for bronze."); outputEventsInCollection(eventApp.wordSearch("bronze")); event.date = -3000; event.description = "Bronze is used for weapons and armor"; if (eventApp.removeEvent(event)) System.out.println("The event was found and removed."); else System.out.println("There was no such event"); if (eventApp.contains("bronze")) System.out.println("There is an event with the word bronze"); else System.out.println("There are no events with the word bronze"); System.out.println("\nRange search for [30-40]."); outputEventsInRange(eventApp.rangeSearch(30,40)); } static void outputEventsInCollection(Collection eventCollection) { for (HistoricalEvent e: eventCollection) System.out.println(e); } static void outputEventsInRange(RangeIterator it) { while (it.hasNext()) System.out.println(it.next()); } }