[count-upper-lower] extract the impl. from the test methods
This commit is contained in:
parent
a7a97689ba
commit
9cbccd4b2f
|
@ -1,2 +1 @@
|
|||
### Relevant Articles:
|
||||
>>>>>>> be317465c ([count-upper-lower] count upper/lowercase letters)
|
||||
|
|
|
@ -9,50 +9,26 @@ public class CountUpperAndLowercaseCharsUnitTest {
|
|||
private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!";
|
||||
|
||||
@Test
|
||||
void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() {
|
||||
int upperCount = 0;
|
||||
int lowerCount = 0;
|
||||
for (char c : MY_STRING.toCharArray()) {
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
upperCount++;
|
||||
}
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
lowerCount++;
|
||||
}
|
||||
}
|
||||
LetterCount result = new LetterCount(upperCount, lowerCount);
|
||||
void whenUsingCountByCharacterRange_thenGetExpectedResult() {
|
||||
LetterCount result = LetterCount.countByCharacterRange(MY_STRING);
|
||||
assertEquals(4, result.getUppercaseCount());
|
||||
assertEquals(31, result.getLowercaseCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingCharacterIsLowerOrUpperCase_thenGetExpectedResult() {
|
||||
int upperCount = 0;
|
||||
int lowerCount = 0;
|
||||
for (char c : MY_STRING.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
upperCount++;
|
||||
}
|
||||
if (Character.isLowerCase(c)) {
|
||||
lowerCount++;
|
||||
}
|
||||
}
|
||||
LetterCount result = new LetterCount(upperCount, lowerCount);
|
||||
void whenUsingCountByCharacterIsLowerOrUpperCase_thenGetExpectedResult() {
|
||||
LetterCount result = LetterCount.countByCharacterIsUpperLower(MY_STRING);
|
||||
assertEquals(4, result.getUppercaseCount());
|
||||
assertEquals(31, result.getLowercaseCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingStreamFilterAndCount_thenGetExpectedResult() {
|
||||
LetterCount result = new LetterCount(
|
||||
(int) MY_STRING.chars().filter(Character::isUpperCase).count(),
|
||||
(int) MY_STRING.chars().filter(Character::isLowerCase).count()
|
||||
);
|
||||
void whenUsingCountByStreamApi_thenGetExpectedResult() {
|
||||
LetterCount result = LetterCount.countByStreamAPI(MY_STRING);
|
||||
assertEquals(4, result.getUppercaseCount());
|
||||
assertEquals(31, result.getLowercaseCount());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void whenUsingIsUpperCaseAndIsLowerCase_thenUnicodeCharactersCanBeChecked() {
|
||||
assertTrue(Character.isLowerCase('ä'));
|
||||
|
@ -64,11 +40,46 @@ class LetterCount {
|
|||
private int uppercaseCount;
|
||||
private int lowercaseCount;
|
||||
|
||||
public LetterCount(int uppercaseCount, int lowercaseCount) {
|
||||
private LetterCount(int uppercaseCount, int lowercaseCount) {
|
||||
this.uppercaseCount = uppercaseCount;
|
||||
this.lowercaseCount = lowercaseCount;
|
||||
}
|
||||
|
||||
public static LetterCount countByCharacterRange(String input) {
|
||||
int upperCount = 0;
|
||||
int lowerCount = 0;
|
||||
for (char c : input.toCharArray()) {
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
upperCount++;
|
||||
}
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
lowerCount++;
|
||||
}
|
||||
}
|
||||
return new LetterCount(upperCount, lowerCount);
|
||||
}
|
||||
|
||||
public static LetterCount countByCharacterIsUpperLower(String input) {
|
||||
int upperCount = 0;
|
||||
int lowerCount = 0;
|
||||
for (char c : input.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
upperCount++;
|
||||
}
|
||||
if (Character.isLowerCase(c)) {
|
||||
lowerCount++;
|
||||
}
|
||||
}
|
||||
return new LetterCount(upperCount, lowerCount);
|
||||
}
|
||||
|
||||
public static LetterCount countByStreamAPI(String input) {
|
||||
return new LetterCount(
|
||||
(int) input.chars().filter(Character::isUpperCase).count(),
|
||||
(int) input.chars().filter(Character::isLowerCase).count()
|
||||
);
|
||||
}
|
||||
|
||||
public int getUppercaseCount() {
|
||||
return uppercaseCount;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue