-
-
Save andrezimmermann/80b572becf936a1323d3 to your computer and use it in GitHub Desktop.
Code Review - PE.SE
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
public class ColorFinder { | |
public static void main(String[] args) { | |
Set<String> colorSet = new HashSet<String>(); | |
colorSet.add("red"); | |
colorSet.add("blue"); | |
colorSet.add("green"); | |
ColorFinder colorFinder = new ColorFinder(colorSet); | |
System.out.println(colorFinder.findColor("xxred")); | |
System.out.println(colorFinder.findColor("greenxx")); | |
System.out.println(colorFinder.findColor("xxbluexx")); | |
} | |
private Set<String> colorSet; | |
public ColorFinder(Set<String> colorSet) { | |
Set<String> lowerCaseSet = new HashSet<String>(colorSet.size()); | |
for (String color : colorSet) { | |
lowerCaseSet.add(color.toLowerCase()); | |
} | |
this.colorSet = lowerCaseSet; | |
} | |
public String findColor(String input) { | |
if (input == null) { | |
throw new IllegalArgumentException("Input can't be null"); | |
} | |
String lowerCaseInput = input.toLowerCase(); | |
for (String color : colorSet) { | |
if (lowerCaseInput.startsWith(color)) { | |
int start = lowerCaseInput.indexOf(color); | |
int end = start + color.length(); | |
return lowerCaseInput.substring(start, end); | |
} | |
} | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment