+
+
\ No newline at end of file
diff --git a/java/Round1Java.iml b/java/Round1Java.iml
new file mode 100644
index 0000000..d714d9d
--- /dev/null
+++ b/java/Round1Java.iml
@@ -0,0 +1,19 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataTool.java b/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataTool.java
index 046dd56..3f6f704 100644
--- a/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataTool.java
+++ b/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataTool.java
@@ -1,7 +1,8 @@
package com.statefarm.codingcompetition.simpledatatool.controller;
-import java.util.List;
-import java.util.Map;
+import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.util.*;
import com.statefarm.codingcompetition.simpledatatool.io.JsonHelper;
import com.statefarm.codingcompetition.simpledatatool.model.Agent;
@@ -58,28 +59,40 @@ public List getDisasters() {
* @return number of closed claims
*/
public int getNumClosedClaims() {
- return 0;
+ int numClosedClaims = 0;
+ for(Claim claim : claims)
+ if("Closed".equals(claim.getStatus()))
+ numClosedClaims++;
+ return numClosedClaims;
}
/**
* Calculates the number of claims assigned to a specific claim handler
- *
+ *
* @param id id of claim handler
* @return number of claims assigned to claim handler
*/
public int getNumClaimsForClaimHandlerId(int id) {
- return 0;
+ int numOfClaims=0;
+ for(Claim claim : claims)
+ if(id == claim.getClaim_handler_assigned_id())
+ numOfClaims++;
+ return numOfClaims;
}
/**
* Calculates the number of disasters for a specific state
- *
+ *
* @param stateName name of a state in the United States of America,
* including the District of Columbia
* @return number of disasters for state
*/
public int getNumDisastersForState(String stateName) {
- return -1;
+ int disastersOfState = 0;
+ for(Disaster disaster : disasters)
+ if(stateName.equals(disaster.getState()))
+ disastersOfState++;
+ return disastersOfState;
}
// endregion
@@ -88,76 +101,144 @@ public int getNumDisastersForState(String stateName) {
/**
* Sums the estimated cost of a specific disaster by its claims
- *
+ *
* @param id id of disaster
* @return estimate cost of disaster, rounded to the nearest hundredths place
* returns null if no claims are found
*/
- public Float getTotalClaimCostForDisaster(int id) {
- return -0.01f;
+ public Double getTotalClaimCostForDisaster(int id) {
+ double sumOfCost = 0;
+ for(Claim claim : claims) {
+ if (id == claim.getDisaster_id())
+ sumOfCost += claim.getEstimate_cost();
+ }
+
+ if(sumOfCost == 0)
+ return null;
+ return sumOfCost;
}
-
/**
* Gets the average estimated cost of all claims assigned to a claim handler
- *
+ *
* @param id id of claim handler
* @return average cost of claims, rounded to the nearest hundredths place,
* or null if no claims are found
*/
public Float getAverageClaimCostforClaimHandler(int id) {
- return -0.01f;
+ float sumOfAllClaims = 0;
+ float avg = 0;
+ int allClaims = getNumClaimsForClaimHandlerId(id);
+ if(allClaims == 0)
+ return null;
+ for(Claim claim : claims)
+ if(id == claim.getClaim_handler_assigned_id())
+ sumOfAllClaims+=claim.getEstimate_cost();
+ avg = sumOfAllClaims/allClaims;
+ avg = Math.round(avg*100) / 100.0f;
+ return avg;
}
/**
* Returns the name of the state with the most disasters based on disaster data
- *
+ *
* If two states have the same number of disasters, then sort by alphabetical
* (a-z) and take the first.
- *
+ *
* Example: Say New Jersey and Delaware both have the highest number of
* disasters at 12 disasters each. Then, this method would return "Delaware"
* since "D"comes before "N" in the alphabet.
- *
+ *
* @return single name of state
*/
public String getStateWithTheMostDisasters() {
- return null;
+ Map stateDisasterCount = new HashMap<>();
+ for(Disaster disaster : disasters) {
+ String state = disaster.getState();
+ stateDisasterCount.put(state,stateDisasterCount.getOrDefault(state,0)+1);
+ }
+ int max = 0;
+ List equalStates = new ArrayList<>();
+ for(String state :stateDisasterCount.keySet()) {
+ int disasterCount = stateDisasterCount.get(state);
+ if(disasterCount > max) {
+ max = disasterCount;
+ equalStates.clear();
+ equalStates.add(state);
+ } else if (disasterCount == max)
+ equalStates.add(state);
+ }
+ Collections.sort(equalStates);
+ if(!equalStates.isEmpty())
+ return equalStates.get(0);
+ else return null;
}
/**
* Returns the name of the state with the least disasters based on disaster data
- *
+ *
* If two states have the same number of disasters, then sort by alphabetical
* (a-z) and take the first.
- *
+ *
* Example: Say New Mexico and West Virginia both have the least number of
* disasters at 1 disaster each. Then, this method would return "New Mexico"
* since "N" comes before "W" in the alphabet.
- *
+ *
* @return single name of state
*/
public String getStateWithTheLeastDisasters() {
- return null;
+ Map stateDisasterCount = new HashMap<>();
+ for(Disaster disaster : disasters) {
+ String state = disaster.getState();
+ stateDisasterCount.put(state,stateDisasterCount.getOrDefault(state,0)+1);
+ }
+ int min = Integer.MAX_VALUE;
+ List equalStates = new ArrayList<>();
+ for(String state :stateDisasterCount.keySet()) {
+ int disasterCount = stateDisasterCount.get(state);
+ if(disasterCount < min) {
+ min = disasterCount;
+ equalStates.clear();
+ equalStates.add(state);
+ } else if (disasterCount == min)
+ equalStates.add(state);
+ }
+ Collections.sort(equalStates);
+ if(!equalStates.isEmpty())
+ return equalStates.get(0);
+ else return null;
}
/**
* Returns the name of the most spoken language by agents (besides English) for
* a specific state
- *
+ *
* @param string name of state
* @return name of language
* or empty string if state doesn't exist
*/
public String getMostSpokenAgentLanguageByState(String string) {
- return null;
+ Map biLingual = new HashMap<>();
+ for(Agent agent : agents) {
+ if(string.equals(agent.getState()))
+ biLingual.put(agent.getSecondary_language(), biLingual.getOrDefault(agent.getSecondary_language(), 0)+1);
+ }
+ int max = 0;
+ String popLanguage = "";
+ for(String language :biLingual.keySet()) {
+ int popular = biLingual.get(language);
+ if (popular > max)
+ max = popular;
+ popLanguage = language;
+ }
+ return popLanguage;
}
/**
* Returns the number of open claims for a specific agent and for a minimum
* severity level and higher
- *
+ *
* Note: Severity rating scale for claims is 1 to 10, inclusive.
- *
+ *
* @param agentId id of agent
* @param minSeverityRating minimum claim severity rating
* @return number of claims that are not closed and have minimum severity rating
@@ -166,7 +247,19 @@ public String getMostSpokenAgentLanguageByState(String string) {
* null if agent does not exist, or agent has no claims (open or not)
*/
public Integer getNumOfOpenClaimsForAgentAndSeverity(int agentId, int minSeverityRating) {
- return -2;
+ int openClaims = 0;
+ if(minSeverityRating < 1 || minSeverityRating > 10)
+ return -1;
+ for(Claim claim : claims) {
+ if (agentId == claim.getAgent_assigned_id() && minSeverityRating <= claim.getSeverity_rating()) {
+ if (!claim.getStatus().equals("Closed"))
+ openClaims++;
+ }
+ }
+ if(openClaims > 0)
+ return openClaims;
+ else
+ return null;
}
// endregion
@@ -179,7 +272,14 @@ public Integer getNumOfOpenClaimsForAgentAndSeverity(int agentId, int minSeverit
* @return number of disasters where the declared date is after the end date
*/
public int getNumDisastersDeclaredAfterEndDate() {
- return -1;
+ int count = 0;
+ for(Disaster disaster : disasters) {
+ LocalDate declared = disaster.getDeclared_date();
+ LocalDate end = disaster.getEnd_date();
+ if(declared.isAfter(end))
+ count++;
+ }
+ return count;
}
/**
@@ -193,12 +293,19 @@ public int getNumDisastersDeclaredAfterEndDate() {
* @return Map where key is agent id, value is total cost of claims associated
* to the agent
*/
- public Map buildMapOfAgentsToTotalClaimCost() {
- return null;
+ public Map buildMapOfAgentsToTotalClaimCost() {
+ Map agentsToCost = new HashMap<>();
+ for(Claim claim : claims) {
+ if (!agentsToCost.containsKey(claim.getAgent_assigned_id()))
+ agentsToCost.put(claim.getAgent_assigned_id(), claim.getEstimate_cost());
+ else if (agentsToCost.containsKey(claim.getAgent_assigned_id()))
+ agentsToCost.replace(claim.getAgent_assigned_id(), agentsToCost.getOrDefault(claim.getAgent_assigned_id(), Double.valueOf(0)) + claim.getEstimate_cost());
+ }
+ return agentsToCost;
}
/**
- * Calculates density of a diaster based on the number of claims and impact
+ * Calculates density of a disaster based on the number of claims and impact
* radius
*
* Hints:
diff --git a/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/model/Claim.java b/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/model/Claim.java
index db223d9..4508406 100644
--- a/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/model/Claim.java
+++ b/java/src/main/java/com/statefarm/codingcompetition/simpledatatool/model/Claim.java
@@ -7,7 +7,7 @@ public class Claim {
private static final Gson GSON = new Gson();
private int id, disaster_id, severity_rating, agent_assigned_id, claim_handler_assigned_id;
- private float estimate_cost;
+ private double estimate_cost;
private boolean total_loss, loss_of_life;
private String status, type;
@@ -51,7 +51,7 @@ public void setClaim_handler_assigned_id(int claim_handler_assigned_id) {
this.claim_handler_assigned_id = claim_handler_assigned_id;
}
- public float getEstimate_cost() {
+ public double getEstimate_cost() {
return this.estimate_cost;
}
diff --git a/java/src/test/java/com/statefarm/codingcompetition/simpledatatool/TestSet3.java b/java/src/test/java/com/statefarm/codingcompetition/simpledatatool/TestSet3.java
index 8dbf87b..8fa548f 100644
--- a/java/src/test/java/com/statefarm/codingcompetition/simpledatatool/TestSet3.java
+++ b/java/src/test/java/com/statefarm/codingcompetition/simpledatatool/TestSet3.java
@@ -31,7 +31,7 @@ public void test9_getNumDisastersDeclaredAfterEndDate() {
@Test
public void test10_buildMapOfAgentsToTotalClaimCost() {
- Map agentCostMap = controller.buildMapOfAgentsToTotalClaimCost();
+ Map agentCostMap = controller.buildMapOfAgentsToTotalClaimCost();
assertEquals(100, agentCostMap.size());