[count-upper-lower] count upper/lowercase letters
This commit is contained in:
parent
3a2f05b17d
commit
6bbef27bc7
|
@ -1,2 +1,2 @@
|
|||
|
||||
### Relevant Articles:
|
||||
>>>>>>> be317465c ([count-upper-lower] count upper/lowercase letters)
|
||||
|
|
|
@ -13,42 +13,6 @@
|
|||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>${apache.commons.lang3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>${commons-text.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>4.9.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.liquibase</groupId>
|
||||
<artifactId>liquibase-core</artifactId>
|
||||
<version>4.9.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.2</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
@ -65,8 +29,6 @@
|
|||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
|
||||
<commons-text.version>1.10.0</commons-text.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,85 @@
|
|||
package com.baeldung.countupperandlowercasechars;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class CountUpperAndLowercaseCharsUnitTest {
|
||||
private static final String MY_STRING = "Hi, Welcome to Baeldung! Let's count letters!";
|
||||
private static final LetterCount EXPECTED = new LetterCount(4, 31);
|
||||
|
||||
@Test
|
||||
void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() {
|
||||
int upperCnt = 0;
|
||||
int lowerCnt = 0;
|
||||
for (char c : MY_STRING.toCharArray()) {
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
upperCnt++;
|
||||
}
|
||||
if (c >= 'a' && c <= 'z') {
|
||||
lowerCnt++;
|
||||
}
|
||||
}
|
||||
LetterCount result = new LetterCount(upperCnt, lowerCnt);
|
||||
assertEquals(EXPECTED, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingCharacterIsLowerOrUpperCase_thenGetExpectedResult() {
|
||||
int upperCnt = 0;
|
||||
int lowerCnt = 0;
|
||||
for (char c : MY_STRING.toCharArray()) {
|
||||
if (Character.isUpperCase(c)) {
|
||||
upperCnt++;
|
||||
}
|
||||
if (Character.isLowerCase(c)) {
|
||||
lowerCnt++;
|
||||
}
|
||||
}
|
||||
LetterCount result = new LetterCount(upperCnt, lowerCnt);
|
||||
assertEquals(EXPECTED, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingStreamFilterAndCount_thenGetExpectedResult() {
|
||||
LetterCount result = new LetterCount(
|
||||
(int) MY_STRING.chars().filter(Character::isUpperCase).count(),
|
||||
(int) MY_STRING.chars().filter(Character::isLowerCase).count()
|
||||
);
|
||||
assertEquals(EXPECTED, result);
|
||||
}
|
||||
}
|
||||
|
||||
class LetterCount {
|
||||
private int uppercaseCnt;
|
||||
private int lowercaseCnt;
|
||||
|
||||
public LetterCount(int uppercaseCnt, int lowercaseCnt) {
|
||||
this.uppercaseCnt = uppercaseCnt;
|
||||
this.lowercaseCnt = lowercaseCnt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof LetterCount)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LetterCount that = (LetterCount) o;
|
||||
|
||||
if (uppercaseCnt != that.uppercaseCnt) {
|
||||
return false;
|
||||
}
|
||||
return lowercaseCnt == that.lowercaseCnt;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = uppercaseCnt;
|
||||
result = 31 * result + lowercaseCnt;
|
||||
return result;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue