forked from grapemoli/MGOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlantBook.java
212 lines (173 loc) · 6.16 KB
/
PlantBook.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//PlantBook.java
//grace nguyen
//april 2, 2022
import java.io.*;
import java.util.*;
import java.text.*;
public class PlantBook extends Book implements Serializable {
//attributes
Map<String, String> content;
public static void main(String[] args) {
//class testing
PlantBook grace = new PlantBook("hi");
grace.addContent("Other", 13);
grace.addContent("Hanging", 5);
grace.addContent("Cactus", 2);
grace.addContent("Cactus", 2); //Java takes care of duplicate keys
grace.addContent("Cactus",4);
grace.listContentKeys();
Object[] keys = grace.getContentKeys();
for(int i = 0; i < 3; i++){
System.out.println(keys[i]);
} //end for
String choice = String.valueOf(keys[0]);
System.out.println(grace.getContent(choice));
} //end main
//constructors
public PlantBook(String name) {
super(name);
content = new HashMap<String, String>();
} //end constructor
//getters and setters
public String getContent(String key) {
//return content at a specific key
return(this.content.get(key));
} //end getContent
public Object[] getContentKeys() {
//return an array of alphabetized keys
Set<String> setKeys = content.keySet();
Object[] arrKeys = setKeys.toArray();
Arrays.sort(arrKeys);
return(arrKeys);
} //end getKeys
public void listContentKeys() {
//list HashMap by alphabetical key order
//sort an array
Set<String> mapKeys = content.keySet();
Object[] arrKeys = mapKeys.toArray();
Arrays.sort(arrKeys);
int counter = 0;
for(Object key : arrKeys) {
counter += 1;
System.out.println(counter + ". " + key);
String stringKey = String.valueOf(key);
} //end for-each
} //end sortContent
//io files methods
public String getPlantLine(String category, Integer line) {
//read the file, and insert information to string as needed
String fileName = "plants" + category + ".dat";
String plant = "";
try{
File readFile = new File(fileName);
Scanner scanner = new Scanner(readFile);
int counter = 0;
Boolean keepGoing = true;
while(keepGoing.equals(true)) {
counter++;
String data = scanner.nextLine();
//get the requested plant information
if(line.equals(counter)) {
plant = data;
keepGoing = false;
} //end if
//exit loop if requested plant info is not found
if(!scanner.hasNextLine()) {
keepGoing = false;
System.out.println("HousePlants101 Entry not found.");
} //end if
} //end while
}catch(Exception e){
System.out.println(e.getMessage());
} //end exception hander
return(plant);
} //end getPlantLine
public String getPlantKey(String plantInfo) {
//get the key for this plant line of information
String[] splitString = plantInfo.split("#");
String key = splitString[0] + " (" + splitString[1] + ")";
return(key);
} // getPlantKey
//formatting strings methods
public String formatLine(String plantInfo) {
//format the line taken in for the 'encyclopedia'
String[] splitString = plantInfo.split("#");
String formatted = "";
for(Integer i = 0; i < splitString.length; i++) {
if(i.equals(0)) {
formatted = "\n~ " + splitString[i] + " ~\n";
}else if(i.equals(1)){
formatted = formatted + "Common Name: " + splitString[i] + "\n";
}else if(i.equals(2)){
formatted = formatted + "Categories: " + splitString[i] + "\n";
}else if(i.equals(3)){
formatted = formatted + "Origin: " + splitString[i] + "\n";
}else if(i.equals(4)){
formatted = formatted + "Climate: " + splitString[i] + "\n";
}else if(i.equals(5)){
String tempString = toFarenheit(splitString[i]);
formatted = formatted + "Temperature Max. (F): " + tempString + "\n";
}else if(i.equals(6)){
String tempString = toFarenheit(splitString[i]);
formatted = formatted + "Temperature Min. (F): " + tempString + "\n";
}else if(i.equals(7)){
formatted = formatted + "Ideal Light: " + splitString[i] + "\n";
}else if(i.equals(8)){
formatted = formatted + "Tolerated Light: " + splitString[i] + "\n";
}else if(i.equals(12)){
if(splitString[i].equals("00")){
formatted = formatted + "Potential Height (ft): --\n";
}else{
String heightString = toFeet(splitString[i]);
formatted = formatted + "Potential Height (ft): " + heightString + "\n";
} //end if
}else if(i.equals(13)){
if(splitString[i].equals("00")){
formatted = formatted + "Potential Width (ft): --\n";
}else{
String widthString = toFeet(splitString[i]);
formatted = formatted + "Potential Width (ft): " + widthString + "\n";
} //end if
}else if(i.equals(14)){
formatted = formatted + "Watering: " + splitString[i] + "\n" ;
}//end if-else
} //end for-loop
return(formatted);
} //end formatLine
//unit conversion methods
public String toFarenheit(String celsius) {
//convert string celsius to farenheit
Double temp = 0d;
String farenheit = "";
try{
temp = Double.valueOf(celsius);
temp = (temp * 9)/5;
temp = temp + 32;
farenheit = String.format("%.2f", temp);
}catch(Exception e){
System.out.println(e.getMessage());
} //end exception-handling
return(farenheit);
} //end toFarenheit
public String toFeet(String meters) {
//convert string meters to feet
Double size = 0d;
String feet = "";
try{
size = Double.valueOf(meters);
size = size * 3.28;
feet = String.format("%.2f", size);
}catch(Exception e){
System.out.println(e.getMessage());
} //end exception-handling
return(feet);
} //end toFeet
//adder methods
public void addContent(String category, Integer index) {
//add content to the hashmap
String plantInfo = getPlantLine(category, index);
String content = formatLine(plantInfo);
String key = getPlantKey(plantInfo);
this.content.put(key, content);
} //end addContent
} //end class def