diff --git a/java/funding-raised/pom.xml b/java/funding-raised/pom.xml index ab06652..a8afa9d 100755 --- a/java/funding-raised/pom.xml +++ b/java/funding-raised/pom.xml @@ -7,6 +7,10 @@ 1.0-SNAPSHOT funding-raised http://maven.apache.org + + 11 + 11 + junit diff --git a/java/funding-raised/src/main/java/com/checkr/interviews/Columns.java b/java/funding-raised/src/main/java/com/checkr/interviews/Columns.java new file mode 100644 index 0000000..3e08666 --- /dev/null +++ b/java/funding-raised/src/main/java/com/checkr/interviews/Columns.java @@ -0,0 +1,30 @@ +package com.acme.interviews; + +public enum Columns { + PERMALINK("permalink", 0), + COMPANY_NAME("company_name", 1), + NUMBER_EMPLOYEES("number_employees", 2), + CATEGORY("category", 3), + CITY("city", 4), + STATE("state", 5), + FUNDED_DATE("funded_date", 6), + RAISED_AMOUNT("raised_amount", 7), + RAISED_CURRENCY("raised_currency", 8), + ROUND("round", 9); + + private String columnName; + private int order; + + Columns(String columnName, int order) { + this.columnName = columnName; + this.order = order; + } + + public String getColumnName() { + return columnName; + } + + public int getOrder() { + return order; + } +} \ No newline at end of file diff --git a/java/funding-raised/src/main/java/com/checkr/interviews/FundingRaised.java b/java/funding-raised/src/main/java/com/checkr/interviews/FundingRaised.java index 3e773d5..581b197 100755 --- a/java/funding-raised/src/main/java/com/checkr/interviews/FundingRaised.java +++ b/java/funding-raised/src/main/java/com/checkr/interviews/FundingRaised.java @@ -1,186 +1,109 @@ package com.acme.interviews; import java.util.*; + import com.opencsv.CSVReader; + import java.io.FileReader; import java.io.IOException; public class FundingRaised { public static List> where(Map options) throws IOException { - List csvData = new ArrayList(); - CSVReader reader = new CSVReader(new FileReader("startup_funding.csv")); - String[] row = null; + List csvData = readCSVFile("startup_funding.csv"); - while((row = reader.readNext()) != null) { - csvData.add(row); + for (Columns c : Columns.values()) { + if (options.containsKey(c.getColumnName())) { + csvData = fındOptions(c, options.get(c.getColumnName()), csvData); + } } + return constructResultMap(csvData); + } - reader.close(); - csvData.remove(0); - - if(options.containsKey("company_name")) { - List results = new ArrayList (); + public static Map findBy(Map options) throws IOException, NoSuchEntryException { + List csvData = readCSVFile("startup_funding.csv"); - for(int i = 0; i < csvData.size(); i++) { - if(csvData.get(i)[1].equals(options.get("company_name"))) { - results.add(csvData.get(i)); - } + Map mapped = new HashMap(); + for (int i = 0; i < csvData.size(); i++) { + mapped = foundData(options, csvData.get(i)); + if (mapped.isEmpty()) { + continue; } - csvData = results; + return mapped; } + throw new NoSuchEntryException(); + } - if(options.containsKey("city")) { - List results = new ArrayList (); - - for(int i = 0; i < csvData.size(); i++) { - if(csvData.get(i)[4].equals(options.get("city"))) { - results.add(csvData.get(i)); - } - } - csvData = results; + public static void main(String[] args) { + try { + Map options = new HashMap(); + options.put("company_name", "Facebook"); + options.put("round", "a"); + System.out.print(FundingRaised.where(options).size()); + } catch (IOException e) { + System.out.print(e.getMessage()); + System.out.print("error"); } + } - if(options.containsKey("state")) { - List results = new ArrayList (); + private static List readCSVFile(String fileName) throws IOException { + CSVReader reader = new CSVReader(new FileReader(fileName)); + String[] row = reader.readNext(); + List csvData = new ArrayList(); - for(int i = 0; i < csvData.size(); i++) { - if(csvData.get(i)[5].equals(options.get("state"))) { - results.add(csvData.get(i)); - } - } - csvData = results; + while (row != null) { + csvData.add(row); + row = reader.readNext(); } + reader.close(); + if (csvData.size() > 0) { + csvData.remove(0); + } + return csvData; + } - if(options.containsKey("round")) { - List results = new ArrayList (); - - for(int i = 0; i < csvData.size(); i++) { - if(csvData.get(i)[9].equals(options.get("round"))) { - results.add(csvData.get(i)); - } + private static List fındOptions(Columns column, String value, List csvData) { + List results = new ArrayList<>(); + for (int i = 0; i < csvData.size(); i++) { + if (csvData.get(i)[column.getOrder()].equals(value)) { + results.add(csvData.get(i)); } - csvData = results; } + return results; + } - List> output = new ArrayList>(); - - for(int i = 0; i < csvData.size(); i++) { - Map mapped = new HashMap (); - mapped.put("permalink", csvData.get(i)[0]); - mapped.put("company_name", csvData.get(i)[1]); - mapped.put("number_employees", csvData.get(i)[2]); - mapped.put("category", csvData.get(i)[3]); - mapped.put("city", csvData.get(i)[4]); - mapped.put("state", csvData.get(i)[5]); - mapped.put("funded_date", csvData.get(i)[6]); - mapped.put("raised_amount", csvData.get(i)[7]); - mapped.put("raised_currency", csvData.get(i)[8]); - mapped.put("round", csvData.get(i)[9]); - output.add(mapped); + private static List> constructResultMap(List csvData) { + List> output = new ArrayList<>(); + for (int i = 0; i < csvData.size(); i++) { + output.add(putColumns(csvData.get(i))); } - return output; } - public static Map findBy(Map options) throws IOException, NoSuchEntryException { - List csvData = new ArrayList(); - CSVReader reader = new CSVReader(new FileReader("startup_funding.csv")); - String[] row = null; - - while((row = reader.readNext()) != null) { - csvData.add(row); + private static Map putColumns(String[] csvData) { + Map mapped = new HashMap<>(); + for (Columns c : Columns.values()) { + mapped.put(c.getColumnName(), csvData[c.getOrder()]); } + return mapped; + } - reader.close(); - csvData.remove(0); - Map mapped = new HashMap (); - - for(int i = 0; i < csvData.size(); i++) { - if(options.containsKey("company_name")) { - if(csvData.get(i)[1].equals(options.get("company_name"))) { - mapped.put("permalink", csvData.get(i)[0]); - mapped.put("company_name", csvData.get(i)[1]); - mapped.put("number_employees", csvData.get(i)[2]); - mapped.put("category", csvData.get(i)[3]); - mapped.put("city", csvData.get(i)[4]); - mapped.put("state", csvData.get(i)[5]); - mapped.put("funded_date", csvData.get(i)[6]); - mapped.put("raised_amount", csvData.get(i)[7]); - mapped.put("raised_currency", csvData.get(i)[8]); - mapped.put("round", csvData.get(i)[9]); - } else { - continue; - } - } - - if(options.containsKey("city")) { - if(csvData.get(i)[4].equals(options.get("city"))) { - mapped.put("permalink", csvData.get(i)[0]); - mapped.put("company_name", csvData.get(i)[1]); - mapped.put("number_employees", csvData.get(i)[2]); - mapped.put("category", csvData.get(i)[3]); - mapped.put("city", csvData.get(i)[4]); - mapped.put("state", csvData.get(i)[5]); - mapped.put("funded_date", csvData.get(i)[6]); - mapped.put("raised_amount", csvData.get(i)[7]); - mapped.put("raised_currency", csvData.get(i)[8]); - mapped.put("round", csvData.get(i)[9]); - } else { - continue; - } - } - - if(options.containsKey("state")) { - if(csvData.get(i)[5].equals(options.get("state"))) { - mapped.put("permalink", csvData.get(i)[0]); - mapped.put("company_name", csvData.get(i)[1]); - mapped.put("number_employees", csvData.get(i)[2]); - mapped.put("category", csvData.get(i)[3]); - mapped.put("city", csvData.get(i)[4]); - mapped.put("state", csvData.get(i)[5]); - mapped.put("funded_date", csvData.get(i)[6]); - mapped.put("raised_amount", csvData.get(i)[7]); - mapped.put("raised_currency", csvData.get(i)[8]); - mapped.put("round", csvData.get(i)[9]); - } else { - continue; - } - } - - if(options.containsKey("round")) { - if(csvData.get(i)[9].equals(options.get("round"))) { - mapped.put("permalink", csvData.get(i)[0]); - mapped.put("company_name", csvData.get(i)[1]); - mapped.put("number_employees", csvData.get(i)[2]); - mapped.put("category", csvData.get(i)[3]); - mapped.put("city", csvData.get(i)[4]); - mapped.put("state", csvData.get(i)[5]); - mapped.put("funded_date", csvData.get(i)[6]); - mapped.put("raised_amount", csvData.get(i)[7]); - mapped.put("raised_currency", csvData.get(i)[8]); - mapped.put("round", csvData.get(i)[9]); + private static Map foundData(Map options, String[] csvData) { + Map mapped = new HashMap<>(); + int foundTimes = 0; + for (Columns c : Columns.values()) { + if(options.containsKey(c.getColumnName())){ + if(csvData[c.getOrder()].equals(options.get(c.getColumnName()))){ + mapped = putColumns(csvData); + foundTimes++; } else { - continue; + break; } } - - return mapped; } - throw new NoSuchEntryException(); - } - - public static void main(String[] args) { - try { - Map options = new HashMap (); - options.put("company_name", "Facebook"); - options.put("round", "a"); - System.out.print(FundingRaised.where(options).size()); - } catch(IOException e) { - System.out.print(e.getMessage()); - System.out.print("error"); - } + return foundTimes == options.size() ? mapped : new HashMap<>(); } } -class NoSuchEntryException extends Exception {} +class NoSuchEntryException extends Exception { +}