Skip to content

Commit 7807734

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent 3aa0cd2 commit 7807734

File tree

6 files changed

+220
-0
lines changed

6 files changed

+220
-0
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ repositories {
1212
dependencies {
1313
testImplementation platform('org.junit:junit-bom:5.9.1')
1414
testImplementation('org.junit.jupiter:junit-jupiter')
15+
testImplementation platform('org.assertj:assertj-bom:3.25.1')
16+
testImplementation('org.assertj:assertj-core')
1517
}
1618

1719
test {

src/main/java/SimpleCalculator.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class SimpleCalculator {
2+
public int add(int firstNum, int secondNum){
3+
return firstNum + secondNum;
4+
}
5+
public int multiply(int firstNum, int secondNum){
6+
return firstNum * secondNum;
7+
}
8+
public int divide(int firstNum, int secondNum){
9+
return firstNum / secondNum;
10+
}
11+
public int minus(int firstNum, int secondNum){
12+
return firstNum - secondNum;
13+
}
14+
15+
}

src/main/java/StringCalculator.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
input을 받고 빈문자열인지 null인지 체크
3+
구분자 체크 -> 구분자 예외 체크
4+
구분자 대로 문자열 계산기 구현
5+
*/
6+
7+
import java.util.regex.Pattern;
8+
9+
public class StringCalculator {
10+
11+
//입력 문자열이 null인지 확인
12+
public String checkInput(String input){
13+
if(input == null) throw new RuntimeException("null은 허용되지 않습니다.");
14+
return input;
15+
}
16+
17+
//커스텀 구분자가 있는지 확인
18+
public int checkDelimiter(String checkedInput) {
19+
if(checkedInput.isEmpty()) return -2; //문자열이 빈 경우
20+
21+
22+
if (checkedInput.startsWith("//")) { //커스텀 구분자를 설정하는가?
23+
int idx = checkedInput.indexOf("\n"); //제대로된 형식인가?
24+
if (idx == -1) throw new RuntimeException("잘못된 커스텀 구분자 형식입니다.");
25+
return idx; //구분자 위치 전달
26+
}
27+
28+
return -1; //기본 구분자대로 문자열을 나눔
29+
}
30+
31+
private String[] splitInput(String checkedInput, int idx){
32+
if(idx == -1){
33+
String delimiter = "[,:]";
34+
return checkedInput.split(delimiter); //기본 구분자대로 나누기
35+
}
36+
else if(idx == -2){
37+
return new String[0]; //빈 문자열인 경우
38+
}
39+
else{
40+
String delimiter = checkedInput.substring(2, idx); //구분자 나누기
41+
String regex = Pattern.quote(delimiter); //정규표현식에 사용되는 문자가 커스텀 구분자일 경우를 위해 설정
42+
return checkedInput.substring(idx + 1).split(regex); //커스텀 구분자대로 문자열을 나눔
43+
}
44+
}
45+
46+
//문자열 덧셈 실행
47+
public int sum(String[] strings){
48+
if(strings.length == 0) return 0;
49+
50+
int sum = 0;
51+
52+
for(var n : strings){
53+
if(n.isEmpty()) throw new RuntimeException("빈 문자열입니다.");
54+
55+
int num = checkException(n);
56+
57+
//예외 처리
58+
if(sum > Integer.MAX_VALUE - num || sum < Integer.MIN_VALUE + num) throw new RuntimeException("int 범위를 벗어났습니다.");
59+
60+
sum += num;
61+
}
62+
return sum;
63+
}
64+
65+
private int checkException(String n){
66+
int num;
67+
try{
68+
num = Integer.parseInt(n);
69+
if(num < 0) throw new RuntimeException("음수는 입력이 불가합니다.");
70+
}
71+
catch(NumberFormatException msg){
72+
throw new RuntimeException("int 값을 벗어났거나 잘못된 숫자 형식입니다.");
73+
}
74+
return num;
75+
}
76+
77+
public int calculate(String input){
78+
String checkedInput = checkInput(input);
79+
int idx = checkDelimiter(checkedInput);
80+
String[] strings = splitInput(checkedInput, idx);
81+
return sum(strings);
82+
}
83+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import org.junit.jupiter.api.Nested;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
public class SimpleCalculatorTest {
6+
@Nested
7+
class SimpleTest {
8+
9+
@Test
10+
void addTest(){
11+
SimpleCalculator calc = new SimpleCalculator();
12+
assertEquals(3, calc.add(1, 2));
13+
assertEquals(5, calc.add(3, 2));
14+
}
15+
16+
@Test
17+
void minusTest(){
18+
SimpleCalculator calc = new SimpleCalculator();
19+
assertEquals(3, calc.minus(5, 2));
20+
assertEquals(-2, calc.minus(3, 5));
21+
}
22+
23+
@Test
24+
void multiply(){
25+
SimpleCalculator calc = new SimpleCalculator();
26+
assertEquals(10, calc.multiply(5, 2));
27+
assertEquals(15, calc.multiply(3, 5));
28+
}
29+
30+
@Test
31+
void divide(){
32+
SimpleCalculator calc = new SimpleCalculator();
33+
assertEquals(5, calc.divide(10, 2));
34+
assertEquals(30, calc.divide(180, 6));
35+
}
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import org.junit.jupiter.api.Nested;
2+
import org.junit.jupiter.api.Test;
3+
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
public class StringCalculatorTest {
8+
@Nested
9+
class StringTest{
10+
@Test
11+
void default_delimiter_Test(){
12+
StringCalculator calc = new StringCalculator();
13+
assertEquals(3, calc.calculate("1,2"));
14+
assertEquals(6, calc.calculate("1,2,3"));
15+
assertEquals(3, calc.calculate("1:2"));
16+
assertEquals(38, calc.calculate("1:2,3:4,5:6:7:10"));
17+
assertEquals(0, calc.calculate(""));
18+
}
19+
20+
@Test
21+
void custom_delimiter_Test(){
22+
StringCalculator calc = new StringCalculator();
23+
assertEquals(6, calc.calculate("//;\n1;2;3"));
24+
assertEquals(5, calc.calculate("//;;\n1;;1;;2;;1"));
25+
assertEquals(6, calc.calculate("//.\n1.2.3"));
26+
assertEquals(3, calc.calculate("//|\n1|2"));
27+
assertEquals(101, calc.calculate("//?\n10?11?12?13?14?20?21"));
28+
}
29+
30+
@Test
31+
void exceptionTest(){
32+
StringCalculator calc = new StringCalculator();
33+
34+
RuntimeException exception = assertThrows(RuntimeException.class, () ->
35+
calc.calculate("-1,2")
36+
);
37+
38+
assertEquals("음수는 입력이 불가합니다.", exception.getMessage());
39+
40+
RuntimeException exception2 = assertThrows(RuntimeException.class, () ->
41+
calc.calculate("2147483647,10")
42+
);
43+
44+
assertEquals("int 범위를 벗어났습니다.", exception2.getMessage());
45+
}
46+
}
47+
}

src/test/java/TestWithAssertJ.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import org.junit.jupiter.api.Nested;
2+
import org.junit.jupiter.api.Test;
3+
4+
import static org.assertj.core.api.Assertions.assertThat;
5+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class TestWithAssertJ {
9+
@Nested
10+
class CalculatorTest{
11+
@Test
12+
void simpleCalcTest(){
13+
SimpleCalculator calc = new SimpleCalculator();
14+
assertThat(calc.add(1, 2)).isEqualTo(3);
15+
assertThat(calc.minus(1,2)).isEqualTo(-1);
16+
assertThat(calc.divide(4, 2)).isEqualTo(2);
17+
assertThat(calc.multiply(3,2)).isEqualTo(6);
18+
}
19+
20+
@Test
21+
void stringCalcTest(){
22+
StringCalculator calc = new StringCalculator();
23+
assertThat(calc.calculate("//;\n1;2;3")).isEqualTo(6);
24+
assertThat(calc.calculate("//?\n10?11?12?13?14?20?21")).isEqualTo(101);
25+
}
26+
27+
@Test
28+
void exceptionTest(){
29+
StringCalculator calc = new StringCalculator();
30+
assertThatThrownBy(() ->
31+
calc.calculate("-1,2")).isInstanceOf(RuntimeException.class);
32+
33+
assertThatThrownBy(() -> calc.calculate("//:123")).isInstanceOf(RuntimeException.class);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)