Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# java-calculator-precourse
# java-calculator-precourse

## 구현할 기능 목록
1. 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다.
- 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6
#
2. 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다.
- 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다.
#
3. 사용자가 잘못된 값을 입력할 경우 `IllegalArgumentException`을 발생시킨 후 애플리케이션은 종료되어야 한다.
41 changes: 40 additions & 1 deletion src/main/java/calculator/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package calculator;

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) {
// TODO: 프로그램 구현
// 값 입력받기
System.out.println("덧셈할 문자열을 입력해 주세요.");
String word = Console.readLine();

// 기본 구분자 설정
ArrayList<String> operatorList = new ArrayList<>();
operatorList.add(",");
operatorList.add(":");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분도 List.of로 한 번에 추가할 수 있을 것 같아요!


// 커스텀 구분자 인식
Pattern p = Pattern.compile("//\\D\\\\n");
Matcher m = p.matcher(word);
if (m.find()) {
operatorList.add(String.valueOf(word.charAt(m.start()+2)));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m.start()에 +2를 하는 이유가 뭔가요??

word = word.substring(m.end());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정규표현식으로 처리한 김에 좀 더 확장해서 정규표현식으로 추출하도 좋을 것 같아요


// 구분자 정리
String[] operatorArray = new String[operatorList.size()];
for (int i = 0; i < operatorList.size(); i++)
operatorArray[i] = operatorList.get(i);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인덱스로 접근하기보다는 원소로 접근하는게 좋을 것 같아요! 1별다른 이유가 없다면 operatorArray도 배열이 아니라 컬렉션, 가령 ArrayList 등으로 표현하면 확장하기 쉬울 것 같아요

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동의합니다!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 배열로 하다가 중간에 ArrayList로 변경했는데 ArrayList가 더 좋아보여요!

String operator = String.join("|", operatorArray);

// 숫자 검수 및 연산

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 처음이라 미숙해서 함수별로 분리 안하였는데 분리하는게 좋을 것 같습니다

int sum = 0;
String[] result = word.split(operator);
for (String s : result) {
int i = Integer.parseInt(s);
if (i <= 0)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0도 예외로 생각하신건가요?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 입력조건에 양수라고 되어있어 따로 0과 음수에 대한 예외처리를 하지않았는데 수정할 때 참고해볼게요..! 예외처리를 신경쓰신 것 같아요!

throw new IllegalArgumentException();
sum += i;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분을 stream으로 표현하면 가독성이 더 높고 효율적일 것 같아요


System.out.printf("결과 : %d%n", sum);
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 파일도 주석을 달지 말고 그 부분마다 함수를 분리하는게 더 좋을 것 같아요