Skip to content

Instantly share code, notes, and snippets.

@darussian
Created December 11, 2018 16:46
Show Gist options
  • Save darussian/bb56a5892486a7feba8bc5cd3dd419ec to your computer and use it in GitHub Desktop.
Save darussian/bb56a5892486a7feba8bc5cd3dd419ec to your computer and use it in GitHub Desktop.
Convert to kotlin
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Convert following class into Kotlin
*/
public class Interview {
public static void main(String args[]) {
UserQuestions questions = new UserQuestions();
questions.printOutNamesWithPrefix(new UserQuestions.NameSuffixDelegate() {
@Override
public String addPrefix(TestUser user) {
String prefix = user.isMale() ? "Mr. " : "Mrs. ";
return String.format("%s %s", prefix, user.name);
}
});
questions.printOutHobbyCount();
}
}
class UserQuestions {
private static final TestUser[] users = {
new TestUser("John", TestUser.GENDER_MALE, "Basketball", "Piano"),
new TestUser("Joe", TestUser.GENDER_MALE),
new TestUser("Rachel", TestUser.GENDER_FEMALE, "Basketball", "Running")
};
public void printOutNamesWithPrefix(NameSuffixDelegate suffixDelegate) {
for (TestUser user : users) {
System.out.println(suffixDelegate.addPrefix(user));
}
}
// Print out all the hobbies from all users and how many times each hobby is entered
public void printOutHobbyCount() {
}
interface NameSuffixDelegate {
String addPrefix(TestUser user);
}
}
class TestUser {
static final String GENDER_MALE = "Male";
static final String GENDER_FEMALE = "Female";
public final String name;
final String gender;
public final List<String> hobbies;
TestUser(String name, String gender) {
this.name = name;
this.gender = gender;
this.hobbies = null;
}
TestUser(String name, String gender, String... hobbies) {
this.name = name;
this.gender = gender;
List<String> hobbyList = new ArrayList<>(Arrays.asList(hobbies));
this.hobbies = hobbyList;
}
public boolean isMale() {
return gender.equals(GENDER_MALE);
}
public boolean isFemale() {
return gender.equals(GENDER_FEMALE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment