-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBetterCleanSwordManager.java
More file actions
99 lines (82 loc) · 4 KB
/
BetterCleanSwordManager.java
File metadata and controls
99 lines (82 loc) · 4 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
package Drones;
import CommonUtils.BetterHashTable;
import CommonUtils.MinHeap;
import Drones.Interfaces.BetterCleanSwordManagerInterface;
import Items.Sword;
import Items.Types.SwordType;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
/**
* Manages everything regarding the cleaning of swords in our game. Will be integrated with
* the other drone classes. UPDATED VERSION -- handles sword management with the new rules.
* Old version-CleanSwordManager-is deprecated.
*
* <bold>251 students: you may use any of the data structures you have previously created, but may not use
* any Java library for stacks, queues, min heaps/priority queues, or hash tables (or any similar classes).</bold>
*/
public class BetterCleanSwordManager implements BetterCleanSwordManagerInterface {
/**
* Gets the cleaning times per the specifications.
*
* @param filename file to read input from
* @return the list of times requests were filled, times it took to fill them,
* and sword returned, as per the specifications
*/
@Override
public ArrayList<DetailedCleanSwordTime<SwordType>> getCleaningTimes(String filename) {
try {
BufferedReader bf = new BufferedReader(new FileReader(filename));
ArrayList<DetailedCleanSwordTime<SwordType>> ans = new ArrayList<>();
String[] in = bf.readLine().split(" ");
int n = Integer.parseInt(in[0]);
int m = Integer.parseInt(in[1]);
BetterHashTable<Sword.Identifier, Sword> table = new BetterHashTable<>();
MinHeap<Sword.WithTime> queue = new MinHeap<>();
long time = 0;
for (int i = 0; i < n; i++) {
String[] in2 = bf.readLine().split(", ");
int c = Integer.parseInt(in2[0]);
int t = Integer.parseInt(in2[1]);
int th = Integer.parseInt(in2[2]);
int hl = Integer.parseInt(in2[3]);
int l = Integer.parseInt(in2[4]);
int dps = Integer.parseInt(in2[5]);
int as = Integer.parseInt(in2[6]);
String name = in2[7].substring(1, in2[7].length() - 1);
String desc = in2[8].substring(1, in2[8].length() - 1);
String com = in2[9].substring(1, in2[9].length() - 1);
String style = in2[10].substring(1, in2[10].length() - 1);
Sword sword = new Sword(th, hl, t, l, dps, as, name, desc, com, style);
table.insert(sword.getIdentifier(), sword);
if (c != -1) queue.add(sword.associateWithTime(c, i));
}
long[] requests = new long[m];
for (int i = 0; i < m; i++) {
String[] in2 = bf.readLine().split(", ");
long zj = Long.parseLong(in2[0]);
requests[i] = zj;
int th = Integer.parseInt(in2[1]);
int dps = Integer.parseInt(in2[2]);
int as = Integer.parseInt(in2[3]);
String style = in2[4].substring(1, in2[4].length() - 1);
// Returned sword will be ready in current time + t
Sword returnedSword = table.get(new Sword.Identifier(th, dps, as, style));
queue.add(returnedSword.associateWithTime(zj + returnedSword.getTimeToClean(), n + i));
}
for (long zj : requests) {
Sword.WithTime nextSword = queue.removeMin();
long nextSwordTime = Math.max(zj, nextSword.getReadyBy());
Sword returned = table.get(nextSword.getIdentifier());
ans.add(new DetailedCleanSwordTime<>(nextSwordTime, nextSwordTime - zj, returned));
}
return ans;
} catch (IOException e) {
//This should never happen... uh oh o.o
System.err.println("ATTENTION TAs: Couldn't find test file: \"" + filename + "\":: " + e.getMessage());
System.exit(1);
}
return null;
}
}