/* * Created on Jun 25, 2004 */ package baseball; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import textfile.DelimitedTextFile; /** * @author dickey */ public class League implements ILeague { /** A list in which each object is a different team. */ List teams; /** A list of all players of all teams. */ List players; /** Create a league object, as well as Team objects, by reading * in a file. * @param teamFilename a text file with one line per team. Format * is basically that of the MLB web site sortable team list. */ public League(String teamFilename) { teams = new ArrayList(); players = new ArrayList(); DelimitedTextFile myfile = new DelimitedTextFile(teamFilename); List fields = myfile.readLineAsFieldList(); while (fields != null) { //System.out.println(fields.get(0) + " " + fields.get(1) + " " + fields.get(2)); String teamFullName = (String) fields.get(1); final int batAvgIndex = 18; String teamHittingAvg = (String) fields.get(batAvgIndex); //last Team oneTeam = new Team(teamFullName, teamHittingAvg); teams.add(oneTeam); //System.out.println(oneTeam); fields = myfile.readLineAsFieldList(); } System.out.println("Read " + this.teams.size() + " teams" + " from " + myfile.getFileName()); } /* * @see baseball2.ILeague#addHitterInformation(java.lang.String) */ public int addHitterInformation(String fileID) { DelimitedTextFile myfile = new DelimitedTextFile(fileID); int recordCount = 0; //number of records read from file so far List fields = myfile.readLineAsFieldList(); while (fields != null) { recordCount++; String hitterName = (String) fields.get(1); String teamCode = (String) fields.get(2); String position = (String) fields.get(3); String battingAverage = (String) fields.get(fields.size()-1); //last Player aHitter = new Hitter(hitterName, teamCode, position, battingAverage); this.players.add(aHitter); //System.out.println(aHitter); fields = myfile.readLineAsFieldList(); } return recordCount; } /* * @see baseball2.ILeague#addPitcherInformation(java.lang.String) */ public int addPitcherInformation(String fileID) { DelimitedTextFile myfile = new DelimitedTextFile(fileID); int recordCount = 0; //number of records read from file so far List fields = myfile.readLineAsFieldList(); while (fields != null) { recordCount++; String pitcherName = (String) fields.get(1); String teamCode = (String) fields.get(2); String gamesWon = (String) fields.get(3); String gamesLost = (String) fields.get(4); String era = (String) fields.get(5); Player aPitcher = new Pitcher(pitcherName, teamCode, gamesWon, gamesLost, era); this.players.add(aPitcher); //System.out.println(aPitcher); fields = myfile.readLineAsFieldList(); } return recordCount; } /* * @see baseball2.ILeague#printTeams() */ public void printTeams() { Collections.sort(this.teams); printList(this.teams, "TEAMS"); } /* * @see baseball2.ILeague#printPlayers() */ public void printPlayers() { Collections.sort(this.players); printList(this.players, "ALL PLAYERS"); } /* * @see baseball2.ILeague#printPlayers(java.lang.String) */ public void printPlayers(String teamName) { List oneTeamsPlayers = getTeamPlayers(teamName); Collections.sort(oneTeamsPlayers); if (oneTeamsPlayers.size() == 0) { //No players found! //try under a different name String teamCode = convertTeamName(teamName); if (teamCode != null) { oneTeamsPlayers = getTeamPlayers(teamCode); } } printList(oneTeamsPlayers, "PLAYERS FOR " + teamName); } /** Get a list of all the players for a particular team. * * @param teamCode team code * @return a list with all and only the players for that team; * the returned list may be empty but will not be null. */ private List getTeamPlayers(String teamCode) { List oneTeamsPlayers = new ArrayList(); Iterator iter = this.players.iterator(); while (iter.hasNext()) { Player aPlayer = (Player) iter.next(); //"trim" removes any spaces before and after the name String pTeam = aPlayer.getTeamName().trim(); if (pTeam.equalsIgnoreCase(teamCode)) { oneTeamsPlayers.add(aPlayer); } } return oneTeamsPlayers; } /** A method primarily for test. * * @param args not used */ public static void main(String[] args) { System.out.println("Start Baseball test"); String fname = "teamstats.txt"; ILeague myLeague = new League(fname); String hfname = "hitterstats.txt"; int hcount = myLeague.addHitterInformation(hfname); System.out.println("Read " + hcount + " lines from " + hfname); String pfname = "pitcherstats.txt"; int pcount = myLeague.addPitcherInformation(pfname); System.out.println("Read " + pcount + " lines from " + pfname); System.out.println("-----------------------------"); myLeague.printTeams(); myLeague.printPlayers(); myLeague.printPlayers("SEA"); myLeague.printPlayers("Seattle Mariners"); myLeague.printPlayers(" mariners "); } /** Print each object in a list. Depends on there being a suitable * toString method for each object. * @param aList any non-null List * * @param title a string to print on a line itself before printing * any objects. */ public static void printList(List aList, String title) { System.out.println(); // leave one blank line System.out.println(title); Iterator iter = aList.iterator(); while (iter.hasNext()) { System.out.println(" " + iter.next()); //depends on toString } } /** Table pairing team abbreviations (code) with full team names. */ public static final String[][] teamNames = new String[][] { {"ANA","Anaheim Angels"}, {"ATL","Atlanta Braves"}, {"ARI","Arizona Diamondbacks"}, {"BAL","Baltimore Orioles"}, {"BOS","Boston Red Sox"}, {"CHC","Chicago Cubs"}, {"CIN","Cincinnati Reds"}, {"CLE","Cleveland Indians"}, {"COL","Colorado Rockies"}, {"CWS","Chicago White Sox"}, {"DET","Detroit Tigers"}, {"FLA","Florida Marlins"}, {"HOU","Houston Astros"}, {"KC","Kansas City Royals"}, {"LA","Los Angeles Dodgers"}, {"MIL","Milwaukee Brewers"}, {"MIN","Minnesota Twins"}, {"MON","Montreal Expos"}, {"NYM","New York Mets"}, {"NYY","New York Yankees"}, {"OAK","Oakland Athletics"}, {"PHI","Philadelphia Phillies"}, {"PIT","Pittsburgh Pirates"}, {"SD","San Diego Padres"}, {"SEA","Seattle Mariners"}, {"SF","San Francisco Giants"}, {"STL","St. Louis Cardinals"}, {"TB","Tampa Bay Devil Rays"}, {"TEX","Texas Rangers"}, {"TOR","Toronto Blue Jays"} }; /** Given a full team name (or a significant part of a full team name), * convert it to a team code; or given a * team code, convert it to a full team name. "Significant" means * 5 or more exactly matching letters. * For example, all of the following * will return "TB": "Tampa Bay Devil Rays", "Tampa", "Devil", * etc. On the other hand, if the input is "TB" then "Tampa Bay Devil Rays" * is returned. If part of a full name occurs in more than one full name, * there is no guarantee about which matching team is returned. * @param tname any string * @return the code for a name, or the name for a code; or null * if the given string is not found. */ public static String convertTeamName(String tname) { if (tname == null || tname.length() == 0) { return null; } String trimmedNameLC = tname.trim().toLowerCase(); for (int n = 0; n < teamNames.length; n++) { if (trimmedNameLC.equalsIgnoreCase(teamNames[n][0])) { return teamNames[n][1]; } if (trimmedNameLC.equalsIgnoreCase(teamNames[n][1])) { return teamNames[n][0]; } } //No exact match found if (trimmedNameLC.length() < 5) { return null; //not enough for a reliable match } //make a last-gasp attempt to match something for (int n = 0; n < teamNames.length; n++) { if (teamNames[n][1].toLowerCase().indexOf(trimmedNameLC) >= 0) { return teamNames[n][0]; } } return null; } }