Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.

All tests passed #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
6 changes: 2 additions & 4 deletions feedback.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
Your team (name of each individual participating):
How many JUnits were you able to get to pass?
Your team (name of each individual participating): Stuart Irwin
How many JUnits were you able to get to pass? 10

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?
14 changes: 13 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>coding-competition</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<packaging>jar</packaging>
<groupId>sf.codingcompetition2020</groupId>

<name>coding-competition</name>
Expand Down
119 changes: 92 additions & 27 deletions src/main/java/sf/codingcompetition2020/CodingCompCsvUtil.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package sf.codingcompetition2020;

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.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
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 java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.function.Predicate;

import sf.codingcompetition2020.structures.Agent;
import sf.codingcompetition2020.structures.Claim;
Expand All @@ -30,7 +19,42 @@ public class CodingCompCsvUtil {
* @return -- List of entries being returned.
*/
public <T> List<T> readCsvFile(String filePath, Class<T> classType) {

return readCsvFile(filePath, classType, null);
}

private <T> List<T> readCsvFile(String filePath, Class<T> classType, Predicate<T> filter) {
//Open file
File file = new File(filePath);
ArrayList<T> list = new ArrayList<>();
try {
//Skip first line
Scanner scan = new Scanner(file);
scan.nextLine();

while(scan.hasNextLine()) {

//Build object
String[] values = scan.nextLine().split(",");
T ob = readLine(values, classType);
if(filter == null || filter.test(ob))
list.add(ob);
}
} catch(FileNotFoundException e) {
System.out.println("File " + filePath + " not found");
}
return list;
}

private <T> T readLine(String[] input, Class<T> classType) {
if(classType == Agent.class)
return (T) new Agent(input);
if(classType == Claim.class)
return (T) new Claim(input);
if(classType == Customer.class)
return (T) new Customer(input);
if(classType == Vendor.class)
return (T) new Vendor(input);
throw new IllegalArgumentException("Invalid class type");
}


Expand All @@ -40,8 +64,8 @@ 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) {
return readCsvFile(filePath, Agent.class, agent -> agent.getArea().equals(area)).size();
}


Expand All @@ -53,7 +77,7 @@ 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) {

return readCsvFile(filePath, Agent.class, agent -> agent.getArea().equals(area) && agent.getLanguage().equals(language));
}


Expand All @@ -66,7 +90,13 @@ public List<Agent> getAgentsInAreaThatSpeakLanguage(String filePath, String area
* @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) {

List<Agent> agents = readCsvFile(csvFilePaths.get("agentList"), Agent.class,
agent -> agent.getFirstName().equals(agentFirstName) && agent.getLastName().equals(agentLastName));
if(agents.size() != 1)
return 0;

return (short)readCsvFile(csvFilePaths.get("customerList"), Customer.class,
customer -> customer.getArea().equals(customerArea) && customer.getAgentId() == agents.get(0).getAgentId()).size();
}


Expand All @@ -77,7 +107,9 @@ 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, customer -> customer.getYearsOfService() == yearsOfService);
customers.sort(Comparator.comparingInt(customer -> Integer.parseInt(customer.getTotalMonthlyPremium().substring(1))));
return customers;
}


Expand All @@ -88,7 +120,8 @@ 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) {

return readCsvFile(filePath, Customer.class, customer ->
!(customer.hasAutoPolicy() || customer.hasHomePolicy() || customer.hasRentersPolicy()));
}


Expand All @@ -103,7 +136,8 @@ b. Whether that vendor is in scope of the insurance (if inScope == false, return
* @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) {

return readCsvFile(filePath, Vendor.class,
vendor -> vendor.getArea().equals(area) && vendor.getVendorRating() >= vendorRating && (!inScope || vendor.isInScope()));
}


Expand All @@ -117,7 +151,9 @@ 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) {

return readCsvFile(filePath, Customer.class,
customer -> customer.getVehiclesInsured() > vehiclesInsured && customer.getDependents().size() <= dependents &&
customer.getAge() >= 40 && customer.getAge() <= 50);
}


Expand All @@ -130,8 +166,30 @@ 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> list = readCsvFile(filePath, Customer.class);
HashMap<Integer, Pair> agents = new HashMap<>();

//Create map of agent ratings
for(Customer c : list) {
int agentId = c.getAgentId();
if(agents.containsKey(agentId))
agents.replace(agentId, agents.get(agentId).add(new Pair(c.getAgentRating(), 1)));
else
agents.put(agentId, new Pair(c.getAgentRating(), 1));
}

//Create new list
List<Pair> agentsList = new ArrayList<>();
for(int agentId : agents.keySet()) {
//Calculate avg rating
Pair p = agents.get(agentId);
agentsList.add(new Pair(agentId, 100 * p.first / p.second));
}

//Sort agents
agentsList.sort(Comparator.comparingInt(pair -> pair.second));
return agentsList.get(agentsList.size() - agentRank).first;
}


/* #10
Expand All @@ -141,7 +199,14 @@ public int getAgentIdGivenRank(String filePath, int agentRank) {
* @return -- List of customers who’ve filed a claim within the last <numberOfMonths>.
*/
public List<Customer> getCustomersWithClaims(Map<String,String> csvFilePaths, short monthsOpen) {

List<Claim> claims = readCsvFile(csvFilePaths.get("claimList"), Claim.class, claim -> claim.getMonthsOpen() <= monthsOpen);
List<Customer> customers = readCsvFile(csvFilePaths.get("customerList"), Customer.class);
Set<Customer> filteredCustomers = new HashSet<>();

for(Claim c : claims)
filteredCustomers.add(customers.get(c.getCustomerId() - 1));

return new ArrayList<>(filteredCustomers);
}

}
15 changes: 15 additions & 0 deletions src/main/java/sf/codingcompetition2020/Pair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package sf.codingcompetition2020;

public class Pair {
public final int first;
public final int second;

public Pair(int first, int second) {
this.first = first;
this.second = second;
}

public Pair add(Pair other) {
return new Pair(first + other.first, second + other.second);
}
}
31 changes: 31 additions & 0 deletions src/main/java/sf/codingcompetition2020/structures/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,35 @@ public class Agent {
private String firstName;
private String lastName;

public Agent(String[] s) {
if(s.length != 5)
return;

//Set values
agentId = Integer.parseInt(s[0]);
area = s[1];
language = s[2];
firstName = s[3];
lastName = s[4];
}

public int getAgentId() {
return agentId;
}

public String getArea() {
return area;
}

public String getLanguage() {
return language;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}
}
26 changes: 26 additions & 0 deletions src/main/java/sf/codingcompetition2020/structures/Claim.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,30 @@ public class Claim {
private boolean closed;
private int monthsOpen;

public Claim(String[] s) {
if(s.length != 4)
return;

//Set values
claimId = Integer.parseInt(s[0]);
customerId = Integer.parseInt(s[1]);
closed = s[2].equals("true");
monthsOpen = Integer.parseInt(s[3]);
}

public int getClaimId() {
return claimId;
}

public int getCustomerId() {
return customerId;
}

public boolean isClosed() {
return closed;
}

public int getMonthsOpen() {
return monthsOpen;
}
}
Loading