For all the following operations please make sure you have JDK 8 and gradle version 5 or above installed
- Instructions
- Installation
- How to execute the code
- How to improve the code
- How to run the unit tests
- Output file
- Proudest achievement to date
We have some customer records in a text file (customers.txt) -- one customer per line, JSON lines formatted. We want to invite any customer within 100km of our Dublin office for some food and drinks on us. 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).
-
You must use the first formula from this Wikipedia article to calculate distance. Don't forget, you'll need to convert degrees to radians.
-
The GPS coordinates for our Dublin office are 53.339428, -6.257664.
-
You can find the Customer list here.
We're looking for you to produce working code, with enough room to demonstrate how to structure components in a small program. Good submissions are well composed. Calculating distances and reading from a file are separate concerns. Classes or functions have clearly defined responsibilities.
Poor submissions will be in the form of one big function. It’s impossible to test anything smaller than the entire operation of the program, including reading from the input file.
Recommended IDE: Intellij
After cloning the project you will need to open the project in an IDE. Please do so by opening the build.gradle file.
Then you will need to open it as a project which will enable the IDE to determine the correct file structure, download necessary dependencies, etc.
The code can be executed by navigating to src/main/java/com/mycompany/myapp/Filter.java and opening the file in an IDE.
Then you will need to click the run button and click on run filter main which will execute the code for printing out the json as highlighted in the screenshot below.
The output highlighted above is also shown more clearly by navigating to the output file.
Alternatively you can always play around and write your own code to see the output. For example running System.out.println(filter.usersWithin100Km);
in the main method
will return an unordered list of users within 100 km.
Additionally, you can do the following in the main method System.out.println(CalculateDistance.usingGivenFormula("userLat", "userLong"));
where userLat and userLong can be replaced with
any latitude and longitude and so you can check the distance between the dublin office and anywhere in the world.
I have tried to write the code so that it is scalable and thus an improvement could be is to have more parameters in the CalculateDistance.usingGivenFormula("userLat", "userLong")
so it becomes CalculateDistance.usingGivenFormula("myLocationLat", "myLocationLong", "userLat", "userLong")
which could replace the presets for the dublin office and then the program would be able to calculate the distance between any 2 locations.
Furthermore, if we wanted to increase the distance to see if people are for example within the 300 km radius then changes to the code are easy enough. If we wanted to change the kilometre distance more frequently then we could parameterise the function to take in a distance, thus making it more flexible. The reason it hasn't been done here is because it is unnecessary since the function only ever needs to calculate for 100 km.
Moreover, a different (more accurate) formula could be used to calculate Great-circle distance such as the haversine formula or vincenty formula.
There are 2 ways to be able to run the test:
- Run ./gradlew clean test in the terminal - alternatively running ./gradlew clean test --info will give a more verbose answer and this especially use if the tests fail
- Opening either of the test classes in an IDE such as Intellij and click the run button
There are 3 different test files and clicking run in either of them will run the tests associated, but to run all of them we can just use method 1.
If the tests do fail the error message from using the info command is quite helpful as depicted in the screenshot below
Screenshot of running it in the terminal (intentionally failed to show output)
The output file is in the root directory and is in the form of a json instead of a txt as it is better suited to display the information. This raw output of this file can also be retrieved by running the main method in the Filter class and checking the console to view the json array where the users, and their ids within 100 km are sorted in ascending order.
Apart from the beauty that is this readme?...That would have to be completing Makers Academy and solidifying my transition into software development. I am thrilled that I was able to take the future into my own hands and refused to let myself hate 5 out of 7 days in my life. It was certainly a turning point for me, and I want to use this opportunity now to work as hard as possible towards my dream of becoming a successful developer.
Only kidding about the readme ;)