Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/target/
/.idea
.classpath
.project
/.settings
17 changes: 17 additions & 0 deletions codingcompetition2019.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
</component>
</module>
13 changes: 8 additions & 5 deletions feedback.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Your team (name of each individual participating):
How many JUnits were you able to get to pass?
Your team (name of each individual participating): Ishan Baniya
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: Added meaningful description for each methods in CodingCompCSVUtil.java file for other developers to understand the code easily.
Step 2: Added a GUI that allows users to view the most impactful disaster by year and category.


Feedback for the coding competition? Things you would like to see in future events?
Feedback for the coding competition? Things you would like to see in future events?
# It would have been better to have comments/example on the methods so it would be easier to know what to do for the implementation.


309 changes: 257 additions & 52 deletions src/main/java/codingcompetition2019/CodingCompCSVUtil.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,263 @@
package codingcompetition2019;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.*;

public class CodingCompCSVUtil {
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
// TODO implement this method
return null;
}

public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
// TODO implement this method
return null;
}

public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
// TODO implement this method
return null;
}

public DisasterDescription getMostImpactfulYear(List<List<String>> records) {
// TODO implement this method
return null;
}

public DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {
// TODO implement this method
return null;
}

public DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {
// TODO implement this method
return null;
}

public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {
// TODO implement this method
return null;
}

/**
* 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<List<String>> records, int min, int max) {
// TODO implement this method
return -1;
}

public boolean firstRecordsHaveMoreReportedIndicents(List<List<String>> records1, List<List<String>> records2) {
// TODO implement this method
return false;
}

/**
* This method reads the CSV file and returns the data about the specified country
* Each row has a data about the disaster for the a year separated by comma.
* Store the data separated by comma in a list if the name of the country matches.
* Store all the list with the matching country name in a list and return it.
*
* @param fileName filePath of the CSV file with data
* @param countryName name of the country to filter the records by
* @return Data about the disaster for that country
* @throws IOException error on reading the file
*/
public List<List<String>> readCSVFileByCountry(String fileName, String countryName) throws IOException {
List<List<String>> countries = readCSVFileWithoutHeaders(fileName);

// Filter the csv values by country
List<List<String>> countryEntries = new ArrayList<List<String>>();
for (List<String> entry : countries) {
// Check if the name of the country matches
if (entry.get(0).trim().toLowerCase().equals(countryName.trim().toLowerCase())) {
countryEntries.add(entry);
}
}
return countryEntries;
}

/**
* This method reads the CSV file including the header.
* Each line of the CSV has data about the disaster separated by comma. Store it in a list.
* Add that list to a master list and return the master list.
*
* @param fileName filePath of the CSV file with data
* @return list with a list of data related to the disaster
* @throws IOException error on reading the file
*/
public List<List<String>> readCSVFileWithHeaders(String fileName) throws IOException {
// Get all entries without the header
File inputFile = new File(fileName);

List<List<String>> csvEntriesWithHeader = new ArrayList<List<String>>();

if (inputFile.exists()) {
try {
Scanner input = new Scanner(inputFile);
// Read each line an add it to the list
while (input.hasNext()) {
String currentLine = input.nextLine();
// Split the line by comma and store it as array
List<String> values = Arrays.asList(currentLine.split(","));
csvEntriesWithHeader.add(values);
}

input.close();
} catch (IOException ex) {
throw ex;
} catch (Exception ex) {
// Other unexpected error occurred
ex.printStackTrace();
return null;
}
}

return csvEntriesWithHeader;
}

/**
* This method reads the CSV file without the header.
* Each line of the CSV has data about the disaster separated by comma. Store it in a list
* Add that list to a master list and return the master list.
*
* @param fileName filePath of the CSV file with data
* @return list with a list of data related to the disaster
* @throws IOException error on reading the file
*/
public List<List<String>> readCSVFileWithoutHeaders(String fileName) throws IOException {
// Get all records with the header
List<List<String>> allCSVEntries = readCSVFileWithHeaders(fileName);

//Remove the first entry of header
allCSVEntries.remove(0);

return allCSVEntries;
}

