-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTroopDamageCalculator.java
More file actions
220 lines (196 loc) · 8 KB
/
Copy pathTroopDamageCalculator.java
File metadata and controls
220 lines (196 loc) · 8 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
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.TreeSet;
import java.util.Scanner;
/**
* A class to calculate and apply the damage received by troops to the database.
* @author Nicholas Alexander Limit
*/
public class TroopDamageCalculator {
////////////////////////////////// FIELDS //////////////////////////////////
/**
* Wrapper for relationMap, divisionMap, and troopMap.
* relationMap stores the relationship map between an ID and its children
* divisionMap stores the definition map between an ID and its division name
* troopMap stores the definition map between an ID and its Troop object.
*/
private static ArmyDatabaseMap databaseMap;
/**
* Stores mapping between damage multiplier and a List of Troop IDs.
*/
private static HashMap<Integer, ArrayList<Integer>> damageMap;
//////////////////////////////// CONSTANTS /////////////////////////////////
private static int CURR_SIZE_TOKEN = 4;
private boolean debug = FALSE;
///////////////////////////////// METHODS //////////////////////////////////
public static void main(String[] args) {
if (args.length != 1) { //checks command line argument amount
System.err.println("Usage: java TroopDamageCalculator [database file]");
System.exit(1);
}
databaseMap = new ArmyDatabaseMap(args[0]); //populates the db map
damageMap = new HashMap<Integer, ArrayList<Integer>>();
promptTroopIDs();
if (debug) {
damageMap.forEach((ID, list) -> {
System.out.println(ID + ": " + list);
});
}
applyDamages();
if (debug) {
damageMap.forEach((percent, IDList) -> {
System.out.println(percent + ": ");
IDList.forEach(troopID -> {
System.out.print(" - ");
System.out.println(databaseMap.getTroopMap().get(troopID));
});
});
}
writeToDatabase(args[0]);
}
private static void promptTroopIDs() {
Scanner sc = new Scanner(System.in);
outerLoop:
while (true) {
/* selecting damage multiplier */
int percent;
while(true) {
try {
System.out.print("Input the desired damage multiplier " +
"(in percentage): ");
percent = sc.nextInt();
if (percent == 25 || percent == 50 || percent == 75 || percent == 100) {
break;
} else {
throw new Exception();
}
} catch (Exception e) {
System.out.println("Your choice is invalid! Allowed " +
"choices are 25, 50, 75, 100!");
}
}
/* listing units to receive damage */
ArrayList<Integer> damageList = new ArrayList<Integer>();
while(true) {
try {
System.out.print("Please enter troop IDs (type \"done\" after finished): ");
while (true) {
damageList.add(sc.nextInt());
}
} catch (InputMismatchException e) {
sc.nextLine();
break;
}
}
/* writes data from user to damageMap */
damageMap.put(percent, damageList);
/* prompting user to enter more or to finish */
while (true) {
System.out.print("Would you like to add more/replace troops? (y/n): ");
String ans = sc.next();
if (ans.equals("Y") || ans.equals("y")) {
break;
} else if (ans.equals("N") || ans.equals("n")) {
break outerLoop;
} else {
System.out.println("You entered an invalid response!");
}
}
}
/* reviewing user selection, then exit */
System.out.println("These are the troop IDs you entered");
damageMap.forEach((ID, list) -> {
System.out.println(ID + " percent: " + list);
});
while (true) {
System.out.println("Would you like to make changes? (y/n):");
String ans = sc.next();
if (ans.equals("Y") || ans.equals("y")) {
promptTroopIDs();
break;
} else if (ans.equals("N") || ans.equals("n")) {
sc.close();
return;
} else {
System.out.println("You entered an invalid response!");
}
}
}
private static void applyDamages() {
TreeSet<Integer> errorSet = new TreeSet<Integer>();
damageMap.forEach((percent, IDList) -> {
ArrayList<Integer> errorIndexList = new ArrayList<Integer>();
double multiplier = 1 - ((double)percent/100);
/* attempts to apply damage to Troop units */
for(int i=0; i<IDList.size(); i++) {
int troopID = IDList.get(i);
Troop troop = databaseMap.getTroopMap().get(troopID);
if (troop != null) { //if ID exists
troop.setCurrSize((int)(troop.getCurrSize()*multiplier));
} else { //if ID doesn't exist
errorSet.add(troopID);
errorIndexList.add(i);
}
}
/* removes non-existent IDs from damageMap */
for (int i=errorIndexList.size()-1; i>=0; i--) {
int index = errorIndexList.get(i);
IDList.remove(index);
}
});
/* warns user about nonexistent IDs */
if (errorSet.size() > 0) {
System.out.println("Warning! The following IDs were not found!");
for (int errorID : errorSet) {
System.out.println(errorID);
}
System.out.println();
}
}
private static void writeToDatabase(String filename) {
try{
/* puts database file into memory */
File database = new File(filename);
/* reads all lines */
ArrayList<String> lines = new ArrayList<String>();
Scanner lineReader = new Scanner(database);
while(lineReader.hasNext()) {
lines.add(lineReader.nextLine());
}
lineReader.close();
damageMap.forEach((percent, IDList) -> {
IDList.forEach(troopID -> {
/* tokenizes desired line */
ArrayList<String> tokens = new ArrayList<String>();
Scanner tokenizer = new Scanner(lines.get(troopID)).useDelimiter("\\|");
while (tokenizer.hasNext()) {
tokens.add(tokenizer.next());
}
tokenizer.close();
/* updates current size token */
int currSize = databaseMap.getTroopMap().get(troopID).getCurrSize();
tokens.set(CURR_SIZE_TOKEN, String.format(" %03d ", currSize));
/* rebuilds line */
StringBuilder newLine = new StringBuilder();
for (String token : tokens) {
newLine.append(token);
newLine.append("|");
}
newLine.deleteCharAt(newLine.length()-1);
/* updates line */
lines.set(troopID, newLine.toString());
});
});
/* writes back into file */
PrintWriter pw = new PrintWriter(database);
lines.forEach(line -> {
pw.println(line);
});
pw.close();
} catch (IOException e) {}
}
}