Last active
June 22, 2022 10:50
-
-
Save akuholla/c46fcad18105a41bbc533960e86940ba to your computer and use it in GitHub Desktop.
Class that simulates a network and responds with list of countries
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
/* | |
* Product Requirement: The function SlowNetwork::getCountries returns a list of countries. Display the name of the first country on screen. | |
* Tapping on the country text will display a toast saying "The capital for <country name> is <country capital>" | |
* | |
* Things to note: | |
* + Start by creating a new project on Android Studio. Copy the entire contents of this file to your project. | |
* + You are not allowed to edit the contents of this file. | |
* + You are free to choose your path for implementing this solution. | |
* + You are allowed to look through any documentation and/or use a search engine for reference on syntax. | |
* + Please think out loud to keep the interviewers engaged in your solution. | |
*/ | |
// Change the package name to the one from your project | |
package com.usertesting.codinginterview.network | |
import android.os.Looper | |
import java.lang.Exception | |
interface SlowNetwork { | |
fun getCountries(): String | |
fun getCapitolFor(countryCode: String): String | |
} | |
class SlowNetworkImpl : SlowNetwork { | |
override fun getCountries(): String { | |
if (Thread.currentThread().equals(Looper.getMainLooper().thread)) { | |
throw Exception("Cannot call from main thread") | |
} | |
Thread.sleep(5000) | |
return COUNTRIES | |
} | |
override fun getCapitolFor(countryCode: String): String { | |
if (Thread.currentThread().equals(Looper.getMainLooper().thread)) { | |
throw Exception("Cannot call from main thread") | |
} | |
Thread.sleep(4000) | |
return when (countryCode) { | |
"AFG" -> { | |
CAPITALS.get(0) | |
} | |
"DEN" -> { | |
CAPITALS.get(1) | |
} | |
"IRL" -> { | |
CAPITALS.get(2) | |
} | |
"RUSS" -> { | |
CAPITALS.get(3) | |
} | |
"TAI" -> { | |
CAPITALS.get(4) | |
} | |
else -> { | |
"Unknown code" | |
} | |
} | |
} | |
} | |
val COUNTRIES = """ | |
[ | |
{ | |
"country_name": "Afghanistan", | |
"country_code": "AFG" | |
}, | |
{ | |
"country_name": "Denmark", | |
"country_code": "DEN" | |
}, | |
{ | |
"country_name": "Ireland", | |
"country_code": "IRL" | |
}, | |
{ | |
"country_name": "Russian Federation", | |
"country_code": "RUSS" | |
}, | |
{ | |
"country_name": "Taiwan", | |
"country_code": "TAI" | |
} | |
] | |
""".trimIndent() | |
val CAPITALS = listOf("""{ "country_name": "Afghanistan", "capital": "Kabul" }""", | |
"""{ "country_name": "Denmark", "capital": "Copenhagen" }""", | |
"""{ "country_name": "Ireland", "capital": "Dublin" }""", | |
"""{ "country_name": "Russian Federation", "capital": "Moscow" }""", | |
"""{ "country_name": "Taiwan Federation", "capital": "Taipei" }""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment