Last active
September 2, 2024 13:34
-
-
Save debu999/64184c22f5d37989bc7ad751f3ee7fbb to your computer and use it in GitHub Desktop.
Rest API university student Max per city for hacker rank
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
/* | |
* University Data App | |
* Description: This Java program retrieves university data from a REST API, performs analysis, and prints results. | |
* Author: Your Name | |
*/ | |
package org.doogle; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.ProtocolException; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import org.json.simple.*; | |
import org.json.simple.parser.JSONParser; | |
import org.json.simple.parser.ParseException; | |
/** | |
* Main class for the University Data App. | |
*/ | |
public class App { | |
/** | |
* Main method of the application. | |
* | |
* @param args Command-line arguments. | |
* @throws IOException If an I/O error occurs. | |
* @throws ParseException If there is an error parsing JSON. | |
*/ | |
public static void main(String[] args) throws IOException, ParseException { | |
// REST API URL for university data | |
String urlString = "https://jsonmock.hackerrank.com/api/universities"; | |
// Get university data from the API | |
JSONObject json = getUniversityData(urlString); | |
// Calculate the number of pages based on total and per_page values | |
Long total = (Long) json.get("total"); | |
Long perPage = (Long) json.get("per_page"); | |
Long numPage = total % perPage == 0 ? total / perPage : (total / perPage) + 1L; | |
// Fetch data from additional pages and append to the JSONArray | |
Long page = (Long) json.get("page"); | |
JSONArray jsonArray = (JSONArray) json.get("data"); | |
for (Long p = page + 1L; p <= numPage; p++) { | |
JSONObject universityJson = getUniversityData(urlString + "?page=" + String.valueOf(p)); | |
jsonArray.addAll((JSONArray) universityJson.get("data")); | |
} | |
// Analysis variables | |
String city1 = "Singapore sdf"; // Replace with the desired city | |
String city2 = ""; // Replace with the desired city | |
String firstUniversity = "NOT_COMPUTED"; | |
String secondUniversity = "NOT_COMPUTED"; | |
Long maxFirstUniversity = 0L; | |
Long maxSecondUniversity = 0L; | |
Map<String, List<String>> cities = new HashMap<String, List<String>>(); | |
// Perform analysis on university data | |
for (int i = 0; i < jsonArray.size(); i++) { | |
JSONObject o = (JSONObject) jsonArray.get(i); | |
String city = (String) ((JSONObject) o.get("location")).get("city"); | |
String university = (String) o.get("university"); | |
Long intStudents = Long.valueOf(((String) o.get("international_students")).replace(",", "")); | |
List<String> univList = cities.getOrDefault(city, new ArrayList<>()); | |
univList.add(university + ":" + (String) o.get("international_students")); | |
cities.put(city, univList); | |
if (city1.equals(city) && intStudents > maxFirstUniversity) { | |
maxFirstUniversity = intStudents; | |
firstUniversity = university; | |
} else if (city2.equals(city) && intStudents > maxSecondUniversity) { | |
maxSecondUniversity = intStudents; | |
secondUniversity = university; | |
} | |
} | |
// Print the result | |
System.out.println("NOT_COMPUTED".equals(firstUniversity) ? secondUniversity : firstUniversity); | |
} | |
/** | |
* Retrieves university data from a given URL. | |
* | |
* @param urlString URL of the REST API. | |
* @return JSONObject containing the university data. | |
* @throws MalformedURLException If the URL is malformed. | |
* @throws IOException If an I/O error occurs. | |
* @throws ProtocolException If there is an issue with the HTTP protocol. | |
* @throws ParseException If there is an error parsing JSON. | |
*/ | |
private static JSONObject getUniversityData(String urlString) | |
throws MalformedURLException, IOException, ProtocolException, ParseException { | |
URL url = new URL(urlString); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setRequestMethod("GET"); | |
int res = conn.getResponseCode(); | |
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
String line; | |
StringBuilder sb = new StringBuilder(); | |
while ((line = reader.readLine()) != null) { | |
sb.append(line); | |
} | |
reader.close(); | |
JSONParser parser = new JSONParser(); | |
JSONObject json = (JSONObject) parser.parse(sb.toString()); | |
return json; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The API should be designed to handle large datasets and provide quick responses. Implementing proper query optimization and data validation will enhance performance. For academic support in crafting well-structured assignments, students can benefit from resources like https://essays.edubirdie.com/cipd-assignment-help to improve their writing and research skills effectively.