-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudit.java
248 lines (230 loc) · 12.3 KB
/
Audit.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import java.io.File;
import java.util.ArrayList;
import java.lang.Math;
import java.util.Comparator;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.io.FileWriter;
import CustomExceptions.*;
import Entities.*;
/**
* COMP90041, Sem1, 2023: Final Project
* @author Jule Valendo Halim
* student id: 1425567
* student email: [email protected]
*
* Class with methods to make, save, and print statistics
* Contains method to check if a log file is provided but does not exist
*/
public class Audit {
//Instance Variables
private ArrayList < String[] > auditArrayList;
private double entityCount;
private double totalAge;
//Constructors
/**
* Default constructor for Audit
* Initializes instance variables
*/
public Audit() {
auditArrayList = new ArrayList < String[] > ();
entityCount = 0;
totalAge = 0;
}
//Public Methods
//Mutators
/**
* Reads in and creates a statistic when a location is provided
* @param location Location to add to statistic
* @param audit Passes in same audit to keep statistic culmulative
* @param judge Passes in judge to call methods to add to the total count statistic in Class judge
*/
public void makeStatistic(Location location, Audit audit, Judge judge) {
for (int i = 0; i < location.getEntityArray().size(); i++) {
if (location.returnTrespassing()) {
judge.addChosenCount("trespassing");
} else {
judge.addChosenCount("legal");
}
Entity currentEntity = location.getEntityArray().get(i);
if (currentEntity instanceof AdultFemale) { //performs checks for adult females
entityCount += 1; //gets total count of humans in a location that was chosen
totalAge += currentEntity.getAge(); //gets total age of humans in a location that was chosen
AdultFemale currentCheckAdultFemale = (AdultFemale) currentEntity;
String bodyType = currentCheckAdultFemale.getBodyType();
String ageCategory = currentCheckAdultFemale.getAgeGroup();
String gender = currentCheckAdultFemale.getGender();
String profession = currentCheckAdultFemale.getProfession();
String pregnant = null;
if (currentCheckAdultFemale.getPregnant()) { //performs checks for pregnant adult females
pregnant = "pregnant";
if (!bodyType.equalsIgnoreCase("unspecified")) {
judge.addChosenCount(bodyType);
}
judge.addChosenCount("human");
judge.addChosenCount(ageCategory);
if (!profession.equalsIgnoreCase("none")) {
judge.addChosenCount(profession);
}
if (!gender.equalsIgnoreCase("unknown")) {
judge.addChosenCount(gender);
}
judge.addChosenCount(pregnant);
} else { //performs checks for not pregnant adult females
if (!bodyType.equalsIgnoreCase("unspecified")) { //adds to statistic only if body type is accepted and not unspecified
judge.addChosenCount(bodyType);
}
judge.addChosenCount(ageCategory);
judge.addChosenCount("human");
if (!profession.equalsIgnoreCase("none")) {//adds to statistic only if profession is accepted and not none
judge.addChosenCount(profession);
}
if (!gender.equalsIgnoreCase("unknown")) {//adds to statistic only if gender is accepted and not unknown
judge.addChosenCount(gender);
}
}
} else if (currentEntity instanceof Human) { //performs checks for humans that are not adult females
entityCount += 1;//gets total count of humans in a location that was chosen
totalAge += currentEntity.getAge();//gets total age of humans in a location that was chosen
Human currentCheckHuman = (Human) currentEntity;
String bodyType = currentCheckHuman.getBodyType();
String ageCategory = currentCheckHuman.getAgeGroup();
String gender = currentCheckHuman.getGender();
if (ageCategory.equalsIgnoreCase("adult")) {
String profession = currentCheckHuman.getProfession();
if (!bodyType.equalsIgnoreCase("unspecified")) {//adds to statistic only if body type is accepted and not unspecified
judge.addChosenCount(bodyType);
}
judge.addChosenCount("human");
judge.addChosenCount(ageCategory);
if (!profession.equalsIgnoreCase("none")) {//adds to statistic only if profession is accepted and not none
judge.addChosenCount(profession);
}
if (!gender.equalsIgnoreCase("unknown")) {//adds to statistic only if gender is accepted and not unknown
judge.addChosenCount(gender);
}
} else if (!ageCategory.equalsIgnoreCase("adult")) { //performs checks for non-adults
if (!bodyType.equalsIgnoreCase("unspecified")) {
judge.addChosenCount(bodyType);
}
judge.addChosenCount(ageCategory);
judge.addChosenCount("human");
if (!gender.equalsIgnoreCase("unknown")) {
judge.addChosenCount(gender);
}
}
} else if (currentEntity instanceof Animal) { //performs checks for animals
Animal currentCheckAnimal = (Animal) currentEntity;
boolean doNotIncludeSpecies = false;
String species = currentCheckAnimal.getSpecies();
if (species.equalsIgnoreCase("unknown")) { //only added if species is valid and not unknown
doNotIncludeSpecies = true;
}
if (currentCheckAnimal.getPetStatus()) {
judge.addChosenCount("pet");
judge.addChosenCount("animal");
if (!doNotIncludeSpecies) {
judge.addChosenCount(species);
}
} else {
if (!doNotIncludeSpecies) {
judge.addChosenCount(species);
}
judge.addChosenCount("animal");
}
}
}
}
//Other public methods
/**
* Checks if a log file is provided and exists, printing an error and exiting if given log does not exist
* @param logFileProvided True if user provides a log file
* @param logFileExists True if the log file exists
*/
public void checkIfLogExists(boolean logFileProvided, boolean logFileExists) {
if (logFileProvided && !logFileExists) {
System.out.println("ERROR: could not print results. Target directory does not exist.");
System.exit(0);
}
}
/**
* Saves audit to log file
* @param logFile Log file to be read to
* @param totalCountStatistic Statistics to be added to log file
* @param fromWho Indicates whether saving from a bot or user input
* @param logFileProvided True if user provides a log file, used to check if log file exists
* @param logFileExists True if the log file exists, used to check if log file exists
* @param totalScenarios Total number of scenarios run to obtain the statistic
* @param totalAge Total age of humans in the statistic
* @param totalEntities Total number of humans in the statistic
*/
public void saveAudit(File logFile, ArrayList < String > totalCountStatistic, String fromWho, boolean logFileProvided, boolean logFileExists, int totalScenarios, int totalAge, int totalEntities) {
checkIfLogExists(logFileProvided, logFileExists);
try {
PrintWriter printWriter = new PrintWriter(new FileWriter(logFile.getPath(), true));
printWriter.print("YYYY-MM-DD DATE//" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "//HH-MM-SS TIME//" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH-mm-ss")));
if (fromWho.equalsIgnoreCase("userRun")) { //saving user audits
printWriter.print("//USERAUDIT");
} else if (fromWho.equalsIgnoreCase("robotRun")) { //saving bot audits
printWriter.print("//BOTAUDIT");
}
printWriter.print("//TOTAL-SCENARIOS//" + String.valueOf(totalScenarios)); //saving scenarios run in an audit
printWriter.println("//TOTAL-HUMAN-AGE//" + String.valueOf(totalAge) + "//TOTAL-HUMANS//" + String.valueOf(totalEntities) + "//"); //saving total age and number of humans for auditing
int shiftCount = 3;
int totalShiftCount = 2;
for (int i = 0; i < totalCountStatistic.size(); i += shiftCount) {
printWriter.println(totalCountStatistic.get(i) + ":SAVED:" + totalCountStatistic.get(i + 1) + ":TOTAL:" + totalCountStatistic.get(i + totalShiftCount)); //saving statistics created during judging
}
printWriter.println("");
printWriter.close();
} catch (Exception e) {
System.out.print("Error in writing to logFile");
}
}
/**
* Prints a statistic
* @param totalCountStatistic Statistic to be read from
* @param scenarioNumber Indicates number of scenarios judged
* @param auditOrNot Checks if being called from an audit
*/
public void printAudit(ArrayList < String > totalCountStatistic, int scenarioNumber, boolean auditOrNot) {
for (int i = 0; i < totalCountStatistic.size(); i += 3) {
String currentCharacteristic = totalCountStatistic.get(i);
double numerator = Double.parseDouble(totalCountStatistic.get(i + 1));
int denominatorPosition = 2;
double denominator = Double.parseDouble(totalCountStatistic.get(i + denominatorPosition));
double ratio = Math.ceil((numerator / denominator) * 100) / 100; //rounds statistics up
boolean isNotIn = true; //check if value is in the statistic to be printed
for (int j = 0; j < auditArrayList.size(); j++) {
if (auditArrayList.get(j)[0].equalsIgnoreCase(currentCharacteristic)) {
isNotIn = false;
}
}
if (isNotIn) { //if not in yet, adds its survival ratio to be printed
String[] insert = {
currentCharacteristic,
String.valueOf(ratio)
};
auditArrayList.add(insert);
}
}
ArrayList < String[] > arraySorted = auditArrayList;
arraySorted.sort(Comparator.comparing((String[] array) -> Double.parseDouble(array[1])).reversed().thenComparing(array -> array[0])); //sorts the list, from descending ratio then alphabetically
if (!auditOrNot) {
System.out.println("======================================");
System.out.println("# Statistic");
System.out.println("======================================");
System.out.printf("- %% SAVED AFTER %d RUNS%n", scenarioNumber + 1);
}
for (int j = 0; j < arraySorted.size(); j++) {
System.out.printf("%s: %.2f%n", arraySorted.get(j)[0], Double.parseDouble(arraySorted.get(j)[1]));
}
if (!auditOrNot) {
double avgAge = Math.ceil((totalAge / entityCount) * 100) / 100; //rounds the average age up
System.out.println("--");
System.out.printf("average age: %.2f%n", avgAge); //shows age to 2 decimal places
}
}
}