Skip to content

[김민지] 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

Open
wants to merge 3 commits into
base: mmm307955
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/main/java/Calculator.java

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1단계 요구사항인 초간단 계산기에 3단계 요구사항인 문자열 계산기 기능을 추가해주셨네요!
미션의 요구사항에는 별도의 파일로 만들라고 되어 있지 않았기에 이렇게 구현해도 상관은 없을 것 같아요!
다만, 사칙연산을 하는 계산기의 기능은 계산기가 하는 행동(메서드)이라고 쉽게 생각할 수 있지만, 문자열 계산기 같은 특수한 목적을 가진 행동은 과연 Calculator가 가질 수 있는 행동이 맞을까요?

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) {

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

의미를 가지는 변수명 👍👍

}

41 changes: 41 additions & 0 deletions src/test/java/CalculatorTest.java
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3단계 미션의 요구사항 중 하나인 문자열 계산기 테스트 코드네요!
다만 이 테스트 코드만 보았을 때는 잘 작성된 테스트 코드와는 거리가 있어 보여요. 😂
해당 규칙을 지켜서 수정해주세요!

  1. assertEquals 과 같은 검증부를 하나만 사용한다.
  2. 테스트가 수행하는 목적을 이름으로 나타낸다. (@DisplayName)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

또한 요구사항 중 하나인 커스텀 구분자에 대한 테스트가 누락되어 있는것 같아요!

}