-
-
Save digitalconceptvisuals/98799a56eab683d3cd12a0ae500cbc75 to your computer and use it in GitHub Desktop.
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
/** | |
* 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