From e81bf33d3ce435c2d29333aebaa437fc734d6d0a Mon Sep 17 00:00:00 2001 From: GO-TE Date: Wed, 16 Oct 2024 17:17:06 +0900 Subject: [PATCH 01/33] =?UTF-8?q?docs:=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bd90ef024..ba2d8984a 100644 --- a/README.md +++ b/README.md @@ -1 +1,45 @@ -# java-calculator-precourse \ No newline at end of file +# java-calculator-precourse +## 기능 구현 목록 +입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다. + +### 입력 +- [ ] 유저의 입력을 전달한다. + +### 입력값 검사기 +- 커스텀 구분자가 있는 입력일 때 + - [ ] 커스텀 구분자가 `//`로 시작해 `\n`가 포함되는지 확인한다. + - [ ] 커스텀 구분자가 길이가 1이 아니라면 예외 처리한다. + - [ ] 커스텀 구분자가 숫자라면 예외 처리한다. + - [ ] 커스텀 구분자가 실수를 표현하는 `.`라면 예외 처리한다. + - [ ] 커스텀 구분자가 기본 구분자라면 예외 처리 한다. + + +- 커스텀 구분자가 없을 입력일 때 + - [ ] 입력값이 숫자로 시작하는 지 확인한다. + + +- 공통 + - [ ] 커스텀 구분자를 입력하려는지 확인한다. + - [ ] 음수가 입력되었다면 예외 처리 한다. + - [ ] 입력값이 출력 범위를 초과한다면 예외 처리한다. (int, float) + - [ ] 더한 값이 출력 범위를 초과한다면 예외 처리한다. (long, double) + - [ ] 구분자, `.`, 숫자 외 문자가 검출 된다면 예외 처리한다. + +### 구분자 관리 +- [ ] 기본 구분자 외 커스텀 구분자를 전달받아 추가한다. +- [ ] 전달받은 구분자가 기본 구분자인지 확인한다. +- [ ] 구분자를 넘겨준다. + +### 문자열 핸들러 +- [ ] 커스텀 구분자를 선언한다면 분리한다. +- [ ] 분리한 커스텀 구분자를 넘겨준다. +- [ ] 전달받은 구분자로 입력받은 문자열을 분리한다. +- [ ] 분리한 문자를 넘겨준다. + +### 덧셈기 +- [ ] 전달 받은 값을 더해 저장한다. +- [ ] 저장한 결과 값을 전달한다. + +### 출력 +- [ ] 시작 메세지를 출력한다. +- [ ] 결과값을 출력한다. \ No newline at end of file From 20471724c2d693b93e2db06e5087f28e42758dbd Mon Sep 17 00:00:00 2001 From: GO-TE Date: Wed, 16 Oct 2024 17:18:31 +0900 Subject: [PATCH 02/33] =?UTF-8?q?feat(Input):=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EB=B0=9B=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/Input.java | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 src/main/java/calculator/Input.java diff --git a/README.md b/README.md index ba2d8984a..fefb7b3bf 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ 입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다. ### 입력 -- [ ] 유저의 입력을 전달한다. +- [X] 유저의 입력을 전달한다. ### 입력값 검사기 - 커스텀 구분자가 있는 입력일 때 diff --git a/src/main/java/calculator/Input.java b/src/main/java/calculator/Input.java new file mode 100644 index 000000000..2476bdead --- /dev/null +++ b/src/main/java/calculator/Input.java @@ -0,0 +1,9 @@ +package calculator; + +import camp.nextstep.edu.missionutils.Console; + +public class Input { + public String readInput() { + return Console.readLine(); + } +} From d6b8224a1638216a4d2d7ae42b17c41f31186114 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Wed, 16 Oct 2024 17:29:16 +0900 Subject: [PATCH 03/33] =?UTF-8?q?feat(Validator):=20=EC=BB=A4=EC=8A=A4?= =?UTF-8?q?=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=EC=9D=98=20=ED=8F=AC?= =?UTF-8?q?=ED=95=A8=20=ED=99=95=EC=9D=B8=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/main/java/calculator/Validator.java | 7 +++++++ src/test/java/calculator/ValidatorTest.java | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 src/main/java/calculator/Validator.java create mode 100644 src/test/java/calculator/ValidatorTest.java diff --git a/README.md b/README.md index fefb7b3bf..519445f80 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ ### 입력값 검사기 - 커스텀 구분자가 있는 입력일 때 - - [ ] 커스텀 구분자가 `//`로 시작해 `\n`가 포함되는지 확인한다. + - [X] 커스텀 구분자가 `//`로 시작해 `\n`가 포함되는지 확인한다. - [ ] 커스텀 구분자가 길이가 1이 아니라면 예외 처리한다. - [ ] 커스텀 구분자가 숫자라면 예외 처리한다. - [ ] 커스텀 구분자가 실수를 표현하는 `.`라면 예외 처리한다. @@ -19,7 +19,7 @@ - 공통 - - [ ] 커스텀 구분자를 입력하려는지 확인한다. + - [X] 커스텀 구분자를 입력하려는지 확인한다. - [ ] 음수가 입력되었다면 예외 처리 한다. - [ ] 입력값이 출력 범위를 초과한다면 예외 처리한다. (int, float) - [ ] 더한 값이 출력 범위를 초과한다면 예외 처리한다. (long, double) diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/Validator.java new file mode 100644 index 000000000..26741ea38 --- /dev/null +++ b/src/main/java/calculator/Validator.java @@ -0,0 +1,7 @@ +package calculator; + +public class Validator { + public boolean hasCustomSeparator(String input) { + return input.startsWith("//") && input.contains("\n"); + } +} diff --git a/src/test/java/calculator/ValidatorTest.java b/src/test/java/calculator/ValidatorTest.java new file mode 100644 index 000000000..c53ce11dd --- /dev/null +++ b/src/test/java/calculator/ValidatorTest.java @@ -0,0 +1,20 @@ +package calculator; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +class ValidatorTest { + Validator validator = new Validator(); + + @Test + void 커스텀_구분자의_포맷이_올바르면_True_반환() { + assertTrue(validator.hasCustomSeparator("//;\n1;2;3")); + } + + @Test + void 커스텀_구분자가_없다면_False_반환() { + assertFalse(validator.hasCustomSeparator("1:2:3")); + } +} \ No newline at end of file From 54a553d4b4cc442c979a8b6e10e420861c27f26b Mon Sep 17 00:00:00 2001 From: GO-TE Date: Wed, 16 Oct 2024 17:39:06 +0900 Subject: [PATCH 04/33] =?UTF-8?q?feat(Validator):=20=EC=8B=9C=EC=9E=91?= =?UTF-8?q?=EA=B0=92=EC=9D=B4=20=EC=88=AB=EC=9E=90=EC=9D=B8=EC=A7=80=20?= =?UTF-8?q?=ED=8C=90=EB=B3=84=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/Validator.java | 4 ++++ src/test/java/calculator/ValidatorTest.java | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 519445f80..32a1b3fc6 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ - 커스텀 구분자가 없을 입력일 때 - - [ ] 입력값이 숫자로 시작하는 지 확인한다. + - [X] 입력값이 숫자로 시작하는 지 확인한다. - 공통 diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/Validator.java index 26741ea38..0a4cfff2a 100644 --- a/src/main/java/calculator/Validator.java +++ b/src/main/java/calculator/Validator.java @@ -4,4 +4,8 @@ public class Validator { public boolean hasCustomSeparator(String input) { return input.startsWith("//") && input.contains("\n"); } + + public boolean isStartWithDigit(String input) { + return Character.isDigit(input.charAt(0)); + } } diff --git a/src/test/java/calculator/ValidatorTest.java b/src/test/java/calculator/ValidatorTest.java index c53ce11dd..26d7138d4 100644 --- a/src/test/java/calculator/ValidatorTest.java +++ b/src/test/java/calculator/ValidatorTest.java @@ -17,4 +17,14 @@ class ValidatorTest { void 커스텀_구분자가_없다면_False_반환() { assertFalse(validator.hasCustomSeparator("1:2:3")); } + + @Test + void 숫자로_시작한다면_True_반환() { + assertTrue(validator.isStartWithDigit("1;2;3")); + } + + @Test + void 숫자로_시작하지_않으면_False_반환() { + assertFalse(validator.isStartWithDigit("//;\n")); + } } \ No newline at end of file From e482abc52ce494bf7656ed1ea714c845fe710530 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Wed, 16 Oct 2024 18:09:33 +0900 Subject: [PATCH 05/33] =?UTF-8?q?feat(SeparatorManager):=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=EC=9E=90=EA=B0=80=20=EC=9D=B4=EB=AF=B8=20=EC=84=A0?= =?UTF-8?q?=EC=96=B8=20=EB=90=98=EC=97=88=EB=8A=94=EC=A7=80=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../java/calculator/SepartorMananger.java | 17 +++++++++++++++ .../java/calculator/SepartorManangerTest.java | 21 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 src/main/java/calculator/SepartorMananger.java create mode 100644 src/test/java/calculator/SepartorManangerTest.java diff --git a/README.md b/README.md index 32a1b3fc6..00292c804 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ ### 구분자 관리 - [ ] 기본 구분자 외 커스텀 구분자를 전달받아 추가한다. -- [ ] 전달받은 구분자가 기본 구분자인지 확인한다. +- [X] 전달받은 구분자가 기본(이미 존재하는지) 구분자인지 확인한다. - [ ] 구분자를 넘겨준다. ### 문자열 핸들러 diff --git a/src/main/java/calculator/SepartorMananger.java b/src/main/java/calculator/SepartorMananger.java new file mode 100644 index 000000000..568aa0d56 --- /dev/null +++ b/src/main/java/calculator/SepartorMananger.java @@ -0,0 +1,17 @@ +package calculator; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SepartorMananger { + List separators; + + public SepartorMananger() { + this.separators = new ArrayList<>(Arrays.asList(",", ":")); + } + + public boolean exists(String separator) { + return separators.contains(separator); + } +} diff --git a/src/test/java/calculator/SepartorManangerTest.java b/src/test/java/calculator/SepartorManangerTest.java new file mode 100644 index 000000000..1da588e18 --- /dev/null +++ b/src/test/java/calculator/SepartorManangerTest.java @@ -0,0 +1,21 @@ +package calculator; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +class SepartorManangerTest { + SepartorMananger separtorMananger = new SepartorMananger(); + + @Test + void 이미_있는_구분자라면_True_반환() { + assertTrue(separtorMananger.exists(":")); + } + + @Test + void 없는_구분자라면_False_반환() { + assertFalse(separtorMananger.exists("!")); + } + +} \ No newline at end of file From bbe1a9b7115501bcc866c38c1369ac66a20acbcc Mon Sep 17 00:00:00 2001 From: GO-TE Date: Wed, 16 Oct 2024 18:12:25 +0900 Subject: [PATCH 06/33] =?UTF-8?q?feat(SeparatorManager):=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=EC=9E=90=20=EC=B6=94=EA=B0=80=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/SepartorMananger.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 00292c804..02caa6c8f 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ - [ ] 구분자, `.`, 숫자 외 문자가 검출 된다면 예외 처리한다. ### 구분자 관리 -- [ ] 기본 구분자 외 커스텀 구분자를 전달받아 추가한다. +- [X] 기본 구분자 외 커스텀 구분자를 전달받아 추가한다. - [X] 전달받은 구분자가 기본(이미 존재하는지) 구분자인지 확인한다. - [ ] 구분자를 넘겨준다. diff --git a/src/main/java/calculator/SepartorMananger.java b/src/main/java/calculator/SepartorMananger.java index 568aa0d56..d879ea3ac 100644 --- a/src/main/java/calculator/SepartorMananger.java +++ b/src/main/java/calculator/SepartorMananger.java @@ -14,4 +14,8 @@ public SepartorMananger() { public boolean exists(String separator) { return separators.contains(separator); } + + public void add(String separator) { + separators.add(separator); + } } From 34ab00fd9863db6dbe9fd24f3f1110473e6fa19a Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 12:48:42 +0900 Subject: [PATCH 07/33] =?UTF-8?q?feat(SeparatorManager):=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EB=B6=88=EA=B0=80=ED=95=9C=20=EA=B5=AC=EB=B6=84?= =?UTF-8?q?=EC=9E=90=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A0=84=EB=8B=AC=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/SepartorMananger.java | 5 +++++ src/test/java/calculator/SepartorManangerTest.java | 7 +++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 02caa6c8f..bf2cb9449 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ ### 구분자 관리 - [X] 기본 구분자 외 커스텀 구분자를 전달받아 추가한다. - [X] 전달받은 구분자가 기본(이미 존재하는지) 구분자인지 확인한다. -- [ ] 구분자를 넘겨준다. +- [X] 구분자를 넘겨준다. ### 문자열 핸들러 - [ ] 커스텀 구분자를 선언한다면 분리한다. diff --git a/src/main/java/calculator/SepartorMananger.java b/src/main/java/calculator/SepartorMananger.java index d879ea3ac..0661d481d 100644 --- a/src/main/java/calculator/SepartorMananger.java +++ b/src/main/java/calculator/SepartorMananger.java @@ -2,6 +2,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; public class SepartorMananger { @@ -18,4 +19,8 @@ public boolean exists(String separator) { public void add(String separator) { separators.add(separator); } + + public List getSeparators() { + return Collections.unmodifiableList(separators); + } } diff --git a/src/test/java/calculator/SepartorManangerTest.java b/src/test/java/calculator/SepartorManangerTest.java index 1da588e18..4a9eda8e1 100644 --- a/src/test/java/calculator/SepartorManangerTest.java +++ b/src/test/java/calculator/SepartorManangerTest.java @@ -1,8 +1,10 @@ package calculator; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; +import java.util.List; import org.junit.jupiter.api.Test; class SepartorManangerTest { @@ -18,4 +20,9 @@ class SepartorManangerTest { assertFalse(separtorMananger.exists("!")); } + @Test + void 받은_구분자_리스트의_수정_불가한지_확인() { + assertThatThrownBy(() -> separtorMananger.getSeparators().remove(0)) + .isInstanceOf(UnsupportedOperationException.class); + } } \ No newline at end of file From 5d7cf6552e7f19a37118446f58ac56f75dacba8f Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 12:50:58 +0900 Subject: [PATCH 08/33] =?UTF-8?q?chore:=20SeparatorManager=20=EC=98=A4?= =?UTF-8?q?=ED=83=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{SepartorMananger.java => SeparatorManager.java} | 4 ++-- src/test/java/calculator/SepartorManangerTest.java | 11 +++++------ 2 files changed, 7 insertions(+), 8 deletions(-) rename src/main/java/calculator/{SepartorMananger.java => SeparatorManager.java} (89%) diff --git a/src/main/java/calculator/SepartorMananger.java b/src/main/java/calculator/SeparatorManager.java similarity index 89% rename from src/main/java/calculator/SepartorMananger.java rename to src/main/java/calculator/SeparatorManager.java index 0661d481d..5a5584c02 100644 --- a/src/main/java/calculator/SepartorMananger.java +++ b/src/main/java/calculator/SeparatorManager.java @@ -5,10 +5,10 @@ import java.util.Collections; import java.util.List; -public class SepartorMananger { +public class SeparatorManager { List separators; - public SepartorMananger() { + public SeparatorManager() { this.separators = new ArrayList<>(Arrays.asList(",", ":")); } diff --git a/src/test/java/calculator/SepartorManangerTest.java b/src/test/java/calculator/SepartorManangerTest.java index 4a9eda8e1..f862cf380 100644 --- a/src/test/java/calculator/SepartorManangerTest.java +++ b/src/test/java/calculator/SepartorManangerTest.java @@ -4,25 +4,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; -import java.util.List; import org.junit.jupiter.api.Test; -class SepartorManangerTest { - SepartorMananger separtorMananger = new SepartorMananger(); +class SeparatorManangerTest { + SeparatorManager separatorManager = new SeparatorManager(); @Test void 이미_있는_구분자라면_True_반환() { - assertTrue(separtorMananger.exists(":")); + assertTrue(separatorManager.exists(":")); } @Test void 없는_구분자라면_False_반환() { - assertFalse(separtorMananger.exists("!")); + assertFalse(separatorManager.exists("!")); } @Test void 받은_구분자_리스트의_수정_불가한지_확인() { - assertThatThrownBy(() -> separtorMananger.getSeparators().remove(0)) + assertThatThrownBy(() -> separatorManager.getSeparators().remove(0)) .isInstanceOf(UnsupportedOperationException.class); } } \ No newline at end of file From 05084c08230d7148f9b6df6114f27b6e233dfb07 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 14:41:20 +0900 Subject: [PATCH 09/33] =?UTF-8?q?feat(StringHandler):=20=EC=BB=A4=EC=8A=A4?= =?UTF-8?q?=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=95=B4=EC=84=9C=20=EB=84=98=EA=B2=A8=EC=A3=BC=EB=8A=94=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- src/main/java/calculator/StringHandler.java | 13 +++++++++++++ .../java/calculator/StringHandlerTest.java | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 src/main/java/calculator/StringHandler.java create mode 100644 src/test/java/calculator/StringHandlerTest.java diff --git a/README.md b/README.md index bf2cb9449..7cb242112 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ - [X] 구분자를 넘겨준다. ### 문자열 핸들러 -- [ ] 커스텀 구분자를 선언한다면 분리한다. -- [ ] 분리한 커스텀 구분자를 넘겨준다. +- [X] 커스텀 구분자를 선언한다면 분리한다. +- [X] 분리한 커스텀 구분자를 넘겨준다. - [ ] 전달받은 구분자로 입력받은 문자열을 분리한다. - [ ] 분리한 문자를 넘겨준다. diff --git a/src/main/java/calculator/StringHandler.java b/src/main/java/calculator/StringHandler.java new file mode 100644 index 000000000..fef7d95fb --- /dev/null +++ b/src/main/java/calculator/StringHandler.java @@ -0,0 +1,13 @@ +package calculator; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class StringHandler { + public String extractSeparator(String input) { + int start = input.indexOf("//") + 2; + int end = input.indexOf("\n"); + return input.substring(start, end); + } +} diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java new file mode 100644 index 000000000..6a150723a --- /dev/null +++ b/src/test/java/calculator/StringHandlerTest.java @@ -0,0 +1,18 @@ +package calculator; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +class StringHandlerTest { + StringHandler handler = new StringHandler(); + + @Test + void 커스텀구분자만_리턴하는지_확인() { + assertEquals("@", + handler.extractSeparator("//@\n1@2@3")); + } +} \ No newline at end of file From 85820c0de8d676ca1c9aff570b52bb41a7af17b2 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 14:44:21 +0900 Subject: [PATCH 10/33] =?UTF-8?q?feat(StringHandler):=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=20=EA=B0=92=EC=97=90=EC=84=9C=20=EA=B5=AC=EB=B6=84=EC=9E=90?= =?UTF-8?q?=EB=A7=8C=20=EC=A0=9C=EA=B1=B0=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/StringHandler.java | 6 ++++++ src/test/java/calculator/StringHandlerTest.java | 8 ++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7cb242112..a8a43d3c9 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ ### 문자열 핸들러 - [X] 커스텀 구분자를 선언한다면 분리한다. - [X] 분리한 커스텀 구분자를 넘겨준다. -- [ ] 전달받은 구분자로 입력받은 문자열을 분리한다. +- [X] 전달받은 구분자로 입력받은 문자열을 분리한다. - [ ] 분리한 문자를 넘겨준다. ### 덧셈기 diff --git a/src/main/java/calculator/StringHandler.java b/src/main/java/calculator/StringHandler.java index fef7d95fb..a1ef9ae35 100644 --- a/src/main/java/calculator/StringHandler.java +++ b/src/main/java/calculator/StringHandler.java @@ -10,4 +10,10 @@ public String extractSeparator(String input) { int end = input.indexOf("\n"); return input.substring(start, end); } + + public List getNumbers(List separators, String input) { + List splitInput = new ArrayList<>(Arrays.asList(input.split(""))); + splitInput.removeAll(separators); + return splitInput; + } } diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java index 6a150723a..046f07241 100644 --- a/src/test/java/calculator/StringHandlerTest.java +++ b/src/test/java/calculator/StringHandlerTest.java @@ -9,10 +9,18 @@ class StringHandlerTest { StringHandler handler = new StringHandler(); + SeparatorManager separatorManager = new SeparatorManager(); @Test void 커스텀구분자만_리턴하는지_확인() { assertEquals("@", handler.extractSeparator("//@\n1@2@3")); } + + + @Test + void 숫자만_반환하는지_확인() { + assertEquals(List.of("1", "2", "3", "4"), + handler.getNumbers(separatorManager.getSeparators(), "1:2,3")); + } } \ No newline at end of file From 3db0f0805c676dbd852de78000362e36d296acbd Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 16:20:28 +0900 Subject: [PATCH 11/33] =?UTF-8?q?feat(Adder):=20=EC=A0=84=EB=8B=AC=20?= =?UTF-8?q?=EB=B0=9B=EC=9D=80=20=EA=B0=92=EC=9D=84=20=EB=8D=94=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/Adder.java | 17 +++++++++++++++++ src/test/java/calculator/AdderTest.java | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/main/java/calculator/Adder.java create mode 100644 src/test/java/calculator/AdderTest.java diff --git a/README.md b/README.md index a8a43d3c9..71f67cb7c 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ - [ ] 분리한 문자를 넘겨준다. ### 덧셈기 -- [ ] 전달 받은 값을 더해 저장한다. +- [X] 전달 받은 값을 더해 저장한다. - [ ] 저장한 결과 값을 전달한다. ### 출력 diff --git a/src/main/java/calculator/Adder.java b/src/main/java/calculator/Adder.java new file mode 100644 index 000000000..d9aa9bab2 --- /dev/null +++ b/src/main/java/calculator/Adder.java @@ -0,0 +1,17 @@ +package calculator; + +import java.util.List; + +public class Adder { + int answer; + + public void add(List numbers) { + answer = 0; + for (String number: numbers) { + if (number.isBlank()) { + continue; + } + answer += Integer.parseInt(number); + } + } +} diff --git a/src/test/java/calculator/AdderTest.java b/src/test/java/calculator/AdderTest.java new file mode 100644 index 000000000..b41249fbe --- /dev/null +++ b/src/test/java/calculator/AdderTest.java @@ -0,0 +1,16 @@ +package calculator; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.List; +import org.junit.jupiter.api.Test; + +class AdderTest { + Adder adder = new Adder(); + + @Test + void 공백일때_0을_return_하는지() { + adder.add(List.of("")); + assertEquals(0, adder.getAnswer()); + } +} \ No newline at end of file From 685a57a139b0b078a804459f4143a4fcdc164e70 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 16:23:42 +0900 Subject: [PATCH 12/33] =?UTF-8?q?feat(Adder):=20=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=EA=B0=92=20=EC=A0=84=EB=8B=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/Adder.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 71f67cb7c..5c8a4ef14 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ ### 덧셈기 - [X] 전달 받은 값을 더해 저장한다. -- [ ] 저장한 결과 값을 전달한다. +- [X] 저장한 결과 값을 전달한다. ### 출력 - [ ] 시작 메세지를 출력한다. diff --git a/src/main/java/calculator/Adder.java b/src/main/java/calculator/Adder.java index d9aa9bab2..eb3669a64 100644 --- a/src/main/java/calculator/Adder.java +++ b/src/main/java/calculator/Adder.java @@ -14,4 +14,8 @@ public void add(List numbers) { answer += Integer.parseInt(number); } } + + public int getAnswer() { + return answer; + } } From 987c91cfe686bcffa413f33cc8fbc598d399ec21 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 17:08:07 +0900 Subject: [PATCH 13/33] =?UTF-8?q?feat(Validator):=20=EC=BB=A4=EC=8A=A4?= =?UTF-8?q?=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- src/main/java/calculator/Validator.java | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5c8a4ef14..a43573dce 100644 --- a/README.md +++ b/README.md @@ -8,10 +8,10 @@ ### 입력값 검사기 - 커스텀 구분자가 있는 입력일 때 - [X] 커스텀 구분자가 `//`로 시작해 `\n`가 포함되는지 확인한다. - - [ ] 커스텀 구분자가 길이가 1이 아니라면 예외 처리한다. - - [ ] 커스텀 구분자가 숫자라면 예외 처리한다. - - [ ] 커스텀 구분자가 실수를 표현하는 `.`라면 예외 처리한다. - - [ ] 커스텀 구분자가 기본 구분자라면 예외 처리 한다. + - [X] 커스텀 구분자가 길이가 1이 아니라면 예외 처리한다. + - [X] 커스텀 구분자가 숫자라면 예외 처리한다. + - [X] 커스텀 구분자가 실수를 표현하는 `.`라면 예외 처리한다. + - [X] 커스텀 구분자가 기본 구분자라면 예외 처리 한다. - 커스텀 구분자가 없을 입력일 때 diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/Validator.java index 0a4cfff2a..bf7c1a318 100644 --- a/src/main/java/calculator/Validator.java +++ b/src/main/java/calculator/Validator.java @@ -8,4 +8,19 @@ public boolean hasCustomSeparator(String input) { public boolean isStartWithDigit(String input) { return Character.isDigit(input.charAt(0)); } + + public void validateSeparator(String separator) { + if (separator.length() != 1) { + throw new IllegalArgumentException("구분자의 길이는 1이어야 합니다."); + } + if (separator.equals(",") || separator.equals(":")) { + throw new IllegalArgumentException("이미 구분자로 등록 되어 있습니다."); + } + if (separator.equals(".")) { + throw new IllegalArgumentException(".은 구분자가 될 수 없습니다."); + } + if (isStartWithDigit(separator)) { + throw new IllegalArgumentException("숫자는 구분자가 될 수 없습니다."); + } + } } From c932d2c90f8fbd6d5c711b2e34c8e0d24faaba01 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Thu, 17 Oct 2024 17:43:48 +0900 Subject: [PATCH 14/33] =?UTF-8?q?docs:=20=EA=B2=80=EC=82=AC=EA=B8=B0?= =?UTF-8?q?=EC=9D=98=20=EB=B2=94=EC=9C=84=20=EC=B4=88=EA=B3=BC=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=20=EB=8E=83?= =?UTF-8?q?=EC=85=88=EA=B8=B0=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 문자를 숫자로 변환시키는 클래스는 Adder라서 Validator에 어울리지 않는다고 생각함 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a43573dce..f046e44d7 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,7 @@ - 공통 - [X] 커스텀 구분자를 입력하려는지 확인한다. - - [ ] 음수가 입력되었다면 예외 처리 한다. - - [ ] 입력값이 출력 범위를 초과한다면 예외 처리한다. (int, float) - - [ ] 더한 값이 출력 범위를 초과한다면 예외 처리한다. (long, double) + - [X] 음수가 입력되었다면 예외 처리 한다. - [ ] 구분자, `.`, 숫자 외 문자가 검출 된다면 예외 처리한다. ### 구분자 관리 @@ -39,6 +37,8 @@ ### 덧셈기 - [X] 전달 받은 값을 더해 저장한다. - [X] 저장한 결과 값을 전달한다. +- [ ] 입력값이 출력 범위를 초과한다면 예외 처리한다. +- [ ] 더한 값이 출력 범위를 초과한다면 예외 처리한다. ### 출력 - [ ] 시작 메세지를 출력한다. From 0f01bc4be31a31c4b9b7c48216dcaae56e71e043 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Fri, 18 Oct 2024 15:34:14 +0900 Subject: [PATCH 15/33] =?UTF-8?q?feat(Validator):=20=ED=8F=AC=EB=A9=A7?= =?UTF-8?q?=EC=97=90=20=EB=A7=9E=EC=A7=80=20=EC=95=8A=EB=8A=94=20=EC=9E=85?= =?UTF-8?q?=EB=A0=A5=EC=9D=BC=20=EC=8B=9C=20=EC=98=88=EC=99=B8=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/StringHandler.java | 5 +++++ src/main/java/calculator/Validator.java | 15 ++++++++++++++- ...ngerTest.java => SepartorManagerTest.java} | 2 +- .../java/calculator/StringHandlerTest.java | 10 +++++++--- src/test/java/calculator/ValidatorTest.java | 19 +++++++++++++++++++ 6 files changed, 47 insertions(+), 6 deletions(-) rename src/test/java/calculator/{SepartorManangerTest.java => SepartorManagerTest.java} (96%) diff --git a/README.md b/README.md index f046e44d7..8d0bc631e 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ - 공통 - [X] 커스텀 구분자를 입력하려는지 확인한다. - [X] 음수가 입력되었다면 예외 처리 한다. - - [ ] 구분자, `.`, 숫자 외 문자가 검출 된다면 예외 처리한다. + - [X] 구분자, `.`, 숫자 외 문자가 검출 된다면 예외 처리한다. ### 구분자 관리 - [X] 기본 구분자 외 커스텀 구분자를 전달받아 추가한다. diff --git a/src/main/java/calculator/StringHandler.java b/src/main/java/calculator/StringHandler.java index a1ef9ae35..e091c404c 100644 --- a/src/main/java/calculator/StringHandler.java +++ b/src/main/java/calculator/StringHandler.java @@ -16,4 +16,9 @@ public List getNumbers(List separators, String input) { splitInput.removeAll(separators); return splitInput; } + + public String removeCustom(String input) { + int end = input.indexOf("\n") + 1; + return input.substring(end); + } } diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/Validator.java index bf7c1a318..d6a98e0a5 100644 --- a/src/main/java/calculator/Validator.java +++ b/src/main/java/calculator/Validator.java @@ -23,4 +23,17 @@ public void validateSeparator(String separator) { throw new IllegalArgumentException("숫자는 구분자가 될 수 없습니다."); } } -} + + public void validateInput(String input) { + if (input.isBlank()) { // 공백일 때 + return; + } + if (hasCustomSeparator(input)) { // //로 시작해 \n이 있을 때 + return; + } + if (isStartWithDigit(input) && (!input.contains("//") && !input.contains("\n"))) { + return; + } + throw new IllegalArgumentException("포멧에 맞게 입력해 주세요.(ex://@\n1@2:3,4)"); + } +} \ No newline at end of file diff --git a/src/test/java/calculator/SepartorManangerTest.java b/src/test/java/calculator/SepartorManagerTest.java similarity index 96% rename from src/test/java/calculator/SepartorManangerTest.java rename to src/test/java/calculator/SepartorManagerTest.java index f862cf380..79db77b4b 100644 --- a/src/test/java/calculator/SepartorManangerTest.java +++ b/src/test/java/calculator/SepartorManagerTest.java @@ -6,7 +6,7 @@ import org.junit.jupiter.api.Test; -class SeparatorManangerTest { +class SeparatorManagerTest { SeparatorManager separatorManager = new SeparatorManager(); @Test diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java index 046f07241..46d10484c 100644 --- a/src/test/java/calculator/StringHandlerTest.java +++ b/src/test/java/calculator/StringHandlerTest.java @@ -2,8 +2,6 @@ import static org.junit.jupiter.api.Assertions.*; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test; @@ -20,7 +18,13 @@ class StringHandlerTest { @Test void 숫자만_반환하는지_확인() { - assertEquals(List.of("1", "2", "3", "4"), + assertEquals(List.of("1", "2", "3"), handler.getNumbers(separatorManager.getSeparators(), "1:2,3")); } + + @Test + void 커스텀_구분자_선언부분만_잘라내는지_확인() { + assertEquals("1,2;3", + handler.removeCustom("//;\n1,2;3")); + } } \ No newline at end of file diff --git a/src/test/java/calculator/ValidatorTest.java b/src/test/java/calculator/ValidatorTest.java index 26d7138d4..1f7f7d645 100644 --- a/src/test/java/calculator/ValidatorTest.java +++ b/src/test/java/calculator/ValidatorTest.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import org.junit.jupiter.api.Test; @@ -27,4 +29,21 @@ class ValidatorTest { void 숫자로_시작하지_않으면_False_반환() { assertFalse(validator.isStartWithDigit("//;\n")); } + + @Test + void 올바른_포맷일때_아무_처리도_하지_않음() { + String[] testCases = {"//@\n1@2@3", " ", "", "//@\n", "1:2:3"}; + for (String testCase : testCases) { + assertDoesNotThrow(() -> validator.validateInput(testCase)); + } + } + + @Test + void 잘못된_포맷은_예외_발생() { + String[] testCases = {"1@2@3//@\n", "\n@//1,2,3", "abc"}; + for (String testCase : testCases) { + assertThatThrownBy(() -> validator.validateInput(testCase)) + .isInstanceOf(IllegalArgumentException.class); + } + } } \ No newline at end of file From eb937f153cfd154d5fedf3f8ddc034fe51be1965 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 00:15:14 +0900 Subject: [PATCH 16/33] =?UTF-8?q?feat(Adder):=20=EC=98=A4=EB=B2=84=20?= =?UTF-8?q?=ED=94=8C=EB=A1=9C=EC=9A=B0=20=EB=B0=9C=EC=83=9D=20=EC=8B=9C=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=B2=98=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 변경된 점 - answer 타입 변경 int -> long - answer 타입 변경에 따른 getAnswer 타입 변경 int -> long --- README.md | 4 ++-- src/main/java/calculator/Adder.java | 15 ++++++++++++--- src/test/java/calculator/AdderTest.java | 9 ++++++++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8d0bc631e..e44ebd2f3 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,8 @@ ### 덧셈기 - [X] 전달 받은 값을 더해 저장한다. - [X] 저장한 결과 값을 전달한다. -- [ ] 입력값이 출력 범위를 초과한다면 예외 처리한다. -- [ ] 더한 값이 출력 범위를 초과한다면 예외 처리한다. +- [X] 입력값이 출력 범위를 초과한다면 예외 처리한다. +- [X] 더한 값이 출력 범위를 초과한다면 예외 처리한다. ### 출력 - [ ] 시작 메세지를 출력한다. diff --git a/src/main/java/calculator/Adder.java b/src/main/java/calculator/Adder.java index eb3669a64..58fca8139 100644 --- a/src/main/java/calculator/Adder.java +++ b/src/main/java/calculator/Adder.java @@ -3,7 +3,7 @@ import java.util.List; public class Adder { - int answer; + long answer; public void add(List numbers) { answer = 0; @@ -11,11 +11,20 @@ public void add(List numbers) { if (number.isBlank()) { continue; } - answer += Integer.parseInt(number); + long element = Long.parseLong(number); + validateOverflow(element); + answer += element; } + validateOverflow(answer); } - public int getAnswer() { + public long getAnswer() { return answer; } + + private void validateOverflow(long number) { + if (number < 0) { + throw new IllegalArgumentException("허용 범위를 초과하였습니다."); + } + } } diff --git a/src/test/java/calculator/AdderTest.java b/src/test/java/calculator/AdderTest.java index b41249fbe..670ed6b26 100644 --- a/src/test/java/calculator/AdderTest.java +++ b/src/test/java/calculator/AdderTest.java @@ -1,6 +1,7 @@ package calculator; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.List; import org.junit.jupiter.api.Test; @@ -13,4 +14,10 @@ class AdderTest { adder.add(List.of("")); assertEquals(0, adder.getAnswer()); } + + @Test + void 오버플로우_발생시_예외_처리() { + assertThatThrownBy(() ->adder.add(List.of(String.valueOf(Double.MAX_VALUE),"1"))) + .isInstanceOf(IllegalArgumentException.class); + } } \ No newline at end of file From d6a31617fc6d8ac8eab8794f8d6a9f9c470d4987 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 00:19:55 +0900 Subject: [PATCH 17/33] =?UTF-8?q?feat(Output):=20=EC=8B=9C=EC=9E=91=20?= =?UTF-8?q?=EB=A9=94=EC=84=B8=EC=A7=80=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/Output.java | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 src/main/java/calculator/Output.java diff --git a/README.md b/README.md index e44ebd2f3..2d42765a4 100644 --- a/README.md +++ b/README.md @@ -41,5 +41,5 @@ - [X] 더한 값이 출력 범위를 초과한다면 예외 처리한다. ### 출력 -- [ ] 시작 메세지를 출력한다. +- [X] 시작 메세지를 출력한다. - [ ] 결과값을 출력한다. \ No newline at end of file diff --git a/src/main/java/calculator/Output.java b/src/main/java/calculator/Output.java new file mode 100644 index 000000000..e06e5e88e --- /dev/null +++ b/src/main/java/calculator/Output.java @@ -0,0 +1,7 @@ +package calculator; + +public class Output { + public void printStart() { + System.out.println("덧셈할 문자열을 입력해 주세요."); + } +} From 5efe9f2359a8db533dc75d7c345d47171218d733 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 00:32:11 +0900 Subject: [PATCH 18/33] =?UTF-8?q?feat(Output):=20=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=EA=B0=92=20=EC=B6=9C=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/java/calculator/Output.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2d42765a4..7e86a6ea7 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,4 @@ ### 출력 - [X] 시작 메세지를 출력한다. -- [ ] 결과값을 출력한다. \ No newline at end of file +- [X] 결과값을 출력한다. \ No newline at end of file diff --git a/src/main/java/calculator/Output.java b/src/main/java/calculator/Output.java index e06e5e88e..ad987a132 100644 --- a/src/main/java/calculator/Output.java +++ b/src/main/java/calculator/Output.java @@ -4,4 +4,8 @@ public class Output { public void printStart() { System.out.println("덧셈할 문자열을 입력해 주세요."); } + + public void printResult(long result) { + System.out.println("결과 : " + result); + } } From bebc033d196b3cfc9a7a30565e3b76512a4a0624 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 16:01:29 +0900 Subject: [PATCH 19/33] =?UTF-8?q?fix(StringHandler):=20getNumbers()=20?= =?UTF-8?q?=EB=AA=A8=EB=93=A0=20=EC=88=98=EA=B0=80=201=EC=9D=98=20?= =?UTF-8?q?=EC=9E=90=EB=A6=AC=EB=A1=9C=20=EB=90=98=EB=8A=94=20=EB=B2=84?= =?UTF-8?q?=EA=B7=B8=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존에 두자리 이상 숫자도 한자리씩 잘라 리턴하는 현상 발견 --- src/main/java/calculator/StringHandler.java | 11 +++++++++-- src/test/java/calculator/StringHandlerTest.java | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/calculator/StringHandler.java b/src/main/java/calculator/StringHandler.java index e091c404c..4e8f2f71f 100644 --- a/src/main/java/calculator/StringHandler.java +++ b/src/main/java/calculator/StringHandler.java @@ -12,8 +12,15 @@ public String extractSeparator(String input) { } public List getNumbers(List separators, String input) { - List splitInput = new ArrayList<>(Arrays.asList(input.split(""))); - splitInput.removeAll(separators); + List splitInput = new ArrayList<>(Arrays.asList(input)); + + for (String separator : separators) { + List tempResult = new ArrayList<>(); + for (String part : splitInput) { + tempResult.addAll(Arrays.asList(part.split(separator))); + } + splitInput = tempResult; + } return splitInput; } diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java index 46d10484c..1f95634cd 100644 --- a/src/test/java/calculator/StringHandlerTest.java +++ b/src/test/java/calculator/StringHandlerTest.java @@ -22,6 +22,12 @@ class StringHandlerTest { handler.getNumbers(separatorManager.getSeparators(), "1:2,3")); } + @Test + void 두자리_이상의_숫자가_잘_반환_되는지_확인() { + assertEquals(List.of("12", "36", "222"), + handler.getNumbers(separatorManager.getSeparators(), "12,36:222")); + } + @Test void 커스텀_구분자_선언부분만_잘라내는지_확인() { assertEquals("1,2;3", From f192a39bb7802caa03368a5b2d7251bb8ed0174c Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 16:53:22 +0900 Subject: [PATCH 20/33] =?UTF-8?q?fix(StringHandler):=20=EC=BB=A4=EC=8A=A4?= =?UTF-8?q?=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=20=EC=84=A0=EC=96=B8?= =?UTF-8?q?=EB=AC=B8=20=EC=B6=94=EC=B6=9C,=20=EC=A0=9C=EA=B1=B0=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * extractSeparactor() - start index 값 말고, 좀 더 static 하게 변경 (변경 될 가능성 큼) - end 입력 되는 \n이 개행문자가 아님을 알리기 위해 이스케이프 문자 추가 (\n -> \n) * removeCustom() - 개행문자가 아님을 알림 (변경 가능성 큼) --- src/main/java/calculator/StringHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/calculator/StringHandler.java b/src/main/java/calculator/StringHandler.java index 4e8f2f71f..a5adc3654 100644 --- a/src/main/java/calculator/StringHandler.java +++ b/src/main/java/calculator/StringHandler.java @@ -6,8 +6,8 @@ public class StringHandler { public String extractSeparator(String input) { - int start = input.indexOf("//") + 2; - int end = input.indexOf("\n"); + int start = "//".length() ; + int end = input.indexOf("\\n"); return input.substring(start, end); } @@ -25,7 +25,7 @@ public List getNumbers(List separators, String input) { } public String removeCustom(String input) { - int end = input.indexOf("\n") + 1; + int end = input.indexOf("\\n") + 2; return input.substring(end); } } From 93dc6496a2678c4428d5a58df5e67dfedf8d7aa9 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 16:54:49 +0900 Subject: [PATCH 21/33] =?UTF-8?q?docs:=20=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=ED=95=B8=EB=93=A4=EB=9F=AC=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=AA=A9=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 이미 분리한 문자열을 넘기고 있어 삭제 --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 7e86a6ea7..82b6e5f80 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,6 @@ - [X] 커스텀 구분자를 선언한다면 분리한다. - [X] 분리한 커스텀 구분자를 넘겨준다. - [X] 전달받은 구분자로 입력받은 문자열을 분리한다. -- [ ] 분리한 문자를 넘겨준다. ### 덧셈기 - [X] 전달 받은 값을 더해 저장한다. From dde4daafd12c42a3bd94dc9f54fc35691240ec32 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:06:23 +0900 Subject: [PATCH 22/33] =?UTF-8?q?fix(Validator):=20=EC=BB=A4=EC=8A=A4?= =?UTF-8?q?=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=20=EC=9C=A0=EB=AC=B4,?= =?UTF-8?q?=20=EC=9E=85=EB=A0=A5=EA=B0=92=20=EA=B2=80=EC=A6=9D=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * hasCustomSeparator() - 개행 문자로 인식하지 않도록 수정 (\n -> \n) * validateInput() - 주석 제거 - 개행문자로 인식하지 않도록 수정 (\n -> \n) --- src/main/java/calculator/Validator.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/Validator.java index d6a98e0a5..fe19cc5fc 100644 --- a/src/main/java/calculator/Validator.java +++ b/src/main/java/calculator/Validator.java @@ -2,7 +2,7 @@ public class Validator { public boolean hasCustomSeparator(String input) { - return input.startsWith("//") && input.contains("\n"); + return input.startsWith("//") && input.contains("\\n"); } public boolean isStartWithDigit(String input) { @@ -25,15 +25,15 @@ public void validateSeparator(String separator) { } public void validateInput(String input) { - if (input.isBlank()) { // 공백일 때 + if (input.isBlank()) { return; } - if (hasCustomSeparator(input)) { // //로 시작해 \n이 있을 때 + if (hasCustomSeparator(input)) { return; } - if (isStartWithDigit(input) && (!input.contains("//") && !input.contains("\n"))) { + if (isStartWithDigit(input) && (!input.contains("//") && !input.contains("\\n"))) { return; } - throw new IllegalArgumentException("포멧에 맞게 입력해 주세요.(ex://@\n1@2:3,4)"); + throw new IllegalArgumentException("포멧에 맞게 입력해 주세요.(ex://@\\n1@2:3,4)"); } } \ No newline at end of file From 10266896f8c10a36e22cf5f1d93814a3293ad1ad Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:08:59 +0900 Subject: [PATCH 23/33] =?UTF-8?q?feat(Controller):=20=EC=8B=9C=EC=8A=A4?= =?UTF-8?q?=ED=85=9C=20=EB=A1=9C=EC=A7=81=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Controller.java | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/calculator/Controller.java diff --git a/src/main/java/calculator/Controller.java b/src/main/java/calculator/Controller.java new file mode 100644 index 000000000..f24030168 --- /dev/null +++ b/src/main/java/calculator/Controller.java @@ -0,0 +1,39 @@ +package calculator; + +import java.util.List; + +public class Controller { + Input input = new Input(); + Output output = new Output(); + SeparatorManager separatorManager = new SeparatorManager(); + StringHandler stringHandler = new StringHandler(); + Validator validator = new Validator(); + Adder adder = new Adder(); + + public void start() { + output.printStart(); + String userInput = input.readInput(); + validator.validateInput(userInput); + + if (validator.hasCustomSeparator(userInput)) { + String separator = stringHandler.extractSeparator(userInput); + validator.validateSeparator(separator); + separatorManager.add(separator); + String rawNumbers = stringHandler.removeCustom(userInput); + List numbers = stringHandler.getNumbers( + separatorManager.getSeparators(), + rawNumbers); + adder.add(numbers); + output.printResult(adder.getAnswer()); + } + + if (validator.isStartWithDigit(userInput)) { + List numbers = stringHandler.getNumbers( + separatorManager.getSeparators(), + userInput + ); + adder.add(numbers); + output.printResult(adder.getAnswer()); + } + } +} From 92538881ad8d1b604fa7d894fe77c426106a17be Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:09:30 +0900 Subject: [PATCH 24/33] =?UTF-8?q?feat(Application):=20=EC=96=B4=ED=94=8C?= =?UTF-8?q?=EB=A6=AC=EC=BC=80=EC=9D=B4=EC=85=98=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 573580fb4..12f94718c 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -2,6 +2,7 @@ public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + Controller controller = new Controller(); + controller.start(); } } From 1d200a74fc3cf2b8cabd96ac9dea1541bb78c6fd Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:15:07 +0900 Subject: [PATCH 25/33] =?UTF-8?q?fix(test):=20=EA=B0=9C=ED=96=89=20?= =?UTF-8?q?=EB=AC=B8=EC=9E=90=EB=A1=9C=20=EC=9D=B8=EC=8B=9D=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?(\n=20->=20\n)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{SepartorManagerTest.java => SeparatorManagerTest.java} | 0 src/test/java/calculator/StringHandlerTest.java | 4 ++-- src/test/java/calculator/ValidatorTest.java | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/test/java/calculator/{SepartorManagerTest.java => SeparatorManagerTest.java} (100%) diff --git a/src/test/java/calculator/SepartorManagerTest.java b/src/test/java/calculator/SeparatorManagerTest.java similarity index 100% rename from src/test/java/calculator/SepartorManagerTest.java rename to src/test/java/calculator/SeparatorManagerTest.java diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java index 1f95634cd..ca3e901e4 100644 --- a/src/test/java/calculator/StringHandlerTest.java +++ b/src/test/java/calculator/StringHandlerTest.java @@ -12,7 +12,7 @@ class StringHandlerTest { @Test void 커스텀구분자만_리턴하는지_확인() { assertEquals("@", - handler.extractSeparator("//@\n1@2@3")); + handler.extractSeparator("//@\\n1@2@3")); } @@ -31,6 +31,6 @@ class StringHandlerTest { @Test void 커스텀_구분자_선언부분만_잘라내는지_확인() { assertEquals("1,2;3", - handler.removeCustom("//;\n1,2;3")); + handler.removeCustom("//;\\n1,2;3")); } } \ No newline at end of file diff --git a/src/test/java/calculator/ValidatorTest.java b/src/test/java/calculator/ValidatorTest.java index 1f7f7d645..142e1cca6 100644 --- a/src/test/java/calculator/ValidatorTest.java +++ b/src/test/java/calculator/ValidatorTest.java @@ -12,7 +12,7 @@ class ValidatorTest { @Test void 커스텀_구분자의_포맷이_올바르면_True_반환() { - assertTrue(validator.hasCustomSeparator("//;\n1;2;3")); + assertTrue(validator.hasCustomSeparator("//;\\n1;2;3")); } @Test @@ -27,12 +27,12 @@ class ValidatorTest { @Test void 숫자로_시작하지_않으면_False_반환() { - assertFalse(validator.isStartWithDigit("//;\n")); + assertFalse(validator.isStartWithDigit("//;\\n")); } @Test void 올바른_포맷일때_아무_처리도_하지_않음() { - String[] testCases = {"//@\n1@2@3", " ", "", "//@\n", "1:2:3"}; + String[] testCases = {"//@\\n1@2@3", " ", "", "//@\\n", "1:2:3"}; for (String testCase : testCases) { assertDoesNotThrow(() -> validator.validateInput(testCase)); } From a801593a211b953e1971135d2fe4a11d3bca48f8 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:20:58 +0900 Subject: [PATCH 26/33] =?UTF-8?q?refactor:=20=EC=9C=A0=ED=8B=B8=EC=84=B1?= =?UTF-8?q?=20=EB=9D=84=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4=20util=20pa?= =?UTF-8?q?ckage=EB=A1=9C=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/{ => util}/Adder.java | 0 src/main/java/calculator/{ => util}/SeparatorManager.java | 0 src/main/java/calculator/{ => util}/StringHandler.java | 0 src/main/java/calculator/{ => util}/Validator.java | 0 src/test/java/calculator/AdderTest.java | 1 + src/test/java/calculator/SeparatorManagerTest.java | 1 + src/test/java/calculator/StringHandlerTest.java | 2 ++ src/test/java/calculator/ValidatorTest.java | 1 + 8 files changed, 5 insertions(+) rename src/main/java/calculator/{ => util}/Adder.java (100%) rename src/main/java/calculator/{ => util}/SeparatorManager.java (100%) rename src/main/java/calculator/{ => util}/StringHandler.java (100%) rename src/main/java/calculator/{ => util}/Validator.java (100%) diff --git a/src/main/java/calculator/Adder.java b/src/main/java/calculator/util/Adder.java similarity index 100% rename from src/main/java/calculator/Adder.java rename to src/main/java/calculator/util/Adder.java diff --git a/src/main/java/calculator/SeparatorManager.java b/src/main/java/calculator/util/SeparatorManager.java similarity index 100% rename from src/main/java/calculator/SeparatorManager.java rename to src/main/java/calculator/util/SeparatorManager.java diff --git a/src/main/java/calculator/StringHandler.java b/src/main/java/calculator/util/StringHandler.java similarity index 100% rename from src/main/java/calculator/StringHandler.java rename to src/main/java/calculator/util/StringHandler.java diff --git a/src/main/java/calculator/Validator.java b/src/main/java/calculator/util/Validator.java similarity index 100% rename from src/main/java/calculator/Validator.java rename to src/main/java/calculator/util/Validator.java diff --git a/src/test/java/calculator/AdderTest.java b/src/test/java/calculator/AdderTest.java index 670ed6b26..b62ab4c9e 100644 --- a/src/test/java/calculator/AdderTest.java +++ b/src/test/java/calculator/AdderTest.java @@ -3,6 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import calculator.util.Adder; import java.util.List; import org.junit.jupiter.api.Test; diff --git a/src/test/java/calculator/SeparatorManagerTest.java b/src/test/java/calculator/SeparatorManagerTest.java index 79db77b4b..5ae18d175 100644 --- a/src/test/java/calculator/SeparatorManagerTest.java +++ b/src/test/java/calculator/SeparatorManagerTest.java @@ -4,6 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; +import calculator.util.SeparatorManager; import org.junit.jupiter.api.Test; class SeparatorManagerTest { diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java index ca3e901e4..2bac79da9 100644 --- a/src/test/java/calculator/StringHandlerTest.java +++ b/src/test/java/calculator/StringHandlerTest.java @@ -2,6 +2,8 @@ import static org.junit.jupiter.api.Assertions.*; +import calculator.util.SeparatorManager; +import calculator.util.StringHandler; import java.util.List; import org.junit.jupiter.api.Test; diff --git a/src/test/java/calculator/ValidatorTest.java b/src/test/java/calculator/ValidatorTest.java index 142e1cca6..20c2718db 100644 --- a/src/test/java/calculator/ValidatorTest.java +++ b/src/test/java/calculator/ValidatorTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import calculator.util.Validator; import org.junit.jupiter.api.Test; class ValidatorTest { From 050b645bcbc88ebe7a3e459ec6797c3bd727831f Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:24:15 +0900 Subject: [PATCH 27/33] =?UTF-8?q?chore(Controller):=20util=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EC=9D=B4=EB=8F=99=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9D=B8=ED=95=9C=20import=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Controller.java | 4 ++++ src/main/java/calculator/util/Adder.java | 2 +- src/main/java/calculator/util/SeparatorManager.java | 2 +- src/main/java/calculator/util/StringHandler.java | 2 +- src/main/java/calculator/util/Validator.java | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/calculator/Controller.java b/src/main/java/calculator/Controller.java index f24030168..5e1685527 100644 --- a/src/main/java/calculator/Controller.java +++ b/src/main/java/calculator/Controller.java @@ -1,5 +1,9 @@ package calculator; +import calculator.util.Adder; +import calculator.util.SeparatorManager; +import calculator.util.StringHandler; +import calculator.util.Validator; import java.util.List; public class Controller { diff --git a/src/main/java/calculator/util/Adder.java b/src/main/java/calculator/util/Adder.java index 58fca8139..fc9333747 100644 --- a/src/main/java/calculator/util/Adder.java +++ b/src/main/java/calculator/util/Adder.java @@ -1,4 +1,4 @@ -package calculator; +package calculator.util; import java.util.List; diff --git a/src/main/java/calculator/util/SeparatorManager.java b/src/main/java/calculator/util/SeparatorManager.java index 5a5584c02..067798c90 100644 --- a/src/main/java/calculator/util/SeparatorManager.java +++ b/src/main/java/calculator/util/SeparatorManager.java @@ -1,4 +1,4 @@ -package calculator; +package calculator.util; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/calculator/util/StringHandler.java b/src/main/java/calculator/util/StringHandler.java index a5adc3654..ff7e064bc 100644 --- a/src/main/java/calculator/util/StringHandler.java +++ b/src/main/java/calculator/util/StringHandler.java @@ -1,4 +1,4 @@ -package calculator; +package calculator.util; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/calculator/util/Validator.java b/src/main/java/calculator/util/Validator.java index fe19cc5fc..e47b2f3e3 100644 --- a/src/main/java/calculator/util/Validator.java +++ b/src/main/java/calculator/util/Validator.java @@ -1,4 +1,4 @@ -package calculator; +package calculator.util; public class Validator { public boolean hasCustomSeparator(String input) { From f9c80d1da176ef742ca98116ee17381f458fbab2 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Sat, 19 Oct 2024 17:52:08 +0900 Subject: [PATCH 28/33] =?UTF-8?q?fix(util):=20Validator=20=EA=B3=B5?= =?UTF-8?q?=EB=B0=B1=20=EC=9E=85=EB=A0=A5=EC=8B=9C=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * isEmpty() 구현 - 기존 공백인지 미리 검증 하지 않아 index 초과 에러 발생 - 공백임을 검증하는 함수 따로 구현 -> 커스텀 구분자 확인, 숫자로 시작하는지 확인 하는 함수에 추가 - validateInput에 공백인지를 검사하는 코드 대체 --- src/main/java/calculator/util/Validator.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/util/Validator.java b/src/main/java/calculator/util/Validator.java index e47b2f3e3..9bc9ff5f9 100644 --- a/src/main/java/calculator/util/Validator.java +++ b/src/main/java/calculator/util/Validator.java @@ -2,13 +2,23 @@ public class Validator { public boolean hasCustomSeparator(String input) { + if (isEmpty(input)) { + return false; + } return input.startsWith("//") && input.contains("\\n"); } public boolean isStartWithDigit(String input) { + if (isEmpty(input)) { + return false; + } return Character.isDigit(input.charAt(0)); } + public boolean isEmpty(String input) { + return input == null || input.isBlank(); + } + public void validateSeparator(String separator) { if (separator.length() != 1) { throw new IllegalArgumentException("구분자의 길이는 1이어야 합니다."); @@ -25,7 +35,7 @@ public void validateSeparator(String separator) { } public void validateInput(String input) { - if (input.isBlank()) { + if (isEmpty(input)) { return; } if (hasCustomSeparator(input)) { From c90a5125cc1a1afd6a22a1404999fc17a9924ae6 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Mon, 21 Oct 2024 10:52:55 +0900 Subject: [PATCH 29/33] =?UTF-8?q?feat(Adder):=20=EC=88=AB=EC=9E=90?= =?UTF-8?q?=EB=A7=8C=20=EC=9E=85=EB=A0=A5=20=EB=90=98=EC=97=88=EB=8A=94?= =?UTF-8?q?=EC=A7=80=20=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 15 ++++++++++++++- src/main/java/calculator/Controller.java | 3 +++ src/main/java/calculator/util/Adder.java | 9 +++++++++ src/test/java/calculator/ApplicationTest.java | 8 ++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 82b6e5f80..06163fa80 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,17 @@ ### 출력 - [X] 시작 메세지를 출력한다. -- [X] 결과값을 출력한다. \ No newline at end of file +- [X] 결과값을 출력한다. + +--- +## 리팩토링 + +- [ ] 하드 코딩 제거 +- [ ] 코딩 컨벤션 준수 +- [ ] 이상한 수식 넣었을 때 에러 + ``` + 덧셈할 문자열을 입력해 주세요. + //;\n112kio12u328948 + Exception in thread "main" java.lang.NumberFormatException: For input string: "112kio12u328948" + ``` +- [ ] 기능 쪼개기 \ No newline at end of file diff --git a/src/main/java/calculator/Controller.java b/src/main/java/calculator/Controller.java index 5e1685527..1015c1bfa 100644 --- a/src/main/java/calculator/Controller.java +++ b/src/main/java/calculator/Controller.java @@ -29,6 +29,7 @@ public void start() { rawNumbers); adder.add(numbers); output.printResult(adder.getAnswer()); + return; } if (validator.isStartWithDigit(userInput)) { @@ -38,6 +39,8 @@ public void start() { ); adder.add(numbers); output.printResult(adder.getAnswer()); + return; } + output.printResult(adder.getAnswer()); } } diff --git a/src/main/java/calculator/util/Adder.java b/src/main/java/calculator/util/Adder.java index fc9333747..fa9367730 100644 --- a/src/main/java/calculator/util/Adder.java +++ b/src/main/java/calculator/util/Adder.java @@ -11,6 +11,7 @@ public void add(List numbers) { if (number.isBlank()) { continue; } + validateNumber(number); long element = Long.parseLong(number); validateOverflow(element); answer += element; @@ -27,4 +28,12 @@ private void validateOverflow(long number) { throw new IllegalArgumentException("허용 범위를 초과하였습니다."); } } + + private void validateNumber(String number) { + for (int i = 0; i < number.length(); i++) { + if (!Character.isDigit(number.charAt(i))) { + throw new IllegalArgumentException("숫자만 더할 수 있습니다."); + } + } + } } diff --git a/src/test/java/calculator/ApplicationTest.java b/src/test/java/calculator/ApplicationTest.java index 93771fb01..0d174ce87 100644 --- a/src/test/java/calculator/ApplicationTest.java +++ b/src/test/java/calculator/ApplicationTest.java @@ -16,6 +16,14 @@ class ApplicationTest extends NsTest { }); } + @Test + void 공백_입력() { + assertSimpleTest(() -> { + run(" "); + assertThat(output()).contains("결과 : 0"); + }); + } + @Test void 예외_테스트() { assertSimpleTest(() -> From 9aad1c318aa77a108e65d8167dc611965e1f8791 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Mon, 21 Oct 2024 10:55:09 +0900 Subject: [PATCH 30/33] =?UTF-8?q?style:=20import=20=EC=99=80=EC=9D=BC?= =?UTF-8?q?=EB=93=9C=20=EC=B9=B4=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/calculator/StringHandlerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/calculator/StringHandlerTest.java b/src/test/java/calculator/StringHandlerTest.java index 2bac79da9..6b903435f 100644 --- a/src/test/java/calculator/StringHandlerTest.java +++ b/src/test/java/calculator/StringHandlerTest.java @@ -1,6 +1,6 @@ package calculator; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import calculator.util.SeparatorManager; import calculator.util.StringHandler; From c3dbbc04f7214e775aa20ece11802bc7fa1fab19 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Mon, 21 Oct 2024 17:35:41 +0900 Subject: [PATCH 31/33] =?UTF-8?q?refactor(SeparatorManage):=20=EC=BB=A4?= =?UTF-8?q?=EC=8A=A4=ED=85=80=20=EA=B5=AC=EB=B6=84=EC=9E=90=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EC=9D=B4=EC=A0=84=20=EC=9D=B4=EB=AF=B8=20=EC=A1=B4?= =?UTF-8?q?=EC=9E=AC=ED=95=98=EB=8A=94=EC=A7=80=20=EA=B2=80=EC=A6=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 이전에 exists 메소드가 사용되지 않았음 add 메소드에 구분자가 이미 존재하는지 검증 과정을 거치도록 바꿈 + test 코드도 이미 존재하는지 없는지 로직에 따라 바꿈 (boolean에서 예외처리하도록) --- src/main/java/calculator/util/SeparatorManager.java | 5 ++++- src/test/java/calculator/SeparatorManagerTest.java | 13 +++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/calculator/util/SeparatorManager.java b/src/main/java/calculator/util/SeparatorManager.java index 067798c90..3e3978aba 100644 --- a/src/main/java/calculator/util/SeparatorManager.java +++ b/src/main/java/calculator/util/SeparatorManager.java @@ -12,11 +12,14 @@ public SeparatorManager() { this.separators = new ArrayList<>(Arrays.asList(",", ":")); } - public boolean exists(String separator) { + private boolean exists(String separator) { return separators.contains(separator); } public void add(String separator) { + if (this.exists(separator)) { + throw new IllegalArgumentException("이미 존재하는 구분자입니다."); + } separators.add(separator); } diff --git a/src/test/java/calculator/SeparatorManagerTest.java b/src/test/java/calculator/SeparatorManagerTest.java index 5ae18d175..5335bd1a0 100644 --- a/src/test/java/calculator/SeparatorManagerTest.java +++ b/src/test/java/calculator/SeparatorManagerTest.java @@ -1,8 +1,7 @@ package calculator; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatCode; import calculator.util.SeparatorManager; import org.junit.jupiter.api.Test; @@ -11,13 +10,15 @@ class SeparatorManagerTest { SeparatorManager separatorManager = new SeparatorManager(); @Test - void 이미_있는_구분자라면_True_반환() { - assertTrue(separatorManager.exists(":")); + void 이미_있는_구분자라면_예외_처리() { + assertThatThrownBy(() -> separatorManager.add(":")) + .isInstanceOf(IllegalArgumentException.class); } @Test - void 없는_구분자라면_False_반환() { - assertFalse(separatorManager.exists("!")); + void 없는_구분자라면_정상_작동() { + assertThatCode(() -> separatorManager.add(";")) + .doesNotThrowAnyException(); } @Test From bdc59bb0b437c5c60887afc268e1f03243e173e1 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Mon, 21 Oct 2024 17:51:26 +0900 Subject: [PATCH 32/33] =?UTF-8?q?refactor:=20=ED=95=98=EB=93=9C=20?= =?UTF-8?q?=EC=BD=94=EB=94=A9=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 예외 메세지는 따로 관리하는게 더 여러울 것 같아서 제외했음 - 커스텀 구분자 접두사, 접미사 Constants에 따로 선언 - 기본 구분자 리스트를 불변한 리스트로 따로 선언 - 올바른 구분자 길이 선언 - 추후 확장을 위한 소수점 선언 --- src/main/java/calculator/util/Adder.java | 2 +- src/main/java/calculator/util/Constants.java | 11 +++++++ .../calculator/util/SeparatorManager.java | 2 +- .../java/calculator/util/StringHandler.java | 29 ++++++++++++------- src/main/java/calculator/util/Validator.java | 15 +++++----- 5 files changed, 39 insertions(+), 20 deletions(-) create mode 100644 src/main/java/calculator/util/Constants.java diff --git a/src/main/java/calculator/util/Adder.java b/src/main/java/calculator/util/Adder.java index fa9367730..5ec67194d 100644 --- a/src/main/java/calculator/util/Adder.java +++ b/src/main/java/calculator/util/Adder.java @@ -32,7 +32,7 @@ private void validateOverflow(long number) { private void validateNumber(String number) { for (int i = 0; i < number.length(); i++) { if (!Character.isDigit(number.charAt(i))) { - throw new IllegalArgumentException("숫자만 더할 수 있습니다."); + throw new IllegalArgumentException("등록된 구분자가 아닙니다."); } } } diff --git a/src/main/java/calculator/util/Constants.java b/src/main/java/calculator/util/Constants.java new file mode 100644 index 000000000..b47145240 --- /dev/null +++ b/src/main/java/calculator/util/Constants.java @@ -0,0 +1,11 @@ +package calculator.util; + +import java.util.List; + +public class Constants { + public static final String CUSTOM_PREFIX = "//"; + public static final String CUSTOM_SUFFIX = "\\n"; + public static final int SEPARATOR_LENGTH = 1; + public static final String DECIMAL_POINT = "."; + public static final List DEFAULT_SEPARATORS = List.of(":", ","); +} diff --git a/src/main/java/calculator/util/SeparatorManager.java b/src/main/java/calculator/util/SeparatorManager.java index 3e3978aba..cfba21158 100644 --- a/src/main/java/calculator/util/SeparatorManager.java +++ b/src/main/java/calculator/util/SeparatorManager.java @@ -9,7 +9,7 @@ public class SeparatorManager { List separators; public SeparatorManager() { - this.separators = new ArrayList<>(Arrays.asList(",", ":")); + this.separators = new ArrayList<>(Constants.DEFAULT_SEPARATORS); } private boolean exists(String separator) { diff --git a/src/main/java/calculator/util/StringHandler.java b/src/main/java/calculator/util/StringHandler.java index ff7e064bc..a752cc753 100644 --- a/src/main/java/calculator/util/StringHandler.java +++ b/src/main/java/calculator/util/StringHandler.java @@ -6,26 +6,35 @@ public class StringHandler { public String extractSeparator(String input) { - int start = "//".length() ; - int end = input.indexOf("\\n"); + int start = Constants.CUSTOM_PREFIX.length() ; + int end = input.indexOf(Constants.CUSTOM_SUFFIX); return input.substring(start, end); } public List getNumbers(List separators, String input) { - List splitInput = new ArrayList<>(Arrays.asList(input)); + List splitInput = new ArrayList<>(); + splitInput.add(input); for (String separator : separators) { - List tempResult = new ArrayList<>(); - for (String part : splitInput) { - tempResult.addAll(Arrays.asList(part.split(separator))); - } - splitInput = tempResult; + splitInput = splitBySeparator(splitInput, separator); } + return splitInput; } + private List splitBySeparator(List inputList, String separator) { + List result = new ArrayList<>(); + + for (String part : inputList) { + String[] splitParts = part.split(separator); + result.addAll(Arrays.asList(splitParts)); + } + + return result; + } + public String removeCustom(String input) { - int end = input.indexOf("\\n") + 2; - return input.substring(end); + int numberIndex = input.indexOf(Constants.CUSTOM_SUFFIX) + 2; + return input.substring(numberIndex); } } diff --git a/src/main/java/calculator/util/Validator.java b/src/main/java/calculator/util/Validator.java index 9bc9ff5f9..e25736cfc 100644 --- a/src/main/java/calculator/util/Validator.java +++ b/src/main/java/calculator/util/Validator.java @@ -5,7 +5,7 @@ public boolean hasCustomSeparator(String input) { if (isEmpty(input)) { return false; } - return input.startsWith("//") && input.contains("\\n"); + return input.startsWith(Constants.CUSTOM_PREFIX) && input.contains(Constants.CUSTOM_SUFFIX); } public boolean isStartWithDigit(String input) { @@ -20,13 +20,10 @@ public boolean isEmpty(String input) { } public void validateSeparator(String separator) { - if (separator.length() != 1) { + if (separator.length() != Constants.SEPARATOR_LENGTH) { throw new IllegalArgumentException("구분자의 길이는 1이어야 합니다."); } - if (separator.equals(",") || separator.equals(":")) { - throw new IllegalArgumentException("이미 구분자로 등록 되어 있습니다."); - } - if (separator.equals(".")) { + if (separator.equals(Constants.DECIMAL_POINT)) { throw new IllegalArgumentException(".은 구분자가 될 수 없습니다."); } if (isStartWithDigit(separator)) { @@ -41,8 +38,10 @@ public void validateInput(String input) { if (hasCustomSeparator(input)) { return; } - if (isStartWithDigit(input) && (!input.contains("//") && !input.contains("\\n"))) { - return; + if (isStartWithDigit(input)) { + if ((!input.contains(Constants.CUSTOM_PREFIX) && !input.contains(Constants.CUSTOM_SUFFIX))) { + return; + } } throw new IllegalArgumentException("포멧에 맞게 입력해 주세요.(ex://@\\n1@2:3,4)"); } From 4115bc9142cd287b50101184f38fe76c915fa859 Mon Sep 17 00:00:00 2001 From: GO-TE Date: Mon, 21 Oct 2024 17:55:11 +0900 Subject: [PATCH 33/33] =?UTF-8?q?docs:=20=EA=B5=AC=ED=98=84=20=EC=99=84?= =?UTF-8?q?=EB=A3=8C=20=EC=B2=B4=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 06163fa80..28b3d5439 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,12 @@ --- ## 리팩토링 -- [ ] 하드 코딩 제거 -- [ ] 코딩 컨벤션 준수 -- [ ] 이상한 수식 넣었을 때 에러 +- [X] 하드 코딩 제거 +- [X] 코딩 컨벤션 준수 +- [X] 이상한 수식 넣었을 때 에러 ``` 덧셈할 문자열을 입력해 주세요. //;\n112kio12u328948 Exception in thread "main" java.lang.NumberFormatException: For input string: "112kio12u328948" ``` -- [ ] 기능 쪼개기 \ No newline at end of file +- [ ] 기능 쪼개기 \ No newline at end of file