This repository was archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSimpleDataToolController.java
More file actions
352 lines (318 loc) · 12.8 KB
/
SimpleDataToolController.java
File metadata and controls
352 lines (318 loc) · 12.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package com.statefarm.codingcompetition.simpledatatool.controller;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import com.statefarm.codingcompetition.simpledatatool.model.Agent;
import com.statefarm.codingcompetition.simpledatatool.model.Claim;
import com.statefarm.codingcompetition.simpledatatool.model.Customer;
import com.statefarm.codingcompetition.simpledatatool.model.Policy;
public class SimpleDataToolController {
/**
* Read in a CSV file and return a list of entries in that file
*
* @param <T>
* @param filePath Path to the file being read in
* @param classType Class of entries beng read in
* @return List of entries from CSV file
*/
public <T> List<T> readCsvFile(String filePath, Class<T> classType) {
List<T> entries = new ArrayList<>();
try
{
CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader();
CsvMapper mapper = new CsvMapper();
mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
ObjectReader oReader = mapper.readerFor(classType).with(bootstrapSchema);
Reader reader = new FileReader(filePath);
MappingIterator<T> mi = oReader.readValues(reader);
while (mi.hasNext())
{
entries.add(mi.next());
}
}
catch (Exception e)
{
e.printStackTrace();
}
return entries;
}
/**
* Gets the number of open claims
*
* @param claims List of all claims
* @return number of open claims
*/
public int getNumberOfOpenClaims(List<Claim> claims) {
return (int) claims.stream()
.filter(claim -> claim.getIsClaimOpen() == true)
.count();
}
/**
* Get the number of customer for an agent id
*
* @param filePath File path to the customers CSV
* @param agentId Agent id as int
* @return number of customer for agent
*/
public int getNumberOfCustomersForAgentId(String filePath, int agentId) {
List<Customer> customers = readCsvFile(filePath, Customer.class);
return (int) customers.stream()
.filter(customer -> customer.getAgentId() == agentId)
.count();
}
/**
* Get the number of customer for an agent id
*
* @param filePath File path to the customers CSV
* @param state Agent id as int
* @return number of customer for agent
*/
public int getNumberOfAgentsForState(String filePath, String state) {
List<Agent> agents = readCsvFile(filePath, Agent.class);
return (int) agents.stream()
.filter(agent -> agent.getState().equals(state))
.count();
}
/**
* Sum total premium for a specific customer id
*
* @param policies List of all policies
* @param customerId Customer id as int
* @return float of monthly premium
*/
public double sumMonthlyPremiumForCustomerId(List<Policy> policies, int customerId) {
return policies.stream()
.filter(policy -> policy.getCustomerId() == customerId)
.mapToDouble(policy -> policy.getPremiumPerMonth())
.sum();
}
/**
* For a given customer (by first and last names), return the number of open
* claims they have
*
* @param filePathToCustomer File path to customers CSV
* @param filePathToPolicy File path to policies CSV
* @param filePathToClaims File path to claims CSV
* @param firstName First name of customer to search for
* @param lastName Last name of customer to search for
* @return Number of open claims for customer or null if customer doesn't exist
*/
public Integer getNumberOfOpenClaimsForCustomerName(String filePathToCustomer, String filePathToPolicy,
String filePathToClaims, String firstName, String lastName) {
/*Read Customer, Policy, Claims csv files*/
List<Customer> customers = readCsvFile(filePathToCustomer, Customer.class);
List<Policy> policies = readCsvFile(filePathToPolicy, Policy.class);
List<Claim> claims = readCsvFile(filePathToClaims, Claim.class);
getNumberOfOpenClaims(claims);
Optional<Customer> customer = customers
.stream()
.filter(c -> c.getFirstName().equals(firstName) && c.getLastName().equals(lastName))
.findFirst();
if (customer.isPresent()) {
List<Policy> customerPolicies = policies
.stream()
.filter(policy -> policy.getCustomerId() == customer.get().getId())
.collect(Collectors.toList());
List<Claim> customerClaims = claims
.stream()
.filter(claim -> customerPolicies.stream().anyMatch(policy -> policy.getId() == claim.getPolicyId()))
.collect(Collectors.toList());
return getNumberOfOpenClaims(customerClaims);
}
return null;
}
/**
* Returns the most spoken language (besides English) for customers in a given
* state
*
* @param customersFilePath File path to customers CSV
* @param state State abbreviation ex: AZ, TX, IL, etc.
* @return String of language
*/
public String getMostSpokenLanguageForState(String customersFilePath, String state) {
List<Customer> customers = readCsvFile(customersFilePath, Customer.class);
Map<String,Integer> language = new HashMap<>();
List<Customer> customers_in_state = customers
.stream()
.filter(customer -> customer.getState().equals(state))
.collect(Collectors.toList());
for (Customer customer : customers_in_state)
{
if (!customer.getPrimaryLanguage().equals("English"))
{
if (language.containsKey(customer.getPrimaryLanguage()))
{
language.put
(
customer.getPrimaryLanguage(),
language.get(customer.getPrimaryLanguage())+1
);
}
else
{
language.put
(
customer.getPrimaryLanguage(),
1
);
}
}
if (!customer.getSecondaryLanguage().equals(""))
{
if (!customer.getSecondaryLanguage().equals("English"))
{
if (language.containsKey(customer.getSecondaryLanguage()))
{
language.put
(
customer.getSecondaryLanguage(),
language.get(customer.getSecondaryLanguage())+1
);
}
else
{
language.put
(
customer.getSecondaryLanguage(),
1
);
}
}
}
}
Map.Entry<String, Integer> maxEntry = null;
for (Map.Entry<String, Integer> entry : language.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
return maxEntry.getKey();
}
/**
* Returns Customer with the highest, total premium
*
* @param customersFilePath File path to customers CSV
* @param policies List of all policies
* @return Customer that has the highest, total premium as Customer object
*/
public Customer getCustomerWithHighestTotalPremium(String customersFilePath, List<Policy> policies) {
List<Customer> customers = readCsvFile(customersFilePath, Customer.class);
Map<Integer, Double> customerPremiums = new HashMap<>();
for (Customer customer : customers) {
if (customerPremiums.containsKey(customer.getId()))
{
customerPremiums.put
(
customer.getId(),
customerPremiums.get(customer.getId()) + sumMonthlyPremiumForCustomerId(policies, customer.getId())
);
}
else
{
customerPremiums.put
(
customer.getId(),
sumMonthlyPremiumForCustomerId(policies, customer.getId())
);
}
}
Map.Entry<Integer, Double> maxEntry = null;
for (Map.Entry<Integer, Double> entry : customerPremiums.entrySet())
{
if (maxEntry == null ||
entry.getValue() > (maxEntry.getValue()))
{
maxEntry = entry;
}
}
for (Customer customer : customers)
{
if (customer.getId() == maxEntry.getKey())
{
return customer;
}
}
return null;
}
/**
* Returns the total number of open claims for a given state
*
* @param customersFilePath File path to customers CSV
* @param policiesFilePath File path to policies CSV
* @param claimsFilePath File path to claims CSV
* @param state State abbreviation ex: AZ, TX, IL, etc.
* @return number of open claims as int
*/
public int getOpenClaimsForState(String customersFilePath, String policiesFilePath, String claimsFilePath,
String state) {
List <Claim> claims = readCsvFile(claimsFilePath, Claim.class);
List <Customer> customers = readCsvFile(customersFilePath, Customer.class);
List <Policy> policies = readCsvFile(policiesFilePath, Policy.class);
List <Claim> open_claims = claims
.stream()
.filter(claim -> claim.getIsClaimOpen())
.collect(Collectors.toList());
List <Policy> open_claims_policies = policies
.stream()
.filter(policy -> open_claims
.stream()
.anyMatch(claim -> claim.getPolicyId() == policy.getId()))
.collect(Collectors.toList());
List <Customer> open_claims_customers = customers
.stream()
.filter(customer -> open_claims_policies
.stream()
.anyMatch(policy -> policy.getCustomerId() == customer.getId()))
.collect(Collectors.toList());
int open_claims_state = (int) open_claims_customers
.stream()
.filter(customer -> customer.getState().equals(state))
.count();
return open_claims_state;
}
/**
* Builds a dictionary/map of agents and their total premium from their
* customers
*
* @param customersFilePath File path to customers CSV
* @param policiesFilePath File path to policies CSV
* @return Map of agent id as int to agent's total premium as double
*/
public Map<Integer, Double> buildMapOfAgentPremiums(
String customersFilePath, String policiesFilePath) {
List<Customer> customers = readCsvFile(customersFilePath, Customer.class);
List<Policy> policies = readCsvFile(policiesFilePath, Policy.class);
Map<Integer,Double> agent = new HashMap<>();
for (Customer customer : customers)
{
if (agent.containsKey(customer.getAgentId()))
{
agent.put
(
customer.getAgentId(),
agent.get(customer.getAgentId())+sumMonthlyPremiumForCustomerId(policies,customer.getId())
);
} else {
agent.put
(
customer.getAgentId(),
sumMonthlyPremiumForCustomerId(policies,customer.getId())
);
}
}
return agent;
}
}