[count-upper-lower] review comments related changes

This commit is contained in:
Kai.Yuan 2024-02-02 17:13:57 +01:00
parent 6bbef27bc7
commit ee12253609
1 changed files with 15 additions and 29 deletions

View File

@ -6,7 +6,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class CountUpperAndLowercaseCharsUnitTest { 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!";
private static final LetterCount EXPECTED = new LetterCount(4, 31);
@Test @Test
void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() { void whenIteratingCharArrayCompareAndCount_thenGetExpectedResult() {
@ -21,7 +20,8 @@ public class CountUpperAndLowercaseCharsUnitTest {
} }
} }
LetterCount result = new LetterCount(upperCnt, lowerCnt); LetterCount result = new LetterCount(upperCnt, lowerCnt);
assertEquals(EXPECTED, result); assertEquals(4, result.getUppercaseCount());
assertEquals(31, result.getLowercaseCount());
} }
@Test @Test
@ -37,7 +37,8 @@ public class CountUpperAndLowercaseCharsUnitTest {
} }
} }
LetterCount result = new LetterCount(upperCnt, lowerCnt); LetterCount result = new LetterCount(upperCnt, lowerCnt);
assertEquals(EXPECTED, result); assertEquals(4, result.getUppercaseCount());
assertEquals(31, result.getLowercaseCount());
} }
@Test @Test
@ -46,40 +47,25 @@ public class CountUpperAndLowercaseCharsUnitTest {
(int) MY_STRING.chars().filter(Character::isUpperCase).count(), (int) MY_STRING.chars().filter(Character::isUpperCase).count(),
(int) MY_STRING.chars().filter(Character::isLowerCase).count() (int) MY_STRING.chars().filter(Character::isLowerCase).count()
); );
assertEquals(EXPECTED, result); assertEquals(4, result.getUppercaseCount());
assertEquals(31, result.getLowercaseCount());
} }
} }
class LetterCount { class LetterCount {
private int uppercaseCnt; private int uppercaseCount;
private int lowercaseCnt; private int lowercaseCount;
public LetterCount(int uppercaseCnt, int lowercaseCnt) { public LetterCount(int uppercaseCount, int lowercaseCount) {
this.uppercaseCnt = uppercaseCnt; this.uppercaseCount = uppercaseCount;
this.lowercaseCnt = lowercaseCnt; this.lowercaseCount = lowercaseCount;
} }
@Override public int getUppercaseCount() {
public boolean equals(Object o) { return uppercaseCount;
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 getLowercaseCount() {
public int hashCode() { return lowercaseCount;
int result = uppercaseCnt;
result = 31 * result + lowercaseCnt;
return result;
} }
} }