/**
* This method iterates through the records list and checks the number of reported incidents for each record
* It finds the year with the most number of reported incidents
*
* @param records list of data related to a disaster event
* @return info about the most impactful Disaster based on year
*/
public DisasterDescription getMostImpactfulYear(List<List<String>> records) {

// See if there is at least one record
if (records.size() > 0) {

//Assume the first entry is the most impactful
int year = Integer.parseInt(records.get(0).get(2));
int maxImpact = Integer.parseInt(records.get(0).get(3));
String entity = records.get(0).get(0);

//Iterate through each entry and get the find the most impact
for (List<String> entry : records) {
int currentImpact = Integer.parseInt(entry.get(3));
if (currentImpact > maxImpact) {
maxImpact = currentImpact;
year = Integer.parseInt(entry.get(2));
entity = entry.get(0);
}
}

DisasterDescription disaster = new DisasterDescription();
disaster.setYear(year);
disaster.setReportedIncidentsNum(maxImpact);
disaster.setCategory(entity);

return disaster;
}

return new DisasterDescription();
}

/**
* This method filters the records by the given category.
* It calls getMostImpactfulYear() method to find the most impactful year after filtering the list.
* Then returns the info about the most impactful disaster for that given category.
*
* @param category Category of disaster
* @param records List of data about the disaster
* @return Info about the most impactful disaster for the given category
*/
public DisasterDescription getMostImpactfulYearByCategory(String category, List<List<String>> records) {

//Filter the list by category
List<List<String>> recordByCategory = new LinkedList<List<String>>();
for (List<String> record : records) {
// Check if the category matches
if (record.get(0).toLowerCase().trim().equals(category.toLowerCase().trim())) {
recordByCategory.add(record);
}
}

return getMostImpactfulYear(recordByCategory);
}

/**
* This method filters the list by the given year.
* It calls getMostImpactfulYear() method to find the most impactful year after filtering the list.
* Then it returns info about the most impactful disaster for that given year.
*
* @param year Filter the record based on this year
* @param records List of data about the disaster
* @return Info about the most impactful disaster for the given year
*/
public DisasterDescription getMostImpactfulDisasterByYear(String year, List<List<String>> records) {

// Filter the record by year
List<List<String>> recordsByYear = new LinkedList<List<String>>();
for (List<String> record : records) {
if (record.get(2).trim().toLowerCase().equals(year.trim().toLowerCase())) {
// Ignore for all natural disasters
if (!record.get(0).trim().toLowerCase().equals("all natural disasters")) {
recordsByYear.add(record);
}
}
}

return getMostImpactfulYear(recordsByYear);
}

/**
* This method iterates through the list of records
* For each record, if the category matches, it retrieves the number of reported incidents
* Then it calculates the total number of reported incidents for that category.
*
* @param category Disaster category to filter the records
* @param records List of data about disaster
* @return Total number of reported incidents for the given category
*/
public DisasterDescription getTotalReportedIncidentsByCategory(String category, List<List<String>> records) {

int totalIncidents = 0;
for (List<String> record : records) {
// Check if the category matches
if (record.get(0).toLowerCase().trim().equals(category.toLowerCase().trim())) {
totalIncidents += Integer.parseInt(record.get(3));
}
}

DisasterDescription disaster = new DisasterDescription();
disaster.setCategory(category);
disaster.setReportedIncidentsNum(totalIncidents);

return disaster;
}


/**
* 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<List<String>> records, int min, int max) {
boolean hasMaximumRange = max != -1;
int count = 0;

for (List<String> record : records) {
int reportedIncident = Integer.parseInt(record.get(3));

if (hasMaximumRange) {
if (reportedIncident >= min && reportedIncident <= max) {
count++;
}
} else { // No maximum range
if (reportedIncident >= min) {
count++;
}
}
}

return count;
}

/**
* This method checks if the first list has more reported incidents than the second list.
* It calls getTotalReportedIncidents() to get the total number of reported incidents for both records
* It compares the total number of reported incidents for both records.
*
* @param records1 List of data about disaster
* @param records2 List of data about disaster
* @return Boolean value indicating record1 has more reported incidents than record2
*/
public boolean firstRecordsHaveMoreReportedIndicents(List<List<String>> records1, List<List<String>> records2) {
return getTotalReportedIncidents(records1) > getTotalReportedIncidents(records2);
}

/**
* This method iterates through the records
* For each record, it retrieves the number of reported incident.
* It calculates the total reported incidents for that given data.
*
* @param records List of data about the disaster
* @return int total reported incidents
*/
private int getTotalReportedIncidents(List<List<String>> records) {
int total = 0;
for (List<String> entry : records) {
total += Integer.parseInt(entry.get(3));
}
return total;
}


}
Loading