-
Notifications
You must be signed in to change notification settings - Fork 9
[문자열 계산기] 김경수 제출 (1주차) #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,22 @@ | ||
| # java-calculator-precourse | ||
| # java-calculator-precourse | ||
|
|
||
| # 문자열 계산기 | ||
| ## 테스트 클래스 만들기 | ||
|
|
||
| ## 문자열 입력받기 | ||
| - camp.nextstep.edu.missionutils의 Console API를 사용하여 구현해야한다(설마 여기에 ArrayList가 없겠어?) | ||
| - 그 API의 readLine()을 사용한다. | ||
|
|
||
| ## ArrayList numbers : 매개변수로 ArrayList를 받아 숫자 N개를 합하는 메소드 만들기 | ||
| - int sum 변수 선언 | ||
|
|
||
| ## extractNum 메소드 : 구분자를 구별해 숫자를 ArrayList에 담아주는 메소드 만들기 | ||
| - 인스턴스 변수로 기존 구분자(쉼표, :)를 담은 ArrayList separator 만들기 | ||
| - 입력받은 문자열을 toCharArray() 메소드를 이용해서 char 배열로 변환해준다. | ||
| - 구분자나 숫자가 아닌 다른 문자가 있다면 IllegalArgumentException를 발생시킨다. | ||
| - 입력받은 문자열에 '//'가 포함되어있다면 '//'이후의 문자를 separator에 add한다. | ||
| - 그리고 다시 문자열을 입력받는다. | ||
| - 구분자 이전까지 문자들을 StringBuilder sb에 append한다 | ||
| - 구분자가 나오면 numbers에 String을 저장한 후 sb.setLength(0)을 해서 비운 후에 다시 진행한다 | ||
|
|
||
| ### 입력받고 -> extractNum 메소드로 이동 -> numbers 메소드로 이동 -> 출력tor-precourse |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,111 @@ | ||
| package calculator; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| // gradlew.bat 오류가 뜬다 아무것도 모르겠다 그냥 | ||
| public class Application { | ||
|
|
||
| // 사용자의 input을 받는다. | ||
| private String userInput; | ||
|
|
||
| // 모든 숫자의 합을 저장한다. | ||
| private int sum = 0; | ||
|
|
||
| // 숫자, 구분자들을 저장한다. | ||
| private ArrayList<String> numbers; | ||
| private ArrayList<String> separator; | ||
|
|
||
| public Application(){ | ||
| numbers = new ArrayList<String>(); | ||
| separator = new ArrayList<String>(); | ||
| setSeparator(); | ||
| } | ||
| // userInput의 입력값을 받는다 | ||
| public void Input(){ | ||
| userInput = Console.readLine(); | ||
| } | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수들로 깔끔하게 정리해준 거 가독성 좋아보여서 좋은 것 같습니다! |
||
| // 구분자 세터 | ||
| public void setSeparator() { | ||
|
|
||
| // 구분자 설정 | ||
| separator.add(","); | ||
| separator.add(":"); | ||
| } | ||
|
|
||
| // nums에 있는 모든 숫자를 더한다. | ||
| public void sumNumbers(ArrayList<String> nums){ | ||
| for (String number : nums) { | ||
| sum += Integer.parseInt(number); | ||
| } | ||
|
|
||
| System.out.println(sum); | ||
| } | ||
|
|
||
| // numbers 개터 | ||
| public ArrayList<String> getNumbers(){ | ||
| return numbers; | ||
| } | ||
|
|
||
| // 구분자를 구별하고 숫자를 ArrayList에 담는다 | ||
| public void extractNum(){ | ||
| //구분자 초기화 | ||
| setSeparator(); | ||
| // 숫자 받아야지 | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| // 문자열 입력받기 | ||
| System.out.println("덧셈할 문자열을 입력해 주세요."); | ||
| Input(); | ||
| for (int i = 0; i < userInput.length(); i++){ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Character을 순회하면 일의자리를 제외한 수를 표현할 수 없지 않나요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 그렇게 생각해서 아래 if문 부분이 해결된다면 숫자 추출을 문자열 내부 함수로 처리할 수 있어 보입니다. |
||
| if ((userInput.charAt(i) == '/' && userInput.charAt(i+1) == '/')) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for문의 수행 횟수가 문자열의 길이와 일치하기 때문에 'i+1'과 같은 접근 표현을 사용 시 인덱스가 오버될 수도 있어보입니다.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아 여기 인덱스 신경을 못썼네요. 이런 곳에서 for문보다는 stream을 사용하는게 더 괜찮을까요? |
||
| // "//"를 인식하면 그 다음 문자를 구분자로 넣는다. | ||
| // 개행문자를 만나면 문자열이 끝나기 때문에 | ||
| // 개행문자를 문자열로 넣는 과정을 추가하기보단 | ||
| // 그냥 다시 문자열을 받아도 상관 없겠다는 생각을 했어요 | ||
| // 근데 구분자를 추가하고 끝나는 경우도 있다는 생각에 | ||
| // if 문을 하나 더 넣었네요 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드 외의 이야기지만 긴 문장의 주석을 작성한다면 각 줄마다 '//'로 주석을 다는 것보다 '/* */'과 같은 블록주석 표현을 쓰는 것도 좋다고 생각합니다.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 말씀 감사합니다 ㅎㅎ |
||
| if (i+2 < userInput.length()) { | ||
| separator.add(String.valueOf(userInput.charAt(i + 2))); | ||
| if(userInput.length() > i + 4) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여기서는 커스텀 구분자 이후에 나오는 input을 받기 위해서 i+4로 한 건가요?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. //"커스텀구분자"\n로 커스텀 구분자가 추가되기만 하고 더이상 입력이 없을 경우에 Input()을 받게 된다면 오류가 발생하기 때문에 방지하기 위해 i+4조건을 붙여주었습니다 |
||
| Input(); | ||
| } | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("잘못된 문자가 있는 것 같은데요?"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외가 생길 조건을 따로 지정해도 좋을 것 같아요! |
||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 윗부분에서 구분자 추출하는 부분은 정규표현식으로 처리하고 밑 부분은 인덱스 접근이 아닌 원소 순회로 표현하면 가독성이 더 높을 것 같아요
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정규표현식 기억하겠습니다..! Matcher와 Pattern 말씀하시는거죠? |
||
| else if (separator.contains(String.valueOf(userInput.charAt(i)))) { | ||
| // sb에 모여있는 숫자들이 구분자를 만나면 | ||
| // numbers로 들어가고 sb는 초기화됨 | ||
| String result = sb.toString(); | ||
| numbers.add(result); | ||
| sb.setLength(0); | ||
| } | ||
| else if (Character.isDigit(userInput.charAt(i)) ) { | ||
| if (Integer.parseInt(String.valueOf(userInput.charAt(i))) < 0) { | ||
| // 문자열에 숫자는 sb에 넣어준다. | ||
| sb.append(userInput.charAt(i)); | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("잘못된 문자가 있는 것 같은데요?"); | ||
| } | ||
| } | ||
| else { | ||
| throw new IllegalArgumentException("잘못된 문자가 있는 것 같은데요?"); | ||
| } | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 함수 깊이가 너무 깊은 것 같아요! 더 분리하면 좋을 것 같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정규표현식 관련 리뷰와 합쳐서 동의하는게 정규표현식으로 커스텀 구분자를 추출한다면 코드가 확실히 짧아질 수 있을 것 같아요! |
||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| //생성자 호출 | ||
| Application a = new Application(); | ||
|
|
||
| a.extractNum(); | ||
| a.sumNumbers(a.getNumbers()); | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전체적으로 역할을 분리하지 않아서 코드 흐름이 흔들리는게 있는 것 같아요! 기능을 쪼개고 기능마다 메서드를 만들어 구현한다면 좀 더 쉽게 표현될 수 있을 것 같아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 기능마다 나눠서 코드를 짜면 좋은 코드가 될 수 있을 것 같아요! 저랑 비슷하지만 다르게 접근하셔서 대단한 것 같습니다! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Application()에 안에 numbers와 separator을 선언한 이유가 따로 있나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MVC를 아직 공부하지 못해 모든 기능을 Application() 안에 다 넣어서 그렇습니다 ㅠㅜ