Skip to content

Instantly share code, notes, and snippets.

@digitalconceptvisuals
Created July 30, 2020 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalconceptvisuals/98799a56eab683d3cd12a0ae500cbc75 to your computer and use it in GitHub Desktop.
Save digitalconceptvisuals/98799a56eab683d3cd12a0ae500cbc75 to your computer and use it in GitHub Desktop.
/**
* 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