Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions feedback.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
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:
Step 2:


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.
103 changes: 89 additions & 14 deletions src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,7 +33,20 @@ public class CodingCompCsvUtil {
* @return -- List of entries being returned.
*/
public <T> List<T> readCsvFile(String filePath, Class<T> 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<T> objects = new ArrayList<>();
try (Reader reader = new FileReader(filePath)) {
MappingIterator<T> mi = oReader.readValues(reader);
while (mi.hasNext()) {
objects.add(mi.next());
}
} catch (Exception e) {
System.out.println(e);
}
return objects;
}


Expand All @@ -40,8 +56,9 @@ public <T> List<T> readCsvFile(String filePath, Class<T> 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<Agent> agents = readCsvFile(filePath, Agent.class);
return agents.stream().filter(agent -> agent.getArea().equals(area)).collect(Collectors.toList()).size();
}


Expand All @@ -53,7 +70,9 @@ public int getAgentCountInArea(String filePath,String area) {
* @return -- The number of agents in a given area
*/
public List<Agent> getAgentsInAreaThatSpeakLanguage(String filePath, String area, String language) {

List<Agent> agents = readCsvFile(filePath, Agent.class);
return agents.stream().filter(agent -> agent.getArea().equals(area) && agent.getLanguage().equals(language))
.collect(Collectors.toList());
}


Expand All @@ -65,8 +84,31 @@ public List<Agent> 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<String,String> csvFilePaths, String customerArea, String agentFirstName, String agentLastName) {

public short countCustomersFromAreaThatUseAgent(Map<String, String> csvFilePaths, String customerArea,
String agentFirstName, String agentLastName) {
AtomicInteger i = new AtomicInteger();
List<Agent> agents = readCsvFile(csvFilePaths.get("agentList"), Agent.class);
Agent agent1 = agents.stream().filter(
agent -> agent.getFirstName().equals(agentFirstName) && agent.getLastName().equals(agentLastName))
.findFirst().get();
List<Customer> 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<Agent> 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<Customer> allCustomers = readCsvFile(filePath, Customer.class);
// i.set(allCustomers.stream().filter(customer -> agents.get(0).getAgentId() == customer.getAgentId()).collect(Collectors.toList())
// .size());
// }
// });

}


Expand All @@ -77,7 +119,10 @@ public short countCustomersFromAreaThatUseAgent(Map<String,String> csvFilePaths,
* @return -- List of customers retained for a given number of years, in ascending order of policy cost.
*/
public List<Customer> getCustomersRetainedForYearsByPlcyCostAsc(String customerFilePath, short yearsOfService) {

List<Customer> 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());
}


Expand All @@ -88,7 +133,10 @@ public List<Customer> getCustomersRetainedForYearsByPlcyCostAsc(String customerF
* @return -- List of customers who’ve made an inquiry for a policy but have not signed up.
*/
public List<Customer> getLeadsForInsurance(String filePath) {

List<Customer> customers = readCsvFile(filePath, Customer.class);
return customers.stream()
.filter(customer -> !customer.isHomePolicy() && !customer.isAutoPolicy() && !customer.isRentersPolicy())
.collect(Collectors.toList());
}


Expand All @@ -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<Vendor> getVendorsWithGivenRatingThatAreInScope(String filePath, String area, boolean inScope, int vendorRating) {

public List<Vendor> getVendorsWithGivenRatingThatAreInScope(String filePath, String area, boolean inScope,
int vendorRating) {
List<Vendor> vendors = readCsvFile(filePath, Vendor.class);
List<Vendor> vendors1 = vendors.stream().filter(vendor -> vendor.getArea().equals(area))
.collect(Collectors.toList());
List<Vendor> 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());
}


Expand All @@ -117,7 +175,12 @@ public List<Vendor> getVendorsWithGivenRatingThatAreInScope(String filePath, Str
* @return -- List of customers filtered by age, number of vehicles insured and the number of dependents.
*/
public List<Customer> getUndisclosedDrivers(String filePath, int vehiclesInsured, int dependents) {

List<Customer> 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());
}


Expand All @@ -130,7 +193,14 @@ public List<Customer> getUndisclosedDrivers(String filePath, int vehiclesInsured
* @return -- Agent ID of agent with the given rank.
*/
public int getAgentIdGivenRank(String filePath, int agentRank) {

List<Customer> customers = readCsvFile(filePath, Customer.class);
Map<Integer, Double> agentWithRating = customers.stream().collect(
Collectors.groupingBy(Customer::getAgentId, Collectors.averagingInt(Customer::getAgentRating)));
List<Entry<Integer, Double>> list = new ArrayList<>(agentWithRating.entrySet());
list.sort(Entry.comparingByValue());
Collections.reverse(list);
System.out.println(list);
return list.get(agentRank - 1).getKey();
}


Expand All @@ -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 <numberOfMonths>.
*/
public List<Customer> getCustomersWithClaims(Map<String,String> csvFilePaths, short monthsOpen) {

public List<Customer> getCustomersWithClaims(Map<String, String> csvFilePaths, short monthsOpen) {
List<Customer> customers = readCsvFile(csvFilePaths.get("customerList"), Customer.class);
List<Claim> claims = readCsvFile(csvFilePaths.get("claimList"), Claim.class);
Set<Integer> 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());
}

}
44 changes: 42 additions & 2 deletions src/main/java/sf/codingcompetition2020/structures/Agent.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
34 changes: 33 additions & 1 deletion src/main/java/sf/codingcompetition2020/structures/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Loading