From 9cbccd4b2fb982e4e1c152fca3a1a30ace3a5766 Mon Sep 17 00:00:00 2001 From: "Kai.Yuan" Date: Fri, 9 Feb 2024 11:19:53 +0100 Subject: [PATCH] [count-upper-lower] extract the impl. from the test methods --- .../core-java-string-operations-8/README.md | 1 - .../CountUpperAndLowercaseCharsUnitTest.java | 73 +++++++++++-------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/core-java-modules/core-java-string-operations-8/README.md b/core-java-modules/core-java-string-operations-8/README.md index 6598a4b9cc..7d843af9ea 100644 --- a/core-java-modules/core-java-string-operations-8/README.md +++ b/core-java-modules/core-java-string-operations-8/README.md @@ -1,2 +1 @@ ### Relevant Articles: ->>>>>>> be317465c ([count-upper-lower] count upper/lowercase letters) diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java index 9137906a5a..64e8b7ebbd 100644 --- a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/countupperandlowercasechars/CountUpperAndLowercaseCharsUnitTest.java @@ -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; }