import java.util.*; import java.io.*; public class TreeClient { private static class HockeyPlayer { int number; String firstName, lastName,school,year, position; boolean isFemale; int heightIn; // height in inches public HockeyPlayer(String info) { // separate the input string into an array of strings, assuming the separators are spaces and commas String [] splitPlayerInfo = info.split("[ ,]"); number = Integer.parseInt(splitPlayerInfo[0]); firstName = splitPlayerInfo[1]; lastName = splitPlayerInfo[2]; isFemale = (splitPlayerInfo[3].equals("F")); school = splitPlayerInfo[4]; year = splitPlayerInfo[5]; position = splitPlayerInfo[6]; heightIn = Integer.parseInt(splitPlayerInfo[8])+12*Integer.parseInt(splitPlayerInfo[7]); } public String toString () { return lastName+", "+firstName + ": number " + number+" on the "+school+ " " + (isFemale?"wo":"")+"men's team. "+position+", "+year+", "+heightIn/12+"'"+heightIn%12+"\"."; } } /** * @param args */ public static void main(String[] args) { System.out.println("Current directory: " + System.getProperty("user.dir")); // Create a new scanner for reading hockey player csv file from the current // directory Scanner fileScanner; try { fileScanner = new Scanner (new File ("data.csv")); } catch (IOException e) { System.out.println("Unable to open file. "+e.getMessage()); return; } // Read in all the hockey players from the file into a tree keyed by last name BinarySearchTree t = new BinarySearchTree(); while (fileScanner.hasNext()) { HockeyPlayer p = new HockeyPlayer(fileScanner.nextLine()); t.insert(p.lastName, p); } // Print all the players System.out.println("All the players:"); for(HockeyPlayer p : t) System.out.println (" "+p); // Print all the BU players System.out.println("BU players only:"); int buCount1 = 0; Iterator it = t.iterator(); while (it.hasNext()) { HockeyPlayer p = it.next(); if (p.school.equals("BU")) { System.out.println(" "+p); buCount1++; } } // Remove all the BC players it = t.iterator(); while (it.hasNext()) { HockeyPlayer p = it.next(); if (p.school.equals("BC")) it.remove(); } // Print all the players again -- should be only the BU players System.out.println("Just removed BC players; printing all players that remain:"); int buCount2 = 0; for(HockeyPlayer p : t) { System.out.println (" "+p); buCount2++; } System.out.println("Found "+buCount1+" BU players the first time, and "+buCount2 + " BU players the second time."); System.out.println("The two numbers should match"); // remove everyone who is left, to make sure remove works correctly it = t.iterator(); while (it.hasNext()) { it.next(); it.remove(); } it = t.iterator(); if (it.hasNext()) System.out.println("Remove everyone test failed.\n"); else System.out.println("Remove everyone test succeeded.\n"); } }