lajdlksadlmla
[rmh3093.git] / lab6 / RCS / BaseballMap.java,v
blob1abf73f2dbca1370b9e011e11aa8aa221c3994c3
1 head    1.1;
2 access;
3 symbols;
4 locks
5         rmh3093:1.1; strict;
6 comment @# @;
9 1.1
10 date    2008.04.21.15.00.32;    author rmh3093; state Exp;
11 branches;
12 next    ;
15 desc
16 @create baseball map
20 1.1
21 log
22 @Initial revision
24 text
25 @/*
26  * BaseballMap.java
27  *
28  * Version:
29  *     $Id$
30  *
31  * Revisions:
32  *     $Log$
33  */
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;
41 /**
42  * reads a list of Major League Baseball home run champions, removes 
43  * duplicates, and displaysplayer information in a sorted order and allow for
44  * modifications
45  * 
46  * @@author Ryan Hope
47  *
48  */
49 public class BaseballMap {
50         
51         HashMap<String,Player> players = new HashMap<String,Player>();
53         /**
54          * create new baseballmap
55          */
56         public BaseballMap(String filename) {
57                 try {
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);
67                         }
68                         input.close();
69                         file.close();
70                 } catch (FileNotFoundException e) {
71                         System.err.println("File " + filename + " not found!");
72                 } catch (IOException e) {
73                         e.printStackTrace();
74                 }
75                 for (String key : players.keySet()) {
76                         System.out.println(players.get(key));
77                 }
78                 Scanner command = new Scanner(System.in);
79                 while (true) {
80                         System.out.print(">");
81                         String[] cmd = command.nextLine().split(" ");
82                         if (cmd[0].equals("quit")) {
83                                 System.exit(0);
84                         } else if (cmd[0].equals("get")) {
85                                 if (cmd.length==2) {
86                                         Player info = players.get(cmd[1]);
87                                         if (info != null) {
88                                                 System.out.println(info.toString());
89                                         } else {
90                                                 System.out.println("No entry is found.");
91                                         }
92                                 } else {
93                                         System.out.println("get needs a year");
94                                 }
95                         } else if (cmd[0].equals("put")) {
96                                 if (cmd.length==7) {
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() +
103                                                                 " has been added.");
104                                         } else {
105                                                 System.out.println("The key already exists in the " +
106                                                                 "map.");
107                                         }
108                                 }
109                         } else if (cmd[0].equals("remove")) {
110                                 if (cmd.length==2) {
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.");
116                                         }
117                                 } else {
118                                         System.out.println("remove needs a year");
119                                 }
120                         } else if (cmd[0].equals("list")) {
121                                 for (String key : players.keySet()) {
122                                         System.out.println(players.get(key));
123                                 }
124                         }
125                 }
126         }
128         /**
129          * @@param args
130          */
131         public static void main(String[] args) {
132                 if (args.length != 1) {
133                         System.out.println("Usage: java BaseballMap [datafile]");
134                         System.exit(0);
135                 }
136                 new BaseballMap(args[0]);
137         }