-
Notifications
You must be signed in to change notification settings - Fork 83
[김민지] 3단계 - 문자열 계산기 구현 #78
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: mmm307955
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,50 @@ | ||
public class Calculator { | ||
int add(int a, int b) { | ||
return a + b; | ||
} | ||
|
||
int subtract(int a, int b){ | ||
return a - b; | ||
} | ||
int multiply(int a, int b){ | ||
return a * b; | ||
} | ||
int divide(int a, int b){ | ||
return a / b; | ||
} | ||
|
||
public int calculateString(String input) { | ||
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. input으로 "1"과 같은 길이가 하나인 문자열을 넣으면 결과가 어떻게 나올까요? |
||
if (input == null | input.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
String separator = "[,:]";//정규표현식. split()메소드에서 사용된다. '[]안의 문자중 하나와 일치'라는 의미를 갖는다. | ||
String numbersStr = input; | ||
|
||
if (input.substring(0, 2).equals("//")) { | ||
int slashStartIndex = input.indexOf("//"); | ||
int newLineStartIndex = input.indexOf("/n"); | ||
|
||
if (newLineStartIndex != -1) { | ||
String customSeparator = input.substring(slashStartIndex + 2, newLineStartIndex); | ||
separator = "[,:" + customSeparator + "]"; | ||
numbersStr = input.substring(newLineStartIndex + 1);//개행문자는 하나의 문자로 취급. | ||
} | ||
} | ||
|
||
String[] numbers = numbersStr.split(separator); | ||
|
||
int sum = 0; | ||
for (String number : numbers) { | ||
if (!number.isEmpty()) { | ||
int value = Integer.parseInt(number);//String 배열에 있는 numbers를 하나씩 int형으로 변환 | ||
if (value < 0) { | ||
throw new RuntimeException("음수는 사용할 수 없습니다 : " + value); | ||
} | ||
sum += value; | ||
} | ||
} | ||
return sum; | ||
} | ||
Comment on lines
+21
to
+48
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,41 @@ | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class CalculatorTest { | ||
Calculator c = new Calculator(); | ||
|
||
@Test | ||
void testAdd(){ | ||
int result = c.add(2,3); | ||
assertEquals(5,result); | ||
} | ||
@Test | ||
void testSubtract(){ | ||
int result = c.subtract(2,3); | ||
assertEquals(-1,result); | ||
} | ||
|
||
@Test | ||
void testMultiply(){ | ||
int result = c.multiply(2,3); | ||
assertEquals(6,result); | ||
} | ||
@Test | ||
void testDivide(){ | ||
int result = c.divide(6,3); | ||
assertEquals(2,result); | ||
} | ||
|
||
@Test | ||
void testCalculateString(){ | ||
int result = c.calculateString("1,2:3"); | ||
assertEquals(6,result); | ||
result = c.calculateString(""); | ||
assertEquals(0,result); | ||
result = c.calculateString("1,2"); | ||
assertEquals(3,result); | ||
result = c.calculateString("1,2,3"); | ||
assertEquals(6,result); | ||
} | ||
Comment on lines
+30
to
+40
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. 3단계 미션의 요구사항 중 하나인 문자열 계산기 테스트 코드네요!
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. 또한 요구사항 중 하나인 커스텀 구분자에 대한 테스트가 누락되어 있는것 같아요! |
||
} |
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.
1단계 요구사항인 초간단 계산기에 3단계 요구사항인 문자열 계산기 기능을 추가해주셨네요!
미션의 요구사항에는 별도의 파일로 만들라고 되어 있지 않았기에 이렇게 구현해도 상관은 없을 것 같아요!
다만, 사칙연산을 하는 계산기의 기능은 계산기가 하는 행동(메서드)이라고 쉽게 생각할 수 있지만, 문자열 계산기 같은 특수한 목적을 가진 행동은 과연
Calculator
가 가질 수 있는 행동이 맞을까요?