1 // SimpleWISE.java 2 // 3 // Exercise collections, inheritance, error handling 4 // Ethan Bolker, for cs110 exam 2, Spring 2004 5 // 6 // Fewer comments than would be appropriate for production code. 7 8 import java.util.*; 9 10 public class SimpleWISE 11 { 12 private TreeMap allLists; 13 14 // Constructor creates lists for SimpleWISE instance. 15 public SimpleWISE() 16 { 17 allLists = new TreeMap(); 18 allLists.put("courses", new CourseList()); 19 allLists.put("students", new StudentList()); 20 } 21 22 // What lists are there? 23 public String getStatus() 24 { 25 return "SimpleWISE: created lists for " + allLists.keySet(); 26 } 27 28 // Get a String representation of one of the lists. 29 public String getPrintableList( String listName ) 30 throws WISEException 31 { 32 return get(listName).getPrintableList(); 33 } 34 35 // Add an Object to one of the lists. 36 public void add( String listName, Object item ) 37 throws WISEException 38 { 39 get(listName).add(item); 40 } 41 42 // Retrieve one of the lists, given its name. 43 private WISEList get( String listName ) 44 throws WISEException 45 { 46 if ( !allLists.containsKey(listName)) { 47 throw new WISEException("No such list: " + listName); 48 } 49 return (WISEList)allLists.get(listName); 50 } 51 52 // For unit testing. 53 public static void main( String[] args ) 54 { 55 Terminal t = new Terminal(); 56 SimpleWISE system = new SimpleWISE(); 57 58 t.println(system.getStatus()); 59 try { 60 for (int i = 0; i < args.length; i++) { 61 system.add("courses", args[i]); 62 } 63 system.add("professors", "Ricardo Menard"); 64 system.add("students", "Solomon"); 65 system.add("students", "Eleanor"); 66 } 67 catch( WISEException e ) { 68 t.errPrintln(e.getMessage()); 69 } 70 try { 71 t.println( system.getPrintableList("courses") ); 72 t.println( system.getPrintableList("students") ); 73 } 74 catch( WISEException e ) { 75 t.errPrintln(e.getMessage()); 76 } 77 } 78 }