lajdlksadlmla
[rmh3093.git] / lab6 / BaseballMap.java
blob699e11d28c4eed564dd6995feb6bff0789137b7e
1 /*
2 * BaseballMap.java
4 * Version:
5 * $Id: BaseballMap.java,v 1.1 2008/04/21 15:00:32 rmh3093 Exp rmh3093 $
7 * Revisions:
8 * $Log: BaseballMap.java,v $
9 * Revision 1.1 2008/04/21 15:00:32 rmh3093
10 * Initial revision
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;
20 /**
21 * reads a list of Major League Baseball home run champions, removes
22 * duplicates, and displaysplayer information in a sorted order and allow for
23 * modifications
25 * @author Ryan Hope
28 public class BaseballMap {
30 HashMap<String,Player> players = new HashMap<String,Player>();
32 /**
33 * create new baseballmap
35 public BaseballMap(String filename) {
36 try {
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);
47 input.close();
48 file.close();
49 } catch (FileNotFoundException e) {
50 System.err.println("File " + filename + " not found!");
51 } catch (IOException e) {
52 e.printStackTrace();
54 for (String key : players.keySet()) {
55 System.out.println(players.get(key));
57 Scanner command = new Scanner(System.in);
58 while (true) {
59 System.out.print(">");
60 String[] cmd = command.nextLine().split(" ");
61 if (cmd[0].equals("quit")) {
62 System.exit(0);
63 } else if (cmd[0].equals("get")) {
64 if (cmd.length==2) {
65 Player info = players.get(cmd[1]);
66 if (info != null) {
67 System.out.println(info.toString());
68 } else {
69 System.out.println("No entry is found.");
71 } else {
72 System.out.println("get needs a year");
74 } else if (cmd[0].equals("put")) {
75 if (cmd.length==7) {
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);
79 if (result == null) {
80 System.out.println(player.getTeam() + "'s " +
81 player.getName() + " in " + player.getYear() +
82 " has been added.");
83 } else {
84 System.out.println("The key already exists in the " +
85 "map.");
88 } else if (cmd[0].equals("remove")) {
89 if (cmd.length==2) {
90 Player result = players.remove(cmd[1]);
91 if (result != null) {
92 System.out.println(result.getTeam() + "'s " +
93 result.getName() + " in " + result.getYear() +
94 " has been removed.");
96 } else {
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));
108 * @param args
110 public static void main(String[] args) {
111 if (args.length != 1) {
112 System.out.println("Usage: java BaseballMap [datafile]");
113 System.exit(0);
115 new BaseballMap(args[0]);