diff --git a/feedback.txt b/feedback.txt index b931d50..7d4b5fe 100644 --- a/feedback.txt +++ b/feedback.txt @@ -1,5 +1,5 @@ -Your team (name of each individual participating): -How many JUnits were you able to get to pass? +Your team (name of each individual participating): Farid Jafri +How many JUnits were you able to get to pass? 9 Document and describe any enhancements included to help the judges properly grade your submission. Step 1: @@ -7,3 +7,4 @@ Document and describe any enhancements included to help the judges properly grad Feedback for the coding competition? Things you would like to see in future events? +I think there was an incorrect test case named: getCustomersRetainedForYearsByPlcyCostAsc2. I verified the expected results manually by filtering the csv. \ No newline at end of file diff --git a/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java b/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java index 58267da..eae9837 100644 --- a/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java +++ b/src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java @@ -3,11 +3,14 @@ import java.io.FileReader; import java.io.Reader; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Collector; import java.util.stream.Collectors; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -30,7 +33,20 @@ public class CodingCompCsvUtil { * @return -- List of entries being returned. */ public List readCsvFile(String filePath, Class classType) { - + CsvMapper csvMapper = new CsvMapper(); + csvMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); + CsvSchema schema = CsvSchema.emptySchema().withHeader(); + ObjectReader oReader = csvMapper.reader(classType).with(schema); + List objects = new ArrayList<>(); + try (Reader reader = new FileReader(filePath)) { + MappingIterator mi = oReader.readValues(reader); + while (mi.hasNext()) { + objects.add(mi.next()); + } + } catch (Exception e) { + System.out.println(e); + } + return objects; } @@ -40,8 +56,9 @@ public List readCsvFile(String filePath, Class classType) { * @param area -- The area from which the agents should be counted. * @return -- The number of agents in a given area */ - public int getAgentCountInArea(String filePath,String area) { - + public int getAgentCountInArea(String filePath, String area) { + List agents = readCsvFile(filePath, Agent.class); + return agents.stream().filter(agent -> agent.getArea().equals(area)).collect(Collectors.toList()).size(); } @@ -53,7 +70,9 @@ public int getAgentCountInArea(String filePath,String area) { * @return -- The number of agents in a given area */ public List getAgentsInAreaThatSpeakLanguage(String filePath, String area, String language) { - + List agents = readCsvFile(filePath, Agent.class); + return agents.stream().filter(agent -> agent.getArea().equals(area) && agent.getLanguage().equals(language)) + .collect(Collectors.toList()); } @@ -65,8 +84,31 @@ public List getAgentsInAreaThatSpeakLanguage(String filePath, String area * @param agentLastName -- Last name of agent. * @return -- The number of customers that use a certain agent in a given area. */ - public short countCustomersFromAreaThatUseAgent(Map csvFilePaths, String customerArea, String agentFirstName, String agentLastName) { - + public short countCustomersFromAreaThatUseAgent(Map csvFilePaths, String customerArea, + String agentFirstName, String agentLastName) { + AtomicInteger i = new AtomicInteger(); + List agents = readCsvFile(csvFilePaths.get("agentList"), Agent.class); + Agent agent1 = agents.stream().filter( + agent -> agent.getFirstName().equals(agentFirstName) && agent.getLastName().equals(agentLastName)) + .findFirst().get(); + List customers = readCsvFile(csvFilePaths.get("customerList"), Customer.class); + return (short) customers.stream().filter( + customer -> customer.getArea().equals(customerArea) && agent1.getAgentId() == customer.getAgentId()) + .collect(Collectors.toList()).size(); +// csvFilePaths.forEach((individual, filePath) -> { +// if (individual.equals("agentList")) { +// List allAgents = readCsvFile(filePath, Agent.class); +// agents.addAll(allAgents.stream().filter(agent -> agent.getFirstName().equals(agentFirstName) +// && agent.getLastName().equals(agentLastName)).collect(Collectors.toList())); +// System.out.println("agent found:"); +// System.out.println(agents.get(0).getFirstName()); +// } else { +// List allCustomers = readCsvFile(filePath, Customer.class); +// i.set(allCustomers.stream().filter(customer -> agents.get(0).getAgentId() == customer.getAgentId()).collect(Collectors.toList()) +// .size()); +// } +// }); + } @@ -77,7 +119,10 @@ public short countCustomersFromAreaThatUseAgent(Map csvFilePaths, * @return -- List of customers retained for a given number of years, in ascending order of policy cost. */ public List getCustomersRetainedForYearsByPlcyCostAsc(String customerFilePath, short yearsOfService) { - + List customers = readCsvFile(customerFilePath, Customer.class); + return customers.stream().filter(customer -> (short) customer.getYearsOfService() == yearsOfService) + .sorted((cus1, cus2) -> cus1.getTotalMonthlyPremium().compareTo(cus2.getTotalMonthlyPremium())) + .collect(Collectors.toList()); } @@ -88,7 +133,10 @@ public List getCustomersRetainedForYearsByPlcyCostAsc(String customerF * @return -- List of customers who’ve made an inquiry for a policy but have not signed up. */ public List getLeadsForInsurance(String filePath) { - + List customers = readCsvFile(filePath, Customer.class); + return customers.stream() + .filter(customer -> !customer.isHomePolicy() && !customer.isAutoPolicy() && !customer.isRentersPolicy()) + .collect(Collectors.toList()); } @@ -102,8 +150,18 @@ b. Whether that vendor is in scope of the insurance (if inScope == false, return * @param vendorRating -- The rating of the vendor. * @return -- List of vendors within a given area, filtered by scope and vendor rating. */ - public List getVendorsWithGivenRatingThatAreInScope(String filePath, String area, boolean inScope, int vendorRating) { - + public List getVendorsWithGivenRatingThatAreInScope(String filePath, String area, boolean inScope, + int vendorRating) { + List vendors = readCsvFile(filePath, Vendor.class); + List vendors1 = vendors.stream().filter(vendor -> vendor.getArea().equals(area)) + .collect(Collectors.toList()); + List vendors2 = vendors1.stream() + .filter(vendor -> vendor.getVendorRating() == vendorRating) + .collect(Collectors.toList()); + if (!inScope) { + return vendors2; + } + return vendors2.stream().filter(vendor -> vendor.isInScope()).collect(Collectors.toList()); } @@ -117,7 +175,12 @@ public List getVendorsWithGivenRatingThatAreInScope(String filePath, Str * @return -- List of customers filtered by age, number of vehicles insured and the number of dependents. */ public List getUndisclosedDrivers(String filePath, int vehiclesInsured, int dependents) { - + List customers = readCsvFile(filePath, Customer.class); + return customers.stream() + .filter(customer -> customer.getAge() >= 40 && customer.getAge() <= 50 + && customer.getVehiclesInsured() > vehiclesInsured + && customer.getDependents().size() <= dependents) + .collect(Collectors.toList()); } @@ -130,7 +193,14 @@ public List getUndisclosedDrivers(String filePath, int vehiclesInsured * @return -- Agent ID of agent with the given rank. */ public int getAgentIdGivenRank(String filePath, int agentRank) { - + List customers = readCsvFile(filePath, Customer.class); + Map agentWithRating = customers.stream().collect( + Collectors.groupingBy(Customer::getAgentId, Collectors.averagingInt(Customer::getAgentRating))); + List> list = new ArrayList<>(agentWithRating.entrySet()); + list.sort(Entry.comparingByValue()); + Collections.reverse(list); + System.out.println(list); + return list.get(agentRank - 1).getKey(); } @@ -140,8 +210,13 @@ public int getAgentIdGivenRank(String filePath, int agentRank) { * @param monthsOpen -- Number of months a policy has been open. * @return -- List of customers who’ve filed a claim within the last . */ - public List getCustomersWithClaims(Map csvFilePaths, short monthsOpen) { - + public List getCustomersWithClaims(Map csvFilePaths, short monthsOpen) { + List customers = readCsvFile(csvFilePaths.get("customerList"), Customer.class); + List claims = readCsvFile(csvFilePaths.get("claimList"), Claim.class); + Set customerIds = claims.stream().filter(claim -> (short) claim.getMonthsOpen() <= monthsOpen) + .map(Claim::getCustomerId).collect(Collectors.toSet()); + return customers.stream().filter(customer -> customerIds.contains(customer.getCustomerId())) + .collect(Collectors.toList()); } } diff --git a/src/main/java/sf/codingcompetition2020/structures/Agent.java b/src/main/java/sf/codingcompetition2020/structures/Agent.java index e2e6f93..01bad39 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Agent.java +++ b/src/main/java/sf/codingcompetition2020/structures/Agent.java @@ -1,11 +1,51 @@ package sf.codingcompetition2020.structures; public class Agent { - + private int agentId; private String area; private String language; private String firstName; private String lastName; - + + public int getAgentId() { + return agentId; + } + + public void setAgentId(int agentId) { + this.agentId = agentId; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + } diff --git a/src/main/java/sf/codingcompetition2020/structures/Claim.java b/src/main/java/sf/codingcompetition2020/structures/Claim.java index 581140a..aed23ca 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Claim.java +++ b/src/main/java/sf/codingcompetition2020/structures/Claim.java @@ -5,5 +5,37 @@ public class Claim { private int customerId; private boolean closed; private int monthsOpen; - + + public int getClaimId() { + return claimId; + } + + public void setClaimId(int claimId) { + this.claimId = claimId; + } + + public int getCustomerId() { + return customerId; + } + + public void setCustomerId(int customerId) { + this.customerId = customerId; + } + + public boolean isClosed() { + return closed; + } + + public void setClosed(boolean closed) { + this.closed = closed; + } + + public int getMonthsOpen() { + return monthsOpen; + } + + public void setMonthsOpen(int monthsOpen) { + this.monthsOpen = monthsOpen; + } + } diff --git a/src/main/java/sf/codingcompetition2020/structures/Customer.java b/src/main/java/sf/codingcompetition2020/structures/Customer.java index f151906..9be901a 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Customer.java +++ b/src/main/java/sf/codingcompetition2020/structures/Customer.java @@ -3,7 +3,10 @@ import java.util.ArrayList; import java.util.List; +import com.fasterxml.jackson.core.JsonParser.Feature; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -24,4 +27,134 @@ public class Customer { private short yearsOfService; private Integer vehiclesInsured; + public int getCustomerId() { + return customerId; + } + + public void setCustomerId(int customerId) { + this.customerId = customerId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public int getAgentId() { + return agentId; + } + + public void setAgentId(int agentId) { + this.agentId = agentId; + } + + public short getAgentRating() { + return agentRating; + } + + public void setAgentRating(short agentRating) { + this.agentRating = agentRating; + } + + public String getPrimaryLanguage() { + return primaryLanguage; + } + + public void setPrimaryLanguage(String primaryLanguage) { + this.primaryLanguage = primaryLanguage; + } + + public List getDependents() { + return dependents; + } + + public void setDependents(String dependents) { + try { + this.dependents = new ObjectMapper().configure(Feature.AUTO_CLOSE_SOURCE, true).readValue(dependents, + new TypeReference>() { + }); + } catch (JsonMappingException e) { + // TODO Auto-generated catch block + this.dependents = new ArrayList<>(); + } catch (JsonProcessingException e) { + // TODO Auto-generated catch block + this.dependents = new ArrayList<>(); + } + } + + public boolean isHomePolicy() { + return homePolicy; + } + + public void setHomePolicy(boolean homePolicy) { + this.homePolicy = homePolicy; + } + + public boolean isAutoPolicy() { + return autoPolicy; + } + + public void setAutoPolicy(boolean autoPolicy) { + this.autoPolicy = autoPolicy; + } + + public boolean isRentersPolicy() { + return rentersPolicy; + } + + public void setRentersPolicy(boolean rentersPolicy) { + this.rentersPolicy = rentersPolicy; + } + + public String getTotalMonthlyPremium() { + return totalMonthlyPremium; + } + + public void setTotalMonthlyPremium(String totalMonthlyPremium) { + this.totalMonthlyPremium = totalMonthlyPremium; + } + + public short getYearsOfService() { + return yearsOfService; + } + + public void setYearsOfService(short yearsOfService) { + this.yearsOfService = yearsOfService; + } + + public Integer getVehiclesInsured() { + return vehiclesInsured; + } + + public void setVehiclesInsured(Integer vehiclesInsured) { + this.vehiclesInsured = vehiclesInsured; + } + } diff --git a/src/main/java/sf/codingcompetition2020/structures/Dependent.java b/src/main/java/sf/codingcompetition2020/structures/Dependent.java index d4deb1a..e5d7cb6 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Dependent.java +++ b/src/main/java/sf/codingcompetition2020/structures/Dependent.java @@ -4,4 +4,20 @@ public class Dependent { private String firstName; private String lastName; + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + } diff --git a/src/main/java/sf/codingcompetition2020/structures/Vendor.java b/src/main/java/sf/codingcompetition2020/structures/Vendor.java index 6b6fb76..31314e3 100644 --- a/src/main/java/sf/codingcompetition2020/structures/Vendor.java +++ b/src/main/java/sf/codingcompetition2020/structures/Vendor.java @@ -5,5 +5,37 @@ public class Vendor { private String area; private int vendorRating; private boolean inScope; - + + public int getVendorId() { + return vendorId; + } + + public void setVendorId(int vendorId) { + this.vendorId = vendorId; + } + + public String getArea() { + return area; + } + + public void setArea(String area) { + this.area = area; + } + + public int getVendorRating() { + return vendorRating; + } + + public void setVendorRating(int vendorRating) { + this.vendorRating = vendorRating; + } + + public boolean isInScope() { + return inScope; + } + + public void setInScope(boolean inScope) { + this.inScope = inScope; + } + }