-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocations.java
More file actions
233 lines (208 loc) · 9 KB
/
Locations.java
File metadata and controls
233 lines (208 loc) · 9 KB
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.dj;
import java.io.*;
import java.util.*;
/**
* Created by timbuchalka on 2/04/2016.
*/
public class Locations implements Map<Integer, Location> {
private static Map<Integer, Location> locations = new LinkedHashMap<Integer, Location>();
public static void main(String[] args) throws IOException {
// try(DataOutputStream locFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("locations.dat")))) {
// for (Location location : locations.values()) {
// locFile.writeInt(location.getLocationID());
// locFile.writeUTF(location.getDescription());
// System.out.println("Writing location " + location.getLocationID() + " : " + location.getDescription());
// System.out.println("Writing " + (location.getExits().size() - 1) + " exits.");
// locFile.writeInt(location.getExits().size() - 1);
// for (String direction : location.getExits().keySet()) {
// if (!direction.equalsIgnoreCase("Q")) {
// System.out.println("\t\t" + direction + "," + location.getExits().get(direction));
// locFile.writeUTF(direction);
// locFile.writeInt(location.getExits().get(direction));
// }
// }
// }
// }
//Commented out 2
// try(BufferedWriter locFile = new BufferedWriter(new FileWriter("locations.txt"));
// BufferedWriter dirFile = new BufferedWriter(new FileWriter("directions.txt"))) {
// for (Location location : locations.values()) {
// locFile.write(location.getLocationID() + "," + location.getDescription() + "\n");
// for (String direction : location.getExits().keySet()) {
// if (!direction.equalsIgnoreCase("Q")) {
// dirFile.write(location.getLocationID() + "," + direction + "," + location.getExits().get(direction) + "\n");
// }
// }
// }
// }
//Commented out 1
// FileWriter locFile = null;
// try {
// locFile = new FileWriter("locations.txt");
// for (Location location : locations.values()) {
// locFile.write(location.getLocationID() + "," + location.getDescription() + "\n");
// }
// locFile.close();
// } finally {
// System.out.println("in finally block");
// if (locFile != null) {
// System.out.println("Attempting to close locfile");
// locFile.close();
// }
// }
try(ObjectOutputStream locFile = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("locations.dat")))) {
for (Location location :locations.values()) {
locFile.writeObject(location);
}
}
}
static {
try(ObjectInputStream locFile = new ObjectInputStream(new BufferedInputStream(new FileInputStream("locations.dat")))) {
boolean eof = false;
while (!eof) {
try {
Location location = (Location) locFile.readObject();
System.out.println("Read location " + location.getLocationID() + " : " + location.getDescription());
System.out.println("Found " + location.getExits().size() + " exits");
locations.put(location.getLocationID(), location);
} catch (EOFException e) {
eof = true;
}
}
} catch (InvalidClassException e) {
System.out.println("Invalid Class Exception " + e.getMessage());
} catch (IOException io) {
System.out.println("IO exception " + io.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException" + e.getMessage());
}
// while (!eof) {
// try {
// Map<String, Integer> exits = new LinkedHashMap<>();
// int locID = locFile.readInt();
// String description = locFile.readUTF();
// int numExits = locFile.readInt();
// System.out.println("Read location " + locID + " : " + description);
// System.out.println("Found " + numExits + "exits");
// for (int i =0; i < numExits; i++) {
// String direction = locFile.readUTF();
// int destination = locFile.readInt();
// exits.put(direction, destination);
// System.out.println("\t\t" + direction + "," + destination);
// }
// locations.put(locID, new Location(locID, description, exits));
// } catch (EOFException e) {
// eof = true;
// }
// }
// try(Scanner scanner = new Scanner(new BufferedReader(new FileReader("locations_big.txt")))) {
// scanner.useDelimiter(",");
// while (scanner.hasNextLine()) {
// int loc = scanner.nextInt();
// scanner.skip(scanner.delimiter());
// String description = scanner.nextLine();
// System.out.println("Imported loc: " + loc + ": " + description);
// Map<String, Integer> tempExit = new HashMap<>();
// locations.put(loc, new Location(loc, description, tempExit));
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// //Now read the exits
// try(BufferedReader dirFile = new BufferedReader(new FileReader("directions_big.txt"))) {
// String input;
// while ((input = dirFile.readLine()) != null) {
//// int loc = scanner.nextInt();
//// scanner.skip(scanner.delimiter());
//// String direction = scanner.next();
//// scanner.skip(scanner.delimiter());
//// String dest = scanner.nextLine();
//// int destination = Integer.parseInt(dest);
// String[] data = input.split(",");
// int loc = Integer.parseInt(data[0]);
// String direction = data[1];
// int destination = Integer.parseInt(data[2]);
//
// System.out.println(loc + ": " + destination);
// Location location = locations.get(loc);
// location.addExit(direction, destination);
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// Map<String, Integer> tempExit = new HashMap<String, Integer>();
// locations.put(0, new Location(0, "You are sitting in front of a computer learning Java",null));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("W", 2);
// tempExit.put("E", 3);
// tempExit.put("S", 4);
// tempExit.put("N", 5);
// locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building",tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("N", 5);
// locations.put(2, new Location(2, "You are at the top of a hill",tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("W", 1);
// locations.put(3, new Location(3, "You are inside a building, a well house for a small spring",tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("N", 1);
// tempExit.put("W", 2);
// locations.put(4, new Location(4, "You are in a valley beside a stream",tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("S", 1);
// tempExit.put("W", 2);
// locations.put(5, new Location(5, "You are in the forest",tempExit));
}
@Override
public int size() {
return locations.size();
}
@Override
public boolean isEmpty() {
return locations.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return locations.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return locations.containsValue(value);
}
@Override
public Location get(Object key) {
return locations.get(key);
}
@Override
public Location put(Integer key, Location value) {
return locations.put(key, value);
}
@Override
public Location remove(Object key) {
return locations.remove(key);
}
@Override
public void putAll(Map<? extends Integer, ? extends Location> m) {
}
@Override
public void clear() {
locations.clear();
}
@Override
public Set<Integer> keySet() {
return locations.keySet();
}
@Override
public Collection<Location> values() {
return locations.values();
}
@Override
public Set<Entry<Integer, Location>> entrySet() {
return locations.entrySet();
}
}