-
Notifications
You must be signed in to change notification settings - Fork 85
[박종범] 1차시 미션 제출입니다. #104
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
Merged
Merged
[박종범] 1차시 미션 제출입니다. #104
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
934f58f
feat: Calculator 클래스 계산 구현
asa9874 091b3bf
feat: CalculatorJunit5Test 테스트 생성
asa9874 92e816f
feat: StringCalculator 클래스 계산 구현
asa9874 800bd3e
feat: StringCalculatorJunit5Test 테스트 생성
asa9874 3d8b897
build: assertj 의존성 추가
asa9874 5e55bf1
feat: CalculatorAssertJTest 테스트 생성
asa9874 b064456
feat: StringCalculatorAssertJTest 테스트 생성
asa9874 fa00843
refact: 미사용 어노테이션 제거
asa9874 79c5831
refact: StringUtils 의존성 추가 및 isEmptyOrNull 메서드 대체
asa9874 4b1116b
refact: delimiter 클래스 상수 처리
asa9874 2e0638f
refact: 줄바꿈 컨벤션 리팩토링
asa9874 c4083f0
refact: validate 로직 분리
asa9874 11c767d
refact: StringCalculator extractCustomDelimiter로직 분리
asa9874 f1add0a
refact: CalculatorAssertJTest 변수 fianl 제거
asa9874 afad03c
refact: 매직넘버 상수화
asa9874 4e7e9ff
refact: Overflow 방지를 위해 Math 클래스 사용
asa9874 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
public class Calculator { | ||
|
||
public int add(int a, int b) { | ||
return Math.addExact(a, b); | ||
} | ||
|
||
public int subtract(int a, int b) { | ||
return Math.subtractExact(a, b); | ||
} | ||
|
||
public int multiply(int a, int b) { | ||
return Math.multiplyExact(a, b); | ||
} | ||
|
||
public int divide(int a, int b) { | ||
if (b == 0) { | ||
throw new IllegalArgumentException("0 나누기 오류"); | ||
} | ||
|
||
return Math.floorDiv(a, b); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import java.util.Arrays; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class StringCalculator { | ||
|
||
private static final String DEFAULT_DELIMITER = ",|:"; | ||
private static final String START_CUSTOM_DELIMITER = "//"; | ||
private static final String END_CUSTOM_DELIMITER = "\n"; | ||
|
||
private String customDelimiter = ""; | ||
|
||
public int add(String input) { | ||
String delimiter = DEFAULT_DELIMITER; | ||
|
||
if (StringUtils.isEmpty(input)) { | ||
return 0; | ||
} | ||
|
||
if (hasCustomDelimiter(input)) { | ||
input = extractCustomDelimiter(input); | ||
delimiter += "|" + this.customDelimiter; | ||
} | ||
|
||
return getSum(input.split(delimiter)); | ||
} | ||
|
||
private int getSum(String[] numbers) { | ||
return Arrays.stream(numbers).mapToInt(num -> { | ||
validateIsNumber(num); | ||
int number = Integer.parseInt(num); | ||
validateIsPositive(number); | ||
return number; | ||
}).reduce(0, Math::addExact); | ||
} | ||
|
||
private String extractCustomDelimiter(String input) { | ||
int endCustomDelimiterIndex = input.indexOf(END_CUSTOM_DELIMITER); | ||
this.customDelimiter = input.substring(START_CUSTOM_DELIMITER.length(), endCustomDelimiterIndex); | ||
return input.substring(endCustomDelimiterIndex + 1); | ||
} | ||
|
||
private void validateIsNumber(String number) { | ||
if (!isNumber(number)) { | ||
throw new RuntimeException("숫자가 아닌 값이 포함되어 있습니다"); | ||
} | ||
} | ||
|
||
private void validateIsPositive(Integer number) { | ||
if (number < 0) { | ||
throw new RuntimeException("음수는 허용되지 않습니다"); | ||
} | ||
} | ||
|
||
private boolean hasCustomDelimiter(String input) { | ||
return input.startsWith(START_CUSTOM_DELIMITER); | ||
} | ||
|
||
private boolean isNumber(String number) { | ||
return number.matches("-?\\d+"); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CalculatorAssertJTest { | ||
|
||
private final Calculator calculator = new Calculator(); | ||
|
||
@Nested | ||
@DisplayName("덧셈 테스트") | ||
public class AddTest { | ||
|
||
@Test | ||
public void 정수_더하기() { | ||
// Given | ||
int a = 2; | ||
int b = 3; | ||
|
||
// When | ||
int actual = calculator.add(a, b); | ||
|
||
// Then | ||
int expected = 5; | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("뺄셈 테스트") | ||
public class SubtractTest { | ||
|
||
@Test | ||
public void 정수_빼기() { | ||
// Given | ||
int a = 3; | ||
int b = 2; | ||
|
||
// When | ||
int actual = calculator.subtract(a, b); | ||
|
||
// Then | ||
int expected = 1; | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("곱셈 테스트") | ||
public class MultiplyTest { | ||
|
||
@Test | ||
public void 정수_곱하기() { | ||
// Given | ||
int a = 2; | ||
int b = 3; | ||
|
||
// When | ||
int actual = calculator.multiply(a, b); | ||
|
||
// Then | ||
int expected = 6; | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("나눗셈 테스트") | ||
public class DivideTest { | ||
|
||
@Test | ||
public void 정수_나누기() { | ||
// Given | ||
int a = 6; | ||
int b = 3; | ||
|
||
// When | ||
int actual = calculator.divide(a, b); | ||
|
||
// Then | ||
int expected = 2; | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
public void 예외_0_나누기() { | ||
// Given | ||
int a = 1; | ||
int b = 0; | ||
|
||
// When & Then | ||
assertThatThrownBy(() -> calculator.divide(a, b)) | ||
.isInstanceOf(IllegalArgumentException.class) | ||
.hasMessage("0 나누기 오류"); | ||
|
||
assertThatCode(() -> calculator.divide(b, a)) | ||
.doesNotThrowAnyException(); | ||
} | ||
|
||
@Test | ||
public void 나누기_결과가_정수가_아닐때() { | ||
// Given | ||
int a = 5; | ||
int b = 2; | ||
|
||
// When | ||
int actual = calculator.divide(a, b); | ||
|
||
// Then | ||
int expected = 2; // 결과값은 정수로 나옴 | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CalculatorJunit5Test { | ||
|
||
private final Calculator calculator = new Calculator(); | ||
|
||
@Nested | ||
@DisplayName("덧셈 테스트") | ||
public class AddTest { | ||
|
||
@Test | ||
public void 정수_더하기() { | ||
assertEquals(5, calculator.add(2, 3)); | ||
assertEquals(0, calculator.add(-1, 1)); | ||
assertEquals(-5, calculator.add(-2, -3)); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("뺄셈 테스트") | ||
public class SubtractTest { | ||
|
||
@Test | ||
public void 정수_빼기() { | ||
assertEquals(1, calculator.subtract(3, 2)); | ||
assertEquals(-2, calculator.subtract(-1, 1)); | ||
assertEquals(1, calculator.subtract(-2, -3)); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("곱셈 테스트") | ||
public class MultiplyTest { | ||
|
||
@Test | ||
public void 정수_곱하기() { | ||
assertEquals(6, calculator.multiply(2, 3)); | ||
assertEquals(-1, calculator.multiply(-1, 1)); | ||
assertEquals(6, calculator.multiply(-2, -3)); | ||
} | ||
} | ||
|
||
@Nested | ||
@DisplayName("나눗셈 테스트") | ||
public class DivideTest { | ||
|
||
@Test | ||
public void 정수_나누기() { | ||
assertEquals(2, calculator.divide(6, 3)); | ||
assertEquals(-1, calculator.divide(-3, 3)); | ||
assertEquals(1, calculator.divide(-3, -3)); | ||
} | ||
|
||
@Test | ||
public void 예외_0_나누기() { | ||
assertThrows(IllegalArgumentException.class, () -> calculator.divide(1, 0)); | ||
assertDoesNotThrow(() -> calculator.divide(0, 1)); | ||
} | ||
|
||
@Test | ||
public void 나누기_결과가_정수가_아닐때() { | ||
assertEquals(2, calculator.divide(5, 2)); | ||
assertEquals(0, calculator.divide(1, 2)); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class StringCalculatorAssertJTest { | ||
|
||
private final StringCalculator stringCalculator = new StringCalculator(); | ||
|
||
@Nested | ||
@DisplayName("덧셈 테스트") | ||
class AddTest { | ||
|
||
@Test | ||
void 빈_문자열() { | ||
// Given | ||
String input = ""; | ||
|
||
// When | ||
int result = stringCalculator.add(input); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(0); | ||
} | ||
|
||
@Test | ||
void 하나의_숫자() { | ||
// Given | ||
String input = "1"; | ||
|
||
// When | ||
int result = stringCalculator.add(input); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(1); | ||
} | ||
|
||
@Test | ||
void 두_숫자_더하기() { | ||
// Given | ||
String input = "1,2"; | ||
|
||
// When | ||
int result = stringCalculator.add(input); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(3); | ||
} | ||
|
||
@Test | ||
void 여러_숫자_더하기() { | ||
// Given | ||
String input = "1,2,3"; | ||
|
||
// When | ||
int result = stringCalculator.add(input); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(6); | ||
} | ||
|
||
@Test | ||
void 커스텀_구분자() { | ||
// Given | ||
String input = "//;\n4;5,1:2"; | ||
|
||
// When | ||
int result = stringCalculator.add(input); | ||
|
||
// Then | ||
assertThat(result).isEqualTo(12); | ||
} | ||
|
||
@Test | ||
void 음수_예외() { | ||
// Given | ||
String input = "-1"; | ||
|
||
// When & Then | ||
assertThatThrownBy(() -> stringCalculator.add(input)) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasMessageContaining("음수는 허용되지 않습니다"); | ||
|
||
} | ||
|
||
@Test | ||
void 숫자가_아닌_예외() { | ||
// Given | ||
String input = "1,a"; | ||
|
||
// When & Then | ||
assertThatThrownBy(() -> stringCalculator.add(input)) | ||
.isInstanceOf(RuntimeException.class) | ||
.hasMessageContaining("숫자가 아닌 값이 포함되어 있습니다"); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
해당 부분은 클래스 시작할때 들여쓰기가 있는데, 다른 부분에는 없네요.
추후 협업을 하는데 코드 컨벤션은 매우 중요한 요소이기에 기준을 잘 정하셨으면 좋겠습니다
(참고로 코인은 클래스 시작은 들여쓰기O, 메서드 시작은 들여쓰기X입니다)
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.
감사합니다! 해당 부분은 코인 방식으로 리팩토링 해보겠습니다.
인텔리제이에 해당 bcsd 컨벤션 포매팅 파일 을 적용해서 사용하였는데, 해당 포매팅파일 말고도 별도로 더 강하게 컨벤션을 잡기 위해 사용하고 계신 툴이 혹시 있으신지 궁금합니다.