From 39319aa60ff82d6d1e7fe3f622fb04522a18d753 Mon Sep 17 00:00:00 2001 From: Regyung Date: Mon, 3 Feb 2025 22:20:25 +0900 Subject: [PATCH 1/5] =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bd90ef0..9a65e15 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# java-calculator-precourse \ No newline at end of file +# java-calculator-precourse + +## 구현할 기능 +1. 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다. + - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 +# +2. 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다. + - 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. +# +3. 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다. \ No newline at end of file From 20c2c23d8ffc89afbe84b9f9023b0d50c8fb31e4 Mon Sep 17 00:00:00 2001 From: Regyung Date: Mon, 3 Feb 2025 22:23:44 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EA=B5=AC=ED=98=84=ED=95=A0=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EB=AA=A9=EB=A1=9D=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a65e15..7d68cac 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # java-calculator-precourse -## 구현할 기능 +## 구현할 기능 목록 1. 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다. - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 # From aa4680c71c81e546ab882703dd14c39ab196c2c1 Mon Sep 17 00:00:00 2001 From: Regyung Date: Mon, 3 Feb 2025 22:25:32 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=EC=89=BC=ED=91=9C(,)=20=EB=98=90=EB=8A=94?= =?UTF-8?q?=20=EC=BD=9C=EB=A1=A0(:)=EC=9D=84=20=EA=B5=AC=EB=B6=84=EC=9E=90?= =?UTF-8?q?=EB=A1=9C=20=EA=B0=80=EC=A7=80=EB=8A=94=20=EB=AC=B8=EC=9E=90?= =?UTF-8?q?=EC=97=B4=EC=9D=84=20=EC=A0=84=EB=8B=AC=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EA=B2=BD=EC=9A=B0=20=EA=B5=AC=EB=B6=84=EC=9E=90=EB=A5=BC=20?= =?UTF-8?q?=EA=B8=B0=EC=A4=80=EC=9C=BC=EB=A1=9C=20=EB=B6=84=EB=A6=AC?= =?UTF-8?q?=ED=95=9C=20=EA=B0=81=20=EC=88=AB=EC=9E=90=EC=9D=98=20=ED=95=A9?= =?UTF-8?q?=EC=9D=84=20=EB=B0=98=ED=99=98=ED=95=9C=EB=8B=A4.?= 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, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 573580f..7c30456 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -1,7 +1,32 @@ package calculator; +import camp.nextstep.edu.missionutils.Console; + +import java.util.ArrayList; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + // 값 입력받기 + System.out.println("덧셈할 문자열을 입력해 주세요."); + String word = Console.readLine(); + System.out.println(word); + + // 구분자 정리 + ArrayList operatorList = new ArrayList(); + operatorList.add(","); + operatorList.add(":"); + String[] operatorArray = new String[operatorList.size()]; + for (int i = 0; i < operatorList.size(); i++) + operatorArray[i] = operatorList.get(i); + String operator = String.join("|", operatorArray); + + // 숫자 연산 + int sum = 0; + String[] result = word.split(operator); + for (String s : result) { + sum += Integer.parseInt(s); + } + + System.out.println(String.format("결과 : %d", sum)); } } From a12bd3571e08fed33946e8adcf71492d05671c96 Mon Sep 17 00:00:00 2001 From: Regyung Date: Tue, 4 Feb 2025 22:18:21 +0900 Subject: [PATCH 4/5] feat(calculator): add custom operator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 있다.커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다. --- src/main/java/calculator/Application.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index 7c30456..a31a524 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -3,18 +3,29 @@ import camp.nextstep.edu.missionutils.Console; import java.util.ArrayList; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Application { public static void main(String[] args) { // 값 입력받기 System.out.println("덧셈할 문자열을 입력해 주세요."); String word = Console.readLine(); - System.out.println(word); - // 구분자 정리 + // 기본 구분자 설정 ArrayList operatorList = new ArrayList(); operatorList.add(","); operatorList.add(":"); + + // 커스텀 구분자 인식 + Pattern p = Pattern.compile("//\\D\\\\n"); + Matcher m = p.matcher(word); + if (m.find()) { + operatorList.add(String.valueOf(word.charAt(m.start()+2))); + } + word = word.substring(m.end()); + + // 구분자 정리 String[] operatorArray = new String[operatorList.size()]; for (int i = 0; i < operatorList.size(); i++) operatorArray[i] = operatorList.get(i); From c2ca88d7624883709ca824cdb9158b2b42f035b4 Mon Sep 17 00:00:00 2001 From: Regyung Date: Wed, 5 Feb 2025 22:26:32 +0900 Subject: [PATCH 5/5] feat(calculator): add IllegalArgumentException MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다. --- src/main/java/calculator/Application.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/calculator/Application.java b/src/main/java/calculator/Application.java index a31a524..1add0ec 100644 --- a/src/main/java/calculator/Application.java +++ b/src/main/java/calculator/Application.java @@ -13,7 +13,7 @@ public static void main(String[] args) { String word = Console.readLine(); // 기본 구분자 설정 - ArrayList operatorList = new ArrayList(); + ArrayList operatorList = new ArrayList<>(); operatorList.add(","); operatorList.add(":"); @@ -22,8 +22,8 @@ public static void main(String[] args) { Matcher m = p.matcher(word); if (m.find()) { operatorList.add(String.valueOf(word.charAt(m.start()+2))); + word = word.substring(m.end()); } - word = word.substring(m.end()); // 구분자 정리 String[] operatorArray = new String[operatorList.size()]; @@ -31,13 +31,16 @@ public static void main(String[] args) { operatorArray[i] = operatorList.get(i); String operator = String.join("|", operatorArray); - // 숫자 연산 + // 숫자 검수 및 연산 int sum = 0; String[] result = word.split(operator); for (String s : result) { - sum += Integer.parseInt(s); + int i = Integer.parseInt(s); + if (i <= 0) + throw new IllegalArgumentException(); + sum += i; } - System.out.println(String.format("결과 : %d", sum)); + System.out.printf("결과 : %d%n", sum); } }