-
Notifications
You must be signed in to change notification settings - Fork 6
[정재]String-adding-calculation #7
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: jj
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package calculator; | ||
|
|
||
| public class Calclator { | ||
| Spliter spliter = new Spliter(); | ||
| public int sum(String[] splitedValue) { | ||
| int result = 0; | ||
| for (int i = 0; i < splitedValue.length; i++) { | ||
| result += spliter.parseToInt(splitedValue[i]); | ||
dongry1002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,29 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import calculator.Calclator; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import calculator.Spliter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Scanner; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class CalculatorApplication { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int result = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("숫자들을 입력하세요 : "); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Scanner scanner = new Scanner(System.in); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String inputValue = scanner.nextLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (inputValue.isEmpty()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(result); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Spliter spliter = new Spliter(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String[] splitedValue = spliter.splitValue(inputValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(splitedValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Calclator calclator = new Calclator(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| result = calclator.sum(splitedValue); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println(result); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+9
to
+25
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. result라는 변수가 꼭 필요한가 싶습니다.
Suggested change
Comment on lines
+8
to
+25
Member
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. main 메서드길이를 최대 10라인이 되도록 수정해주세요! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||
| package calculator; | ||||||
| import java.util.regex.Matcher; | ||||||
| import java.util.regex.Pattern; | ||||||
|
|
||||||
| public class Spliter { | ||||||
|
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. https://88240.tistory.com/440 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. 가변필드를 가지고 있지 않고 Util 성을 띄고있는 것 같아 static 클래스로 만들어줘도 좋을 것 같습니다 ! 그렇게 하면 Main과 Calculator 에서 불필요하게 두번씩 생성할 필요도 없을 것 같습니다!
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. 네 static으로 바꿨습니다. 감사합니다~ |
||||||
|
|
||||||
| public static final String DELIMITER1 = "[,|:]"; | ||||||
| public static final String CUSTOMER_DELIMITER = "//(.)\n(.*)"; | ||||||
|
Comment on lines
+7
to
+8
Member
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 String[] splitValue(String value) { | ||||||
| Matcher m = Pattern.compile(CUSTOMER_DELIMITER).matcher(value); | ||||||
|
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. 여기서 Pattern.complie 은 항상 같은 Pattern 을 가리키잖아요?
Suggested change
아, 그리고 이 클래스의 멤버변수는 private 이어도 될 것 같습니다! Splitter 클래스만 알고 있어도 되는 변수로 보여요! 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.
Member
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.
|
||||||
| System.out.println(m.find()); | ||||||
| if(m.find()){ | ||||||
| String customDelimiter = m.group(1); | ||||||
| String tokens =m.group(2); | ||||||
| String[] splitedValue = tokens.split(CUSTOMER_DELIMITER); | ||||||
| System.out.println(splitedValue[0]+"custom"); | ||||||
|
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. System.out 은 테스트용인건가요!?
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. 넵 지웠습니다. 감사해요 |
||||||
| System.out.println(splitedValue[1]+"custom"); | ||||||
| System.out.println(splitedValue[2]+"custom"); | ||||||
| } | ||||||
|
|
||||||
| String[] splitedValue = value.split(DELIMITER1); | ||||||
| System.out.println(splitedValue[0]); | ||||||
| System.out.println(splitedValue[1]); | ||||||
| System.out.println(splitedValue[2]); | ||||||
| return splitedValue; | ||||||
|
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. 이렇게 인덱싱을 했을때 outofbound 에러가 나지는 않을지 예외케이스를 생각해보세요 |
||||||
| } | ||||||
|
|
||||||
| public int parseToInt(String splitedValue) { | ||||||
| if (isNumberic(splitedValue)) { | ||||||
|
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. isNumberic과 아래 코드에서 parseInt를 두번 체크하는데 다른 방식을 고려해보세요 |
||||||
| int number = Integer.parseInt(splitedValue); | ||||||
| checkPositve(number); | ||||||
| return number; | ||||||
| } | ||||||
| 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. isNumeric 의 catch 문에서 throw 하는 것은 어떨까요?? return false를 굳이 하지 않아도 될 것 같아요 ! isNumeric ParseInt 중복 부분을 해결하면 같이 해결될 것 같습니다 ~ |
||||||
| } | ||||||
|
|
||||||
| public void checkPositve(int parseInt) { | ||||||
| if (parseInt > 0) { | ||||||
| return; | ||||||
|
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. 여기있는 return 부분이 불필요해 보입니다. if 안의 조건문을 변경해보세요
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. 네 변경하였습니다. |
||||||
| } | ||||||
| throw new IllegalArgumentException("음수가 아닌 양수만 입력하세요."); | ||||||
| } | ||||||
|
|
||||||
| public boolean isNumberic(String splitedValue) { | ||||||
| try { | ||||||
| Integer.parseInt(splitedValue); | ||||||
| return true; | ||||||
| } catch (IllegalArgumentException e) { | ||||||
| return false; | ||||||
| } | ||||||
|
Comment on lines
+46
to
+51
Member
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.
한번 검색해서 참고해 보시면 좋을것 같아요! |
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package calculator; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
|
|
||
|
|
||
|
|
||
| class CalculatorTest { | ||
| Spliter spliter = new Spliter(); | ||
| Calclator calclator = new Calclator(); | ||
| @Test | ||
| @DisplayName("올바른 계산") | ||
| void sum() { | ||
| String[] splitedValue = {"1","2","5"}; | ||
| assertThat(calclator.sum(splitedValue)).isEqualTo(8); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package calculator; | ||
|
|
||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; | ||
|
|
||
| class SpliterTest { | ||
| Spliter spliter = new Spliter(); | ||
| @Test | ||
| void splitValue() { | ||
| // String inputValue = new String("1,2:3"); | ||
| // assertThat(spliter.splitValue(inputValue)).isEqualTo(["1", "2", "3"]); | ||
| } | ||
|
Comment on lines
+11
to
+15
Member
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. 불필요한 테스트는 제거해주세요 ㅎㅎ
Member
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. 추가적으로 기본식이 입력 되었을 때와 커스텀 구분자가 들어간 식에 대한 계산 테스트가 존재하지 않네요! |
||
|
|
||
| @Test | ||
| @DisplayName("숫자가 아닐 때 테스트") | ||
| void parseToInt() { | ||
| String splitedValue = " "; | ||
| assertThatThrownBy(() -> spliter.parseToInt(splitedValue)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("양수로 된 숫자만 입력하세요."); | ||
| } | ||
| @Test | ||
| @DisplayName("음수를 입력했을 때 테스트") | ||
| void checkPositve() { | ||
| int negative = -1; | ||
| assertThatThrownBy(() -> spliter.checkPositve(negative)) | ||
| .isInstanceOf(IllegalArgumentException.class) | ||
| .hasMessage("음수가 아닌 양수만 입력하세요."); | ||
| } | ||
|
|
||
| } | ||
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.
여기서 선언한 Spliter가 새로 바뀌거나 한다면 위처럼 가변 참조 필드로 두어도 되겠지만, 그것이 아니기 때문에 private final 걸어주면 좋을것같아요
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.
혹시 private final로 걸어야하는부분이 spliter 인가요?ㅠ