Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* Our EVIL code has turned into an angel
* We now have a small and maintanable code
*/
class Angel {
// Get rid of old switch-case/if-else ladder
// Replace it with a sweet map
HashMap<String, CaseConverter> converters =
(HashMap<String, CaseConverter>)
Map.of(
"lower", new LowerConverter(),
"camel", new CamelConverter()
);
public String changeCase(String caseType, String... words) {
// We support lower and camel cases
// Sanitize the user input
caseType = caseType
.trim()
.toLowerCase();
// Lookup which converter the user wants
CaseConverter converter = converters.get(caseType);
if (converter!=null)
return converter.convert(words);
else
return null;
}
public static void main(String[] args) {
Angel obj = new Angel();
String output = obj.changeCase("camel", "This", "Word");
System.out.println(output);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment