-
-
Save dumptruckman/0cc849da0c56c1e4125942a152162e51 to your computer and use it in GitHub Desktop.
Program 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************************************** | |
// Programmer: Jonathan Wheeler CSC110 Programming Assignment 5 | |
// Date: May 8, 2017 | |
// Description: | |
// Input: | |
// Output: | |
****************************************************************************/ | |
import java.io.*; | |
import java.util.*; | |
public class Prog05 { | |
private static Scanner console = new Scanner(System.in); | |
public static void main(String[] args) throws FileNotFoundException{ | |
System.out.println("Give me a 5-digit zip code and a"); | |
System.out.println("proximity and I'll tell you where"); | |
System.out.println("that zip code is located along"); | |
System.out.println("with a list of other zip codes"); | |
System.out.println("within the given proximity."); | |
System.out.println(); | |
System.out.println(); | |
Zipcode[] zipcodes = readFile(); | |
Scanner console = new Scanner(System.in); | |
int option = 0; | |
while (option != 3) { | |
System.out.println(" 1. Find zip codes in a radius"); | |
System.out.println(" 2. Find zip codes in a state"); | |
System.out.println(" 3. Exit application"); | |
System.out.println("Your choice:"); | |
if (console.hasNextInt()) { | |
option = console.nextInt(); | |
} else { | |
System.our.println("Invalid selection. Please enter 1-3."); | |
} | |
console.nextLine(); | |
//add input checking for choice | |
if (option == 1) { | |
radius(zipcodes); | |
} | |
if (option == 2) { | |
stateCheck(zipcodes); | |
} | |
} | |
} | |
public static Zipcode[] readFile() throws FileNotFoundException { | |
Scanner input = new Scanner(new File("zipcode.txt")); | |
Zipcode[] zipcodes; | |
int lines = 0; | |
String fileContents = ""; | |
while(input.hasNextLine()) { | |
String line = input.nextLine(); | |
fileContents += line + "\n"; | |
lines++; | |
} | |
lines = lines / 3; | |
zipcodes = new Zipcode[lines]; | |
String zipcode; | |
String city; | |
String state; | |
double latitude; | |
double longitude; | |
Zipcode currentZip; | |
input.close(); | |
input = new Scanner(fileContents); | |
for (int i = 0; i < zipcodes.length; i++) { | |
zipcode = input.next(); | |
city = input.next(); | |
city = city.substring(0, city.length() - 1); | |
state = input.next(); | |
latitude = input.nextDouble(); | |
longitude = input.nextDouble(); | |
currentZip = new Zipcode(zipcode, city, state, latitude, longitude); | |
zipcodes[i] = currentZip; | |
} | |
return zipcodes; | |
} | |
public static void radius(Zipcode[] zipcodes) { | |
double radius = 0; | |
Zipcode zipcode = null; | |
//change this because zip changed from int to string | |
System.out.println("What proximity?"); | |
radius = getValidDouble(); | |
System.out.print("What zip code are you interested in?"); | |
zipcode = getValidZipCode(); | |
for (int i = 0; i < zipcodes.length; i++) { | |
if (zipCode.distance(zipcodes[i]) <= radius) { | |
System.out.println(zipcodes[i]); | |
} | |
} | |
} | |
public static double getValidDouble() { | |
while (true) { | |
if (console.hasNextDouble()) { | |
double ret = console.nextDouble(); | |
console.nextLine(); | |
return ret; | |
} | |
console.nextLine(); // discards invalid input | |
System.out.println("Please enter a numerical value"); | |
} | |
} | |
public static Zipcode getValidZipCode(Zipcode[] zipcodes) { | |
while (true) { | |
if (console.hasNextInt()) { | |
String zip = console.next(); | |
if (zip.length() == 5) { | |
Zipcode zipcode = findZipcode(zipcodes, zip); | |
if (zipcode != null) { | |
console.nextLine(); | |
return zip; | |
} | |
} | |
} | |
console.nextLine(); // discards invalid input | |
System.out.println("That is not a valid zip code, please try again."); | |
} | |
} | |
public static int findZipcode(Zipcode[] zipcodes, String zipcodeToFind) { | |
for(int i = 0; i < zipcodes.length; i++) { | |
Zipcode zipcode = zipcodes[i]; | |
if (zipcode.getZipcode().equals(zipcodeToFind)) { | |
return zipcode; | |
} | |
} | |
return null; | |
} | |
public static void stateCheck(Zipcode[] zipcodes) { | |
//input checking | |
System.out.println("Please enter the state"); | |
String state = console.next(); | |
Zipcode test = new Zipcode("0", "city", state, 0, 0); | |
for(int i = 0; i < zipcodes.length; i++){ | |
if(test.get_state().equals(zipcodes[i].get_state())){ | |
System.out.println(zipcodes[i]); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Zipcode { | |
public static final double RADIUS = 3956.6; | |
private String zipcode; | |
private String city; | |
private String state; | |
private double latitude; | |
private double longitude; | |
public Zipcode(String zipcode, String city, String state, double latitude, | |
double longitude) { | |
this.zipcode = zipcode; | |
this.city = city; | |
this.state = state; | |
this.latitude = latitude; | |
this.longitude = longitude; | |
} | |
public boolean equals(Zipcode zip1) { | |
return (zip1.getZipcode().equals(this.zipcode)); | |
} | |
public double distance(Zipcode check) { | |
double lat1 = Math.toRadians(latitude); | |
double long1 = Math.toRadians(longitude); | |
double lat2 = Math.toRadians(check.getLatitude()); | |
double long2 = Math.toRadians(check.getLongitude()); | |
double theCos = Math.sin(lat1) * Math.sin(lat2) + | |
Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2); | |
double arcLength = Math.acos(theCos); | |
return arcLength * RADIUS; | |
} | |
public String getZipcode() { | |
return zipcode; | |
} | |
public String getCity() { | |
return city; | |
} | |
public String getState() { | |
return state; | |
} | |
public double getLatitude() { | |
return latitude; | |
} | |
public double getLongitude() { | |
return longitude; | |
} | |
public String toString() { | |
return zipcode + " - " + city + ", " + state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment