Created
August 1, 2015 17:01
-
-
Save rmxsantiago/7d470618a7b0f6e52572 to your computer and use it in GitHub Desktop.
Problem: Write a program that will read the full list of customers and output the names and user ids of matching customers (within 100km), sorted by user id (ascending).
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
package br.com.rmxs.intercom.entity; | |
/** | |
* Created by rmxsantiago on 7/29/15. | |
*/ | |
public class Customer { | |
private long user_id; | |
private String name; | |
private double latitude; | |
private double longitude; | |
public Double getLongitude() { | |
return longitude; | |
} | |
public void setLongitude(String longitude) { | |
this.longitude = Double.parseDouble(longitude); | |
} | |
public Double getLatitude() { | |
return latitude; | |
} | |
public void setLatitude(String latitude) { | |
this.latitude = Double.parseDouble(latitude); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public long getUser_id() { | |
return user_id; | |
} | |
public void setUser_id(long user_id) { | |
this.user_id = user_id; | |
} | |
@Override | |
public String toString() { | |
String separator = " - "; | |
StringBuilder builder = new StringBuilder(); | |
builder.append("Id: " + getUser_id()); | |
builder.append(separator); | |
builder.append("Name: " + getName()); | |
/*builder.append(separator); | |
builder.append("latitude " + getLatitude()); | |
builder.append(separator); | |
builder.append("longitude " + getLongitude());*/ | |
return builder.toString(); | |
} | |
} |
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
package br.com.rmxs.intercom.comparator; | |
import br.com.rmxs.intercom.entity.Customer; | |
import java.util.Comparator; | |
/** | |
* Created by rmxsantiago on 7/29/15. | |
*/ | |
public class CustomerComparator implements Comparator<Customer> { | |
@Override | |
public int compare(Customer o1, Customer o2) { | |
return (o1.getUser_id() < o2.getUser_id()) ? -1 : 1; | |
} | |
} |
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
[{"latitude": "52.986375", "user_id": 12, "name": "Christina McArdle", "longitude": "-6.043701"}, | |
{"latitude": "51.92893", "user_id": 1, "name": "Alice Cahill", "longitude": "-10.27699"}, | |
{"latitude": "51.8856167", "user_id": 2, "name": "Ian McArdle", "longitude": "-10.4240951"}, | |
{"latitude": "52.3191841", "user_id": 3, "name": "Jack Enright", "longitude": "-8.5072391"}, | |
{"latitude": "53.807778", "user_id": 28, "name": "Charlie Halligan", "longitude": "-7.714444"}, | |
{"latitude": "53.4692815", "user_id": 7, "name": "Frank Kehoe", "longitude": "-9.436036"}, | |
{"latitude": "54.0894797", "user_id": 8, "name": "Eoin Ahearn", "longitude": "-6.18671"}, | |
{"latitude": "53.038056", "user_id": 26, "name": "Stephen McArdle", "longitude": "-7.653889"}, | |
{"latitude": "54.1225", "user_id": 27, "name": "Enid Gallagher", "longitude": "-8.143333"}, | |
{"latitude": "53.1229599", "user_id": 6, "name": "Theresa Enright", "longitude": "-6.2705202"}, | |
{"latitude": "52.2559432", "user_id": 9, "name": "Jack Dempsey", "longitude": "-7.1048927"}, | |
{"latitude": "52.240382", "user_id": 10, "name": "Georgina Gallagher", "longitude": "-6.972413"}, | |
{"latitude": "53.2451022", "user_id": 4, "name": "Ian Kehoe", "longitude": "-6.238335"}, | |
{"latitude": "53.1302756", "user_id": 5, "name": "Nora Dempsey", "longitude": "-6.2397222"}, | |
{"latitude": "53.008769", "user_id": 11, "name": "Richard Finnegan", "longitude": "-6.1056711"}, | |
{"latitude": "53.1489345", "user_id": 31, "name": "Alan Behan", "longitude": "-6.8422408"}, | |
{"latitude": "53", "user_id": 13, "name": "Olive Ahearn", "longitude": "-7"}, | |
{"latitude": "51.999447", "user_id": 14, "name": "Helen Cahill", "longitude": "-9.742744"}, | |
{"latitude": "52.966", "user_id": 15, "name": "Michael Ahearn", "longitude": "-6.463"}, | |
{"latitude": "52.366037", "user_id": 16, "name": "Ian Larkin", "longitude": "-8.179118"}, | |
{"latitude": "54.180238", "user_id": 17, "name": "Patricia Cahill", "longitude": "-5.920898"}, | |
{"latitude": "53.0033946", "user_id": 39, "name": "Lisa Ahearn", "longitude": "-6.3877505"}, | |
{"latitude": "52.228056", "user_id": 18, "name": "Bob Larkin", "longitude": "-7.915833"}, | |
{"latitude": "54.133333", "user_id": 24, "name": "Rose Enright", "longitude": "-6.433333"}, | |
{"latitude": "55.033", "user_id": 19, "name": "Enid Cahill", "longitude": "-8.112"}, | |
{"latitude": "53.521111", "user_id": 20, "name": "Enid Enright", "longitude": "-9.831111"}, | |
{"latitude": "51.802", "user_id": 21, "name": "David Ahearn", "longitude": "-9.442"}, | |
{"latitude": "54.374208", "user_id": 22, "name": "Charlie McArdle", "longitude": "-8.371639"}, | |
{"latitude": "53.74452", "user_id": 29, "name": "Oliver Ahearn", "longitude": "-7.11167"}, | |
{"latitude": "53.761389", "user_id": 30, "name": "Nick Enright", "longitude": "-7.2875"}, | |
{"latitude": "54.080556", "user_id": 23, "name": "Eoin Gallagher", "longitude": "-6.361944"}, | |
{"latitude": "52.833502", "user_id": 25, "name": "David Behan", "longitude": "-8.522366"}] |
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
package br.com.rmxs.intercom; | |
import br.com.rmxs.intercom.comparator.CustomerComparator; | |
import br.com.rmxs.intercom.entity.Customer; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
public class Main { | |
private static String FILE_PATH = "resources/json/customers.json"; | |
private static final double OFFICE_LATITUDE = Math.toRadians(53.3381985); | |
private static final double OFFICE_LONGITUDE = Math.toRadians(-6.2592576); | |
private static final double R = 6371000; /** Meters **/ | |
private static final double PERIMETER = 100000.00; /** Meters **/ | |
public static void main(String[] args) { | |
/** Read the file **/ | |
List<Customer> customers = readJsonFile(); | |
/** Process List **/ | |
customers = calculateNearCustomers(customers, R); | |
/** Print Lines **/ | |
showLines(customers); | |
} | |
private static List<Customer> calculateNearCustomers(List<Customer> customers, double r) { | |
List<Customer> filter = new ArrayList<Customer>(); | |
for(Customer customer : customers){ | |
double deltaLongitude = OFFICE_LONGITUDE - Math.toRadians(customer.getLongitude()); | |
double deltaSigma = Math.acos( | |
Math.sin(OFFICE_LATITUDE)*Math.sin(Math.toRadians(customer.getLatitude())) + | |
Math.cos(OFFICE_LATITUDE)*Math.cos(Math.toRadians(customer.getLatitude()))*Math.cos(deltaLongitude) | |
); | |
double distance = R * deltaSigma; | |
if(distance <= PERIMETER){ | |
filter.add(customer); | |
} | |
} | |
return filter; | |
} | |
public static List<Customer> readJsonFile(){ | |
try { | |
FileReader reader = new FileReader(FILE_PATH); | |
ObjectMapper mapper = new ObjectMapper(); | |
try { | |
return mapper.readValue(reader, mapper.getTypeFactory().constructCollectionType( | |
List.class, Customer.class)); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
return new ArrayList<Customer>(); | |
} | |
private static void showLines(List<Customer> customers){ | |
Collections.sort(customers, new CustomerComparator()); | |
System.out.println("=========================================================================="); | |
System.out.println("====================== Customers next to the office ======================"); | |
System.out.println("=========================================================================="); | |
for(Customer customer : customers){ | |
System.out.println(customer); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment