-
Notifications
You must be signed in to change notification settings - Fork 21
[4기][이윤수] 문자열 계산기 과제 #33
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: yslee96
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,30 @@ | ||
package stringCalculator; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public class Calculator { | ||
|
||
private static final Pattern regExp = Pattern.compile("^[0-9]*$"); //operand, operator 구분 용도 | ||
|
||
public int calculate(String[] inputExp){ | ||
|
||
int result = 0; | ||
// 맨처음 숫자 더해짐 | ||
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. 공부의 목적으로 주석이 좋지만, 코드를 이용해 의도를 나타내는게 중요한듯 합니다.
이런식으로 말이죠 ㅎㅎ |
||
Operator currentOperator = Operator.PLUS; | ||
|
||
for(String input : inputExp){ | ||
if(regExp.matcher(input).find()){ | ||
result = currentOperator.operate(result, Integer.parseInt(input)); | ||
continue; | ||
} | ||
// 연산자 매칭 | ||
for(Operator operator : Operator.values()){ | ||
if(operator.getSymbol().equals(input)){ | ||
currentOperator = operator; | ||
} | ||
Comment on lines
+21
to
+24
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. 연산자 매칭을 values를 stream을 돌면서 findFirst 메서드를 이용해 해보면 어떨까요? |
||
} | ||
} | ||
|
||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package stringCalculator; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) { | ||
Calculator calculator = new Calculator(); | ||
String[] input = receiveInput(); | ||
int result = calculator.calculate(input); | ||
System.out.println("결과 : " + result); | ||
} | ||
|
||
public static String[] receiveInput() { | ||
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. 프로그램 실행 시 몇가지 고민해볼 만한 것들이 있는 것 같습니다. 이미 작성되어 있는 코드를 수정하지 않고 추가적인 요구사항에 대해서 추가 및 수정이 가능하도록 |
||
Scanner sc = new Scanner(System.in); | ||
String input = sc.nextLine().replaceAll("\\s+", " "); | ||
return input.split(" "); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||||||||||
package stringCalculator; | ||||||||||||||||
|
||||||||||||||||
import java.util.function.BiFunction; | ||||||||||||||||
|
||||||||||||||||
public enum Operator { | ||||||||||||||||
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. Operator 같은 정의된 속성과 행위를 함께 관리하고 있는 클래스를 구현했다면 build.gradle 파일 내에 아래 의존성 추가하신 후에 코드를 실행해보시는 것을 권장합니다. dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
testImplementation 'org.assertj:assertj-core:3.20.2'
} class OperatorTest {
private Stream<Arguments> operatorProvider() {
return Stream.of(
Arguments.of("+", Operator.PLUS),
Arguments.of("-", Operator.MINUS),
Arguments.of("*", Operator.MULTIPLY),
Arguments.of("/", Operator.DIVIDE)
);
}
@DisplayName("연산자 확인 테스트")
@MethodSource("operatorProvider")
@Test
void testCase1(String actual, Operator expected) {
Operator operator = Operator.findSymbol(actual);
assertThat(operator).isEqualTo(Operator.PLUS);
}
} 연산자를 찾는 검증 테스트를 수행했으니 연산자와 피연산자를 전달하여 연산처리하는 테스트도 작성해보실 수도 있을 것 같네요. 한번 시도해보시죠!! |
||||||||||||||||
PLUS("+", (first, second) -> first + second), | ||||||||||||||||
MINUS("-", (first, second) -> first - second), | ||||||||||||||||
MULTIPLY("*", (first, second) -> first * second), | ||||||||||||||||
DIVIDE("/", (first, second) -> first / second); | ||||||||||||||||
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.
Suggested change
나누기는 예외가 발생할 수 있는 케이스가 존재하는 것 같습니다.! |
||||||||||||||||
|
||||||||||||||||
private String symbol; | ||||||||||||||||
private BiFunction<Integer, Integer, Integer> operation; | ||||||||||||||||
|
||||||||||||||||
Operator(String symbol, BiFunction<Integer, Integer, Integer> operation) { | ||||||||||||||||
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. 함수형 인터페이스를 사용할 때 몇 가지 고민해볼 수 있는 내용이 있습니다.
|
||||||||||||||||
this.symbol = symbol; | ||||||||||||||||
this.operation = operation; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
public String getSymbol(){ return symbol; } | ||||||||||||||||
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.
Suggested change
현재 Operator 에 정의된 내용을 이 Operator를 사용하는 클라이언트 클래스에서 가져다가 로직을 만드는게 아니라 |
||||||||||||||||
|
||||||||||||||||
public int operate(int first, int second) { | ||||||||||||||||
return operation.apply(first, second); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
} |
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.
👍