5 * $Id: BaseballMap.java,v 1.1 2008/04/21 15:00:32 rmh3093 Exp rmh3093 $
8 * $Log: BaseballMap.java,v $
9 * Revision 1.1 2008/04/21 15:00:32 rmh3093
14 import java
.io
.FileNotFoundException
;
15 import java
.io
.FileReader
;
16 import java
.io
.IOException
;
17 import java
.util
.Scanner
;
18 import java
.util
.HashMap
;
21 * reads a list of Major League Baseball home run champions, removes
22 * duplicates, and displaysplayer information in a sorted order and allow for
28 public class BaseballMap
{
30 HashMap
<String
,Player
> players
= new HashMap
<String
,Player
>();
33 * create new baseballmap
35 public BaseballMap(String filename
) {
37 FileReader file
= new FileReader(filename
);
38 Scanner input
= new Scanner(file
);
39 while (input
.hasNext()) {
40 String year
= input
.next();
41 String name
= input
.next() + " " + input
.next();
42 String team
= input
.next() + " " + input
.next();
43 int homeruns
= Integer
.parseInt(input
.next());
44 Player player
= new Player(year
,name
,team
,homeruns
);
45 players
.put(year
,player
);
49 } catch (FileNotFoundException e
) {
50 System
.err
.println("File " + filename
+ " not found!");
51 } catch (IOException e
) {
54 for (String key
: players
.keySet()) {
55 System
.out
.println(players
.get(key
));
57 Scanner command
= new Scanner(System
.in
);
59 System
.out
.print(">");
60 String
[] cmd
= command
.nextLine().split(" ");
61 if (cmd
[0].equals("quit")) {
63 } else if (cmd
[0].equals("get")) {
65 Player info
= players
.get(cmd
[1]);
67 System
.out
.println(info
.toString());
69 System
.out
.println("No entry is found.");
72 System
.out
.println("get needs a year");
74 } else if (cmd
[0].equals("put")) {
76 Player player
= new Player(cmd
[1], cmd
[2] + " " + cmd
[3],
77 cmd
[4] + " " + cmd
[5], Integer
.parseInt(cmd
[6]));
78 Player result
= players
.put(cmd
[1], player
);
80 System
.out
.println(player
.getTeam() + "'s " +
81 player
.getName() + " in " + player
.getYear() +
84 System
.out
.println("The key already exists in the " +
88 } else if (cmd
[0].equals("remove")) {
90 Player result
= players
.remove(cmd
[1]);
92 System
.out
.println(result
.getTeam() + "'s " +
93 result
.getName() + " in " + result
.getYear() +
94 " has been removed.");
97 System
.out
.println("remove needs a year");
99 } else if (cmd
[0].equals("list")) {
100 for (String key
: players
.keySet()) {
101 System
.out
.println(players
.get(key
));
110 public static void main(String
[] args
) {
111 if (args
.length
!= 1) {
112 System
.out
.println("Usage: java BaseballMap [datafile]");
115 new BaseballMap(args
[0]);