From dd4b867febd4c6dc25f06498edad1dfeaea14208 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Tue, 15 Oct 2024 23:15:18 +0900 Subject: [PATCH 01/46] docs:README.md update --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bd90ef0247..346aae1e1d 100644 --- a/README.md +++ b/README.md @@ -1 +1,8 @@ -# java-calculator-precourse \ No newline at end of file +# java-calculator-precourse + +## 구현할 기능 목록 +### 1. 문자열을 입력 받는 기능 +### 2. 문자열에 추가 구분자 입력이 있는지 확인하는 기능 +### 3. 문자열을 구분자를 기준으로 나누는 기능 +### 4. 구분자로 나눠진 문자열에 숫자 외의 다른 문자가 있는지 확인하는 기능 +### 5. 나눠진 숫자를 더하는 기능 From dac220dcb55ba0b1c3bfedb9a33fa7ec380ff3ac Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Tue, 15 Oct 2024 23:29:34 +0900 Subject: [PATCH 02/46] =?UTF-8?q?feat:=EB=AC=B8=EC=9E=90=EC=97=B4=20?= =?UTF-8?q?=EC=9E=85=EB=A0=A5=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 573580fb40..c471ece70e 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -1,7 +1,10 @@ package calculator; +import camp.nextstep.edu.missionutils.Console; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + System.out.println("덧셈할 문자열을 입력해 주세요."); + String s = Console.readLine(); } } From 9c524939e6e297a73b97d369907eee779b87a5f6 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 00:25:24 +0900 Subject: [PATCH 03/46] =?UTF-8?q?feat:Calculator=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=B6=94=EA=B0=80,=20checkPlusSeparator()=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 27 +++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index c471ece70e..bb27c03c78 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -6,5 +6,32 @@ public class Application { public static void main(String[] args) { System.out.println("덧셈할 문자열을 입력해 주세요."); String s = Console.readLine(); + + Calculator c = new Calculator(s); + c.run(); + } +} + +class Calculator { + private String s; + private String plusSeparator; + + public Calculator(String s) { + this.s = s; + } + + public void run(){ + System.out.println(checkPlusSeparator()); + } + private boolean checkPlusSeparator() { + //추가 구분자가 있으려면 문자열 길이가 최소 5글자는 넘어야 됨. + //추가 구분자는 //로 시작함. + // "//<추가 구분자>\n 이므로 3,4 번째가 \n 인지 확인 해야됨. + // IF 추가 구문자까지만 있고 숫자가 없다면 어떡함?? + if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { + plusSeparator = String.valueOf(s.charAt(2)); + return true; + } + return false; } } From 2e7cd6d86e87677a60085b81890789643c9fa22b Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 00:39:14 +0900 Subject: [PATCH 04/46] docs:update README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 346aae1e1d..f133f37371 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,9 @@ ### 1. 문자열을 입력 받는 기능 ### 2. 문자열에 추가 구분자 입력이 있는지 확인하는 기능 ### 3. 문자열을 구분자를 기준으로 나누는 기능 -### 4. 구분자로 나눠진 문자열에 숫자 외의 다른 문자가 있는지 확인하는 기능 +### 4. 구분자로 나눠진 문자열에 숫자 외의 다른 문자가 있는지 확인하는 기능 있으면 IllegalArgumentException ### 5. 나눠진 숫자를 더하는 기능 + +# 테스트 필요 하다 생각 되는 것들 ++ 문자열을 아무 것도 주지 않았을 때 ++ 추가 구분자로 숫자를 줬을 때 \ No newline at end of file From 0fe1bba76d98c298b4c361bed24142d93c806c9d Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 00:43:46 +0900 Subject: [PATCH 05/46] =?UTF-8?q?feat:separate=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index bb27c03c78..7839dc9392 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -2,6 +2,8 @@ import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; + public class Application { public static void main(String[] args) { System.out.println("덧셈할 문자열을 입력해 주세요."); @@ -21,17 +23,31 @@ public Calculator(String s) { } public void run(){ - System.out.println(checkPlusSeparator()); + //hasPlusSeparator:추가 구분자가 있는가? + boolean hasPlusSeparator = checkPlusSeparator(); + System.out.println(hasPlusSeparator); + //List 형태로 숫자가 분해된 것을 확인함. + System.out.println(Arrays.stream(separate(hasPlusSeparator)).toList()); + } private boolean checkPlusSeparator() { //추가 구분자가 있으려면 문자열 길이가 최소 5글자는 넘어야 됨. //추가 구분자는 //로 시작함. // "//<추가 구분자>\n 이므로 3,4 번째가 \n 인지 확인 해야됨. - // IF 추가 구문자까지만 있고 숫자가 없다면 어떡함?? + //끝났으면 추가 구분자를 확인하는 부분을 제거한 문자열로 바꿈. + // IF 추가 구분자까지만 있고 숫자가 없다면 어떡함?? if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { plusSeparator = String.valueOf(s.charAt(2)); + s = s.substring(5); return true; } return false; } + private String[] separate(boolean hasPlusSeparator){ + if(!hasPlusSeparator){ + return s.split("[:,]"); + }else{ + return s.split("[:,"+plusSeparator+"]"); + } + } } From dedece8d3ea119930ec6f1e223308fd93a85042b Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 00:58:54 +0900 Subject: [PATCH 06/46] docs:update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f133f37371..290d4e5186 100644 --- a/README.md +++ b/README.md @@ -9,4 +9,6 @@ # 테스트 필요 하다 생각 되는 것들 + 문자열을 아무 것도 주지 않았을 때 -+ 추가 구분자로 숫자를 줬을 때 \ No newline at end of file ++ 추가 구분자로 숫자를 줬을 때 ++ 음수 넣어 보기(추가 구분자로 -를 줬을 때와 아닐 때) ++ 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) \ No newline at end of file From 061b1216b23827dafdcd3d24a4448a6c12a3d32a Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 01:02:13 +0900 Subject: [PATCH 07/46] docs:update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 290d4e5186..1c70593742 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,5 @@ + 문자열을 아무 것도 주지 않았을 때 + 추가 구분자로 숫자를 줬을 때 + 음수 넣어 보기(추가 구분자로 -를 줬을 때와 아닐 때) -+ 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) \ No newline at end of file ++ 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) ++ 역슬래시(이스케이프 문자를 구분자로 줬을 때) \ No newline at end of file From fc10e2983797dd86bbac96198457ea62d8ef422e Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 01:05:56 +0900 Subject: [PATCH 08/46] =?UTF-8?q?feat:hasNaN=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 7839dc9392..da2f2bb183 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -27,7 +27,10 @@ public void run(){ boolean hasPlusSeparator = checkPlusSeparator(); System.out.println(hasPlusSeparator); //List 형태로 숫자가 분해된 것을 확인함. - System.out.println(Arrays.stream(separate(hasPlusSeparator)).toList()); + String[] separatedStringArr = separate(hasPlusSeparator); + System.out.println(Arrays.stream(separatedStringArr).toList()); + //숫자가 아닌 것이 남아 있는지 확인. 여기서 통과 여부는 throw 가 안되면 통과임. + hasNaN(separatedStringArr); } private boolean checkPlusSeparator() { @@ -47,7 +50,18 @@ private String[] separate(boolean hasPlusSeparator){ if(!hasPlusSeparator){ return s.split("[:,]"); }else{ + if(plusSeparator.equals("\\")) + return s.split("[:,"+plusSeparator.repeat(2)+"]"); return s.split("[:,"+plusSeparator+"]"); } } + //숫자가 아닌 문자를 발견할 경우 throw new IllegalArgumentException(); 해버림 + private void hasNaN(String[] separatedStringArr){ + for(String s:separatedStringArr){ + for(char c:s.toCharArray()){ + if(c < 48 || c > 57) + throw new IllegalArgumentException(); + } + } + } } From 2fc1483c6ec57672487d4a16b4984cb11e035d99 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 01:14:18 +0900 Subject: [PATCH 09/46] docs:update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c70593742..97af5634a8 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,6 @@ # 테스트 필요 하다 생각 되는 것들 + 문자열을 아무 것도 주지 않았을 때 + 추가 구분자로 숫자를 줬을 때 -+ 음수 넣어 보기(추가 구분자로 -를 줬을 때와 아닐 때) ++ 음수 넣어 보기(추가 구분자로 -를 줬을 때와 아닐 때) -> 생각해 보니까 필요 없을듯. + 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) + 역슬래시(이스케이프 문자를 구분자로 줬을 때) \ No newline at end of file From 3a094f5c48631d543d1bcd277e033b1476c2ece5 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 01:15:55 +0900 Subject: [PATCH 10/46] =?UTF-8?q?feat:sum=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index da2f2bb183..2335dc7c0e 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -25,13 +25,12 @@ public Calculator(String s) { public void run(){ //hasPlusSeparator:추가 구분자가 있는가? boolean hasPlusSeparator = checkPlusSeparator(); - System.out.println(hasPlusSeparator); - //List 형태로 숫자가 분해된 것을 확인함. + //List 형태로 숫자가 분해. String[] separatedStringArr = separate(hasPlusSeparator); - System.out.println(Arrays.stream(separatedStringArr).toList()); //숫자가 아닌 것이 남아 있는지 확인. 여기서 통과 여부는 throw 가 안되면 통과임. hasNaN(separatedStringArr); - + //숫자를 합한 뒤 출력 + System.out.println("결과 : "+sum(separatedStringArr)); } private boolean checkPlusSeparator() { //추가 구분자가 있으려면 문자열 길이가 최소 5글자는 넘어야 됨. @@ -64,4 +63,11 @@ private void hasNaN(String[] separatedStringArr){ } } } + private int sum(String[] separatedStringArr){ + int sum = 0; + for(String separatedString:separatedStringArr){ + sum += Integer.parseInt(separatedString); + } + return sum; + } } From 5f2ea86a08d5dc4056c5008517a7fcd6532a10a4 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 14:03:08 +0900 Subject: [PATCH 11/46] style:clean code --- src/main/java/calculator/Application.java | 36 +++++++++++++---------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 2335dc7c0e..6d83f2440f 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -2,8 +2,6 @@ import camp.nextstep.edu.missionutils.Console; -import java.util.Arrays; - public class Application { public static void main(String[] args) { System.out.println("덧셈할 문자열을 입력해 주세요."); @@ -22,7 +20,7 @@ public Calculator(String s) { this.s = s; } - public void run(){ + public void run() { //hasPlusSeparator:추가 구분자가 있는가? boolean hasPlusSeparator = checkPlusSeparator(); //List 형태로 숫자가 분해. @@ -30,8 +28,9 @@ public void run(){ //숫자가 아닌 것이 남아 있는지 확인. 여기서 통과 여부는 throw 가 안되면 통과임. hasNaN(separatedStringArr); //숫자를 합한 뒤 출력 - System.out.println("결과 : "+sum(separatedStringArr)); + System.out.println("결과 : " + sum(separatedStringArr)); } + private boolean checkPlusSeparator() { //추가 구분자가 있으려면 문자열 길이가 최소 5글자는 넘어야 됨. //추가 구분자는 //로 시작함. @@ -45,27 +44,32 @@ private boolean checkPlusSeparator() { } return false; } - private String[] separate(boolean hasPlusSeparator){ - if(!hasPlusSeparator){ + + private String[] separate(boolean hasPlusSeparator) { + if (!hasPlusSeparator) { return s.split("[:,]"); - }else{ - if(plusSeparator.equals("\\")) - return s.split("[:,"+plusSeparator.repeat(2)+"]"); - return s.split("[:,"+plusSeparator+"]"); + } else { + if (plusSeparator.equals("\\")) { + return s.split("[:," + plusSeparator.repeat(2) + "]"); + } + return s.split("[:," + plusSeparator + "]"); } } + //숫자가 아닌 문자를 발견할 경우 throw new IllegalArgumentException(); 해버림 - private void hasNaN(String[] separatedStringArr){ - for(String s:separatedStringArr){ - for(char c:s.toCharArray()){ - if(c < 48 || c > 57) + private void hasNaN(String[] separatedStringArr) { + for (String s : separatedStringArr) { + for (char c : s.toCharArray()) { + if (c < 48 || c > 57) { throw new IllegalArgumentException(); + } } } } - private int sum(String[] separatedStringArr){ + + private int sum(String[] separatedStringArr) { int sum = 0; - for(String separatedString:separatedStringArr){ + for (String separatedString : separatedStringArr) { sum += Integer.parseInt(separatedString); } return sum; From b9b2316c6da0764320a9ed466b9c5a2e9f3d437c Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 14:22:26 +0900 Subject: [PATCH 12/46] =?UTF-8?q?refactor:separate=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20else=20=EB=AC=B8=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 6d83f2440f..33275d74f0 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -1,6 +1,7 @@ package calculator; import camp.nextstep.edu.missionutils.Console; +import java.util.Arrays; public class Application { public static void main(String[] args) { @@ -48,12 +49,11 @@ private boolean checkPlusSeparator() { private String[] separate(boolean hasPlusSeparator) { if (!hasPlusSeparator) { return s.split("[:,]"); - } else { - if (plusSeparator.equals("\\")) { - return s.split("[:," + plusSeparator.repeat(2) + "]"); - } - return s.split("[:," + plusSeparator + "]"); } + if (plusSeparator.equals("\\")) { + return s.split("[:," + plusSeparator.repeat(2) + "]"); + } + return s.split("[:," + plusSeparator + "]"); } //숫자가 아닌 문자를 발견할 경우 throw new IllegalArgumentException(); 해버림 From fdb0c7cb8016fec0a158c5a5b554a062f31da0da Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 15:08:48 +0900 Subject: [PATCH 13/46] =?UTF-8?q?comment:Calculator=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=EC=97=90=20=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 32 ++++++++++++++--------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 33275d74f0..22d8849342 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -1,7 +1,6 @@ package calculator; import camp.nextstep.edu.missionutils.Console; -import java.util.Arrays; public class Application { public static void main(String[] args) { @@ -21,23 +20,18 @@ public Calculator(String s) { this.s = s; } + /** Run all logic step by step. */ public void run() { - //hasPlusSeparator:추가 구분자가 있는가? boolean hasPlusSeparator = checkPlusSeparator(); - //List 형태로 숫자가 분해. String[] separatedStringArr = separate(hasPlusSeparator); - //숫자가 아닌 것이 남아 있는지 확인. 여기서 통과 여부는 throw 가 안되면 통과임. hasNaN(separatedStringArr); - //숫자를 합한 뒤 출력 System.out.println("결과 : " + sum(separatedStringArr)); } - + /** + * Check existence of another separator. + * @return if exist another separator, return true. Or return false. + */ private boolean checkPlusSeparator() { - //추가 구분자가 있으려면 문자열 길이가 최소 5글자는 넘어야 됨. - //추가 구분자는 //로 시작함. - // "//<추가 구분자>\n 이므로 3,4 번째가 \n 인지 확인 해야됨. - //끝났으면 추가 구분자를 확인하는 부분을 제거한 문자열로 바꿈. - // IF 추가 구분자까지만 있고 숫자가 없다면 어떡함?? if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { plusSeparator = String.valueOf(s.charAt(2)); s = s.substring(5); @@ -46,6 +40,11 @@ private boolean checkPlusSeparator() { return false; } + /** + * Separate input string. if escape string(\) added separator, need action. + * @param hasPlusSeparator if another separator exist, set true. Or false. + * @return separated string. + */ private String[] separate(boolean hasPlusSeparator) { if (!hasPlusSeparator) { return s.split("[:,]"); @@ -56,7 +55,11 @@ private String[] separate(boolean hasPlusSeparator) { return s.split("[:," + plusSeparator + "]"); } - //숫자가 아닌 문자를 발견할 경우 throw new IllegalArgumentException(); 해버림 + /** + * Check input string array contains not a number. if it contains NaN, + * throw IllegalArgumentException. + * @param separatedStringArr String separated by separator. + */ private void hasNaN(String[] separatedStringArr) { for (String s : separatedStringArr) { for (char c : s.toCharArray()) { @@ -67,6 +70,11 @@ private void hasNaN(String[] separatedStringArr) { } } + /** + * Sum all of them + * @param separatedStringArr clean integer string array. + * @return Sum + */ private int sum(String[] separatedStringArr) { int sum = 0; for (String separatedString : separatedStringArr) { From a033f1849f763c4b431e0e45756096db34546152 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Wed, 16 Oct 2024 16:03:01 +0900 Subject: [PATCH 14/46] test:add test cases --- src/test/java/calculator/ApplicationTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/test/java/calculator/ApplicationTest.java b/src/test/java/calculator/ApplicationTest.java index 93771fb011..cf7b21b5ec 100644 --- a/src/test/java/calculator/ApplicationTest.java +++ b/src/test/java/calculator/ApplicationTest.java @@ -28,4 +28,44 @@ class ApplicationTest extends NsTest { public void runMain() { Application.main(new String[]{}); } + + @Test + public void 숫자_구분자_사용(){ + assertSimpleTest(()->{ + run("//1\\n2:213"); + assertThat(output()).contains("결과 : 7"); + }); + } + + @Test + public void 탈출_문자_사용(){ + assertSimpleTest(() -> { + assertThatThrownBy(() -> runException("1\\2:3")) + .isInstanceOf(IllegalArgumentException.class); + }); + } + + @Test + public void 탈출_문자_구분자_사용(){ + assertSimpleTest(() -> { + run("//\\\\n1\\2:3,4"); + assertThat(output()).contains("결과 : 10"); + }); + } + + @Test + public void 마침표_구분자_사용(){ + assertSimpleTest(()->{ + run("//.\\n1.2.4:3"); + assertThat(output()).contains("결과 : 10"); + }); + } + + @Test + public void 따옴표_구분자_사용(){ + assertSimpleTest(()->{ + run("//\"\\n1\"2\"4:3"); + assertThat(output()).contains("결과 : 10"); + }); + } } From bef0412c96977c544db3e585c1c7af1a5599f828 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Thu, 17 Oct 2024 01:13:39 +0900 Subject: [PATCH 15/46] =?UTF-8?q?fix:=EB=AC=B8=EC=9E=90=EC=97=B4=EC=9D=84?= =?UTF-8?q?=20=EC=95=84=EB=AC=B4=20=EA=B2=83=EB=8F=84=20=EC=A3=BC=EC=A7=80?= =?UTF-8?q?=20=EC=95=8A=EC=95=98=EC=9D=84=20=EB=95=8C=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=20=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 22d8849342..af6ff7c13a 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -17,6 +17,8 @@ class Calculator { private String plusSeparator; public Calculator(String s) { + if(s.isEmpty()) + throw new IllegalArgumentException(); this.s = s; } From 93e9eaa0c7b6c236359405aa7b1f38897c57da02 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Thu, 17 Oct 2024 15:14:19 +0900 Subject: [PATCH 16/46] =?UTF-8?q?refactor:=EB=94=94=C3=AB=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=20=EA=B8=B0=EB=B0=98=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=C3=AB=EB=94=94=EB=A0=89=ED=86=A0=EB=A6=AC=20=EB=82=98=EB=88=84?= =?UTF-8?q?=EB=8A=94=20=EC=A4=91.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 10 ----- .../java/calculator/config/Configuration.java | 16 ++++++++ .../controller/CalculatorController.java | 16 ++++++++ .../calculator/service/CalculatorService.java | 41 +++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 src/main/java/calculator/config/Configuration.java create mode 100644 src/main/java/calculator/controller/CalculatorController.java create mode 100644 src/main/java/calculator/service/CalculatorService.java diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 2335dc7c0e..1aea393458 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -23,21 +23,12 @@ public Calculator(String s) { } public void run(){ - //hasPlusSeparator:추가 구분자가 있는가? boolean hasPlusSeparator = checkPlusSeparator(); - //List 형태로 숫자가 분해. String[] separatedStringArr = separate(hasPlusSeparator); - //숫자가 아닌 것이 남아 있는지 확인. 여기서 통과 여부는 throw 가 안되면 통과임. hasNaN(separatedStringArr); - //숫자를 합한 뒤 출력 System.out.println("결과 : "+sum(separatedStringArr)); } private boolean checkPlusSeparator() { - //추가 구분자가 있으려면 문자열 길이가 최소 5글자는 넘어야 됨. - //추가 구분자는 //로 시작함. - // "//<추가 구분자>\n 이므로 3,4 번째가 \n 인지 확인 해야됨. - //끝났으면 추가 구분자를 확인하는 부분을 제거한 문자열로 바꿈. - // IF 추가 구분자까지만 있고 숫자가 없다면 어떡함?? if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { plusSeparator = String.valueOf(s.charAt(2)); s = s.substring(5); @@ -54,7 +45,6 @@ private String[] separate(boolean hasPlusSeparator){ return s.split("[:,"+plusSeparator+"]"); } } - //숫자가 아닌 문자를 발견할 경우 throw new IllegalArgumentException(); 해버림 private void hasNaN(String[] separatedStringArr){ for(String s:separatedStringArr){ for(char c:s.toCharArray()){ diff --git a/src/main/java/calculator/config/Configuration.java b/src/main/java/calculator/config/Configuration.java new file mode 100644 index 0000000000..f53c08715b --- /dev/null +++ b/src/main/java/calculator/config/Configuration.java @@ -0,0 +1,16 @@ +package calculator.config; + +import calculator.controller.CalculatorController; +import calculator.service.CalculatorService; + +public class Configuration { + private final CalculatorController calculatorController; + + public Configuration() { + this.calculatorController = new CalculatorController(new CalculatorService()); + } + + public CalculatorController getCalculatorController() { + return calculatorController; + } +} diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java new file mode 100644 index 0000000000..ac42eeda7c --- /dev/null +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -0,0 +1,16 @@ +package calculator.controller; + +import calculator.service.CalculatorService; + +public class CalculatorController { + private final CalculatorService calculatorService; + + public CalculatorController(CalculatorService calculatorService){ + this.calculatorService = calculatorService; + } + + public void input(String s){ + int sum = calculatorService.sum(s); + System.out.println(sum); + } +} diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java new file mode 100644 index 0000000000..3a3265919b --- /dev/null +++ b/src/main/java/calculator/service/CalculatorService.java @@ -0,0 +1,41 @@ +package calculator.service; + +public class CalculatorService { + private String s; + private String plusSeparator; + + public int sum(String s){ + boolean hasPlusSeparator = checkPlusSeparator(); + String[] separatedString = separate(hasPlusSeparator); + hasNaN(separatedString); + + } + + private boolean checkPlusSeparator() { + if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { + plusSeparator = String.valueOf(s.charAt(2)); + s = s.substring(5); + return true; + } + return false; + } + + private String[] separate(boolean hasPlusSeparator){ + if(!hasPlusSeparator){ + return s.split("[:,]"); + }else{ + if(plusSeparator.equals("\\")) + return s.split("[:,"+plusSeparator.repeat(2)+"]"); + return s.split("[:,"+plusSeparator+"]"); + } + } + + private void hasNaN(String[] separatedStringArr){ + for(String s:separatedStringArr){ + for(char c:s.toCharArray()){ + if(c < 48 || c > 57) + throw new IllegalArgumentException(); + } + } + } +} From 0ea8127acc19a07529d46c15466fb0d247dd2a50 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Thu, 17 Oct 2024 15:25:30 +0900 Subject: [PATCH 17/46] =?UTF-8?q?refactor:=EC=BD=94=EB=93=9C=20=EC=98=AE?= =?UTF-8?q?=EA=B8=B0=EA=B8=B0=20=EB=81=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 11 +++++++---- .../calculator/controller/CalculatorController.java | 3 ++- .../java/calculator/service/CalculatorService.java | 10 +++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 1aea393458..0d2cc63b89 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -1,16 +1,19 @@ package calculator; +import calculator.config.Configuration; +import calculator.controller.CalculatorController; import camp.nextstep.edu.missionutils.Console; -import java.util.Arrays; - public class Application { + private static final Configuration configuration = new Configuration(); public static void main(String[] args) { System.out.println("덧셈할 문자열을 입력해 주세요."); String s = Console.readLine(); + CalculatorController calculatorController = configuration.getCalculatorController(); + calculatorController.input(s); - Calculator c = new Calculator(s); - c.run(); +// Calculator c = new Calculator(s); +// c.run(); } } diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index ac42eeda7c..dc113dca27 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -11,6 +11,7 @@ public CalculatorController(CalculatorService calculatorService){ public void input(String s){ int sum = calculatorService.sum(s); - System.out.println(sum); + System.out.println("결과 : "+sum); } + } diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 3a3265919b..18ad45cb4d 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -5,10 +5,11 @@ public class CalculatorService { private String plusSeparator; public int sum(String s){ + this.s = s; boolean hasPlusSeparator = checkPlusSeparator(); String[] separatedString = separate(hasPlusSeparator); hasNaN(separatedString); - + return sumSeparatedStringArr(separatedString); } private boolean checkPlusSeparator() { @@ -38,4 +39,11 @@ private void hasNaN(String[] separatedStringArr){ } } } + private int sumSeparatedStringArr(String[] separatedStringArr){ + int sum = 0; + for(String separatedString:separatedStringArr){ + sum += Integer.parseInt(separatedString); + } + return sum; + } } From 338341cd64acbd860de884bd1b1e3dde8d1ed028 Mon Sep 17 00:00:00 2001 From: Chogunhee <153486036+whrjsgml0000@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:54:17 +0900 Subject: [PATCH 18/46] =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EB=8B=AC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 82 ++----------------- .../calculator/service/CalculatorService.java | 23 +++++- 2 files changed, 25 insertions(+), 80 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index af6ff7c13a..7f3196a4b9 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -1,87 +1,15 @@ package calculator; +import calculator.config.Configuration; +import calculator.controller.CalculatorController; import camp.nextstep.edu.missionutils.Console; public class Application { + private static final Configuration configuration = new Configuration(); public static void main(String[] args) { System.out.println("덧셈할 문자열을 입력해 주세요."); String s = Console.readLine(); - - Calculator c = new Calculator(s); - c.run(); - } -} - -class Calculator { - private String s; - private String plusSeparator; - - public Calculator(String s) { - if(s.isEmpty()) - throw new IllegalArgumentException(); - this.s = s; - } - - /** Run all logic step by step. */ - public void run() { - boolean hasPlusSeparator = checkPlusSeparator(); - String[] separatedStringArr = separate(hasPlusSeparator); - hasNaN(separatedStringArr); - System.out.println("결과 : " + sum(separatedStringArr)); - } - /** - * Check existence of another separator. - * @return if exist another separator, return true. Or return false. - */ - private boolean checkPlusSeparator() { - if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { - plusSeparator = String.valueOf(s.charAt(2)); - s = s.substring(5); - return true; - } - return false; - } - - /** - * Separate input string. if escape string(\) added separator, need action. - * @param hasPlusSeparator if another separator exist, set true. Or false. - * @return separated string. - */ - private String[] separate(boolean hasPlusSeparator) { - if (!hasPlusSeparator) { - return s.split("[:,]"); - } - if (plusSeparator.equals("\\")) { - return s.split("[:," + plusSeparator.repeat(2) + "]"); - } - return s.split("[:," + plusSeparator + "]"); - } - - /** - * Check input string array contains not a number. if it contains NaN, - * throw IllegalArgumentException. - * @param separatedStringArr String separated by separator. - */ - private void hasNaN(String[] separatedStringArr) { - for (String s : separatedStringArr) { - for (char c : s.toCharArray()) { - if (c < 48 || c > 57) { - throw new IllegalArgumentException(); - } - } - } - } - - /** - * Sum all of them - * @param separatedStringArr clean integer string array. - * @return Sum - */ - private int sum(String[] separatedStringArr) { - int sum = 0; - for (String separatedString : separatedStringArr) { - sum += Integer.parseInt(separatedString); - } - return sum; + CalculatorController calculatorController = configuration.getCalculatorController(); + calculatorController.input(s); } } diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 18ad45cb4d..5298b5a644 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -4,6 +4,7 @@ public class CalculatorService { private String s; private String plusSeparator; + /** Run all logic step by step. */ public int sum(String s){ this.s = s; boolean hasPlusSeparator = checkPlusSeparator(); @@ -11,7 +12,10 @@ public int sum(String s){ hasNaN(separatedString); return sumSeparatedStringArr(separatedString); } - + /** + * Check existence of another separator. + * @return if exist another separator, return true. Or return false. + */ private boolean checkPlusSeparator() { if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { plusSeparator = String.valueOf(s.charAt(2)); @@ -20,7 +24,11 @@ private boolean checkPlusSeparator() { } return false; } - + /** + * Separate input string. if escape string(\) added separator, need action. + * @param hasPlusSeparator if another separator exist, set true. Or false. + * @return separated string. + */ private String[] separate(boolean hasPlusSeparator){ if(!hasPlusSeparator){ return s.split("[:,]"); @@ -30,7 +38,11 @@ private String[] separate(boolean hasPlusSeparator){ return s.split("[:,"+plusSeparator+"]"); } } - + /** + * Check input string array contains not a number. if it contains NaN, + * throw IllegalArgumentException. + * @param separatedStringArr String separated by separator. + */ private void hasNaN(String[] separatedStringArr){ for(String s:separatedStringArr){ for(char c:s.toCharArray()){ @@ -39,6 +51,11 @@ private void hasNaN(String[] separatedStringArr){ } } } + /** + * Sum all of them + * @param separatedStringArr clean integer string array. + * @return Sum + */ private int sumSeparatedStringArr(String[] separatedStringArr){ int sum = 0; for(String separatedString:separatedStringArr){ From 1f14323099a9fbd94099ea749ed30113e2a63f63 Mon Sep 17 00:00:00 2001 From: Chogunhee <153486036+whrjsgml0000@users.noreply.github.com> Date: Thu, 17 Oct 2024 15:57:47 +0900 Subject: [PATCH 19/46] docs:update README.md --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 97af5634a8..ec65943d89 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,5 @@ + 추가 구분자로 숫자를 줬을 때 + 음수 넣어 보기(추가 구분자로 -를 줬을 때와 아닐 때) -> 생각해 보니까 필요 없을듯. + 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) -+ 역슬래시(이스케이프 문자를 구분자로 줬을 때) \ No newline at end of file ++ 역슬래시(이스케이프 문자를 구분자로 줬을 때) ++ 구분자 사이에 숫자가 없을 때 \ No newline at end of file From 50c64a77ddafa86b085e10cdb64001d26964075d Mon Sep 17 00:00:00 2001 From: Chogunhee <153486036+whrjsgml0000@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:01:34 +0900 Subject: [PATCH 20/46] =?UTF-8?q?fix:=EA=B5=AC=EB=B6=84=EC=9E=90=20?= =?UTF-8?q?=EC=82=AC=EC=9D=B4=EC=97=90=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EC=9D=B4=20=EC=97=86=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/service/CalculatorService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 5298b5a644..2a94b22639 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -59,7 +59,8 @@ private void hasNaN(String[] separatedStringArr){ private int sumSeparatedStringArr(String[] separatedStringArr){ int sum = 0; for(String separatedString:separatedStringArr){ - sum += Integer.parseInt(separatedString); + if(!separatedString.isEmpty()) + sum += Integer.parseInt(separatedString); } return sum; } From 93954de8962766e5089db756a38b762d0c7fca41 Mon Sep 17 00:00:00 2001 From: Chogunhee <153486036+whrjsgml0000@users.noreply.github.com> Date: Thu, 17 Oct 2024 16:02:37 +0900 Subject: [PATCH 21/46] =?UTF-8?q?test:=EA=B5=AC=EB=B6=84=EC=9E=90=20?= =?UTF-8?q?=EC=82=AC=EC=9D=B4=EC=97=90=20=EB=AC=B8=EC=9E=90=EC=97=B4?= =?UTF-8?q?=EC=9D=B4=20=EC=97=86=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/calculator/ApplicationTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/calculator/ApplicationTest.java b/src/test/java/calculator/ApplicationTest.java index cf7b21b5ec..357c8e9288 100644 --- a/src/test/java/calculator/ApplicationTest.java +++ b/src/test/java/calculator/ApplicationTest.java @@ -68,4 +68,12 @@ public void runMain() { assertThat(output()).contains("결과 : 10"); }); } + + @Test + public void 구분자_사이에_문자열_없음(){ + assertSimpleTest(() -> { + run("1,:2"); + assertThat(output()).contains("결과 : 3"); + }); + } } From a9ee17397651df93aacb51bda4227aa0d7bcc65a Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:28:10 +0900 Subject: [PATCH 22/46] =?UTF-8?q?refactor:=EB=A9=94=EC=9D=B8=EC=97=90=20?= =?UTF-8?q?=EC=9E=88=EB=8D=98=20=EC=BD=94=EB=93=9C=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=EB=A1=9C=20=EC=98=AE=EA=B9=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 6 ++---- .../java/calculator/controller/CalculatorController.java | 5 ++++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 7f3196a4b9..7abdbe3dad 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -2,14 +2,12 @@ import calculator.config.Configuration; import calculator.controller.CalculatorController; -import camp.nextstep.edu.missionutils.Console; public class Application { private static final Configuration configuration = new Configuration(); + public static void main(String[] args) { - System.out.println("덧셈할 문자열을 입력해 주세요."); - String s = Console.readLine(); CalculatorController calculatorController = configuration.getCalculatorController(); - calculatorController.input(s); + calculatorController.input(); } } diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index dc113dca27..5e5728f810 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -1,6 +1,7 @@ package calculator.controller; import calculator.service.CalculatorService; +import camp.nextstep.edu.missionutils.Console; public class CalculatorController { private final CalculatorService calculatorService; @@ -9,7 +10,9 @@ public CalculatorController(CalculatorService calculatorService){ this.calculatorService = calculatorService; } - public void input(String s){ + public void input(){ + System.out.println("덧셈할 문자열을 입력해 주세요."); + String s = Console.readLine(); int sum = calculatorService.sum(s); System.out.println("결과 : "+sum); } From 48eaf2d3e6a510b98dcfaf92491826bc62adface Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:29:26 +0900 Subject: [PATCH 23/46] =?UTF-8?q?refactor:input=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=EC=99=80=20output=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=EB=A1=9C=20=EA=B8=B0=EB=8A=A5=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/controller/CalculatorController.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index 5e5728f810..a936afbfcb 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -13,6 +13,9 @@ public CalculatorController(CalculatorService calculatorService){ public void input(){ System.out.println("덧셈할 문자열을 입력해 주세요."); String s = Console.readLine(); + output(s); + } + private void output(String s){ int sum = calculatorService.sum(s); System.out.println("결과 : "+sum); } From c7a6f7f958faf611cdfa1992a0f9ffb9c6ead790 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:30:34 +0900 Subject: [PATCH 24/46] =?UTF-8?q?style:=EC=BD=94=EB=93=9C=20=EC=A0=95?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CalculatorController.java | 9 ++-- .../calculator/service/CalculatorService.java | 46 ++++++++++++------- src/test/java/calculator/ApplicationTest.java | 28 +++++------ 3 files changed, 48 insertions(+), 35 deletions(-) diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index a936afbfcb..9e03705d45 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -6,18 +6,19 @@ public class CalculatorController { private final CalculatorService calculatorService; - public CalculatorController(CalculatorService calculatorService){ + public CalculatorController(CalculatorService calculatorService) { this.calculatorService = calculatorService; } - public void input(){ + public void input() { System.out.println("덧셈할 문자열을 입력해 주세요."); String s = Console.readLine(); output(s); } - private void output(String s){ + + private void output(String s) { int sum = calculatorService.sum(s); - System.out.println("결과 : "+sum); + System.out.println("결과 : " + sum); } } diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 2a94b22639..42c0b78acf 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -4,16 +4,20 @@ public class CalculatorService { private String s; private String plusSeparator; - /** Run all logic step by step. */ - public int sum(String s){ + /** + * Run all logic step by step. + */ + public int sum(String s) { this.s = s; boolean hasPlusSeparator = checkPlusSeparator(); String[] separatedString = separate(hasPlusSeparator); hasNaN(separatedString); return sumSeparatedStringArr(separatedString); } + /** * Check existence of another separator. + * * @return if exist another separator, return true. Or return false. */ private boolean checkPlusSeparator() { @@ -24,43 +28,51 @@ private boolean checkPlusSeparator() { } return false; } + /** * Separate input string. if escape string(\) added separator, need action. + * * @param hasPlusSeparator if another separator exist, set true. Or false. * @return separated string. */ - private String[] separate(boolean hasPlusSeparator){ - if(!hasPlusSeparator){ + private String[] separate(boolean hasPlusSeparator) { + if (!hasPlusSeparator) { return s.split("[:,]"); - }else{ - if(plusSeparator.equals("\\")) - return s.split("[:,"+plusSeparator.repeat(2)+"]"); - return s.split("[:,"+plusSeparator+"]"); + } else { + if (plusSeparator.equals("\\")) { + return s.split("[:," + plusSeparator.repeat(2) + "]"); + } + return s.split("[:," + plusSeparator + "]"); } } + /** - * Check input string array contains not a number. if it contains NaN, - * throw IllegalArgumentException. + * Check input string array contains not a number. if it contains NaN, throw IllegalArgumentException. + * * @param separatedStringArr String separated by separator. */ - private void hasNaN(String[] separatedStringArr){ - for(String s:separatedStringArr){ - for(char c:s.toCharArray()){ - if(c < 48 || c > 57) + private void hasNaN(String[] separatedStringArr) { + for (String s : separatedStringArr) { + for (char c : s.toCharArray()) { + if (c < 48 || c > 57) { throw new IllegalArgumentException(); + } } } } + /** * Sum all of them + * * @param separatedStringArr clean integer string array. * @return Sum */ - private int sumSeparatedStringArr(String[] separatedStringArr){ + private int sumSeparatedStringArr(String[] separatedStringArr) { int sum = 0; - for(String separatedString:separatedStringArr){ - if(!separatedString.isEmpty()) + for (String separatedString : separatedStringArr) { + if (!separatedString.isEmpty()) { sum += Integer.parseInt(separatedString); + } } return sum; } diff --git a/src/test/java/calculator/ApplicationTest.java b/src/test/java/calculator/ApplicationTest.java index 357c8e9288..4ce5a37d10 100644 --- a/src/test/java/calculator/ApplicationTest.java +++ b/src/test/java/calculator/ApplicationTest.java @@ -1,12 +1,12 @@ package calculator; -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; - import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import camp.nextstep.edu.missionutils.test.NsTest; +import org.junit.jupiter.api.Test; + class ApplicationTest extends NsTest { @Test void 커스텀_구분자_사용() { @@ -19,8 +19,8 @@ class ApplicationTest extends NsTest { @Test void 예외_테스트() { assertSimpleTest(() -> - assertThatThrownBy(() -> runException("-1,2,3")) - .isInstanceOf(IllegalArgumentException.class) + assertThatThrownBy(() -> runException("-1,2,3")) + .isInstanceOf(IllegalArgumentException.class) ); } @@ -30,15 +30,15 @@ public void runMain() { } @Test - public void 숫자_구분자_사용(){ - assertSimpleTest(()->{ + public void 숫자_구분자_사용() { + assertSimpleTest(() -> { run("//1\\n2:213"); assertThat(output()).contains("결과 : 7"); }); } @Test - public void 탈출_문자_사용(){ + public void 탈출_문자_사용() { assertSimpleTest(() -> { assertThatThrownBy(() -> runException("1\\2:3")) .isInstanceOf(IllegalArgumentException.class); @@ -46,7 +46,7 @@ public void runMain() { } @Test - public void 탈출_문자_구분자_사용(){ + public void 탈출_문자_구분자_사용() { assertSimpleTest(() -> { run("//\\\\n1\\2:3,4"); assertThat(output()).contains("결과 : 10"); @@ -54,23 +54,23 @@ public void runMain() { } @Test - public void 마침표_구분자_사용(){ - assertSimpleTest(()->{ + public void 마침표_구분자_사용() { + assertSimpleTest(() -> { run("//.\\n1.2.4:3"); assertThat(output()).contains("결과 : 10"); }); } @Test - public void 따옴표_구분자_사용(){ - assertSimpleTest(()->{ + public void 따옴표_구분자_사용() { + assertSimpleTest(() -> { run("//\"\\n1\"2\"4:3"); assertThat(output()).contains("결과 : 10"); }); } @Test - public void 구분자_사이에_문자열_없음(){ + public void 구분자_사이에_문자열_없음() { assertSimpleTest(() -> { run("1,:2"); assertThat(output()).contains("결과 : 3"); From 5e7a5591e027d8c9db950325958c6b13ac4327e6 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:39:44 +0900 Subject: [PATCH 25/46] =?UTF-8?q?test:=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/calculator/ApplicationTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/calculator/ApplicationTest.java b/src/test/java/calculator/ApplicationTest.java index 4ce5a37d10..0ad411b78d 100644 --- a/src/test/java/calculator/ApplicationTest.java +++ b/src/test/java/calculator/ApplicationTest.java @@ -61,6 +61,12 @@ public void runMain() { }); } + @Test + public void 마침표_사용(){ + assertThatThrownBy(() -> runException("1.2.3:4")) + .isInstanceOf(IllegalArgumentException.class); + } + @Test public void 따옴표_구분자_사용() { assertSimpleTest(() -> { @@ -76,4 +82,12 @@ public void runMain() { assertThat(output()).contains("결과 : 3"); }); } + + @Test + public void 문자열_없음(){ + assertSimpleTest(() -> { + run("\n"); + assertThat(output()).contains("결과 : 0"); + }); + } } From 955ccb67d0cc8ac51be47aee5a40f9b4b1ef3b25 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:40:21 +0900 Subject: [PATCH 26/46] docs:Update README.md --- README.md | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ec65943d89..256cd2cb49 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,21 @@ # java-calculator-precourse ## 구현할 기능 목록 + ### 1. 문자열을 입력 받는 기능 + ### 2. 문자열에 추가 구분자 입력이 있는지 확인하는 기능 + ### 3. 문자열을 구분자를 기준으로 나누는 기능 + ### 4. 구분자로 나눠진 문자열에 숫자 외의 다른 문자가 있는지 확인하는 기능 있으면 IllegalArgumentException + ### 5. 나눠진 숫자를 더하는 기능 # 테스트 필요 하다 생각 되는 것들 -+ 문자열을 아무 것도 주지 않았을 때 -+ 추가 구분자로 숫자를 줬을 때 -+ 음수 넣어 보기(추가 구분자로 -를 줬을 때와 아닐 때) -> 생각해 보니까 필요 없을듯. -+ 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) -+ 역슬래시(이스케이프 문자를 구분자로 줬을 때) -+ 구분자 사이에 숫자가 없을 때 \ No newline at end of file + +- [x] 문자열을 아무 것도 주지 않았을 때 +- [x] 추가 구분자로 숫자를 줬을 때 +- [x] 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) +- [x] 역슬래시(이스케이프 문자를 구분자로 줬을 때) +- [x] 구분자 사이에 숫자가 없을 때 \ No newline at end of file From 358957cdc8c0dd30725468bac41e6ea3c2ab9d17 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:48:37 +0900 Subject: [PATCH 27/46] =?UTF-8?q?comment:=EC=A3=BC=EC=84=9D=20=ED=95=9C?= =?UTF-8?q?=EA=B8=80=ED=99=94=ED=95=A8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/service/CalculatorService.java | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 42c0b78acf..c09e134e6f 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -5,7 +5,7 @@ public class CalculatorService { private String plusSeparator; /** - * Run all logic step by step. + * 덧셈 로직을 순차적으로 진행한다. */ public int sum(String s) { this.s = s; @@ -16,9 +16,9 @@ public int sum(String s) { } /** - * Check existence of another separator. + * 커스텀 구분자가 있는지 확인한다. * - * @return if exist another separator, return true. Or return false. + * @return 만약 커스텀 구분자가 있다면 true, 없다면 false 를 반환한다. */ private boolean checkPlusSeparator() { if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { @@ -30,15 +30,15 @@ private boolean checkPlusSeparator() { } /** - * Separate input string. if escape string(\) added separator, need action. + * 받은 문자열을 구분자를 기준으로 나눈다. 만약 커스텀 구분자에 역슬래시가 있다면 문제가 생기지 않도록 한다. * - * @param hasPlusSeparator if another separator exist, set true. Or false. - * @return separated string. + * @param hasPlusSeparator 커스텀 구분자가 있다면 true, 없다면 false 를 입력한다. + * @return 분리된 문자열을 반환한다. 이 문자열에는 반드시 숫자만 있는 것은 아니다. */ private String[] separate(boolean hasPlusSeparator) { if (!hasPlusSeparator) { return s.split("[:,]"); - } else { + } else { //else 문 삭제해도 됨. #################################################### if (plusSeparator.equals("\\")) { return s.split("[:," + plusSeparator.repeat(2) + "]"); } @@ -47,9 +47,9 @@ private String[] separate(boolean hasPlusSeparator) { } /** - * Check input string array contains not a number. if it contains NaN, throw IllegalArgumentException. + * 숫자가 아닌 문자열이 있는지 확인한다. 만약 있다면 IllegalArgumentException 으로 처리한다. * - * @param separatedStringArr String separated by separator. + * @param separatedStringArr 숫자 외의 문자가 있는지 확인하고 싶은 문자열을 입력한다. */ private void hasNaN(String[] separatedStringArr) { for (String s : separatedStringArr) { @@ -62,10 +62,10 @@ private void hasNaN(String[] separatedStringArr) { } /** - * Sum all of them + * 문자열을 int 형으로 바꾼 뒤 합한다. * - * @param separatedStringArr clean integer string array. - * @return Sum + * @param separatedStringArr 숫자로만 이루어진 문자열 배열을 입력한다. + * @return 합한다. */ private int sumSeparatedStringArr(String[] separatedStringArr) { int sum = 0; From 2af965bb944d2eca83c1b8b87d3d94f23a53f251 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 12:49:22 +0900 Subject: [PATCH 28/46] =?UTF-8?q?refator:=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20else=20=EB=AC=B8=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/service/CalculatorService.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index c09e134e6f..cd2a740f5e 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -38,12 +38,11 @@ private boolean checkPlusSeparator() { private String[] separate(boolean hasPlusSeparator) { if (!hasPlusSeparator) { return s.split("[:,]"); - } else { //else 문 삭제해도 됨. #################################################### - if (plusSeparator.equals("\\")) { - return s.split("[:," + plusSeparator.repeat(2) + "]"); - } - return s.split("[:," + plusSeparator + "]"); } + if (plusSeparator.equals("\\")) { + return s.split("[:," + plusSeparator.repeat(2) + "]"); + } + return s.split("[:," + plusSeparator + "]"); } /** From ba1863264f5b075197176d1e7d9ef9cf7414ff01 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 14:09:50 +0900 Subject: [PATCH 29/46] docs:Update README.md --- README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 256cd2cb49..bde8de2f6c 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,28 @@ ### 5. 나눠진 숫자를 더하는 기능 -# 테스트 필요 하다 생각 되는 것들 +## 테스트 필요 하다 생각 되는 것들 - [x] 문자열을 아무 것도 주지 않았을 때 - [x] 추가 구분자로 숫자를 줬을 때 - [x] 소수 넣어 보기(추가 구분자로 .을 줬을 때와 아닐 때) - [x] 역슬래시(이스케이프 문자를 구분자로 줬을 때) -- [x] 구분자 사이에 숫자가 없을 때 \ No newline at end of file +- [x] 구분자 사이에 숫자가 없을 때 + +## 확인 필요 + +### 1. 커스텀 구분자가 여러 개라면? + 현재 만들어진 로직은 커스텀 구분자가 한 개인 경우만을 받고 있으나 그렇지 않을 수도 있음. + +### 2. 커스텀 구분자에 숫자가 들어온다면? + 당연히 계산기라면 커스텀 구분자에 숫자가 들어오면 안되긴 함. + 커스텀 구분자에 숫자가 들어오는 경우 IllegalArgumentException 해야하는가? + 아니면 구분자로 인식하고 계산시켜야 하는가? + 지금은 계산하도록 만들어둠. + +### 3. 아무 입력도 하지 않았다면? + 입력이 없으면 0을 출력하도록 했음. 근데 IllegalArgumentException 해야하는지? + +### 4. 구분자 사이에 숫자가 없다면? + 현재는 사이에 숫자가 없다면 0으로 인식하고 계산을 진행하도록 하였음. + 이것도 throw 해야하는 경우인가? \ No newline at end of file From 31920dda22ebdcb87b77ec3a312ec1537f19b0b9 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 18:53:36 +0900 Subject: [PATCH 30/46] =?UTF-8?q?2=EC=A4=91=20for=20=EB=AC=B8=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0.=20stream=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/calculator/config/Configuration.java | 2 +- .../controller/CalculatorController.java | 4 +-- .../calculator/service/CalculatorService.java | 34 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/calculator/config/Configuration.java b/src/main/java/calculator/config/Configuration.java index f53c08715b..6bb996f606 100644 --- a/src/main/java/calculator/config/Configuration.java +++ b/src/main/java/calculator/config/Configuration.java @@ -7,7 +7,7 @@ public class Configuration { private final CalculatorController calculatorController; public Configuration() { - this.calculatorController = new CalculatorController(new CalculatorService()); + this.calculatorController = new CalculatorController(); } public CalculatorController getCalculatorController() { diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index 9e03705d45..455af28aa5 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -6,8 +6,8 @@ public class CalculatorController { private final CalculatorService calculatorService; - public CalculatorController(CalculatorService calculatorService) { - this.calculatorService = calculatorService; + public CalculatorController() { + this.calculatorService = new CalculatorService(); } public void input() { diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index cd2a740f5e..28442b528a 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -1,16 +1,18 @@ package calculator.service; +import java.util.Arrays; + public class CalculatorService { private String s; - private String plusSeparator; + private String customSeparator; /** * 덧셈 로직을 순차적으로 진행한다. */ public int sum(String s) { this.s = s; - boolean hasPlusSeparator = checkPlusSeparator(); - String[] separatedString = separate(hasPlusSeparator); + boolean hasCustomSeparator = checkCustomSeparator(); + String[] separatedString = separate(hasCustomSeparator); hasNaN(separatedString); return sumSeparatedStringArr(separatedString); } @@ -20,9 +22,9 @@ public int sum(String s) { * * @return 만약 커스텀 구분자가 있다면 true, 없다면 false 를 반환한다. */ - private boolean checkPlusSeparator() { + private boolean checkCustomSeparator() { if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { - plusSeparator = String.valueOf(s.charAt(2)); + customSeparator = String.valueOf(s.charAt(2)); s = s.substring(5); return true; } @@ -32,17 +34,17 @@ private boolean checkPlusSeparator() { /** * 받은 문자열을 구분자를 기준으로 나눈다. 만약 커스텀 구분자에 역슬래시가 있다면 문제가 생기지 않도록 한다. * - * @param hasPlusSeparator 커스텀 구분자가 있다면 true, 없다면 false 를 입력한다. + * @param hasCustomSeparator 커스텀 구분자가 있다면 true, 없다면 false 를 입력한다. * @return 분리된 문자열을 반환한다. 이 문자열에는 반드시 숫자만 있는 것은 아니다. */ - private String[] separate(boolean hasPlusSeparator) { - if (!hasPlusSeparator) { + private String[] separate(boolean hasCustomSeparator) { + if (!hasCustomSeparator) { return s.split("[:,]"); } - if (plusSeparator.equals("\\")) { - return s.split("[:," + plusSeparator.repeat(2) + "]"); + if (customSeparator.equals("\\")) { + return s.split("[:," + customSeparator.repeat(2) + "]"); } - return s.split("[:," + plusSeparator + "]"); + return s.split("[:," + customSeparator + "]"); } /** @@ -51,12 +53,10 @@ private String[] separate(boolean hasPlusSeparator) { * @param separatedStringArr 숫자 외의 문자가 있는지 확인하고 싶은 문자열을 입력한다. */ private void hasNaN(String[] separatedStringArr) { - for (String s : separatedStringArr) { - for (char c : s.toCharArray()) { - if (c < 48 || c > 57) { - throw new IllegalArgumentException(); - } - } + if (Arrays.stream(separatedStringArr) + .flatMapToInt(String::chars) + .anyMatch(it -> it < '0' || it > '9')) { + throw new IllegalArgumentException(); } } From 71f7fedfed27c2c857427cb5cbb86a65425402a8 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 21:09:25 +0900 Subject: [PATCH 31/46] docs:Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bde8de2f6c..3a3b2e5b86 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ ### 1. 커스텀 구분자가 여러 개라면? 현재 만들어진 로직은 커스텀 구분자가 한 개인 경우만을 받고 있으나 그렇지 않을 수도 있음. + 만약에 //와 \n 사이에 두 개가 들어가면 그건 따로 떨어뜨려서 구분을 해야하나? + //ea\n1ea2ea3ea 이 주어지면 ea로 묶어서 구분자로 사용해야 하나? ### 2. 커스텀 구분자에 숫자가 들어온다면? 당연히 계산기라면 커스텀 구분자에 숫자가 들어오면 안되긴 함. From e9e55966b75448a4769840ba4b1bc820a25c0db0 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sat, 19 Oct 2024 21:17:19 +0900 Subject: [PATCH 32/46] =?UTF-8?q?refactor:Configuration=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EC=97=90=20=EC=8B=B1=EA=B8=80=ED=86=A4=20?= =?UTF-8?q?=ED=8C=A8=ED=84=B4=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 2 +- src/main/java/calculator/config/Configuration.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 7abdbe3dad..36d4b3fe83 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -4,7 +4,7 @@ import calculator.controller.CalculatorController; public class Application { - private static final Configuration configuration = new Configuration(); + private static final Configuration configuration = Configuration.getInstance(); public static void main(String[] args) { CalculatorController calculatorController = configuration.getCalculatorController(); diff --git a/src/main/java/calculator/config/Configuration.java b/src/main/java/calculator/config/Configuration.java index 6bb996f606..5854292068 100644 --- a/src/main/java/calculator/config/Configuration.java +++ b/src/main/java/calculator/config/Configuration.java @@ -4,10 +4,14 @@ import calculator.service.CalculatorService; public class Configuration { - private final CalculatorController calculatorController; + private static final Configuration configuration = new Configuration(); + private final CalculatorController calculatorController = new CalculatorController(); - public Configuration() { - this.calculatorController = new CalculatorController(); + private Configuration() { + } + + public static Configuration getInstance() { + return configuration; } public CalculatorController getCalculatorController() { From f5514b576c7f03d27bdd0788f5fb85f774b98248 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 12:13:33 +0900 Subject: [PATCH 33/46] =?UTF-8?q?refactor:CalculatorService=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EC=9D=98=20sum()=20->=20add()=20=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/controller/CalculatorController.java | 2 +- src/main/java/calculator/service/CalculatorService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index 455af28aa5..6532bf8816 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -17,7 +17,7 @@ public void input() { } private void output(String s) { - int sum = calculatorService.sum(s); + int sum = calculatorService.add(s); System.out.println("결과 : " + sum); } diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 28442b528a..6f5ab39132 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -9,7 +9,7 @@ public class CalculatorService { /** * 덧셈 로직을 순차적으로 진행한다. */ - public int sum(String s) { + public int add(String s) { this.s = s; boolean hasCustomSeparator = checkCustomSeparator(); String[] separatedString = separate(hasCustomSeparator); From 9a99a54e4700150623d648b2bec2c069ce11e45b Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 12:16:53 +0900 Subject: [PATCH 34/46] =?UTF-8?q?refactor:CalculatorController=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=EC=9D=98=20=EB=B3=80=EC=88=98=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/controller/CalculatorController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index 6532bf8816..c394ac5b66 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -17,8 +17,8 @@ public void input() { } private void output(String s) { - int sum = calculatorService.add(s); - System.out.println("결과 : " + sum); + int result = calculatorService.add(s); + System.out.println("결과 : " + result); } } From 40b348910e8ed2ec43b97a4f3fe1356812234193 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 12:38:27 +0900 Subject: [PATCH 35/46] =?UTF-8?q?refactor:view=EB=A5=BC=20=EB=94=B0?= =?UTF-8?q?=EB=A1=9C=20=EA=B4=80=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC=ED=95=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CalculatorController.java | 19 ++++++++++++------- src/main/java/calculator/view/InputView.java | 14 ++++++++++++++ src/main/java/calculator/view/OutputView.java | 7 +++++++ 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 src/main/java/calculator/view/InputView.java create mode 100644 src/main/java/calculator/view/OutputView.java diff --git a/src/main/java/calculator/controller/CalculatorController.java b/src/main/java/calculator/controller/CalculatorController.java index c394ac5b66..202f34b428 100644 --- a/src/main/java/calculator/controller/CalculatorController.java +++ b/src/main/java/calculator/controller/CalculatorController.java @@ -1,24 +1,29 @@ package calculator.controller; import calculator.service.CalculatorService; -import camp.nextstep.edu.missionutils.Console; +import calculator.view.InputView; +import calculator.view.OutputView; public class CalculatorController { private final CalculatorService calculatorService; + private final InputView inputView; + private final OutputView outputView; public CalculatorController() { + this.inputView = new InputView(); + this.outputView = new OutputView(); this.calculatorService = new CalculatorService(); } public void input() { - System.out.println("덧셈할 문자열을 입력해 주세요."); - String s = Console.readLine(); - output(s); + String s = inputView.inputString(); + inputView.close(); + int addedResult = calculatorService.add(s); + output(addedResult); } - private void output(String s) { - int result = calculatorService.add(s); - System.out.println("결과 : " + result); + private void output(int i) { + outputView.outputInteger(i); } } diff --git a/src/main/java/calculator/view/InputView.java b/src/main/java/calculator/view/InputView.java new file mode 100644 index 0000000000..1032659f10 --- /dev/null +++ b/src/main/java/calculator/view/InputView.java @@ -0,0 +1,14 @@ +package calculator.view; + +import camp.nextstep.edu.missionutils.Console; + +public class InputView { + public String inputString() { + System.out.println("덧셈할 문자열을 입력해주세요."); + return Console.readLine(); + } + + public void close() { + Console.close(); + } +} diff --git a/src/main/java/calculator/view/OutputView.java b/src/main/java/calculator/view/OutputView.java new file mode 100644 index 0000000000..1dfd3c9b5d --- /dev/null +++ b/src/main/java/calculator/view/OutputView.java @@ -0,0 +1,7 @@ +package calculator.view; + +public class OutputView { + public void outputInteger(int i) { + System.out.println("결과 : " + i); + } +} From 8451b22f2248d51e7d4ffb777b386e661d86d326 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 12:39:13 +0900 Subject: [PATCH 36/46] =?UTF-8?q?refactor:=EC=82=AC=EC=9A=A9=ED=95=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20import=20=EB=AC=B8=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/config/Configuration.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/calculator/config/Configuration.java b/src/main/java/calculator/config/Configuration.java index 5854292068..c08a708298 100644 --- a/src/main/java/calculator/config/Configuration.java +++ b/src/main/java/calculator/config/Configuration.java @@ -1,7 +1,6 @@ package calculator.config; import calculator.controller.CalculatorController; -import calculator.service.CalculatorService; public class Configuration { private static final Configuration configuration = new Configuration(); From 98313ec785f67332acd8762cadde853fbb506853 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 17:19:07 +0900 Subject: [PATCH 37/46] =?UTF-8?q?refactor:=EC=98=A4=EB=A5=98=20=EB=A9=94?= =?UTF-8?q?=EC=84=B8=EC=A7=80=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/service/CalculatorService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 6f5ab39132..f5b29e7a48 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -56,7 +56,7 @@ private void hasNaN(String[] separatedStringArr) { if (Arrays.stream(separatedStringArr) .flatMapToInt(String::chars) .anyMatch(it -> it < '0' || it > '9')) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("입력한 문자열에 구분자, 숫자 외의 문자가 존재합니다."); } } From 5897fa63aec8d45f992c00f3ddbfc7e2e50fcbb1 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 17:20:49 +0900 Subject: [PATCH 38/46] =?UTF-8?q?refactor:=EB=B3=80=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/service/CalculatorService.java | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index f5b29e7a48..a49e572a07 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -3,14 +3,14 @@ import java.util.Arrays; public class CalculatorService { - private String s; + private String inputString; private String customSeparator; /** * 덧셈 로직을 순차적으로 진행한다. */ - public int add(String s) { - this.s = s; + public int add(String inputString) { + this.inputString = inputString; boolean hasCustomSeparator = checkCustomSeparator(); String[] separatedString = separate(hasCustomSeparator); hasNaN(separatedString); @@ -23,9 +23,9 @@ public int add(String s) { * @return 만약 커스텀 구분자가 있다면 true, 없다면 false 를 반환한다. */ private boolean checkCustomSeparator() { - if (s.length() >= 5 && s.startsWith("//") && s.startsWith("\\n", 3)) { - customSeparator = String.valueOf(s.charAt(2)); - s = s.substring(5); + if (inputString.length() >= 5 && inputString.startsWith("//") && inputString.startsWith("\\n", 3)) { + customSeparator = String.valueOf(inputString.charAt(2)); + inputString = inputString.substring(5); return true; } return false; @@ -39,12 +39,12 @@ private boolean checkCustomSeparator() { */ private String[] separate(boolean hasCustomSeparator) { if (!hasCustomSeparator) { - return s.split("[:,]"); + return inputString.split("[:,]"); } if (customSeparator.equals("\\")) { - return s.split("[:," + customSeparator.repeat(2) + "]"); + return inputString.split("[:," + customSeparator.repeat(2) + "]"); } - return s.split("[:," + customSeparator + "]"); + return inputString.split("[:," + customSeparator + "]"); } /** From 0ea77458c99c2f2c7980e60a12cdffda324133c8 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 17:32:04 +0900 Subject: [PATCH 39/46] =?UTF-8?q?refactor:CalculatorValidator=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/service/CalculatorService.java | 22 ++++++------------- .../validator/CalculatorValidator.java | 19 ++++++++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 src/main/java/calculator/validator/CalculatorValidator.java diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index a49e572a07..88da90d2ec 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -1,11 +1,16 @@ package calculator.service; -import java.util.Arrays; +import calculator.validator.CalculatorValidator; public class CalculatorService { + private final CalculatorValidator calculatorValidator; private String inputString; private String customSeparator; + public CalculatorService() { + this.calculatorValidator = new CalculatorValidator(); + } + /** * 덧셈 로직을 순차적으로 진행한다. */ @@ -13,7 +18,7 @@ public int add(String inputString) { this.inputString = inputString; boolean hasCustomSeparator = checkCustomSeparator(); String[] separatedString = separate(hasCustomSeparator); - hasNaN(separatedString); + calculatorValidator.hasNaN(separatedString); return sumSeparatedStringArr(separatedString); } @@ -47,19 +52,6 @@ private String[] separate(boolean hasCustomSeparator) { return inputString.split("[:," + customSeparator + "]"); } - /** - * 숫자가 아닌 문자열이 있는지 확인한다. 만약 있다면 IllegalArgumentException 으로 처리한다. - * - * @param separatedStringArr 숫자 외의 문자가 있는지 확인하고 싶은 문자열을 입력한다. - */ - private void hasNaN(String[] separatedStringArr) { - if (Arrays.stream(separatedStringArr) - .flatMapToInt(String::chars) - .anyMatch(it -> it < '0' || it > '9')) { - throw new IllegalArgumentException("입력한 문자열에 구분자, 숫자 외의 문자가 존재합니다."); - } - } - /** * 문자열을 int 형으로 바꾼 뒤 합한다. * diff --git a/src/main/java/calculator/validator/CalculatorValidator.java b/src/main/java/calculator/validator/CalculatorValidator.java new file mode 100644 index 0000000000..638758278c --- /dev/null +++ b/src/main/java/calculator/validator/CalculatorValidator.java @@ -0,0 +1,19 @@ +package calculator.validator; + +import java.util.Arrays; + +public class CalculatorValidator { + + /** + * 숫자가 아닌 문자열이 있는지 확인한다. 만약 있다면 IllegalArgumentException 으로 처리한다. + * + * @param separatedStringArr 숫자 외의 문자가 있는지 확인하고 싶은 문자열을 입력한다. + */ + public void hasNaN(String[] separatedStringArr) { + if (Arrays.stream(separatedStringArr) + .flatMapToInt(String::chars) + .anyMatch(it -> it < '0' || it > '9')) { + throw new IllegalArgumentException("입력한 문자열에 구분자, 숫자 외의 문자가 존재합니다."); + } + } +} From a4d6f60b5c1e862021fb216d39488d0613259c5b Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 18:49:03 +0900 Subject: [PATCH 40/46] =?UTF-8?q?refactor:=EC=83=81=EC=88=98=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20enum=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/calculator/constant/CalConst.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/calculator/constant/CalConst.java diff --git a/src/main/java/calculator/constant/CalConst.java b/src/main/java/calculator/constant/CalConst.java new file mode 100644 index 0000000000..aa667dc854 --- /dev/null +++ b/src/main/java/calculator/constant/CalConst.java @@ -0,0 +1,19 @@ +package calculator.constant; + +public enum CalConst { + MINIMUM_LENGTH_IF_EXIST_CUSTOM_SEPARATOR(5), + START_INDEX_OF_CALCULATING(5), + END_INDEX_OF_CUSTOM_SEPARATOR(3), + INDEX_OF_CUSTOM_SEPARATOR(2), + ; + + private final int value; + + CalConst(int value) { + this.value = value; + } + + public int value() { + return value; + } +} From ce6ed735bc107d0fae1cce2b57107474f98b74f1 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 18:49:54 +0900 Subject: [PATCH 41/46] =?UTF-8?q?refactor:=EB=B3=80=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Application.java | 4 ++-- src/main/java/calculator/config/Configuration.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 36d4b3fe83..88ba3001de 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -4,10 +4,10 @@ import calculator.controller.CalculatorController; public class Application { - private static final Configuration configuration = Configuration.getInstance(); + private static final Configuration CONFIGURATION = Configuration.getInstance(); public static void main(String[] args) { - CalculatorController calculatorController = configuration.getCalculatorController(); + CalculatorController calculatorController = CONFIGURATION.getCalculatorController(); calculatorController.input(); } } diff --git a/src/main/java/calculator/config/Configuration.java b/src/main/java/calculator/config/Configuration.java index c08a708298..2a9e172937 100644 --- a/src/main/java/calculator/config/Configuration.java +++ b/src/main/java/calculator/config/Configuration.java @@ -3,14 +3,14 @@ import calculator.controller.CalculatorController; public class Configuration { - private static final Configuration configuration = new Configuration(); + private static final Configuration CONFIGURATION = new Configuration(); private final CalculatorController calculatorController = new CalculatorController(); private Configuration() { } public static Configuration getInstance() { - return configuration; + return CONFIGURATION; } public CalculatorController getCalculatorController() { From 831a58fd8686b0c21b32c8670e3f438c2e6b9bd5 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 18:57:40 +0900 Subject: [PATCH 42/46] =?UTF-8?q?refactor:=ED=81=B4=EB=9E=98=EC=8A=A4=20?= =?UTF-8?q?=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EA=B7=B8?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EB=B3=80=EC=88=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/service/CalculatorService.java | 9 +++------ .../validator/Validator.java} | 4 ++-- 2 files changed, 5 insertions(+), 8 deletions(-) rename src/main/java/calculator/{validator/CalculatorValidator.java => service/validator/Validator.java} (90%) diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 88da90d2ec..9f97be8de9 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -1,15 +1,12 @@ package calculator.service; -import calculator.validator.CalculatorValidator; +import calculator.service.validator.Validator; public class CalculatorService { - private final CalculatorValidator calculatorValidator; + private final Validator validator = new Validator(); private String inputString; private String customSeparator; - public CalculatorService() { - this.calculatorValidator = new CalculatorValidator(); - } /** * 덧셈 로직을 순차적으로 진행한다. @@ -18,7 +15,7 @@ public int add(String inputString) { this.inputString = inputString; boolean hasCustomSeparator = checkCustomSeparator(); String[] separatedString = separate(hasCustomSeparator); - calculatorValidator.hasNaN(separatedString); + validator.hasNaN(separatedString); return sumSeparatedStringArr(separatedString); } diff --git a/src/main/java/calculator/validator/CalculatorValidator.java b/src/main/java/calculator/service/validator/Validator.java similarity index 90% rename from src/main/java/calculator/validator/CalculatorValidator.java rename to src/main/java/calculator/service/validator/Validator.java index 638758278c..82fbc01ba3 100644 --- a/src/main/java/calculator/validator/CalculatorValidator.java +++ b/src/main/java/calculator/service/validator/Validator.java @@ -1,8 +1,8 @@ -package calculator.validator; +package calculator.service.validator; import java.util.Arrays; -public class CalculatorValidator { +public class Validator { /** * 숫자가 아닌 문자열이 있는지 확인한다. 만약 있다면 IllegalArgumentException 으로 처리한다. From c145fc07344a1fe8d91241a0cbe5ff3d0d0afcf2 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 19:19:13 +0900 Subject: [PATCH 43/46] =?UTF-8?q?refactor:Separator=20=ED=81=B4=EB=9E=98?= =?UTF-8?q?=EC=8A=A4=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/dto/CheckSeparatorDto.java | 22 ++++++++++ .../calculator/service/CalculatorService.java | 43 ++++--------------- .../service/separator/Separator.java | 33 ++++++++++++++ 3 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 src/main/java/calculator/dto/CheckSeparatorDto.java create mode 100644 src/main/java/calculator/service/separator/Separator.java diff --git a/src/main/java/calculator/dto/CheckSeparatorDto.java b/src/main/java/calculator/dto/CheckSeparatorDto.java new file mode 100644 index 0000000000..532693dda6 --- /dev/null +++ b/src/main/java/calculator/dto/CheckSeparatorDto.java @@ -0,0 +1,22 @@ +package calculator.dto; + +public class CheckSeparatorDto { + private String inputString; + private String customSeparator = null; + + public String getInputString() { + return inputString; + } + + public void setInputString(String inputString) { + this.inputString = inputString; + } + + public String getCustomSeparator() { + return customSeparator; + } + + public void setCustomSeparator(String customSeparator) { + this.customSeparator = customSeparator; + } +} diff --git a/src/main/java/calculator/service/CalculatorService.java b/src/main/java/calculator/service/CalculatorService.java index 9f97be8de9..9fa2344e99 100644 --- a/src/main/java/calculator/service/CalculatorService.java +++ b/src/main/java/calculator/service/CalculatorService.java @@ -1,54 +1,27 @@ package calculator.service; +import calculator.dto.CheckSeparatorDto; +import calculator.service.separator.Separator; import calculator.service.validator.Validator; public class CalculatorService { private final Validator validator = new Validator(); - private String inputString; - private String customSeparator; + private final Separator separator = new Separator(); /** * 덧셈 로직을 순차적으로 진행한다. */ public int add(String inputString) { - this.inputString = inputString; - boolean hasCustomSeparator = checkCustomSeparator(); - String[] separatedString = separate(hasCustomSeparator); + CheckSeparatorDto checkSeparatorDto = new CheckSeparatorDto(); + checkSeparatorDto.setInputString(inputString); + separator.checkCustomSeparator(checkSeparatorDto); + String[] separatedString = separator.separate(checkSeparatorDto); + validator.hasNaN(separatedString); return sumSeparatedStringArr(separatedString); } - /** - * 커스텀 구분자가 있는지 확인한다. - * - * @return 만약 커스텀 구분자가 있다면 true, 없다면 false 를 반환한다. - */ - private boolean checkCustomSeparator() { - if (inputString.length() >= 5 && inputString.startsWith("//") && inputString.startsWith("\\n", 3)) { - customSeparator = String.valueOf(inputString.charAt(2)); - inputString = inputString.substring(5); - return true; - } - return false; - } - - /** - * 받은 문자열을 구분자를 기준으로 나눈다. 만약 커스텀 구분자에 역슬래시가 있다면 문제가 생기지 않도록 한다. - * - * @param hasCustomSeparator 커스텀 구분자가 있다면 true, 없다면 false 를 입력한다. - * @return 분리된 문자열을 반환한다. 이 문자열에는 반드시 숫자만 있는 것은 아니다. - */ - private String[] separate(boolean hasCustomSeparator) { - if (!hasCustomSeparator) { - return inputString.split("[:,]"); - } - if (customSeparator.equals("\\")) { - return inputString.split("[:," + customSeparator.repeat(2) + "]"); - } - return inputString.split("[:," + customSeparator + "]"); - } - /** * 문자열을 int 형으로 바꾼 뒤 합한다. * diff --git a/src/main/java/calculator/service/separator/Separator.java b/src/main/java/calculator/service/separator/Separator.java new file mode 100644 index 0000000000..54fccb5c9e --- /dev/null +++ b/src/main/java/calculator/service/separator/Separator.java @@ -0,0 +1,33 @@ +package calculator.service.separator; + +import calculator.constant.CalConst; +import calculator.dto.CheckSeparatorDto; + +public class Separator { + + public void checkCustomSeparator(CheckSeparatorDto checkSeparatorDto) { + String inputString = checkSeparatorDto.getInputString(); + String customSeparator; + if (inputString.length() >= CalConst.MINIMUM_LENGTH_IF_EXIST_CUSTOM_SEPARATOR.value() + && inputString.startsWith("//") + && inputString.startsWith("\\n", CalConst.END_INDEX_OF_CUSTOM_SEPARATOR.value())) { + customSeparator = String.valueOf(inputString.charAt(CalConst.INDEX_OF_CUSTOM_SEPARATOR.value())); + inputString = inputString.substring(CalConst.START_INDEX_OF_CALCULATING.value()); + + checkSeparatorDto.setCustomSeparator(customSeparator); + checkSeparatorDto.setInputString(inputString); + } + } + + public String[] separate(CheckSeparatorDto checkSeparatorDto) { + String customSeparator = checkSeparatorDto.getCustomSeparator(); + String inputString = checkSeparatorDto.getInputString(); + if (customSeparator==null) { + return inputString.split("[:,]"); + } + if (customSeparator.equals("\\")) { + return inputString.split("[:," + customSeparator.repeat(2) + "]"); + } + return inputString.split("[:," + customSeparator + "]"); + } +} From c677162388933a8f73c19b549c29b661c41c754a Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Sun, 20 Oct 2024 19:31:49 +0900 Subject: [PATCH 44/46] =?UTF-8?q?comment:=EC=A3=BC=EC=84=9D=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../calculator/service/separator/Separator.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/service/separator/Separator.java b/src/main/java/calculator/service/separator/Separator.java index 54fccb5c9e..ae6f1203e2 100644 --- a/src/main/java/calculator/service/separator/Separator.java +++ b/src/main/java/calculator/service/separator/Separator.java @@ -5,9 +5,16 @@ public class Separator { + /** + * 커스텀 구분자가 있는지 확인한다. 만약 있다면, checkSeparatorDto.customSeparator 에 null 대신 커스텀 구분자를 넣는다. 또한, + * checkSeparatorDto.inputString 에 커스텀 구분자를 확인하는 부분을 제거한 문자열을 넣는다. + * + * @param checkSeparatorDto inputString 만 set 되어 있는 dto. + */ public void checkCustomSeparator(CheckSeparatorDto checkSeparatorDto) { String inputString = checkSeparatorDto.getInputString(); String customSeparator; + if (inputString.length() >= CalConst.MINIMUM_LENGTH_IF_EXIST_CUSTOM_SEPARATOR.value() && inputString.startsWith("//") && inputString.startsWith("\\n", CalConst.END_INDEX_OF_CUSTOM_SEPARATOR.value())) { @@ -19,10 +26,17 @@ public void checkCustomSeparator(CheckSeparatorDto checkSeparatorDto) { } } + /** + * 문자열을 구분자를 이용해 나눈다. + * + * @param checkSeparatorDto 커스텀 구분자(or null)와 커스텀 구분자 확인 부분을 제거한 문자열을 넣은 Dto. + * @return 구분자를 이용해 나눠진 문자열을 반환한다. 이 문자열에는 숫자 외의 문자가 존재할 수 있다. 계산에 이용하려면 추가적인 확인 과정이 필요하다. + */ public String[] separate(CheckSeparatorDto checkSeparatorDto) { String customSeparator = checkSeparatorDto.getCustomSeparator(); String inputString = checkSeparatorDto.getInputString(); - if (customSeparator==null) { + + if (customSeparator == null) { return inputString.split("[:,]"); } if (customSeparator.equals("\\")) { From 9ed071ba13d18bf1af11735221cc27a804cdde32 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Mon, 21 Oct 2024 01:14:38 +0900 Subject: [PATCH 45/46] docs:Update README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 3a3b2e5b86..baa604516d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,25 @@ # java-calculator-precourse + calculator + ┣ config + ┃ ┗ Configuration.java + ┣ constant + ┃ ┗ CalConst.java + ┣ controller + ┃ ┗ CalculatorController.java + ┣ dto + ┃ ┗ CheckSeparatorDto.java + ┣ service + ┃ ┣ separator + ┃ ┃ ┗ Separator.java + ┃ ┣ validator + ┃ ┃ ┗ Validator.java + ┃ ┗ CalculatorService.java + ┣ view + ┃ ┣ InputView.java + ┃ ┗ OutputView.java + ┗ Application.java + ## 구현할 기능 목록 ### 1. 문자열을 입력 받는 기능 From f4a9c3d70af4c7c922f4701a5b89710e48e37c91 Mon Sep 17 00:00:00 2001 From: whrjsgml0000 Date: Mon, 21 Oct 2024 01:17:50 +0900 Subject: [PATCH 46/46] docs:Update README.md --- README.md | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index baa604516d..cadd33b4f1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # java-calculator-precourse +# Project Directory calculator ┣ config ┃ ┗ Configuration.java @@ -22,15 +23,11 @@ ## 구현할 기능 목록 -### 1. 문자열을 입력 받는 기능 - -### 2. 문자열에 추가 구분자 입력이 있는지 확인하는 기능 - -### 3. 문자열을 구분자를 기준으로 나누는 기능 - -### 4. 구분자로 나눠진 문자열에 숫자 외의 다른 문자가 있는지 확인하는 기능 있으면 IllegalArgumentException - -### 5. 나눠진 숫자를 더하는 기능 + 1. 문자열을 입력 받는 기능 + 2. 문자열에 추가 구분자 입력이 있는지 확인하는 기능 + 3. 문자열을 구분자를 기준으로 나누는 기능 + 4. 구분자로 나눠진 문자열에 숫자 외의 다른 문자가 있는지 확인하는 기능 있으면 IllegalArgumentException + 5. 나눠진 숫자를 더하는 기능 ## 테스트 필요 하다 생각 되는 것들