This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
2회차 과제 PR (차준혁) #20
Open
JunHyeok-Cha
wants to merge
4
commits into
SoongSilComputingClub:JunHyeok-Cha
Choose a base branch
from
JunHyeok-Cha:JunHyeok
base: JunHyeok-Cha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
2회차 과제 PR (차준혁) #20
Changes from all commits
Commits
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 @@ | ||
public class Calculator { | ||
|
||
private final String[] operationType = {"+", "-", "*", "/"}; | ||
|
||
public int calculate(String formula){ | ||
String[] input = formula.split(" "); | ||
int result = Integer.parseInt(input[0]); | ||
String operator = null; | ||
|
||
for(int i = 1; i < input.length; i++){ | ||
|
||
if(isPermittedOperator(input[i])){ | ||
operator = input[i]; | ||
}else{ | ||
result = partCalculate(operator, result, input[i]); | ||
} | ||
|
||
} | ||
|
||
return result; | ||
} | ||
|
||
public int partCalculate(String operator, int result, String input){ | ||
|
||
if(input == null || input.equals("")) throw new IllegalArgumentException(); | ||
|
||
switch (operator){ | ||
case "+": | ||
return plus(result, input); | ||
case "-": | ||
return minus(result, input); | ||
case "*": | ||
return multiply(result, input); | ||
case "/": | ||
return division(result, input); | ||
default: | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
} | ||
|
||
private boolean isPermittedOperator(String input){ | ||
|
||
for(String operator : operationType){ | ||
if(input.equals(operator)) return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public int plus(int result, String input){ | ||
return result + Integer.parseInt(input); | ||
} | ||
|
||
public int minus(int result, String input){ | ||
return result - Integer.parseInt(input); | ||
} | ||
|
||
public int multiply(int result, String input){ | ||
return result * Integer.parseInt(input); | ||
} | ||
|
||
public int division(int result, String input){ | ||
return result / Integer.parseInt(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,54 @@ | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
|
||
class CalculatorTest { | ||
|
||
private final Calculator strcalculator = new Calculator(); | ||
|
||
@Test | ||
void plusTest() { | ||
assertThat(strcalculator.plus(2, "3")).isEqualTo(5); | ||
} | ||
|
||
@Test | ||
void minusTest() { | ||
assertThat(strcalculator.minus(10, "3")).isEqualTo(7); | ||
} | ||
|
||
@Test | ||
void multiplyTest() { | ||
assertThat(strcalculator.multiply(2, "5")).isEqualTo(10); | ||
} | ||
|
||
@Test | ||
void divisionTest() { | ||
assertThat(strcalculator.division(20, "2")).isEqualTo(10); | ||
} | ||
|
||
@Test | ||
void calculateTest(){ | ||
assertThat(strcalculator.calculate("2 + 3 * 4 / 2")).isEqualTo(10); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = {"#", "$"}) | ||
void partCalculatePermittedOperatorTest(String input){ | ||
assertThatIllegalArgumentException().isThrownBy(() -> { | ||
assertThat(strcalculator.partCalculate(input, 5, "5")).isEqualTo(10); | ||
}); | ||
} | ||
|
||
@ParameterizedTest | ||
@NullAndEmptySource | ||
void partCalculateNullInputTest(String input){ | ||
assertThatIllegalArgumentException().isThrownBy(() -> { | ||
assertThat(strcalculator.partCalculate("+", 5, input)).isEqualTo(10); | ||
}); | ||
} | ||
} |
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.
Enum 과 BiFunction을 활용하는 방식을 적용해 볼 수 있습니다.
Operator로직을 별도의 클래스로 분리해보세요.
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.
return minus(result, input); 이 코드는 테스트 코드에서 수행되지 않았습니다.
minus 의 경우도 테스트 되도록 테스트 코드를 보완해주세요.