-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTech.java
114 lines (106 loc) · 3.76 KB
/
Tech.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.awt.*;
import java.util.*;
import java.io.File;
/**
* Write a description of class Technologies here.
*
* @author Sean Walsh
* @version 1.0
*/
public class Tech extends Card {
int numberAvalable, cost;
Technology name;
String description, nameString;
private static final String path =
"resources\\images\\cards\\Technologies\\";
/**
* 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;cost;numberAvalable;description
*/
public Tech(String line, String delim) {
// the following block of code breaks a single string into the
// parameter values for the class storing them in local strings to be
// parsed into values for the class
StringTokenizer st = new StringTokenizer(line, delim);
String nameStr = st.nextToken();
String costStr = st.nextToken();
String numberAvalableStr = st.nextToken();
String description = st.nextToken();
// identifying message for this class
String classMsg = "(ERR CONST Tech)";
// attempts to parse the values from the given string into the classes
// values
// if a parse fails it is caught and an error is printed
try {
name = Technology.valueOf(nameStr.replace(" ", ""));
nameString = nameStr;
} catch (Exception e) {
System.err.println(classMsg + nameStr + ": Invalid Tech name");
}
try {
cost = Integer.parseInt(costStr);
} catch (Exception e) {
System.err.println(classMsg + cost + ": Cannot parse cost to int");
}
try {
numberAvalable = Integer.parseInt(numberAvalableStr);
} catch (Exception e) {
System.err.println(classMsg + numberAvalable
+ ": Cannot parse numberAvalable to int");
}
}
/**
* static method to get a genral image for this object type
* @param color type of card
* @return an image based on the type of card
*/
public static Image getImage(Technology name) {
String filePath = path + name + ".jpg";
try (Scanner sc = new Scanner(new File(filePath))) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
return toolkit.getImage(filePath);
} catch (Exception e) {
System.err.println("(ERR Tech.getImage): Cannot find file \""
+ filePath + "\"");
}
return null;
}
/**
* a method that returns a image representing this object
* @return an image that represents this object
*/
public Image getImage() {
String filePath = path + name + ".jpg";
try (Scanner sc = new Scanner(new File(filePath))) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
return toolkit.getImage(filePath);
} catch (Exception e) {
System.err.println("(ERR Tech.getImage): Cannot find file \""
+ filePath + "\"");
}
return null;
}
/**
* method to compare this Tech card to other Objects
*
* @param o A Object to compare to
* @return false if the Objects are not equal
*/
@Override
public boolean equals(Object o) {
if (o instanceof Tech)
if (this.name == ((Tech) o).name)
return true;
return false;
}
/**
* method that returns a unique int for this object
* @return a unique int for this object
*/
@Override
public int hashCode() {
return name.hashCode();
}
}