10 date 2008.04.21.15.00.32; author rmh3093; state Exp;
35 import java.io.FileNotFoundException;
36 import java.io.FileReader;
37 import java.io.IOException;
38 import java.util.Scanner;
39 import java.util.HashMap;
42 * reads a list of Major League Baseball home run champions, removes
43 * duplicates, and displaysplayer information in a sorted order and allow for
49 public class BaseballMap {
51 HashMap<String,Player> players = new HashMap<String,Player>();
54 * create new baseballmap
56 public BaseballMap(String filename) {
58 FileReader file = new FileReader(filename);
59 Scanner input = new Scanner(file);
60 while (input.hasNext()) {
61 String year = input.next();
62 String name = input.next() + " " + input.next();
63 String team = input.next() + " " + input.next();
64 int homeruns = Integer.parseInt(input.next());
65 Player player = new Player(year,name,team,homeruns);
66 players.put(year,player);
70 } catch (FileNotFoundException e) {
71 System.err.println("File " + filename + " not found!");
72 } catch (IOException e) {
75 for (String key : players.keySet()) {
76 System.out.println(players.get(key));
78 Scanner command = new Scanner(System.in);
80 System.out.print(">");
81 String[] cmd = command.nextLine().split(" ");
82 if (cmd[0].equals("quit")) {
84 } else if (cmd[0].equals("get")) {
86 Player info = players.get(cmd[1]);
88 System.out.println(info.toString());
90 System.out.println("No entry is found.");
93 System.out.println("get needs a year");
95 } else if (cmd[0].equals("put")) {
97 Player player = new Player(cmd[1], cmd[2] + " " + cmd[3],
98 cmd[4] + " " + cmd[5], Integer.parseInt(cmd[6]));
99 Player result = players.put(cmd[1], player);
100 if (result == null) {
101 System.out.println(player.getTeam() + "'s " +
102 player.getName() + " in " + player.getYear() +
105 System.out.println("The key already exists in the " +
109 } else if (cmd[0].equals("remove")) {
111 Player result = players.remove(cmd[1]);
112 if (result != null) {
113 System.out.println(result.getTeam() + "'s " +
114 result.getName() + " in " + result.getYear() +
115 " has been removed.");
118 System.out.println("remove needs a year");
120 } else if (cmd[0].equals("list")) {
121 for (String key : players.keySet()) {
122 System.out.println(players.get(key));
131 public static void main(String[] args) {
132 if (args.length != 1) {
133 System.out.println("Usage: java BaseballMap [datafile]");
136 new BaseballMap(args[0]);