BAEL-5562 Check if character is vowel (#12221)

This commit is contained in:
Ashley Frieze 2022-05-18 08:54:10 +01:00 committed by GitHub
parent ee9b5a41fb
commit 7e3cddafaa
3 changed files with 95 additions and 0 deletions

View File

@ -30,6 +30,12 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>${apache-commons-lang3.version}</version> <version>${apache-commons-lang3.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -0,0 +1,38 @@
package com.baeldung.checkvowels;
import java.util.regex.Pattern;
public class CheckVowels {
private static final String VOWELS = "aeiouAEIOU";
private static final Pattern VOWELS_PATTERN = Pattern.compile("[aeiou]", Pattern.CASE_INSENSITIVE);
public static boolean isInVowelsString(char c) {
return VOWELS.indexOf(c) != -1;
}
public static boolean isInVowelsString(String c) {
return VOWELS.contains(c);
}
public static boolean isVowelBySwitch(char c) {
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
return true;
default:
return false;
}
}
public static boolean isVowelByRegex(String c) {
return VOWELS_PATTERN.matcher(c).matches();
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.checkvowels;
import org.junit.jupiter.api.Test;
import static com.baeldung.checkvowels.CheckVowels.*;
import static org.assertj.core.api.Assertions.*;
class CheckVowelsUnitTest {
@Test
void givenAVowelCharacter_thenInVowelString() {
assertThat(isInVowelsString('e')).isTrue();
}
@Test
void givenAConsonantCharacter_thenNotInVowelString() {
assertThat(isInVowelsString('z')).isFalse();
}
@Test
void givenAVowelString_thenInVowelString() {
assertThat(isInVowelsString("e")).isTrue();
}
@Test
void givenAConsonantString_thenNotInVowelString() {
assertThat(isInVowelsString("z")).isFalse();
}
@Test
void givenAVowelCharacter_thenInVowelSwitch() {
assertThat(isVowelBySwitch('e')).isTrue();
}
@Test
void givenAConsonantCharacter_thenNotInVowelSwitch() {
assertThat(isVowelBySwitch('z')).isFalse();
}
@Test
void givenAVowelString_thenInVowelPattern() {
assertThat(isVowelByRegex("e")).isTrue();
assertThat(isVowelByRegex("E")).isTrue();
}
@Test
void givenAVowelCharacter_thenInVowelPattern() {
assertThat(isVowelByRegex(Character.toString('e'))).isTrue();
assertThat(isVowelByRegex("E")).isTrue();
}
}