diff --git a/CodingCompCSVUtil.java b/CodingCompCSVUtil.java new file mode 100644 index 0000000..0219818 --- /dev/null +++ b/CodingCompCSVUtil.java @@ -0,0 +1,199 @@ +package codingcompetition2019; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; + +public class CodingCompCSVUtil { + public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { + List> rows = new ArrayList>(); + + try { + Scanner file = new Scanner (new File (fileName)); + + if (file.nextLine ().split(",")[0].equals ("Entity")){ + file.nextLine (); + } + + while (file.hasNext()) { + + + String x = file.nextLine(); + if (x.contains(countryName)) { + rows.add(Arrays.asList(x.split(","))); + } + + + } + + } + catch (FileNotFoundException e){ + System.out.print(e); + } + + return rows; + } + + public List> readCSVFileWithHeaders(String fileName) throws IOException { + List> rows = new ArrayList>(); + + try { + Scanner file = new Scanner (new File (fileName)); + + + while (file.hasNext()) { + + + rows.add(Arrays.asList(file.nextLine ().split(","))); + + } + + } + catch (FileNotFoundException e){ + System.out.print(e); + } + + return rows; + } + + + + public List> readCSVFileWithoutHeaders(String fileName) throws IOException { + List> rows = new ArrayList>(); + + try { + Scanner file = new Scanner (new File (fileName)); + + + file.nextLine (); + + while (file.hasNext()) { + + + rows.add(Arrays.asList(file.nextLine ().split(","))); + + } + + } + catch (FileNotFoundException e){ + System.out.print(e); + } + + return rows; + } + + public DisasterDescription getMostImpactfulYear(List> records) { + int max = 0; + String year = ""; + String cat = ""; + + for (List x : records) { + int number2 = Integer.valueOf(x.get(3)); + if(number2 > max) { + max = number2; + year = x.get(2); + cat = x.get(0); + } + + } + + + return new DisasterDescription (year,cat,max); + } + + public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { + int max = 0; + String year = ""; + String cat = ""; + + for (List x : records) { + if (x.contains (category)) { + int number2 = Integer.valueOf(x.get(3)); + if(number2 > max) { + max = number2; + year = x.get(2); + cat = x.get(0); + } + } + + } + return new DisasterDescription (year,cat,max); + + } + + public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { + int max = 0; + String ourYear = ""; + String cat = ""; + + for (List x : records) { + if (x.contains (year)&& !(x.contains("All natural disasters"))) { + + int number2 = Integer.valueOf(x.get(3)); + if(number2 > max) { + max = number2; + ourYear = x.get(2); + cat = x.get(0); + } + } + + } + return new DisasterDescription (ourYear,cat,max); + } + + public DisasterDescription getTotalReportedIncidentsByCategory(String category, List> records) { + int total = 0; + String year = "0000"; + String cat = category; + + for (List x : records) { + if (x.contains(category)) { + total += Integer.valueOf(x.get(3)); + + } + } + + + return new DisasterDescription (year,cat,total); + + } + + /** + * This method will return the count if the number of incident falls within the provided range. + * To simplify the problem, we assume: + * + A value of -1 is provided if the max range is NOT applicable. + * + A min value can be provided, without providing a max value (which then has to be -1 like indicated above). + * + If a max value is provided, then a max value is also needed. + */ + public int countImpactfulYearsWithReportedIncidentsWithinRange(List> records, int min, int max) { + // TODO implement this method + + int count = 0; + for (List list : records) { + if (max == -1 && Integer.valueOf (list.get(3))>=min){ + count++; + } + + else if (Integer.valueOf (list.get(3))<= max && Integer.valueOf (list.get(3))>=min){ + count++; + } + } + return count; + } + + public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { + int count1 = 0; + int count2 = 0; + for (List list : records1) { + count1+=Integer.valueOf (list.get(3)); + } + for (List list : records2) { + count2+=Integer.valueOf (list.get(3)); + } + return count1>count2; + } +} diff --git a/DisasterDescription.java b/DisasterDescription.java new file mode 100644 index 0000000..9ee2338 --- /dev/null +++ b/DisasterDescription.java @@ -0,0 +1,28 @@ +package codingcompetition2019; + +public class DisasterDescription { + // TODO finish this class + private String year; + private String category; + private int IncedentNum = 0; + + public DisasterDescription(String year, String cat, int num) { + this.year = year; + this.IncedentNum = num; + this.category = cat; + } + + public String getCategory() { + + return this.category; + + } + public int getReportedIncidentsNum() { + + return this.IncedentNum; + } + + public String getYear () { + return this.year; + } +}