-
Notifications
You must be signed in to change notification settings - Fork 50
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
[디디] 온보딩 리뷰요청 #8
Merged
+380
−0
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b2d4bc1
String 클래스에 대한 학습 테스트 (요구사항 1,2,3)
moonyoungCHAE 3589f36
String 클래스에 대한 학습 테스트 (요구사항 3) 기능 추가
moonyoungCHAE 0199380
Set Collection 학습 테스트 (요구사항 1, 2, 3)
moonyoungCHAE 6294623
문자열 계산기 기능 구현
moonyoungCHAE 1abbf30
문자열 계산기 테스트 구현
moonyoungCHAE c4c6cd5
문자열 계산기 기능 리팩토링
moonyoungCHAE 11ab12a
문자열 계산기 테스트 코드 수정
moonyoungCHAE 3b70148
문자열 계산기 테스트 기능 추가
moonyoungCHAE 66f9d1d
리뷰 내용 반영
fucct 00be48b
리뷰 내용 반영(추가)
fucct 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 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 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,24 @@ | ||
public class Calculator { | ||
private static final int FIRST = 0; | ||
private static final int TWO = 2; | ||
private double result = 0; | ||
|
||
void calculate(InputValues inputValues) { | ||
Input input = inputValues.getInput(); | ||
int length = input.getValuesLength(); | ||
double nextNumber; | ||
|
||
result = Double.parseDouble(input.getValueByIndex(FIRST)); | ||
Operator nowOperator; | ||
for (int i = 1; i < length; i += TWO) { | ||
nowOperator = | ||
Operator.getOperatorByString(input.getValueByIndex(i)); | ||
nextNumber = Double.parseDouble(input.getValueByIndex(i + 1)); | ||
result = nowOperator.operate(result, nextNumber); | ||
} | ||
} | ||
|
||
double getResult(){ | ||
return result; | ||
} | ||
} |
This file contains 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,67 @@ | ||
import java.util.Scanner; | ||
class Input{ | ||
String value; | ||
String[] values; | ||
|
||
public Input(){ | ||
Scanner scanner = new Scanner(System.in); | ||
System.out.println("문자열 수식을 입력해주세요 : "); | ||
value = scanner.nextLine(); | ||
values = value.split(" "); | ||
} | ||
|
||
public Input(String input){ | ||
this.value = input; | ||
this.values = value.split(" "); | ||
} | ||
|
||
public int getValuesLength(){ | ||
return values.length; | ||
} | ||
|
||
public String getValueByIndex(int index){ | ||
return values[index]; | ||
} | ||
} | ||
public class InputValues { | ||
private static final int ZERO = 0; | ||
|
||
Input input; | ||
|
||
public InputValues(Input input) throws IllegalArgumentException { | ||
this.input = input; | ||
int valueLength = input.getValuesLength(); | ||
|
||
validateEndWithOperator(valueLength); | ||
for (int i = 0; i < valueLength; i++) { | ||
validateValues(i); | ||
} | ||
|
||
} | ||
|
||
public InputValues(String value) { | ||
Input input = new Input(value); | ||
} | ||
|
||
void validateValues(int index) throws IllegalArgumentException{ | ||
if (index % 2 == ZERO) { | ||
try { | ||
Double.parseDouble(input.getValueByIndex(index)); | ||
} catch (NumberFormatException ne) { | ||
throw new IllegalArgumentException("피연산자가 잘못되었습니다."); | ||
} | ||
} else{ | ||
Operator.getOperatorByString(input.getValueByIndex(index)); | ||
} | ||
} | ||
|
||
void validateEndWithOperator(int valueLength) throws IllegalArgumentException{ | ||
if (valueLength % 2 == ZERO) { | ||
throw new IllegalArgumentException("연산자와 숫자가 맞지 않습니다."); | ||
} | ||
} | ||
|
||
Input getInput(){ | ||
return input; | ||
} | ||
} |
This file contains 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,10 @@ | ||
public class Main { | ||
public static void main(String[] args) throws IllegalArgumentException{ | ||
Input input = new Input(); | ||
InputValues inputValues = new InputValues(input); | ||
Calculator calculator = new Calculator(); | ||
|
||
calculator.calculate(inputValues); | ||
OutputView.printResult(calculator); | ||
} | ||
} |
This file contains 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,54 @@ | ||
import java.util.function.BinaryOperator; | ||
|
||
public enum Operator { | ||
PLUS("+") { | ||
double operate(double num1, double num2) { | ||
return operate2((s, t) -> s + t, num1, num2); | ||
} | ||
}, | ||
MINUS("-") { | ||
double operate(double num1, double num2) { | ||
return operate2((s, t) -> s - t, num1, num2); | ||
} | ||
}, | ||
MUL("*") { | ||
double operate(double num1, double num2) { | ||
return operate2((s, t) -> s * t, num1, num2); | ||
} | ||
}, | ||
DIV("/") { | ||
double operate(double num1, double num2) { | ||
double result = num1 / num2; | ||
|
||
if (Double.isInfinite(result) || Double.isNaN(result)) { | ||
throw new IllegalArgumentException("0으로 나눌 수 없습니다."); | ||
} | ||
|
||
return operate2((s, t) -> s / t, num1, num2); | ||
} | ||
}; | ||
|
||
private String stringOperator; | ||
|
||
Operator(String stringOperator) { | ||
this.stringOperator = stringOperator; | ||
} | ||
|
||
abstract double operate(double num1, double num2); | ||
|
||
double operate2(BinaryOperator<Double> binaryOperator, double num1, | ||
double num2) { | ||
return binaryOperator.apply(num1, num2); | ||
} | ||
|
||
static Operator getOperatorByString(String stringOperator) throws IllegalArgumentException { | ||
Operator[] operators = values(); | ||
|
||
for (Operator o : operators) { | ||
if (o.stringOperator.equals(stringOperator)) { | ||
return o; | ||
} | ||
} | ||
throw new IllegalArgumentException("연산자가 잘못되었습니다."); | ||
} | ||
} |
This file contains 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,5 @@ | ||
public class OutputView { | ||
public static void printResult(Calculator calculator){ | ||
System.out.println("결과 : " + calculator.getResult()); | ||
} | ||
} |
Empty file.
This file contains 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,127 @@ | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
public class CalculatorTest { | ||
@ParameterizedTest | ||
@DisplayName("입력이 올바른지 검증") | ||
@CsvSource(value = {"1.9 * 6.2:true", "5 * 6 * 7 * 9:true", "0 / 0:true"} | ||
, delimiter = ':') | ||
void validateValuesTest(String value, boolean expected) { | ||
boolean actualResult = true; | ||
try { | ||
Input input = new Input(value); | ||
InputValues inputValues = new InputValues(input); | ||
int length = input.getValuesLength(); | ||
|
||
for (int i = 0; i < length; i++) { | ||
inputValues.validateValues(i); | ||
} | ||
} catch (NumberFormatException ne) { | ||
actualResult = false; | ||
} catch (IllegalArgumentException ie) { | ||
actualResult = false; | ||
} | ||
Assertions.assertThat(actualResult).isEqualTo(expected); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("올바르지 않은 연산자가 오는 경우 에러메세지 검증") | ||
@ValueSource(strings = {"3 ! 3 * 3", "3 3 3", "5 ) 9"}) | ||
void validateValuesTest2(String value) { | ||
Assertions.assertThatThrownBy(() -> { | ||
Input input = new Input(value); | ||
InputValues inputValues = new InputValues(input); | ||
int length = input.getValuesLength(); | ||
|
||
for (int i = 0; i < length; i++) { | ||
inputValues.validateValues(i); | ||
} | ||
}).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( | ||
"연산자가 잘못되었습니다."); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("올바르지 않은 피연산자가 오는 경우 에러메세지 검증") | ||
@ValueSource(strings = {"! * 3 * 3", "a / 3", "5 * * + 6"}) | ||
void validateValuesTest3(String value) { | ||
Assertions.assertThatThrownBy(() -> { | ||
Input input = new Input(value); | ||
InputValues inputValues = new InputValues(input); | ||
int length = input.getValuesLength(); | ||
|
||
for (int i = 0; i < length; i++) { | ||
inputValues.validateValues(i); | ||
} | ||
}).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( | ||
"피연산자가 잘못되었습니다."); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("연산자로 수식이 끝나는 경우 검증") | ||
@CsvSource(value = {"2 + 3 * 5 / 2:true", "2 +:false", "2 + 3 *:false"}, delimiter = ':') | ||
void validateEndWithOperatorTest(String value, boolean expected) { | ||
boolean actualResult = true; | ||
try { | ||
Input input = new Input(value); | ||
InputValues inputValues = new InputValues(input); | ||
int length = input.getValuesLength(); | ||
|
||
inputValues.validateEndWithOperator(length); | ||
} catch (IllegalArgumentException ie) { | ||
actualResult = false; | ||
} | ||
Assertions.assertThat(actualResult).isEqualTo(expected); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("연산자로 수식이 끝나는 경우 에러메세지 검증") | ||
@ValueSource(strings = {"2 + 3 *", "2 +", "2 + 3 -"}) | ||
void validateEndWithOperatorTest2(String value) { | ||
Assertions.assertThatThrownBy(() -> { | ||
Input input = new Input(value); | ||
InputValues inputValues = new InputValues(input); | ||
int length = input.getValuesLength(); | ||
|
||
inputValues.validateEndWithOperator(length); | ||
}).isInstanceOf(IllegalArgumentException.class).hasMessageMatching( | ||
"연산자와 숫자가 맞지 않습니다."); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("연산자 enum 이 올바른지 검증") | ||
@CsvSource(value = {"3:false", "+:true", "/:true", | ||
"문자입력:false", "):false"}, delimiter = ':') | ||
void getOperatorByStringTest(String value, boolean expected) { | ||
boolean actualResult = true; | ||
try { | ||
Operator.getOperatorByString(value); | ||
} catch (IllegalArgumentException e) { | ||
actualResult = false; | ||
} | ||
Assertions.assertThat(actualResult).isEqualTo(expected); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("연산 결과가 올바른지 검증") | ||
@CsvSource(value = {"+:1:2:3", "-:2:1:1", "*:7:4:28", "*:2:3:6"}, delimiter = ':') | ||
void operateTest(String stringOperator, double num1, double num2, double expected) { | ||
Operator operator = Operator.getOperatorByString(stringOperator); | ||
double actualResult = operator.operate(num1, num2); | ||
|
||
Assertions.assertThat(actualResult).isEqualTo(expected); | ||
} | ||
|
||
@ParameterizedTest | ||
@DisplayName("0으로 나눈 경우 에러메세지 검증") | ||
@CsvSource(value = {"0:0", "1:0", "2:0"}, delimiter = ':') | ||
void divideByZeroTest(double num1, double num2) { | ||
Assertions.assertThatThrownBy(() -> { | ||
Operator operator = Operator.getOperatorByString("/"); | ||
operator.operate(num1, num2); | ||
}).isInstanceOf(IllegalArgumentException.class) | ||
.hasMessageContaining("0으로 나눌 수 없습니다."); | ||
} | ||
} |
Empty file.
This file contains 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,45 @@ | ||
package study; | ||
|
||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class SetTest { | ||
private Set numbers; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
numbers = new HashSet<>(); | ||
numbers.add(1); | ||
numbers.add(1); | ||
numbers.add(2); | ||
numbers.add(3); | ||
} | ||
|
||
// Test Case 구현 | ||
@Test | ||
void size(){ | ||
int size = numbers.size(); | ||
Assertions.assertThat(size).isEqualTo(3); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {6,2,3}) | ||
void contains(int number) { | ||
Assertions.assertThat(numbers.contains(number)).isTrue(); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({"9,false", "2,false", "3,true"}) | ||
void contains2(int input, boolean expected) { | ||
boolean result = numbers.contains(input); | ||
Assertions.assertThat(result).isEqualTo(expected); | ||
} | ||
} |
This file contains 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,44 @@ | ||
package study; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class StringTest { | ||
@Test | ||
void split() { | ||
String value1 = "1,2"; | ||
String value2 = "1"; | ||
|
||
String[] result1 = value1.split(","); | ||
Assertions.assertThat(result1).contains("1"); | ||
Assertions.assertThat(result1).contains("2"); | ||
|
||
String[] result2 = value2.split(","); | ||
Assertions.assertThat(result2).containsExactly("1"); | ||
} | ||
|
||
@Test | ||
void substring() { | ||
String value = "(1,2)"; | ||
String result = value.substring(1, 4); | ||
|
||
Assertions.assertThat(result).isEqualTo("1,2"); | ||
} | ||
|
||
@Test | ||
@DisplayName("String 범위 확인") | ||
void charAt() { | ||
Assertions.assertThatThrownBy(()-> { | ||
String value = "abc"; | ||
char value2 = value.charAt(4); | ||
}).isInstanceOf(IndexOutOfBoundsException.class).hasMessageContaining("index out of range: 4"); | ||
|
||
Assertions.assertThatExceptionOfType(IndexOutOfBoundsException.class) | ||
.isThrownBy(() -> { | ||
String value = "abc"; | ||
char value2 = value.charAt(2); | ||
}).withMessageMatching("String index out of range: \\d+"); | ||
} | ||
} |
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.
훨씬 깔끔하네요 👍