From 6f196024f572fb1efdf775de1cd96d0d4ced1e71 Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sat, 5 Oct 2019 21:45:28 -0400 Subject: [PATCH 01/15] Implemented CountryParse Function --- .../CodingCompCSVUtil.java | 28 ++++- .../CodingCompCSVUtilTest.java | 110 +++++++++--------- 2 files changed, 81 insertions(+), 57 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 74f3074..e70e193 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -1,19 +1,43 @@ package codingcompetition2019; +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.FileReader; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; + public class CodingCompCSVUtil { public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { // TODO implement this method - return null; + BufferedReader br = null; + String line = ""; + List> countryLines = new ArrayList>(); + String delimiter = ","; + try { + br = new BufferedReader(new FileReader(fileName)); + while ((line = br.readLine()) != null) { + String[] country = line.split(delimiter, -1); + if (country[0].equals(countryName)) { + List countryCast = Arrays.asList(country); + countryLines.add(countryCast); + } + } + } catch(FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return countryLines; } public List> readCSVFileWithHeaders(String fileName) throws IOException { // TODO implement this method return null; } - + public List> readCSVFileWithoutHeaders(String fileName) throws IOException { // TODO implement this method return null; diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index 85ab612..b12591c 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -34,59 +34,59 @@ public void testReadCSVFileWithHeadersByCountry() throws IOException { assertEquals(343, tempRecords.size()); } - @Test - public void testGetTheMostImpactfulYearInUSAByEarthquake() throws IOException { - List> earthquakeRecords = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); - assertEquals("2011", util.getMostImpactfulYear(earthquakeRecords).getYear()); - } - - @Test - public void testReadCSVFileWithHeaders() throws IOException { - assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); - } - - @Test - public void testReadCSVFileWithoutHeaders() throws IOException { - assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); - } - - @Test - public void testGetMostImpactfulYear() { - assertEquals("2005", util.getMostImpactfulYear(records).getYear()); - } - - @Test - public void testGetMostImpactfulYearByCategory() { - assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); - } - - @Test - public void testGetMostImpactfulDisasterByYear() { - DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); - - assertEquals("Flood", dd.getCategory()); - assertEquals(193, dd.getReportedIncidentsNum()); - } - - @Test - public void testGetTotalReportedIncidentsByCategoryy() { - assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); - } - - @Test - public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { - List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); - - assertEquals(40, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 1, -1)); - assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); - } - - @Test - public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { - List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); - List> tempRecords2 = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); - - assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); - assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); - } +// @Test +// public void testGetTheMostImpactfulYearInUSAByEarthquake() throws IOException { +// List> earthquakeRecords = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); +// assertEquals("2011", util.getMostImpactfulYear(earthquakeRecords).getYear()); +// } +// +// @Test +// public void testReadCSVFileWithHeaders() throws IOException { +// assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); +// } +// +// @Test +// public void testReadCSVFileWithoutHeaders() throws IOException { +// assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); +// } +// +// @Test +// public void testGetMostImpactfulYear() { +// assertEquals("2005", util.getMostImpactfulYear(records).getYear()); +// } +// +// @Test +// public void testGetMostImpactfulYearByCategory() { +// assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); +// } +// +// @Test +// public void testGetMostImpactfulDisasterByYear() { +// DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); +// +// assertEquals("Flood", dd.getCategory()); +// assertEquals(193, dd.getReportedIncidentsNum()); +// } +// +// @Test +// public void testGetTotalReportedIncidentsByCategoryy() { +// assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); +// } +// +// @Test +// public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { +// List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); +// +// assertEquals(40, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 1, -1)); +// assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); +// } +// +// @Test +// public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { +// List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); +// List> tempRecords2 = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); +// +// assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); +// assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); +// } } From e98da05d250564a057560090c14fafee987d16ef Mon Sep 17 00:00:00 2001 From: koolcards Date: Sat, 5 Oct 2019 21:49:00 -0400 Subject: [PATCH 02/15] updates --- .gitignore | 2 + pom.xml | 14 ++- .../CodingCompCSVUtil.java | 31 ++++- .../DisasterDescription.java | 9 +- .../CodingCompCSVUtilTest.java | 110 +++++++++--------- 5 files changed, 109 insertions(+), 57 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92322c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +target/ diff --git a/pom.xml b/pom.xml index 78eb746..c0b19d6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,19 @@ codingcompetition2019 codingcompetition2019 0.0.1-SNAPSHOT - jar + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + jar codingcompetition2019 http://maven.apache.org diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 74f3074..59743a1 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -1,6 +1,10 @@ package codingcompetition2019; +import java.io.BufferedReader; +import java.io.FileReader; import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class CodingCompCSVUtil { @@ -15,13 +19,34 @@ public List> readCSVFileWithHeaders(String fileName) throws IOExcep } public List> readCSVFileWithoutHeaders(String fileName) throws IOException { - // TODO implement this method + List> records = new ArrayList<>(); + boolean firstLine = false; + try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { + String line; + while ((line = br.readLine()) != null) { + if (!firstLine) { + firstLine = true; + continue; + } + String[] values = line.split(","); + records.add(Arrays.asList(values)); + } + } + System.out.println(records); return null; } public DisasterDescription getMostImpactfulYear(List> records) { - // TODO implement this method - return null; + String mostImpactfulYear = null; + int maxYearImpact = 0; + for (List record : records) { + int currImpact = Integer.parseInt(record.get(3)); + if (currImpact > maxYearImpact) { + maxYearImpact = currImpact; + mostImpactfulYear = record.get(2); + } + } + return new DisasterDescription(mostImpactfulYear); } public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index 662626b..aac42e7 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -1,5 +1,12 @@ package codingcompetition2019; public class DisasterDescription { - // TODO finish this class + private String year = ""; + public DisasterDescription(String year) { + this.year = year; + } + + public String getYear() { + return year; + } } diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index 85ab612..0b5fe38 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -23,70 +23,76 @@ public class CodingCompCSVUtilTest extends TestCase { public void setUp() throws Exception { util = new CodingCompCSVUtil(); records = util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile); - } + System.out.println(records); + } + + @Test + public void testDefault() { + assertEquals(143, 143); + } @Test public void testReadCSVFileWithHeadersByCountry() throws IOException { List> tempRecords = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); assertEquals(945, tempRecords.size()); - + tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); assertEquals(343, tempRecords.size()); } - + @Test public void testGetTheMostImpactfulYearInUSAByEarthquake() throws IOException { List> earthquakeRecords = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); assertEquals("2011", util.getMostImpactfulYear(earthquakeRecords).getYear()); } - - @Test - public void testReadCSVFileWithHeaders() throws IOException { - assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); - } - - @Test - public void testReadCSVFileWithoutHeaders() throws IOException { - assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); - } - - @Test - public void testGetMostImpactfulYear() { - assertEquals("2005", util.getMostImpactfulYear(records).getYear()); - } - - @Test - public void testGetMostImpactfulYearByCategory() { - assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); - } - - @Test - public void testGetMostImpactfulDisasterByYear() { - DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); - - assertEquals("Flood", dd.getCategory()); - assertEquals(193, dd.getReportedIncidentsNum()); - } - - @Test - public void testGetTotalReportedIncidentsByCategoryy() { - assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); - } - - @Test - public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { - List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); - - assertEquals(40, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 1, -1)); - assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); - } - - @Test - public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { - List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); - List> tempRecords2 = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); - assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); - assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); - } +// @Test +// public void testReadCSVFileWithHeaders() throws IOException { +// assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); +// } +// +// @Test +// public void testReadCSVFileWithoutHeaders() throws IOException { +// assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); +// } +// +// @Test +// public void testGetMostImpactfulYear() { +// assertEquals("2005", util.getMostImpactfulYear(records).getYear()); +// } +// +// @Test +// public void testGetMostImpactfulYearByCategory() { +// assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); +// } +// +// @Test +// public void testGetMostImpactfulDisasterByYear() { +// DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); +// +// assertEquals("Flood", dd.getCategory()); +// assertEquals(193, dd.getReportedIncidentsNum()); +// } +// +// @Test +// public void testGetTotalReportedIncidentsByCategoryy() { +// assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); +// } +// +// @Test +// public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { +// List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); +// +// assertEquals(40, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 1, -1)); +// assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); +// } +// +// @Test +// public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { +// List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); +// List> tempRecords2 = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); +// +// assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); +// assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); +// } } From 3888a21f2120474623d30e7bc0c6df93e6aa04f3 Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sat, 5 Oct 2019 22:00:49 -0400 Subject: [PATCH 03/15] Merged Java 7 changes --- .../CodingCompCSVUtil.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index deaeb34..9ff797d 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -11,7 +11,6 @@ public class CodingCompCSVUtil { public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { - // TODO implement this method BufferedReader br = null; String line = ""; List> countryLines = new ArrayList>(); @@ -32,26 +31,29 @@ public List> readCSVFileByCountry(String fileName, String countryNa } public List> readCSVFileWithHeaders(String fileName) throws IOException { - // TODO implement this method - return null; + return null; } public List> readCSVFileWithoutHeaders(String fileName) throws IOException { - List> records = new ArrayList<>(); - boolean firstLine = false; - try (BufferedReader br = new BufferedReader(new FileReader(fileName))) { - String line; - while ((line = br.readLine()) != null) { - if (!firstLine) { - firstLine = true; - continue; - } - String[] values = line.split(","); - records.add(Arrays.asList(values)); - } - } - System.out.println(records); - return null; + List> csvFile = new ArrayList>(); + String delimiter = ","; + boolean first_line = false; + try { + BufferedReader br = new BufferedReader(new FileReader(fileName)); + String line = ""; + while ((line = br.readLine()) != null) { + if (!first_line) { + first_line = true; + continue; + } + String[] lineData = line.split(delimiter, -1); + List lineDataList = Arrays.asList(lineData); + csvFile.add(lineDataList); + } + } catch(FileNotFoundException e) { + e.printStackTrace(); + } + return csvFile; } public DisasterDescription getMostImpactfulYear(List> records) { From a08c5f358f5340753caf57373408a8af1a805f2a Mon Sep 17 00:00:00 2001 From: koolcards Date: Sat, 5 Oct 2019 22:03:20 -0400 Subject: [PATCH 04/15] read csv file with headers --- .../codingcompetition2019/CodingCompCSVUtil.java | 15 ++++++++++++++- .../CodingCompCSVUtilTest.java | 10 +++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 9ff797d..d45dc28 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -31,7 +31,20 @@ public List> readCSVFileByCountry(String fileName, String countryNa } public List> readCSVFileWithHeaders(String fileName) throws IOException { - return null; + List> csvFile = new ArrayList>(); + String delimiter = ","; + try { + BufferedReader br = new BufferedReader(new FileReader(fileName)); + String line = ""; + while ((line = br.readLine()) != null) { + String[] lineData = line.split(delimiter, -1); + List lineDataList = Arrays.asList(lineData); + csvFile.add(lineDataList); + } + } catch(FileNotFoundException e) { + e.printStackTrace(); + } + return csvFile; } public List> readCSVFileWithoutHeaders(String fileName) throws IOException { diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index 8b63fe2..4b4f565 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -41,11 +41,11 @@ public void testGetTheMostImpactfulYearInUSAByEarthquake() throws IOException { assertEquals("2011", util.getMostImpactfulYear(earthquakeRecords).getYear()); } -// @Test -// public void testReadCSVFileWithHeaders() throws IOException { -// assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); -// } -// + @Test + public void testReadCSVFileWithHeaders() throws IOException { + assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); + } + // @Test // public void testReadCSVFileWithoutHeaders() throws IOException { // assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); From 05a324cc2860853fb8d8a8bc81a8a6322104901b Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sat, 5 Oct 2019 22:17:57 -0400 Subject: [PATCH 05/15] updated Disaster Description --- .../codingcompetition2019/DisasterDescription.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index aac42e7..d99f8b7 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -2,8 +2,19 @@ public class DisasterDescription { private String year = ""; + private String category = ""; + private int numInYear = 0; public DisasterDescription(String year) { + this(year, "", 0); + } + public DisasterDescription(String year, String type, int num) { this.year = year; + this.category = type; + this.numInYear = num; + } + + public int getReportedIncidentsNum() { + return numInYear; } public String getYear() { From cdab7fa5735c8ab93f6c43916e32efaf74838df3 Mon Sep 17 00:00:00 2001 From: koolcards Date: Sat, 5 Oct 2019 22:18:52 -0400 Subject: [PATCH 06/15] modified Disaster Description wording --- .../CodingCompCSVUtil.java | 21 +++++++++- .../DisasterDescription.java | 9 +++-- .../CodingCompCSVUtilTest.java | 40 +++++++++---------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index d45dc28..f367f1f 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -83,8 +83,25 @@ public DisasterDescription getMostImpactfulYear(List> records) { } public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { - // TODO implement this method - return null; + String mostImpactfulYear = null; + int maxYearImpact = 0; + + for (List record : records) { + if (record.get(0).compareTo(category) > 0) { + break; + } + + if (!record.get(0).equals(category)) { + continue; + } + + int currImpact = Integer.parseInt(record.get(3)); + if (currImpact > maxYearImpact) { + maxYearImpact = currImpact; + mostImpactfulYear = record.get(2); + } + } + return new DisasterDescription(mostImpactfulYear); } public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index d99f8b7..e352ede 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -3,18 +3,19 @@ public class DisasterDescription { private String year = ""; private String category = ""; - private int numInYear = 0; + private int reportedIncidents = 0; + public DisasterDescription(String year) { this(year, "", 0); } - public DisasterDescription(String year, String type, int num) { + public DisasterDescription(String year, String type, int reportedIncidents) { this.year = year; this.category = type; - this.numInYear = num; + this.reportedIncidents = reportedIncidents; } public int getReportedIncidentsNum() { - return numInYear; + return reportedIncidents; } public String getYear() { diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index 4b4f565..c95af30 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -46,21 +46,21 @@ public void testReadCSVFileWithHeaders() throws IOException { assertEquals(829, util.readCSVFileWithHeaders(naturalDisasterByTypeFile).size()); } -// @Test -// public void testReadCSVFileWithoutHeaders() throws IOException { -// assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); -// } -// -// @Test -// public void testGetMostImpactfulYear() { -// assertEquals("2005", util.getMostImpactfulYear(records).getYear()); -// } -// -// @Test -// public void testGetMostImpactfulYearByCategory() { -// assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); -// } -// + @Test + public void testReadCSVFileWithoutHeaders() throws IOException { + assertEquals(828, util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile).size()); + } + + @Test + public void testGetMostImpactfulYear() { + assertEquals("2005", util.getMostImpactfulYear(records).getYear()); + } + + @Test + public void testGetMostImpactfulYearByCategory() { + assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); + } + // @Test // public void testGetMostImpactfulDisasterByYear() { // DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); @@ -69,11 +69,11 @@ public void testReadCSVFileWithHeaders() throws IOException { // assertEquals(193, dd.getReportedIncidentsNum()); // } // -// @Test -// public void testGetTotalReportedIncidentsByCategoryy() { -// assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); -// } -// + @Test + public void testGetTotalReportedIncidentsByCategoryy() { + assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); + } + // @Test // public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { // List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); From 0cd2a4b24ae5322ba680c1b859363aa9f097477a Mon Sep 17 00:00:00 2001 From: koolcards Date: Sat, 5 Oct 2019 22:38:30 -0400 Subject: [PATCH 07/15] updated testImpactfulRange --- .../CodingCompCSVUtil.java | 27 ++++++++++++++++--- .../DisasterDescription.java | 4 +++ .../CodingCompCSVUtilTest.java | 16 +++++------ 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index f367f1f..91596ec 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -110,8 +110,19 @@ public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { - // TODO implement this method - return null; + DisasterDescription disaster = new DisasterDescription("year=" + category, category, 0); + + for (List record : records) { + if (record.get(0).compareTo(category) > 0) { + break; + } + + if (!record.get(0).equals(category)) { + continue; + } + disaster.addReportedIncidents(Integer.parseInt(record.get(3))); + } + return disaster; } /** @@ -122,8 +133,16 @@ public DisasterDescription getTotalReportedIncidentsByCategory(String category, * + 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 - return -1; + if (max == -1) { + max = Integer.MAX_VALUE; + } + int numYearsInRange = 0; + for (List record : records) { + if (Integer.parseInt(record.get(3)) >= min && Integer.parseInt(record.get(3)) <= max) { + numYearsInRange++; + } + } + return numYearsInRange; } public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index e352ede..7df27db 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -8,6 +8,7 @@ public class DisasterDescription { public DisasterDescription(String year) { this(year, "", 0); } + public DisasterDescription(String year, String type, int reportedIncidents) { this.year = year; this.category = type; @@ -18,6 +19,9 @@ public int getReportedIncidentsNum() { return reportedIncidents; } + public void addReportedIncidents(int numIncidents) { + reportedIncidents += numIncidents; + } public String getYear() { return year; } diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index c95af30..f5f983e 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -74,14 +74,14 @@ public void testGetTotalReportedIncidentsByCategoryy() { assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); } -// @Test -// public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { -// List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); -// -// assertEquals(40, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 1, -1)); -// assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); -// } -// + @Test + public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOException { + List> tempRecords = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); + + assertEquals(40, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 1, -1)); + assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); + } + // @Test // public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { // List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); From 079cefd5aa5edd35118e9ccb8521753c315a1697 Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sat, 5 Oct 2019 22:44:38 -0400 Subject: [PATCH 08/15] implemented next method --- src/main/java/codingcompetition2019/DisasterDescription.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index 7df27db..21bb717 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -25,4 +25,7 @@ public void addReportedIncidents(int numIncidents) { public String getYear() { return year; } + public String getCategory() { + return category; + } } From a4987b3153bfce62a1443df1a28f5521baa4bb33 Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sat, 5 Oct 2019 22:49:33 -0400 Subject: [PATCH 09/15] implemented impactful disasters by year --- .../codingcompetition2019/CodingCompCSVUtil.java | 16 +++++++++++++++- .../CodingCompCSVUtilTest.java | 16 ++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 91596ec..06e5447 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -106,7 +106,21 @@ public DisasterDescription getMostImpactfulYearByCategory(String category, List< public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { // TODO implement this method - return null; + String maxCategory = null; + int maxDisaster = 0; + for (List record : records) { + if (!record.get(0).equals("All natural disasters")) { + if (record.get(2).equals(year)) { + String category = record.get(0); + int disaster = Integer.parseInt(record.get(record.size() - 1)); + if (disaster > maxDisaster) { + maxDisaster = disaster; + maxCategory = category; + } + } + } + } + return new DisasterDescription(year, maxCategory, maxDisaster); } public DisasterDescription getTotalReportedIncidentsByCategory(String category, List> records) { diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index f5f983e..0f9fb6c 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -61,14 +61,14 @@ public void testGetMostImpactfulYearByCategory() { assertEquals("1990", util.getMostImpactfulYearByCategory("Earthquake", records).getYear()); } -// @Test -// public void testGetMostImpactfulDisasterByYear() { -// DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); -// -// assertEquals("Flood", dd.getCategory()); -// assertEquals(193, dd.getReportedIncidentsNum()); -// } -// + @Test + public void testGetMostImpactfulDisasterByYear() { + DisasterDescription dd = util.getMostImpactfulDisasterByYear("2005", records); + + assertEquals("Flood", dd.getCategory()); + assertEquals(193, dd.getReportedIncidentsNum()); + } + @Test public void testGetTotalReportedIncidentsByCategoryy() { assertEquals(1372, util.getTotalReportedIncidentsByCategory("Earthquake", records).getReportedIncidentsNum()); From d572d006a9174e74cbffaf3c5f44b020892f897f Mon Sep 17 00:00:00 2001 From: koolcards Date: Sat, 5 Oct 2019 22:50:09 -0400 Subject: [PATCH 10/15] finished first records more --- .../CodingCompCSVUtil.java | 38 +++++++++++-------- .../CodingCompCSVUtilTest.java | 16 ++++---- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 91596ec..351d049 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -10,6 +10,7 @@ public class CodingCompCSVUtil { + final String ALL_CATEGORIES = "ALL"; public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { BufferedReader br = null; String line = ""; @@ -110,19 +111,7 @@ public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { - DisasterDescription disaster = new DisasterDescription("year=" + category, category, 0); - - for (List record : records) { - if (record.get(0).compareTo(category) > 0) { - break; - } - - if (!record.get(0).equals(category)) { - continue; - } - disaster.addReportedIncidents(Integer.parseInt(record.get(3))); - } - return disaster; + return helperGetTotalReportedIncidents(category, records); } /** @@ -146,7 +135,26 @@ public int countImpactfulYearsWithReportedIncidentsWithinRange(List } public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { - // TODO implement this method - return false; + return helperGetTotalReportedIncidents(ALL_CATEGORIES, records1).getReportedIncidentsNum() > helperGetTotalReportedIncidents(ALL_CATEGORIES, records2).getReportedIncidentsNum(); + } + + public DisasterDescription helperGetTotalReportedIncidents (String category, List> records) { + boolean allCategories = false; + if (category.equals(ALL_CATEGORIES)) { + allCategories = true; + } + DisasterDescription disaster = new DisasterDescription("N/A", category, 0); + + for (List record : records) { + if (!allCategories && record.get(0).compareTo(category) > 0) { + break; + } + + if (!allCategories && !record.get(0).equals(category)) { + continue; + } + disaster.addReportedIncidents(Integer.parseInt(record.get(3))); + } + return disaster; } } diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index f5f983e..08e20fa 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -82,12 +82,12 @@ public void testCountImpactfulYearsWithReportedIncidentsWithinRange() throws IOE assertEquals(4, util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, 2, 4)); } -// @Test -// public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { -// List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); -// List> tempRecords2 = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); -// -// assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); -// assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); -// } + @Test + public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { + List> tempRecords1 = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); + List> tempRecords2 = util.readCSVFileByCountry(significantVolcanicEruptionsFileName, "United States"); + + assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); + assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); + } } From 3c320c0af697e3cdf431bd81751d3bdfc70dd470 Mon Sep 17 00:00:00 2001 From: koolcards Date: Sat, 5 Oct 2019 23:47:06 -0400 Subject: [PATCH 11/15] javadoc updates --- .../CodingCompCSVUtil.java | 278 ++++++++++++------ .../DisasterDescription.java | 34 ++- .../CodingCompCSVUtilTest.java | 13 +- 3 files changed, 222 insertions(+), 103 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index dc2d4bb..9cf3044 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -10,112 +10,80 @@ public class CodingCompCSVUtil { - final String ALL_CATEGORIES = "ALL"; + private final String ALL_CATEGORIES = "ALL"; + private final String COMMA_DELIMITER = ","; + private final String SKIP_HEADER = "SKIP_HEADER"; + private final String ALL_DISASTERS = "All natural disasters"; + private final int ENTITY_COLMN = 0; + private final int YEAR_COLMN = 2; + private final int INCIDENTS_COLMN = 3; + + /** + * Returns a list of all the records parsed by country + * @param fileName the csv filename + * @param countryName the country + * @return the list of all records + * @throws IOException if file is not found + */ public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { - BufferedReader br = null; - String line = ""; - List> countryLines = new ArrayList>(); - String delimiter = ","; - try { - br = new BufferedReader(new FileReader(fileName)); - while ((line = br.readLine()) != null) { - String[] country = line.split(delimiter, -1); - if (country[0].equals(countryName)) { - List countryCast = Arrays.asList(country); - countryLines.add(countryCast); - } - } - } catch(FileNotFoundException e) { - e.printStackTrace(); - } - return countryLines; + return helperReadCSVFile(fileName, countryName); } - + + /** + * Returns a list of all the records with the first header line + * @param fileName the csv filename + * @return the list of all the records + * @throws IOException if file is not found + */ public List> readCSVFileWithHeaders(String fileName) throws IOException { - List> csvFile = new ArrayList>(); - String delimiter = ","; - try { - BufferedReader br = new BufferedReader(new FileReader(fileName)); - String line = ""; - while ((line = br.readLine()) != null) { - String[] lineData = line.split(delimiter, -1); - List lineDataList = Arrays.asList(lineData); - csvFile.add(lineDataList); - } - } catch(FileNotFoundException e) { - e.printStackTrace(); - } - return csvFile; + return helperReadCSVFile(fileName, ALL_CATEGORIES); } + /** + * Returns a list of all the records without the first header line + * @param fileName the csv filename + * @return the list of all the records + * @throws IOException if file is not found + */ public List> readCSVFileWithoutHeaders(String fileName) throws IOException { - List> csvFile = new ArrayList>(); - String delimiter = ","; - boolean first_line = false; - try { - BufferedReader br = new BufferedReader(new FileReader(fileName)); - String line = ""; - while ((line = br.readLine()) != null) { - if (!first_line) { - first_line = true; - continue; - } - String[] lineData = line.split(delimiter, -1); - List lineDataList = Arrays.asList(lineData); - csvFile.add(lineDataList); - } - } catch(FileNotFoundException e) { - e.printStackTrace(); - } - return csvFile; + return helperReadCSVFile(fileName, SKIP_HEADER); } - + + /** + * Returns a disaster description containing the most impactful year + * @param records the records from the csv file + * @return the disaster description with the most impactful year + */ public DisasterDescription getMostImpactfulYear(List> records) { - String mostImpactfulYear = null; - int maxYearImpact = 0; - for (List record : records) { - int currImpact = Integer.parseInt(record.get(3)); - if (currImpact > maxYearImpact) { - maxYearImpact = currImpact; - mostImpactfulYear = record.get(2); - } - } - return new DisasterDescription(mostImpactfulYear); + return helperGetMostImpactfulYear(ALL_CATEGORIES, records); } + /** + * Returns a disaster description containing the most impactful year by category + * @param category the category of the record + * @param records the records from the csv file + * @return the disaster description with the most impactful year by category + */ public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { - String mostImpactfulYear = null; - int maxYearImpact = 0; - - for (List record : records) { - if (record.get(0).compareTo(category) > 0) { - break; - } - - if (!record.get(0).equals(category)) { - continue; - } - - int currImpact = Integer.parseInt(record.get(3)); - if (currImpact > maxYearImpact) { - maxYearImpact = currImpact; - mostImpactfulYear = record.get(2); - } - } - return new DisasterDescription(mostImpactfulYear); + return helperGetMostImpactfulYear(category, records); } + /** + * Returns the most impactful disaster for the specified year. + * @param year the specified year + * @param records the records from the csv file + * @return the disaster description with the most impactful year + */ public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { - // TODO implement this method String maxCategory = null; int maxDisaster = 0; for (List record : records) { - if (!record.get(0).equals("All natural disasters")) { - if (record.get(2).equals(year)) { - String category = record.get(0); - int disaster = Integer.parseInt(record.get(record.size() - 1)); - if (disaster > maxDisaster) { - maxDisaster = disaster; + if (!getLineEntity(record).equals(ALL_DISASTERS)) { + if (getLineYear(record).equals(year)) { + String category = getLineEntity(record); + int numDisasters = Integer.parseInt(getNumIncidents(record)); + if (numDisasters > maxDisaster) { + maxDisaster = numDisasters; maxCategory = category; } } @@ -124,6 +92,12 @@ public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { return helperGetTotalReportedIncidents(category, records); } @@ -141,17 +115,29 @@ public int countImpactfulYearsWithReportedIncidentsWithinRange(List } int numYearsInRange = 0; for (List record : records) { - if (Integer.parseInt(record.get(3)) >= min && Integer.parseInt(record.get(3)) <= max) { + if (Integer.parseInt(getNumIncidents(record)) >= min && Integer.parseInt(getNumIncidents(record)) <= max) { numYearsInRange++; } } return numYearsInRange; } - + + /** + * Determines whether the first record has more total incidents than the second record. + * @param records1 the first csv file records + * @param records2 the second csv file records + * @return true if the first csv file has more incidents or false otherwise + */ public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { return helperGetTotalReportedIncidents(ALL_CATEGORIES, records1).getReportedIncidentsNum() > helperGetTotalReportedIncidents(ALL_CATEGORIES, records2).getReportedIncidentsNum(); } + /** + * Helper method to get the total number of incidents that match the category. + * @param category the category of incidents or all categories + * @param records the records from the csv file + * @return a disaster description object containing the total reported incidents + */ public DisasterDescription helperGetTotalReportedIncidents (String category, List> records) { boolean allCategories = false; if (category.equals(ALL_CATEGORIES)) { @@ -159,6 +145,34 @@ public DisasterDescription helperGetTotalReportedIncidents (String category, Lis } DisasterDescription disaster = new DisasterDescription("N/A", category, 0); + for (List record : records) { + if (!allCategories && getLineEntity(record).compareTo(category) > 0) { + break; + } + + if (!allCategories && !getLineEntity(record).equals(category)) { + continue; + } + disaster.addReportedIncidents(Integer.parseInt(getNumIncidents(record))); + } + return disaster; + } + + /** + * Helper method to get the most impactful year (most number of incidents) by category. + * @param category the category of the entity in the record or all categories + * @param records the records from the csv file + * @return the disaster description containing the most impactful year + */ + public DisasterDescription helperGetMostImpactfulYear (String category, List> records) { + boolean allCategories = false; + if (category.equals(ALL_CATEGORIES)) { + allCategories = true; + } + + String mostImpactfulYear = null; + int maxYearImpact = 0; + for (List record : records) { if (!allCategories && record.get(0).compareTo(category) > 0) { break; @@ -167,8 +181,86 @@ public DisasterDescription helperGetTotalReportedIncidents (String category, Lis if (!allCategories && !record.get(0).equals(category)) { continue; } - disaster.addReportedIncidents(Integer.parseInt(record.get(3))); + + int currImpact = Integer.parseInt(record.get(3)); + if (currImpact > maxYearImpact) { + maxYearImpact = currImpact; + mostImpactfulYear = record.get(2); + } } - return disaster; + return new DisasterDescription(mostImpactfulYear); + } + + /** + * Helper method to parse csv file based on the specified parameters. Extra parameter is ALL_CATEGORIES + * if entire csv file needs to be parsed, SKIP_HEADER if entire csv needs to be parsed without header, + * or the country name if a specific country needs to be parsed. + * + * @param fileName the csv file + * @param extraParam the extra parameter (all categories/skip header/country name) + * @return a list of all the entries in csv file matching the query + * @throws IOException if file is not found + */ + private List> helperReadCSVFile(String fileName, String extraParam) throws IOException{ + boolean allCategories = false; + boolean header = true; + if (extraParam.equals(SKIP_HEADER)) { + header = false; + allCategories = true; + } else if (extraParam.equals(ALL_CATEGORIES)) { + allCategories = true; + } + List> csvFile = new ArrayList>(); + try { + BufferedReader br = new BufferedReader(new FileReader(fileName)); + String line = ""; + while ((line = br.readLine()) != null) { + if (!header) { + header = true; + continue; + } + String[] lineData = line.split(COMMA_DELIMITER, -1); + List lineDataList = Arrays.asList(lineData); + + if (!allCategories && getLineEntity(lineDataList).compareTo(extraParam) > 0) { + break; + } + + if (!allCategories && !getLineEntity(lineDataList).equals(extraParam)) { + continue; + } + csvFile.add(lineDataList); + } + } catch(FileNotFoundException e) { + e.printStackTrace(); + } + return csvFile; + } + + /** + * Helper method returns the year from the current line in csv file. + * @param lineData current line in csv file + * @return year of current line + */ + private String getLineYear(List lineData) { + return lineData.get(YEAR_COLMN); + } + + /** + * Helper method returns the entity from the current line in csv file. + * @param lineData current line in csv file + * @return entity of current line + */ + private String getLineEntity(List lineData) { + return lineData.get(ENTITY_COLMN); + } + + /** + * Helper method returns the number of incidents from the current line in csv file. + * @param lineData current line in csv file + * @return year of current line + */ + private String getNumIncidents(List lineData) { + return lineData.get(INCIDENTS_COLMN); } } diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index 21bb717..57dedae 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -5,26 +5,54 @@ public class DisasterDescription { private String category = ""; private int reportedIncidents = 0; + /** + * Creates a new Disaster Description object with just a year + * @param year the year of the disaster + */ public DisasterDescription(String year) { this(year, "", 0); } + /** + * Creates a new Disaster Description object with specified parameters + * @param year year of disaster or N/A if over multiple years + * @param type the category of the disaster + * @param reportedIncidents the number of reported incidents + */ public DisasterDescription(String year, String type, int reportedIncidents) { this.year = year; this.category = type; this.reportedIncidents = reportedIncidents; } - public int getReportedIncidentsNum() { + /** + * Getter method for the number of reported incidents. + * @return the number of reported incidents. + */ + public int getReportedIncidentsNum() { return reportedIncidents; } - public void addReportedIncidents(int numIncidents) { + /** + * Adds number of new incidents to the current number of reported incidents. + * @param numIncidents the number of incidents + */ + public void addReportedIncidents(int numIncidents) { reportedIncidents += numIncidents; } - public String getYear() { + + /** + * Getter method for the year + * @return the year + */ + public String getYear() { return year; } + + /** + * Getter method for the category + * @return the category + */ public String getCategory() { return category; } diff --git a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java index f7b5b9b..6c2d47d 100644 --- a/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java +++ b/src/test/java/codingcompetition2019/CodingCompCSVUtilTest.java @@ -10,22 +10,21 @@ public class CodingCompCSVUtilTest extends TestCase { private CodingCompCSVUtil util; - + private List> records; - + private String naturalDisasterByTypeFile = "src/main/resources/natural-disasters-by-type.csv"; - + private String significantEarthquakeFileNameName = "src/main/resources/significant-earthquakes.csv"; - + private String significantVolcanicEruptionsFileName = "src/main/resources/significant-volcanic-eruptions.csv"; @Before public void setUp() throws Exception { util = new CodingCompCSVUtil(); records = util.readCSVFileWithoutHeaders(naturalDisasterByTypeFile); - System.out.println(records); } - + @Test public void testReadCSVFileWithHeadersByCountry() throws IOException { List> tempRecords = util.readCSVFileByCountry(significantEarthquakeFileNameName, "United States"); @@ -90,4 +89,4 @@ public void testFirstRecordsHaveMoreReportedIndicents() throws IOException { assertTrue(util.firstRecordsHaveMoreReportedIndicents(tempRecords1, tempRecords2)); assertFalse(util.firstRecordsHaveMoreReportedIndicents(tempRecords2, tempRecords1)); } -} +} \ No newline at end of file From 4fcc1f80da15546573663a54bcd4015e43dee041 Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sun, 6 Oct 2019 00:22:49 -0400 Subject: [PATCH 12/15] implemented impactful disasters by year --- .../codingcompetition2019/DisasterCLI.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/main/java/codingcompetition2019/DisasterCLI.java diff --git a/src/main/java/codingcompetition2019/DisasterCLI.java b/src/main/java/codingcompetition2019/DisasterCLI.java new file mode 100644 index 0000000..951f4ea --- /dev/null +++ b/src/main/java/codingcompetition2019/DisasterCLI.java @@ -0,0 +1,101 @@ +package codingcompetition2019; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Scanner; +public class DisasterCLI { + private static CodingCompCSVUtil util = new CodingCompCSVUtil(); + private static List> records; + public static void main(String[] args) { + HashMap directions = directions(); + System.out.println("Please enter the number of which method that you want to peruse."); + System.out.println("0: getMostImpactfulYear"); + System.out.println("1: getMostImpactfulYearByCategory"); + System.out.println("2: getMostImpactfulDisasterByYear"); + System.out.println("3: getTotalReportedIncidentsByCategory"); + System.out.println("4: countImpactfulYearsWithReportedIncidentsWithinRange"); + Scanner scanner = new Scanner(System.in); + int method = scanner.nextInt(); + if (method == 0) { + try { + System.out.println(impactfulYear()); + } catch(IOException e) { + e.printStackTrace(); + } + } else if (method == 1) { + System.out.println(directions.get(method)); + String category = scanner.next(); + try { + System.out.println(categoryImpactfulYear(category)); + } catch (IOException e) { + e.printStackTrace(); + } + } else if (method == 2) { + System.out.println(directions.get(method)); + String category = scanner.next(); + try { + System.out.println(yearImpactfulDisaster(category)); + } catch (IOException e) { + e.printStackTrace(); + } + } else if (method == 3) { + System.out.println(directions.get(method)); + String category = scanner.next(); + try { + System.out.println(totalIncidents(category)); + } catch (IOException e) { + e.printStackTrace(); + } + } else if (method == 4){ + System.out.println(directions.get(method)); + String country = scanner.next(); + int min = scanner.nextInt(); + int max = scanner.nextInt(); + try { + System.out.println(rangeIncidents(country, min, max)); + } catch (IOException e) { + e.printStackTrace(); + } + } else { + System.out.println("Please rerun and enter proper input."); + } + + + } + + public static HashMap directions() { + HashMap directions = new HashMap(); + directions.put(1, "Please enter the category you want to look at."); + directions.put(2, "Please enter the year you want to look at."); + directions.put(3, "Please enter the category you want to look at."); + directions.put(4, " First enter a country you want to look at. Then please enter two integer " + + "values. One min and max value which represent your range. "); + return directions; + } + + public static String impactfulYear() throws IOException { + records = util.readCSVFileWithoutHeaders("src/main/resources/natural-disasters-by-type.csv"); + return util.getMostImpactfulYear(records).getYear(); + } + + public static String categoryImpactfulYear(String category) throws IOException { + records = util.readCSVFileWithoutHeaders("src/main/resources/natural-disasters-by-type.csv"); + return util.getMostImpactfulYearByCategory(category, records).getYear(); + } + + public static String yearImpactfulDisaster(String year) throws IOException { + records = util.readCSVFileWithoutHeaders("src/main/resources/natural-disasters-by-type.csv"); + return util.getMostImpactfulDisasterByYear(year, records).getCategory(); + } + + public static int totalIncidents(String category) throws IOException { + records = util.readCSVFileWithoutHeaders("src/main/resources/natural-disasters-by-type.csv"); + return util.getTotalReportedIncidentsByCategory(category, records).getReportedIncidentsNum(); + } + public static int rangeIncidents(String country, int min, int max) throws IOException { + List> tempRecords = util.readCSVFileByCountry("src/main/resources/signifi" + + "cant-volcanic-eruptions.csv", country); + return util.countImpactfulYearsWithReportedIncidentsWithinRange(tempRecords, min, max); + } +} From 0a21ec9c4976774703a69a1051e010fb2de9cd09 Mon Sep 17 00:00:00 2001 From: koolcards Date: Sun, 6 Oct 2019 00:34:29 -0400 Subject: [PATCH 13/15] final updates --- .../CodingCompCSVUtil.java | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 9cf3044..58cc204 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -146,9 +146,6 @@ public DisasterDescription helperGetTotalReportedIncidents (String category, Lis DisasterDescription disaster = new DisasterDescription("N/A", category, 0); for (List record : records) { - if (!allCategories && getLineEntity(record).compareTo(category) > 0) { - break; - } if (!allCategories && !getLineEntity(record).equals(category)) { continue; @@ -174,18 +171,14 @@ public DisasterDescription helperGetMostImpactfulYear (String category, List record : records) { - if (!allCategories && record.get(0).compareTo(category) > 0) { - break; - } - - if (!allCategories && !record.get(0).equals(category)) { + if (!allCategories && !getLineEntity(record).equals(category)) { continue; } - int currImpact = Integer.parseInt(record.get(3)); + int currImpact = Integer.parseInt(getNumIncidents(record)); if (currImpact > maxYearImpact) { maxYearImpact = currImpact; - mostImpactfulYear = record.get(2); + mostImpactfulYear = getLineYear(record); } } return new DisasterDescription(mostImpactfulYear); @@ -221,11 +214,6 @@ private List> helperReadCSVFile(String fileName, String extraParam) } String[] lineData = line.split(COMMA_DELIMITER, -1); List lineDataList = Arrays.asList(lineData); - - if (!allCategories && getLineEntity(lineDataList).compareTo(extraParam) > 0) { - break; - } - if (!allCategories && !getLineEntity(lineDataList).equals(extraParam)) { continue; } @@ -254,7 +242,7 @@ private String getLineYear(List lineData) { private String getLineEntity(List lineData) { return lineData.get(ENTITY_COLMN); } - + /** * Helper method returns the number of incidents from the current line in csv file. * @param lineData current line in csv file From 8db9787e609cb46c8cf9cc1b5cb99628c36736c2 Mon Sep 17 00:00:00 2001 From: rmitra34 Date: Sun, 6 Oct 2019 00:36:33 -0400 Subject: [PATCH 14/15] fixed input exception --- src/main/java/codingcompetition2019/DisasterCLI.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/codingcompetition2019/DisasterCLI.java b/src/main/java/codingcompetition2019/DisasterCLI.java index 951f4ea..5f9a34b 100644 --- a/src/main/java/codingcompetition2019/DisasterCLI.java +++ b/src/main/java/codingcompetition2019/DisasterCLI.java @@ -16,7 +16,13 @@ public static void main(String[] args) { System.out.println("3: getTotalReportedIncidentsByCategory"); System.out.println("4: countImpactfulYearsWithReportedIncidentsWithinRange"); Scanner scanner = new Scanner(System.in); - int method = scanner.nextInt(); + String numMethod = scanner.next(); + int method = -1; + try { + method = Integer.parseInt(numMethod); + } catch(Exception e) { + + } if (method == 0) { try { System.out.println(impactfulYear()); From 3d3c6fdc62df6c94e193623e7f728b4d1c04f0b5 Mon Sep 17 00:00:00 2001 From: koolcards Date: Sun, 6 Oct 2019 00:37:20 -0400 Subject: [PATCH 15/15] feedback added --- feedback.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/feedback.txt b/feedback.txt index 8234592..568dd8e 100644 --- a/feedback.txt +++ b/feedback.txt @@ -1,9 +1,9 @@ -Your team (name of each individual participating): -How many JUnits were you able to get to pass? +Your team (name of each individual participating): Kris Satya and Rishab Mitra +How many JUnits were you able to get to pass? 10/10 Document and describe any enhancements included to help the judges properly grade your submission. - Step 1: - Step 2: + Step 1: All methods documented with javadocs + Step 2: Added command line test that can be executed by running main method of DisasterCLI Feedback for the coding competition? Things you would like to see in future events? \ No newline at end of file