Purpose
This problem comes from this dailyProgrammer subreddit challenge.
The task is to take an input of C / cs and V / vs and to replace Cs with random consonants and Vs with random vowels (English alphabet) while keeping the case the same. Y is not considered a vowel in this case.
Example:
CcVvv=>BgAoiccVVV=>zqUUE
Implementation
For this case, I've put all methods in the same class - in reality, I might split these methods out into different classes.
Edit: I forgot to throw an exception in generateRandomString when the isValidCharacter method is called and returns false.
public class RandomCharacterReplacer {
public static final char C = 'C';
public static final char V = 'V';
public static final List<Character> CONSONANTS = new ArrayList<>(
Arrays.asList('B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z')
);
public static final List<Character> VOWELS = new ArrayList<>(
Arrays.asList(
'A', 'E', 'I', 'O', 'U'
)
);
private static boolean isValidCharacter(final char c) {
final char upperCaseChar = Character.toUpperCase(c);
return upperCaseChar == C || upperCaseChar == V;
}
public static AlphabetCharacterCase returnAlphabetCharacterCase(final char c) {
if (!Character.isAlphabetic(c)) {
throw new RuntimeException("character is non-alphabetic");
}
if (Character.isUpperCase(c)) {
return AlphabetCharacterCase.UPPER;
}
if (Character.isLowerCase(c)) {
return AlphabetCharacterCase.LOWER;
}
throw new RuntimeException("unexpected character case");
}
public static AlphabetCharacterType returnAlphabetCharacterType(final char c) {
if (!Character.isAlphabetic(c)) {
throw new RuntimeException("character is non-alphabetic");
}
final char upperCaseChar = Character.toUpperCase(c);
if (upperCaseChar == C) {
return AlphabetCharacterType.CONSONANT;
}
if (upperCaseChar == V) {
return AlphabetCharacterType.VOWEL;
}
throw new RuntimeException("unexpected character type");
}
public static char returnRandomCharacter(final List<Character> characterList) {
if (characterList.isEmpty()) {
throw new IllegalArgumentException("character list must be non-empty");
}
final Random random = new Random();
return characterList.get(random.nextInt(characterList.size() - 1));
}
public static List<Character> returnAlphabetCharacterTypeList(final AlphabetCharacterType characterType) {
switch (characterType) {
case CONSONANT: {
return CONSONANTS;
}
case VOWEL: {
return VOWELS;
}
default: {
throw new RuntimeException("unexpected character type");
}
}
}
public static char generateRandomCharacter(final AlphabetCharacterCase alphabetCharacterCase, final AlphabetCharacterType characterType) {
final List<Character> characters = returnAlphabetCharacterTypeList(characterType);
switch (alphabetCharacterCase) {
case UPPER: {
return returnRandomCharacter(characters);
}
case LOWER: {
return Character.toLowerCase(returnRandomCharacter(characters));
}
default: {
throw new RuntimeException("unexpected character case");
}
}
}
public static String generateRandomString(final String input) {
final char[] chars = input.toCharArray();
final StringBuilder stringBuilder = new StringBuilder();
for (final char c : chars) {
if (isValidCharacter(c)) {
final AlphabetCharacterCase alphabetCharacterCase = returnAlphabetCharacterCase(c);
final AlphabetCharacterType alphabetCharacterType = returnAlphabetCharacterType(c);
stringBuilder.append(generateRandomCharacter(alphabetCharacterCase, alphabetCharacterType));
}
}
return stringBuilder.toString();
}
}