From 41ed27dc8d293059557e20fbd1a80200a6828533 Mon Sep 17 00:00:00 2001 From: SeoJigyeong Date: Mon, 20 Nov 2023 20:43:05 +0900 Subject: [PATCH] =?UTF-8?q?step2=20=EB=AC=B8=EC=9E=90=EC=97=B4=EB=8D=A7?= =?UTF-8?q?=EC=85=88=20=EA=B3=84=EC=82=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/study/StringAddCalculator.java | 137 ++++++++++++++++++ src/test/java/study/SetTest.java | 7 + .../java/study/StringAddCalculatorTest.java | 52 +++++++ src/test/java/study/StringTest.java | 14 +- 4 files changed, 202 insertions(+), 8 deletions(-) create mode 100644 src/main/java/study/StringAddCalculator.java create mode 100644 src/test/java/study/StringAddCalculatorTest.java diff --git a/src/main/java/study/StringAddCalculator.java b/src/main/java/study/StringAddCalculator.java new file mode 100644 index 00000000000..46fbc491213 --- /dev/null +++ b/src/main/java/study/StringAddCalculator.java @@ -0,0 +1,137 @@ +package study; + +import javafx.css.Match; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class StringAddCalculator { + + private static final StringAddCalculator instance = new StringAddCalculator(); + public static int ZERO = 0; + public static String COMMA = ","; + public static String COLON = ":"; + + public int splitAndSum(String input) { + + int result = ZERO; + + // 1. 빈 문자열 또는 null 값을 입력할 경우 0을 반환해야 한다. + if(_checkValidNull(input)) { + return result; + } + + // 2. 숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다. + + if(_isNumeric(input)) { + result = _parseInt(input); + } + + + // 3. 숫자 두개를 컴마(,)구분자로 입력할 경우 두 숫자의 합을 반환한다. + // 4. 구분자를 컴마(,) 이외에 콜론(:)을 사용할 수 있다. + if(_containsCharacter(input, COMMA) || _containsCharacter(input, COLON)) { + result =_sumNumbericSplitByCommaOrColon(input); + } + + // 5. "//"와 "\n" 문자 사이에 커스텀 구분자를 지정할 수 있다. + + Matcher m = _containsCharacter(input); + if(m != null) { + result = _sumNumbericSplitByCharacter(input, m); + } + + return result; + + } + + private void _checkMinusNumeric(int input) { + if(input < 0) { + throw new RuntimeException(); + } + + } + private boolean _checkValidNull(String input) { + if(input == null ) { + return true; + } + if(input.isEmpty()) { + return true; + } + return false; + } + + private int _parseInt(String input){ + + int inputInteger = ZERO; + if(!_isNumeric(input)) { + return inputInteger; + } + + inputInteger = Integer.parseInt(input); + + /** + * 6. 음수 체크 + * */ + _checkMinusNumeric(inputInteger); + + return inputInteger; + } + + private boolean _isNumeric(String input) { + return Pattern.matches("-?\\d+", input); + } + + private boolean _containsCharacter(String input, String Character) { + // 문자열에 컴마가 포함되어 있는지 확인 + if(COMMA.equals(Character)) { + return input.contains(COMMA); + } else if(COLON.equals(Character)) { + return input.contains(COLON); + } + return false; + } + + private int _sumNumbericSplitByCommaOrColon(String input) { + int sumResult = 0; + String[] numbers = input.split(COMMA); + for(String number : numbers) { + if (_isNumeric(number)) { + sumResult += _parseInt(number); + } else { + if(_containsCharacter(number, COLON)) { + String[] numbersColon = number.split(COLON); + for(String numberColon : numbersColon) { + sumResult += _parseInt(numberColon); + } + } + } + } + + return sumResult; + } + + private Matcher _containsCharacter(String input) { + Matcher m = Pattern.compile("//(.)\n(.*)").matcher(input); + if (m.find()) { + return m; + } else { + return null; + } + } + private int _sumNumbericSplitByCharacter(String input, Matcher m) { + int sumResult = 0; + + String customDelimiter = m.group(1); + String[] tokens= m.group(2).split(customDelimiter); + for(String token : tokens) { + sumResult += _parseInt(token); + } + + return sumResult; + } + + public static StringAddCalculator getInstance() { + return instance; + } +} diff --git a/src/test/java/study/SetTest.java b/src/test/java/study/SetTest.java index ab60720d346..243b2be8dea 100644 --- a/src/test/java/study/SetTest.java +++ b/src/test/java/study/SetTest.java @@ -25,6 +25,13 @@ public void setUp() { numbers.add(3); } + @Test + @DisplayName("set 자료구조 중복 제거 검사") + public void contains1() { + + assertThat(numbers.size() == 3).isTrue(); + } + @ParameterizedTest @ValueSource(ints = {1, 2, 3}) @DisplayName("특정 값이 Set에 존재하는지 확인하는 테스트") diff --git a/src/test/java/study/StringAddCalculatorTest.java b/src/test/java/study/StringAddCalculatorTest.java new file mode 100644 index 00000000000..a89504d730b --- /dev/null +++ b/src/test/java/study/StringAddCalculatorTest.java @@ -0,0 +1,52 @@ +package study; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class StringAddCalculatorTest { + + StringAddCalculator stringAddCalculator = StringAddCalculator.getInstance(); + + @Test + public void splitAndSum_null_또는_빈문자() { + + int result = stringAddCalculator.splitAndSum(null); + assertThat(result).isEqualTo(0); + + result = stringAddCalculator.splitAndSum(""); + assertThat(result).isEqualTo(0); + } + + @Test + public void splitAndSum_숫자하나() throws Exception { + int result = stringAddCalculator.splitAndSum("1"); + assertThat(result).isEqualTo(1); + } + + @Test + public void splitAndSum_쉼표구분자() throws Exception { + int result = stringAddCalculator.splitAndSum("1,2"); + assertThat(result).isEqualTo(3); + } + + @Test + public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception { + int result = stringAddCalculator.splitAndSum("1,2:3"); + assertThat(result).isEqualTo(6); + } + + @Test + public void splitAndSum_custom_구분자() throws Exception { + int result = stringAddCalculator.splitAndSum("//;\n1;2;3"); + assertThat(result).isEqualTo(6); + } + + @Test + public void splitAndSum_negative() throws Exception { + assertThatThrownBy(() -> stringAddCalculator.splitAndSum("-1,2,3")) + .isInstanceOf(RuntimeException.class); + } + +} diff --git a/src/test/java/study/StringTest.java b/src/test/java/study/StringTest.java index f8f25938fbd..5450eedaa7e 100644 --- a/src/test/java/study/StringTest.java +++ b/src/test/java/study/StringTest.java @@ -2,6 +2,8 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -19,14 +21,11 @@ public void split1(String word) { } - @Test + @ParameterizedTest @DisplayName("특정 문자열 (괄호) 제거") - public String split2(String input) { - - String result = removeParentheses(input); - - return result; - // assertThat(result).contains("1,2"); + @CsvSource(value = {"(1,2):1,2"}, delimiter = ':') + public void split2(String input, String expected) { + assertThat(removeParentheses(input)).isEqualTo(expected); } private String removeParentheses(String input) { @@ -38,7 +37,6 @@ private String removeParentheses(String input) { @DisplayName("제한 위치 외 문자열 검색 에러") public void split3() { - // 범위를 벗어나는 위치의 문자 가져오기 - StringIndexOutOfBoundsException 예상 assertThrows(StringIndexOutOfBoundsException.class, () -> { char result = ABC.charAt(5); });