Repository navigation
Expand file tree
/
Copy pathDataStore.java
More file actions
183 lines (155 loc) · 4.9 KB
/
Copy pathDataStore.java
File metadata and controls
183 lines (155 loc) · 4.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
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class DataStore {
private String timeDataFile = "timeData.txt";
private String winDataFile = "winData.txt";
private HashMap<Long, String> winDataStore;
private HashMap<Long, Double> timeDataStore;
public DataStore() {
winDataStore = new HashMap<>();
timeDataStore = new HashMap<>();
loadTimeData();
loadWinData();
}
public void putTime(long key, double val) {
timeDataStore.put(key, val);
saveTimeData();
}
public void putWin(long key, String val) {
winDataStore.put(key, val);
saveWinData();
}
public void delTime(long key) {
timeDataStore.remove(key);
saveTimeData();
}
public void delWin(long key) {
winDataStore.remove(key);
saveWinData();
}
public Double getTime(long key) {
return timeDataStore.get(key);
}
public String getWin(long key) {
return winDataStore.get(key);
}
public boolean timeKeyExists(long key) {
return timeDataStore.containsKey(key);
}
public boolean winKeyExists(long key) {
return winDataStore.containsKey(key);
}
public long findNextTimeKey() {
long key = 0;
while(timeKeyExists(key)) {
key++;
}
return key;
}
public long findNextWinKey() {
long key = 0;
while(winKeyExists(key)) {
key++;
}
return key;
}
public double averageRuntime() {
double counter = 0;
double timeTotal = 0;
for(Map.Entry<Long, Double> entry : timeDataStore.entrySet()) {
counter++;
timeTotal += entry.getValue();
}
return (timeTotal/counter);
}
public float immuneWinPercentage() {
float immuneCount = 0;
float total = 0;
for(Map.Entry<Long, String> entry : winDataStore.entrySet()) {
if(Objects.equals(entry.getValue(), "Immune")) {
immuneCount++;
}
total++;
}
return (immuneCount/total)*100;
}
public float infectedWinPercentage() {
float infectedCount = 0;
float total = 0;
for(Map.Entry<Long, String> entry : winDataStore.entrySet()) {
if(Objects.equals(entry.getValue(), "Infected")) {
infectedCount++;
}
total++;
}
return (infectedCount/total)*100;
}
private void loadTimeData() {
try(BufferedReader reader = new BufferedReader(new FileReader(timeDataFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(":");
if(data.length == 2) {
timeDataStore.put(Long.valueOf(data[0]), Double.valueOf(data[1]));
saveTimeData();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void loadWinData() {
try(BufferedReader reader = new BufferedReader(new FileReader(winDataFile))) {
String line;
while ((line = reader.readLine()) != null) {
String[] data = line.split(":");
if(data.length == 2) {
winDataStore.put(Long.valueOf(data[0]), data[1]);
saveWinData();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveTimeData() {
try(BufferedWriter writer = new BufferedWriter(new FileWriter(this.timeDataFile))) {
for(Map.Entry<Long, Double> entry : timeDataStore.entrySet()) {
writer.write(entry.getKey() + ":" + entry.getValue());
writer.newLine();
}
} catch(IOException e) {
e.printStackTrace();
}
}
private void saveWinData() {
try(BufferedWriter writer = new BufferedWriter(new FileWriter(winDataFile))) {
for(Map.Entry<Long, String> entry : winDataStore.entrySet()) {
writer.write(entry.getKey() + ":" + entry.getValue());
writer.newLine();
}
} catch(IOException e) {
e.printStackTrace();
}
}
public void wipeData() {
long key = 0;
while(winKeyExists(key)) {
delWin(key);
delTime(key);
key++;
}
System.out.println("----------DATA HAS BEEN WIPED----------");
}
public void printDataStore() {
System.out.println("-----Win Data Store-----");
System.out.println(winDataStore);
System.out.println("-----Time Data Store-----");
System.out.println(timeDataStore);
System.out.println("-------------------------");
}
public static void main(String[] args) {
}
}