Add challenge 1

This commit is contained in:
2022-10-31 10:17:01 +00:00
commit 6fcb266c17
11 changed files with 178 additions and 0 deletions

8
SCChallengeEmail/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@@ -0,0 +1,10 @@
<component name="libraryTable">
<library name="google.code.gson" type="repository">
<properties maven-id="com.google.code.gson:gson:2.9.1" />
<CLASSES>
<root url="jar://$PROJECT_DIR$/lib/gson-2.9.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

6
SCChallengeEmail/.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
SCChallengeEmail/.idea/modules.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/SCChallengeEmail.iml" filepath="$PROJECT_DIR$/SCChallengeEmail.iml" />
</modules>
</component>
</project>

6
SCChallengeEmail/.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="google.code.gson" level="project" />
</component>
</module>

Binary file not shown.

View File

@@ -0,0 +1,76 @@
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.HashMap;
public class GetPersonDetails {
public static void getName(String userId) {
try {
String parseLine = "";
URL url = new URL("https://www.ecs.soton.ac.uk/people/" + userId);
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder result = new StringBuilder();
String userEmail = userId+"@soton.ac.uk";
while (!parseLine.contains(userEmail)) {
parseLine = br.readLine();
result.append(parseLine);
}
int additionalOpenedBrackets = 1;
while (!parseLine.contains("}") || additionalOpenedBrackets > 0) {
parseLine = br.readLine();
if (parseLine.contains("{")) additionalOpenedBrackets++;
if (parseLine.contains("}")) additionalOpenedBrackets--;
result.append(parseLine);
}
br.close();
int userDataStart = result.substring(0,result.lastIndexOf(userEmail)).lastIndexOf("{");
int addressStart = result.indexOf("\"address\":") + 11;
int imageStart = result.indexOf("\"image\":") + 9;
String userDataString = result.substring(userDataStart,userDataStart + result.substring(userDataStart,userDataStart + result.substring(userDataStart + 1).indexOf("{")).lastIndexOf(",")) + "}";
String addressDataString = result.substring(addressStart,addressStart + result.substring(addressStart).indexOf("}")) + "}";
String imageDataString = result.substring(imageStart,imageStart + result.substring(imageStart).indexOf("}")) + "}";
int imageUrlStart = imageDataString.indexOf("\"url\": \"") + 8;
String imageUrl = imageDataString.substring(imageUrlStart,imageUrlStart + imageDataString.substring(imageUrlStart).indexOf("}") - 1).replace("\"","").strip();
HashMap<String, String> userData = new Gson().fromJson(userDataString, new TypeToken<HashMap<String, String>>() {}.getType());
HashMap<String, String> addressData = new Gson().fromJson(addressDataString, new TypeToken<HashMap<String, String>>() {}.getType());
userData.put("imageUrl","https://www.southampton.ac.uk" + imageUrl);
printSeparator();
System.out.println("User Data:");
for (String key: userData.keySet()) {
System.out.println(key + ": " + userData.get(key));
}
printSeparator(2, "");
System.out.println("Address Data:");
for (String key: addressData.keySet()) {
System.out.println(key + ": " + addressData.get(key));
}
printSeparator();
} catch (IOException err) {
System.out.println(err);
}
}
public static void printSeparator() {
System.out.println("###############################");
}
public static void printSeparator(Integer count) {
for (int i=1; i <= count; i++) {
printSeparator();
}
}
public static void printSeparator(Integer count, String separator) {
for (int i=1; i <= count; i++) {
printSeparator();
if (i != count) System.out.println(separator);
}
}
public static void main(String[] args) {
getName("dem");
}
}