import javax.swing.JOptionPane; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import javax.swing.JTextArea; import javax.swing.JScrollPane; import java.util.ArrayList; import java.util.Comparator; import java.util.Collections; public class NCAASummer03 { public NCAASummer03() { BufferedReader in; ArrayList teams = new ArrayList(); String fileName = null; try{ fileName = JOptionPane.showInputDialog("Enter the file name","ncaa2003.data"); in = new BufferedReader(new FileReader(fileName)); String aLine; while( (aLine = in.readLine()) != null ) { StringTokenizer t = new StringTokenizer(aLine,":"); String year = t.nextToken(); // process winning team TeamInfo winningTeam = new TeamInfo(t.nextToken()); int indexOfWinner = teams.indexOf(winningTeam); if( indexOfWinner != -1 ) { winningTeam = (TeamInfo)teams.get(indexOfWinner); } else { teams.add(winningTeam); } // end if winningTeam.incrementWins(); winningTeam.addYear(year); t.nextToken(); // process losing team TeamInfo losingTeam = new TeamInfo(t.nextToken()); int indexOfLoser = teams.indexOf(losingTeam); if( indexOfLoser != -1 ) { losingTeam = (TeamInfo)teams.get(indexOfLoser); } else { teams.add(losingTeam); } // end if losingTeam.incrementLosses(); losingTeam.addYear(year); } // end while } catch( IOException e ) { JOptionPane.showMessageDialog(null, "Could not open the file " + fileName + ". Aborting program."); return; } // end try/catch // sort the ArrayList in alphabetic order using the sort method from the Collections class Collections.sort(teams,new NameComparator()); String out = ""; // add (concatenate) each team info to out so each is on a separate line for( int i = 0; i < teams.size(); i++ ) { out += teams.get(i) + "\n"; } // end for JTextArea outArea = new JTextArea(out,30,50); JOptionPane.showMessageDialog(null,new JScrollPane(outArea)); // sort the ArrayList in wins order using the sort method from the Collections class Collections.sort(teams,new WinsComparator()); out = ""; // add (concatenate) each eam info to out so each is on a separate line for( int i = 0; i < teams.size(); i++ ) { out += teams.get(i) + "\n"; } // end for outArea.setText(out); JOptionPane.showMessageDialog(null,new JScrollPane(outArea)); } // end constructor public static void main(String[] a) { new NCAASummer03(); System.exit(0); } // end main class NameComparator implements Comparator { public int compare(Object x, Object y) { return ((TeamInfo)x).getTeam().compareTo(((TeamInfo)y).getTeam()); } // end compare } // end NameComparator class WinsComparator implements Comparator { public int compare(Object x, Object y) { int winsDifference = ((TeamInfo)y).getWins() - ((TeamInfo)x).getWins(); if( winsDifference != 0 ) return winsDifference; int lossesDifference = ((TeamInfo)x).getLosses() - ((TeamInfo)y).getLosses(); if( lossesDifference != 0 ) return lossesDifference; return ((TeamInfo)x).getTeam().compareTo(((TeamInfo)y).getTeam()); } // end compare } // end NameComparator } // end NCAASummer03