-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCity.java
61 lines (56 loc) · 1.78 KB
/
City.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.*;
/**
* Write a description of class City here.
*
* @author Sean Walsh
* @version 1.0
*/
public class City {
CityName name;
Region region;
protected int x, y;
protected boolean hover;
/**
* Constructor that takes a string deliminated with the given
* deliminator to
* initlize the values of this class in the following format (example uses
* ";" as a deliminator) name;region;x;y
*/
public City(String s, String delim) {
StringTokenizer st = new StringTokenizer(s, delim);
String nameStr = st.nextToken();
String regionStr = st.nextToken();
String xStr = st.nextToken();
String yStr = st.nextToken();
String classMsg = "(ERR CONST City)";
try {
name = CityName.valueOf(nameStr.replace(" ", ""));
} catch (Exception e) {
System.err.println(classMsg + nameStr + ": Invalid City name");
}
try {
region = Region.valueOf(regionStr.replace(" ", ""));
} catch (Exception e) {
System.err.println(classMsg + regionStr + ": Invalid Region name");
}
try {
x = Integer.parseInt(xStr);
} catch (Exception e) {
System.err.println(classMsg + xStr + ": Cannot parse x to int");
}
try {
y = Integer.parseInt(yStr);
} catch (Exception e) {
System.err.println(classMsg + yStr + ": Cannot parse y to int");
}
hover = false;
}
/**
* The toString method for the city class
*
* @return The string that represents the city
*/
public String toString() {
return name + "," + region + " (" + x + "," + y + ")";
}
}