Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poverty project #2

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
52 changes: 52 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.DS_Store

.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# Eclipse Core
.project

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# JDT-specific (Eclipse Java Development Tools)
.classpath

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/
30 changes: 30 additions & 0 deletions PovertyInfo/src/interactiveinfo/GetPovertyInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package interactiveinfo;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class GetPovertyInfo {

public static String getMatch(String county) throws FileNotFoundException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you added the throws here. I would say it is more typical to put a try and catch around the block of code than to put the throws at the method level.

File data = new File("support/povWA.txt");
Scanner scnr = new Scanner(data);
scnr.nextLine(); // skip first line

while(scnr.hasNextLine()) {
String line = scnr.nextLine();
if ((county + " County").equals(line.substring(193, 238).trim()) ) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you adjust the substring function to ignore the "county" part and simply check if county.equals( etc?

String match = line;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this extra variable needed?

scnr.close();
return match;
}
}

scnr.close();
return null;
}

public static void printCountyInfo(String countyRecord) {
System.out.println(String.format("In %s, %s children (%s%%) are in poverty. The median household income is $%s.", countyRecord.substring(193, 239).trim(), countyRecord.substring(49, 57).trim(), countyRecord.substring(76, 80).trim(), countyRecord.substring(133, 139).trim()));
}
}
35 changes: 35 additions & 0 deletions PovertyInfo/src/interactiveinfo/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package interactiveinfo;

import java.io.FileNotFoundException;
import java.util.Scanner;

import org.apache.commons.lang3.text.WordUtils;

public class Program {

public static void main(String[] args) throws FileNotFoundException {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter a county name in Washington (e.g., King), or type `quit` to quit: ");
String input = WordUtils.capitalizeFully(scnr.nextLine().trim());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job finding the functions that would do this for you


String record = null;
while (!input.equals("Quit") && !input.equals("Q")) {
record = GetPovertyInfo.getMatch(input);

while (record == null) {
scnr = new Scanner(System.in);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could adjust to have record start out as null and put all of the code from lines 11-13 in this while look as well so that none of it is duplicated. (Though, this would change your error message for the user)

System.out.println("No match found. Try again: ");
input = WordUtils.capitalizeFully(scnr.nextLine().trim());
record = GetPovertyInfo.getMatch(input);
}

GetPovertyInfo.printCountyInfo(record);
scnr = new Scanner(System.in);
System.out.println("Enter another county, or type `quit` to quit: ");
input = WordUtils.capitalizeFully(scnr.nextLine().trim());
}

scnr.close();
}

}
87 changes: 87 additions & 0 deletions PovertyInfo/src/stateinfo/GetPovertyInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package stateinfo;

import java.util.Scanner;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.io.FileNotFoundException;

public class GetPovertyInfo {

private static final Map<String, String> stateFiles;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good use of the static final since this data shouldn't change

static
{
stateFiles = new HashMap<String, String>();
stateFiles.put("Washington", "support/povWA.txt");
stateFiles.put("South Dakota", "support/povSD.txt");
}

public static String getMinimumChildPovertyRecord(String state) throws FileNotFoundException {
File data = new File(stateFiles.get(state));
Scanner scnr = new Scanner(data);

scnr.nextLine(); // skip first line

String minCountyRecord = null;
Double currentMin = null;

while(scnr.hasNextLine()){

String line = scnr.nextLine();

if (currentMin == null) {
currentMin = Double.parseDouble(line.substring(76, 80));
minCountyRecord = line;
}

if (Double.parseDouble(line.substring(76, 80)) < currentMin) {
currentMin = Double.parseDouble(line.substring(76, 80));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these same substring values are used many times. line.substring(76,80). Consider setting the result of this equal to a variable once for reuse purposes.

minCountyRecord = line;
}

}

scnr.close();
return minCountyRecord;
}

public static String getMaximumChildPovertyRecord(String state) throws FileNotFoundException {
File data = new File(stateFiles.get(state));
Scanner scnr = new Scanner(data);

scnr.nextLine(); // skip first line

String maxCountyRecord = null;
Double currentMax = null;

while(scnr.hasNextLine()){

String line = scnr.nextLine();

if (currentMax == null) {
currentMax = Double.parseDouble(line.substring(76, 80));
maxCountyRecord = line;
}

if (Double.parseDouble(line.substring(76, 80)) > currentMax) {
currentMax = Double.parseDouble(line.substring(76, 80));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment re: this line.substring

maxCountyRecord = line;
}

}

scnr.close();
return maxCountyRecord;
}

public static void printChildPovertyInfo(String state) throws FileNotFoundException {
String minCountyRecord = GetPovertyInfo.getMinimumChildPovertyRecord(state);
String maxCountyRecord = GetPovertyInfo.getMaximumChildPovertyRecord(state);

System.out.println(String.format("In %s:", state));
System.out.println(String.format("The county with the lowest percentage of children in poverty is %s. In that county, %s children (%s%%) are in poverty. The median household income is $%s.", minCountyRecord.substring(193, 239).trim(), minCountyRecord.substring(49, 57).trim(), minCountyRecord.substring(76, 80).trim(), minCountyRecord.substring(133, 139).trim()));
System.out.println(String.format("The county with the highest percentage of children in poverty is %s. In that county, %s children (%s%%) are in poverty. The median household income is $%s.", maxCountyRecord.substring(193, 239).trim(), maxCountyRecord.substring(49, 57).trim(), maxCountyRecord.substring(76, 80).trim(), maxCountyRecord.substring(133, 139).trim()));

}

}
12 changes: 12 additions & 0 deletions PovertyInfo/src/stateinfo/Program.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package stateinfo;

import java.io.FileNotFoundException;

public class Program {

public static void main(String[] args) throws FileNotFoundException {
GetPovertyInfo.printChildPovertyInfo("Washington");
GetPovertyInfo.printChildPovertyInfo("South Dakota");
}

}
Binary file added PovertyInfo/support/commons-lang3-3.4.jar
Binary file not shown.
Loading