From d71ede4447eb176035e676b5ef7bf42215252fd1 Mon Sep 17 00:00:00 2001 From: Jarrett Date: Sat, 15 Oct 2022 21:53:23 -0700 Subject: [PATCH 1/2] all tests --- .../controller/SimpleDataToolController.java | 225 +++++++++++++++++- 1 file changed, 216 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataToolController.java b/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataToolController.java index fc50da3..34d34b1 100644 --- a/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataToolController.java +++ b/src/main/java/com/statefarm/codingcompetition/simpledatatool/controller/SimpleDataToolController.java @@ -31,7 +31,25 @@ public class SimpleDataToolController { * @return List of entries from CSV file */ public List readCsvFile(String filePath, Class classType) { - return null; + List 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 mi = oReader.readValues(reader); + while (mi.hasNext()) + { + entries.add(mi.next()); + } + } + catch (Exception e) + { + e.printStackTrace(); + } + return entries; } /** @@ -41,7 +59,9 @@ public List readCsvFile(String filePath, Class classType) { * @return number of open claims */ public int getNumberOfOpenClaims(List claims) { - return 0; + return (int) claims.stream() + .filter(claim -> claim.getIsClaimOpen() == true) + .count(); } /** @@ -52,7 +72,10 @@ public int getNumberOfOpenClaims(List claims) { * @return number of customer for agent */ public int getNumberOfCustomersForAgentId(String filePath, int agentId) { - return 0; + List customers = readCsvFile(filePath, Customer.class); + return (int) customers.stream() + .filter(customer -> customer.getAgentId() == agentId) + .count(); } /** @@ -63,7 +86,10 @@ public int getNumberOfCustomersForAgentId(String filePath, int agentId) { * @return number of customer for agent */ public int getNumberOfAgentsForState(String filePath, String state) { - return 0; + List agents = readCsvFile(filePath, Agent.class); + return (int) agents.stream() + .filter(agent -> agent.getState().equals(state)) + .count(); } /** @@ -74,7 +100,10 @@ public int getNumberOfAgentsForState(String filePath, String state) { * @return float of monthly premium */ public double sumMonthlyPremiumForCustomerId(List policies, int customerId) { - return 0d; + return policies.stream() + .filter(policy -> policy.getCustomerId() == customerId) + .mapToDouble(policy -> policy.getPremiumPerMonth()) + .sum(); } /** @@ -90,6 +119,31 @@ public double sumMonthlyPremiumForCustomerId(List policies, int customer */ public Integer getNumberOfOpenClaimsForCustomerName(String filePathToCustomer, String filePathToPolicy, String filePathToClaims, String firstName, String lastName) { + + /*Read Customer, Policy, Claims csv files*/ + List customers = readCsvFile(filePathToCustomer, Customer.class); + List policies = readCsvFile(filePathToPolicy, Policy.class); + List claims = readCsvFile(filePathToClaims, Claim.class); + + getNumberOfOpenClaims(claims); + + Optional customer = customers + .stream() + .filter(c -> c.getFirstName().equals(firstName) && c.getLastName().equals(lastName)) + .findFirst(); + + if (customer.isPresent()) { + List customerPolicies = policies + .stream() + .filter(policy -> policy.getCustomerId() == customer.get().getId()) + .collect(Collectors.toList()); + List customerClaims = claims + .stream() + .filter(claim -> customerPolicies.stream().anyMatch(policy -> policy.getId() == claim.getPolicyId())) + .collect(Collectors.toList()); + + return getNumberOfOpenClaims(customerClaims); + } return null; } @@ -102,7 +156,71 @@ public Integer getNumberOfOpenClaimsForCustomerName(String filePathToCustomer, S * @return String of language */ public String getMostSpokenLanguageForState(String customersFilePath, String state) { - return null; + + List customers = readCsvFile(customersFilePath, Customer.class); + Map language = new HashMap<>(); + + List 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 maxEntry = null; + for (Map.Entry entry : language.entrySet()) + { + if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) + { + maxEntry = entry; + } + } + return maxEntry.getKey(); } /** @@ -113,9 +231,47 @@ public String getMostSpokenLanguageForState(String customersFilePath, String sta * @return Customer that has the highest, total premium as Customer object */ public Customer getCustomerWithHighestTotalPremium(String customersFilePath, List policies) { + List customers = readCsvFile(customersFilePath, Customer.class); + Map 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 maxEntry = null; + for (Map.Entry 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 * @@ -127,7 +283,36 @@ public Customer getCustomerWithHighestTotalPremium(String customersFilePath, Lis */ public int getOpenClaimsForState(String customersFilePath, String policiesFilePath, String claimsFilePath, String state) { - return 0; + + List claims = readCsvFile(claimsFilePath, Claim.class); + List customers = readCsvFile(customersFilePath, Customer.class); + List policies = readCsvFile(policiesFilePath, Policy.class); + + List open_claims = claims + .stream() + .filter(claim -> claim.getIsClaimOpen()) + .collect(Collectors.toList()); + + List open_claims_policies = policies + .stream() + .filter(policy -> open_claims + .stream() + .anyMatch(claim -> claim.getPolicyId() == policy.getId())) + .collect(Collectors.toList()); + + List 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; } /** @@ -140,6 +325,28 @@ public int getOpenClaimsForState(String customersFilePath, String policiesFilePa */ public Map buildMapOfAgentPremiums( String customersFilePath, String policiesFilePath) { - return null; + + List customers = readCsvFile(customersFilePath, Customer.class); + List policies = readCsvFile(policiesFilePath, Policy.class); + + Map 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; } -} \ No newline at end of file +} From 24f3066aaa6324316b6cfbd40e9475ae9ce3b264 Mon Sep 17 00:00:00 2001 From: Jarrett Date: Sat, 15 Oct 2022 21:57:45 -0700 Subject: [PATCH 2/2] Update feedback.txt --- feedback.txt | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/feedback.txt b/feedback.txt index b931d50..dc9fe18 100644 --- a/feedback.txt +++ b/feedback.txt @@ -1,9 +1,16 @@ Your team (name of each individual participating): -How many JUnits were you able to get to pass? + +Nick Nguyen +Jarrett Devereaux + +How many JUnits were you able to get to pass? +We were able to pass all the JUnit tests. Document and describe any enhancements included to help the judges properly grade your submission. - Step 1: - Step 2: + - We used the CsvSchema, CsvMapper, and ObjectReader classes to create the readCsvFile method. + - We used streaming (A newer Java feature) in place of for loops where applicable. + - Feedback for the coding competition? Things you would like to see in future events? + - The most difficult part was getting git to cooperate.