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