From 08fc4255c11832664a901e449cf5bf4630f085d4 Mon Sep 17 00:00:00 2001 From: Jacob Williams <51760452+JacobWilliams180@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:11:08 -0700 Subject: [PATCH 1/6] added solutions --- CodingCompCSVUtil.java | 109 +++++++++++++++++++++++++++++++++++++++ DisasterDescription.java | 104 +++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+) create mode 100644 CodingCompCSVUtil.java create mode 100644 DisasterDescription.java diff --git a/CodingCompCSVUtil.java b/CodingCompCSVUtil.java new file mode 100644 index 0000000..811c56c --- /dev/null +++ b/CodingCompCSVUtil.java @@ -0,0 +1,109 @@ +package codingcompetition2019; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class CodingCompCSVUtil { + public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { + Scanner sc = new Scanner(new File(fileName)); + List> fin = new ArrayList>(); + while(sc.hasNextLine()) { + String line = sc.nextLine(); + String[] ar = line.split(","); + if(ar[0].equals(countryName)) { + ArrayList temp = new ArrayList(); + temp.add(line); + fin.add(temp); + } + } + sc.close(); + return fin; + } + + public List> readCSVFileWithHeaders(String fileName) throws IOException { + Scanner sc = new Scanner(new File(fileName)); + List> fin = new ArrayList>(); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + String[]ar = line.split(","); + ArrayList temp = new ArrayList(); + temp.add(line); + fin.add(temp); + } + sc.close(); + return fin; + } + + public List> readCSVFileWithoutHeaders(String fileName) throws IOException { + Scanner sc = new Scanner(new File(fileName)); + sc.nextLine(); + List> fin = new ArrayList>(); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + String[]ar = line.split(","); + ArrayList temp = new ArrayList(); + temp.add(line); + fin.add(temp); + } + sc.close(); + return fin; + } + + public DisasterDescription getMostImpactfulYear(List> records) { + DisasterDescription dd = new DisasterDescription(records); + return dd; + } + + public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { + DisasterDescription dd = new DisasterDescription(records); + dd.setYearByCategory(category); + return dd; + } + + public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { + DisasterDescription dd = new DisasterDescription(records); + dd.setDisasterByYear(year); + return dd; + } + + public DisasterDescription getTotalReportedIncidentsByCategory(String category, List> records) { + DisasterDescription dd = new DisasterDescription(records); + dd.addUpIncedentsByCategory(category); + return dd; + } + + /** + * 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) { + int inc = 0; + for(int i = 0; i < records.size();i++) { + String[] ar = records.get(i).get(0).split(","); + System.out.println(ar[0] + " "+ ar[3] +" " + records.size()); + if(max == -1) { + if(Integer.parseInt(ar[3]) >= min){ + inc += 1; + } + }else { + if(Integer.parseInt(ar[3]) >= min && Integer.parseInt(ar[3])<=max){ + inc += 1; + } + } + + } + return inc; + } + + public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { + DisasterDescription dd1 = new DisasterDescription(records1); + DisasterDescription dd2 = new DisasterDescription(records2); + return dd1.getReportedIncidentsNum() > dd2.getReportedIncidentsNum(); + } +} diff --git a/DisasterDescription.java b/DisasterDescription.java new file mode 100644 index 0000000..3a33a4f --- /dev/null +++ b/DisasterDescription.java @@ -0,0 +1,104 @@ +package codingcompetition2019; + +import java.util.List; + +public class DisasterDescription { + private String year; + private String disaster; + private int inc; + private List> records; + + + public DisasterDescription(List> records) { + this.records = records; + String year = ""; + int inc = 0; + for(int i = 0; i < records.size();i++) { + //System.out.println(records.get(i).get(0)+ " "+ i); + String[] ar = records.get(i).get(0).split(","); + if(ar.length == 4) { + if(Integer.parseInt(ar[3]) > inc){ + inc = Integer.parseInt(ar[3]); + year = ar[2]; + } + } + } + this.inc = inc; + this.year = year; + } + + public String getYear() { + return year; + } + + public void setYearByCategory(String category) { + String year = ""; + int inc = 0; + for(int i = 0; i < records.size();i++) { + //System.out.println(records.get(i).get(0)+ " "+ i); + String[] ar = records.get(i).get(0).split(","); + if(ar.length == 4) { + if(Integer.parseInt(ar[3]) > inc && ar[0].equals(category)){ + inc = Integer.parseInt(ar[3]); + year = ar[2]; + } + } + } + this.year = year; + } + + public void setDisasterByYear(String year) { + String disaster = ""; + int inc = 0; + for(int i = 0; i < records.size();i++) { + //System.out.println(records.get(i).get(0)+ " "+ i); + String[] ar = records.get(i).get(0).split(","); + if(!ar[0].equals("All natural disasters")) { + if(Integer.parseInt(ar[3]) > inc && ar[2].equals(year)){ + inc = Integer.parseInt(ar[3]); + disaster = ar[0]; + } + } + + } + this.inc = inc; + this.disaster = disaster; + } + + public String getCategory() { + return disaster; + } + + public int getReportedIncidentsNum() { + return inc; + } + + public void setIncByCategory(String category) { + int inc = 0; + for(int i = 0; i < records.size();i++) { + String[] ar = records.get(i).get(0).split(","); + if(!ar[0].equals("All natural disasters")) { + if(Integer.parseInt(ar[3]) > inc && ar[0].equals(category)){ + inc = Integer.parseInt(ar[3]); + } + } + + } + this.inc = inc; + } + + public void addUpIncedentsByCategory(String category) { + int inc = 0; + for(int i = 0; i < records.size();i++) { + String[] ar = records.get(i).get(0).split(","); + if(!ar[0].equals("All natural disasters")) { + if(ar[0].equals(category)){ + inc += Integer.parseInt(ar[3]); + } + } + + } + this.inc = inc; + } + +} From 04143b209ad648f851ac0898f9103cb05e4dec92 Mon Sep 17 00:00:00 2001 From: Jacob Williams <51760452+JacobWilliams180@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:20:32 -0700 Subject: [PATCH 2/6] Delete CodingCompCSVUtil.java --- CodingCompCSVUtil.java | 109 ----------------------------------------- 1 file changed, 109 deletions(-) delete mode 100644 CodingCompCSVUtil.java diff --git a/CodingCompCSVUtil.java b/CodingCompCSVUtil.java deleted file mode 100644 index 811c56c..0000000 --- a/CodingCompCSVUtil.java +++ /dev/null @@ -1,109 +0,0 @@ -package codingcompetition2019; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.Scanner; - -public class CodingCompCSVUtil { - public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { - Scanner sc = new Scanner(new File(fileName)); - List> fin = new ArrayList>(); - while(sc.hasNextLine()) { - String line = sc.nextLine(); - String[] ar = line.split(","); - if(ar[0].equals(countryName)) { - ArrayList temp = new ArrayList(); - temp.add(line); - fin.add(temp); - } - } - sc.close(); - return fin; - } - - public List> readCSVFileWithHeaders(String fileName) throws IOException { - Scanner sc = new Scanner(new File(fileName)); - List> fin = new ArrayList>(); - while (sc.hasNextLine()) { - String line = sc.nextLine(); - String[]ar = line.split(","); - ArrayList temp = new ArrayList(); - temp.add(line); - fin.add(temp); - } - sc.close(); - return fin; - } - - public List> readCSVFileWithoutHeaders(String fileName) throws IOException { - Scanner sc = new Scanner(new File(fileName)); - sc.nextLine(); - List> fin = new ArrayList>(); - while (sc.hasNextLine()) { - String line = sc.nextLine(); - String[]ar = line.split(","); - ArrayList temp = new ArrayList(); - temp.add(line); - fin.add(temp); - } - sc.close(); - return fin; - } - - public DisasterDescription getMostImpactfulYear(List> records) { - DisasterDescription dd = new DisasterDescription(records); - return dd; - } - - public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { - DisasterDescription dd = new DisasterDescription(records); - dd.setYearByCategory(category); - return dd; - } - - public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { - DisasterDescription dd = new DisasterDescription(records); - dd.setDisasterByYear(year); - return dd; - } - - public DisasterDescription getTotalReportedIncidentsByCategory(String category, List> records) { - DisasterDescription dd = new DisasterDescription(records); - dd.addUpIncedentsByCategory(category); - return dd; - } - - /** - * 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) { - int inc = 0; - for(int i = 0; i < records.size();i++) { - String[] ar = records.get(i).get(0).split(","); - System.out.println(ar[0] + " "+ ar[3] +" " + records.size()); - if(max == -1) { - if(Integer.parseInt(ar[3]) >= min){ - inc += 1; - } - }else { - if(Integer.parseInt(ar[3]) >= min && Integer.parseInt(ar[3])<=max){ - inc += 1; - } - } - - } - return inc; - } - - public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { - DisasterDescription dd1 = new DisasterDescription(records1); - DisasterDescription dd2 = new DisasterDescription(records2); - return dd1.getReportedIncidentsNum() > dd2.getReportedIncidentsNum(); - } -} From dc0b2508f55843fb9f1562bfae3c70fa44132b71 Mon Sep 17 00:00:00 2001 From: Jacob Williams <51760452+JacobWilliams180@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:20:42 -0700 Subject: [PATCH 3/6] Delete DisasterDescription.java --- DisasterDescription.java | 104 --------------------------------------- 1 file changed, 104 deletions(-) delete mode 100644 DisasterDescription.java diff --git a/DisasterDescription.java b/DisasterDescription.java deleted file mode 100644 index 3a33a4f..0000000 --- a/DisasterDescription.java +++ /dev/null @@ -1,104 +0,0 @@ -package codingcompetition2019; - -import java.util.List; - -public class DisasterDescription { - private String year; - private String disaster; - private int inc; - private List> records; - - - public DisasterDescription(List> records) { - this.records = records; - String year = ""; - int inc = 0; - for(int i = 0; i < records.size();i++) { - //System.out.println(records.get(i).get(0)+ " "+ i); - String[] ar = records.get(i).get(0).split(","); - if(ar.length == 4) { - if(Integer.parseInt(ar[3]) > inc){ - inc = Integer.parseInt(ar[3]); - year = ar[2]; - } - } - } - this.inc = inc; - this.year = year; - } - - public String getYear() { - return year; - } - - public void setYearByCategory(String category) { - String year = ""; - int inc = 0; - for(int i = 0; i < records.size();i++) { - //System.out.println(records.get(i).get(0)+ " "+ i); - String[] ar = records.get(i).get(0).split(","); - if(ar.length == 4) { - if(Integer.parseInt(ar[3]) > inc && ar[0].equals(category)){ - inc = Integer.parseInt(ar[3]); - year = ar[2]; - } - } - } - this.year = year; - } - - public void setDisasterByYear(String year) { - String disaster = ""; - int inc = 0; - for(int i = 0; i < records.size();i++) { - //System.out.println(records.get(i).get(0)+ " "+ i); - String[] ar = records.get(i).get(0).split(","); - if(!ar[0].equals("All natural disasters")) { - if(Integer.parseInt(ar[3]) > inc && ar[2].equals(year)){ - inc = Integer.parseInt(ar[3]); - disaster = ar[0]; - } - } - - } - this.inc = inc; - this.disaster = disaster; - } - - public String getCategory() { - return disaster; - } - - public int getReportedIncidentsNum() { - return inc; - } - - public void setIncByCategory(String category) { - int inc = 0; - for(int i = 0; i < records.size();i++) { - String[] ar = records.get(i).get(0).split(","); - if(!ar[0].equals("All natural disasters")) { - if(Integer.parseInt(ar[3]) > inc && ar[0].equals(category)){ - inc = Integer.parseInt(ar[3]); - } - } - - } - this.inc = inc; - } - - public void addUpIncedentsByCategory(String category) { - int inc = 0; - for(int i = 0; i < records.size();i++) { - String[] ar = records.get(i).get(0).split(","); - if(!ar[0].equals("All natural disasters")) { - if(ar[0].equals(category)){ - inc += Integer.parseInt(ar[3]); - } - } - - } - this.inc = inc; - } - -} From 1505a75f6f4d98c52b7a3c1ab4cd59d5dd3f1350 Mon Sep 17 00:00:00 2001 From: Jacob Williams <51760452+JacobWilliams180@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:21:26 -0700 Subject: [PATCH 4/6] Updated code with my solutions --- .../CodingCompCSVUtil.java | 87 +++++++++++++++---- 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java index 74f3074..811c56c 100644 --- a/src/main/java/codingcompetition2019/CodingCompCSVUtil.java +++ b/src/main/java/codingcompetition2019/CodingCompCSVUtil.java @@ -1,42 +1,78 @@ package codingcompetition2019; +import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import java.util.Scanner; public class CodingCompCSVUtil { public List> readCSVFileByCountry(String fileName, String countryName) throws IOException { - // TODO implement this method - return null; + Scanner sc = new Scanner(new File(fileName)); + List> fin = new ArrayList>(); + while(sc.hasNextLine()) { + String line = sc.nextLine(); + String[] ar = line.split(","); + if(ar[0].equals(countryName)) { + ArrayList temp = new ArrayList(); + temp.add(line); + fin.add(temp); + } + } + sc.close(); + return fin; } public List> readCSVFileWithHeaders(String fileName) throws IOException { - // TODO implement this method - return null; + Scanner sc = new Scanner(new File(fileName)); + List> fin = new ArrayList>(); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + String[]ar = line.split(","); + ArrayList temp = new ArrayList(); + temp.add(line); + fin.add(temp); + } + sc.close(); + return fin; } public List> readCSVFileWithoutHeaders(String fileName) throws IOException { - // TODO implement this method - return null; + Scanner sc = new Scanner(new File(fileName)); + sc.nextLine(); + List> fin = new ArrayList>(); + while (sc.hasNextLine()) { + String line = sc.nextLine(); + String[]ar = line.split(","); + ArrayList temp = new ArrayList(); + temp.add(line); + fin.add(temp); + } + sc.close(); + return fin; } public DisasterDescription getMostImpactfulYear(List> records) { - // TODO implement this method - return null; + DisasterDescription dd = new DisasterDescription(records); + return dd; } public DisasterDescription getMostImpactfulYearByCategory(String category, List> records) { - // TODO implement this method - return null; + DisasterDescription dd = new DisasterDescription(records); + dd.setYearByCategory(category); + return dd; } public DisasterDescription getMostImpactfulDisasterByYear(String year, List> records) { - // TODO implement this method - return null; + DisasterDescription dd = new DisasterDescription(records); + dd.setDisasterByYear(year); + return dd; } public DisasterDescription getTotalReportedIncidentsByCategory(String category, List> records) { - // TODO implement this method - return null; + DisasterDescription dd = new DisasterDescription(records); + dd.addUpIncedentsByCategory(category); + return dd; } /** @@ -47,12 +83,27 @@ 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; + int inc = 0; + for(int i = 0; i < records.size();i++) { + String[] ar = records.get(i).get(0).split(","); + System.out.println(ar[0] + " "+ ar[3] +" " + records.size()); + if(max == -1) { + if(Integer.parseInt(ar[3]) >= min){ + inc += 1; + } + }else { + if(Integer.parseInt(ar[3]) >= min && Integer.parseInt(ar[3])<=max){ + inc += 1; + } + } + + } + return inc; } public boolean firstRecordsHaveMoreReportedIndicents(List> records1, List> records2) { - // TODO implement this method - return false; + DisasterDescription dd1 = new DisasterDescription(records1); + DisasterDescription dd2 = new DisasterDescription(records2); + return dd1.getReportedIncidentsNum() > dd2.getReportedIncidentsNum(); } } From 7a8a229aeb930fd2b15fc547ffe6a263a0db291f Mon Sep 17 00:00:00 2001 From: Jacob Williams <51760452+JacobWilliams180@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:22:09 -0700 Subject: [PATCH 5/6] Updated Class with my solutions --- .../DisasterDescription.java | 101 +++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/src/main/java/codingcompetition2019/DisasterDescription.java b/src/main/java/codingcompetition2019/DisasterDescription.java index 662626b..3a33a4f 100644 --- a/src/main/java/codingcompetition2019/DisasterDescription.java +++ b/src/main/java/codingcompetition2019/DisasterDescription.java @@ -1,5 +1,104 @@ package codingcompetition2019; +import java.util.List; + public class DisasterDescription { - // TODO finish this class + private String year; + private String disaster; + private int inc; + private List> records; + + + public DisasterDescription(List> records) { + this.records = records; + String year = ""; + int inc = 0; + for(int i = 0; i < records.size();i++) { + //System.out.println(records.get(i).get(0)+ " "+ i); + String[] ar = records.get(i).get(0).split(","); + if(ar.length == 4) { + if(Integer.parseInt(ar[3]) > inc){ + inc = Integer.parseInt(ar[3]); + year = ar[2]; + } + } + } + this.inc = inc; + this.year = year; + } + + public String getYear() { + return year; + } + + public void setYearByCategory(String category) { + String year = ""; + int inc = 0; + for(int i = 0; i < records.size();i++) { + //System.out.println(records.get(i).get(0)+ " "+ i); + String[] ar = records.get(i).get(0).split(","); + if(ar.length == 4) { + if(Integer.parseInt(ar[3]) > inc && ar[0].equals(category)){ + inc = Integer.parseInt(ar[3]); + year = ar[2]; + } + } + } + this.year = year; + } + + public void setDisasterByYear(String year) { + String disaster = ""; + int inc = 0; + for(int i = 0; i < records.size();i++) { + //System.out.println(records.get(i).get(0)+ " "+ i); + String[] ar = records.get(i).get(0).split(","); + if(!ar[0].equals("All natural disasters")) { + if(Integer.parseInt(ar[3]) > inc && ar[2].equals(year)){ + inc = Integer.parseInt(ar[3]); + disaster = ar[0]; + } + } + + } + this.inc = inc; + this.disaster = disaster; + } + + public String getCategory() { + return disaster; + } + + public int getReportedIncidentsNum() { + return inc; + } + + public void setIncByCategory(String category) { + int inc = 0; + for(int i = 0; i < records.size();i++) { + String[] ar = records.get(i).get(0).split(","); + if(!ar[0].equals("All natural disasters")) { + if(Integer.parseInt(ar[3]) > inc && ar[0].equals(category)){ + inc = Integer.parseInt(ar[3]); + } + } + + } + this.inc = inc; + } + + public void addUpIncedentsByCategory(String category) { + int inc = 0; + for(int i = 0; i < records.size();i++) { + String[] ar = records.get(i).get(0).split(","); + if(!ar[0].equals("All natural disasters")) { + if(ar[0].equals(category)){ + inc += Integer.parseInt(ar[3]); + } + } + + } + this.inc = inc; + } + } From d1f95cf04bacf67ad5668e4c42cdc1f3bec5775a Mon Sep 17 00:00:00 2001 From: Jacob Williams <51760452+JacobWilliams180@users.noreply.github.com> Date: Sat, 5 Oct 2019 21:22:39 -0700 Subject: [PATCH 6/6] Updated feedback with mine --- feedback.txt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/feedback.txt b/feedback.txt index 8234592..920dab6 100644 --- a/feedback.txt +++ b/feedback.txt @@ -1,9 +1,11 @@ -Your team (name of each individual participating): -How many JUnits were you able to get to pass? +Your team (name of each individual participating): Jacob Williams and Paul Cseley +How many JUnits were you able to get to pass? all 10 tests Document and describe any enhancements included to help the judges properly grade your submission. - Step 1: - Step 2: + Step 1: Alot of System.out.println() statments called and a couple left over + Step 2: just started storing data into the lists and parsing into Objects + Step 3: used my new Objects and methods to simplify more code -Feedback for the coding competition? Things you would like to see in future events? \ No newline at end of file +Feedback for the coding competition? Things you would like to see in future events? I enjoyed the events but I think +adding an instruction for the IDE and other applications before the start would be good.