Skip to content

Commit c7a6f7f

Browse files
committed
style:코드 정리
1 parent 48eaf2d commit c7a6f7f

3 files changed

Lines changed: 48 additions & 35 deletions

File tree

src/main/java/calculator/controller/CalculatorController.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
public class CalculatorController {
77
private final CalculatorService calculatorService;
88

9-
public CalculatorController(CalculatorService calculatorService){
9+
public CalculatorController(CalculatorService calculatorService) {
1010
this.calculatorService = calculatorService;
1111
}
1212

13-
public void input(){
13+
public void input() {
1414
System.out.println("덧셈할 문자열을 입력해 주세요.");
1515
String s = Console.readLine();
1616
output(s);
1717
}
18-
private void output(String s){
18+
19+
private void output(String s) {
1920
int sum = calculatorService.sum(s);
20-
System.out.println("결과 : "+sum);
21+
System.out.println("결과 : " + sum);
2122
}
2223

2324
}

src/main/java/calculator/service/CalculatorService.java

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ public class CalculatorService {
44
private String s;
55
private String plusSeparator;
66

7-
/** Run all logic step by step. */
8-
public int sum(String s){
7+
/**
8+
* Run all logic step by step.
9+
*/
10+
public int sum(String s) {
911
this.s = s;
1012
boolean hasPlusSeparator = checkPlusSeparator();
1113
String[] separatedString = separate(hasPlusSeparator);
1214
hasNaN(separatedString);
1315
return sumSeparatedStringArr(separatedString);
1416
}
17+
1518
/**
1619
* Check existence of another separator.
20+
*
1721
* @return if exist another separator, return true. Or return false.
1822
*/
1923
private boolean checkPlusSeparator() {
@@ -24,43 +28,51 @@ private boolean checkPlusSeparator() {
2428
}
2529
return false;
2630
}
31+
2732
/**
2833
* Separate input string. if escape string(\) added separator, need action.
34+
*
2935
* @param hasPlusSeparator if another separator exist, set true. Or false.
3036
* @return separated string.
3137
*/
32-
private String[] separate(boolean hasPlusSeparator){
33-
if(!hasPlusSeparator){
38+
private String[] separate(boolean hasPlusSeparator) {
39+
if (!hasPlusSeparator) {
3440
return s.split("[:,]");
35-
}else{
36-
if(plusSeparator.equals("\\"))
37-
return s.split("[:,"+plusSeparator.repeat(2)+"]");
38-
return s.split("[:,"+plusSeparator+"]");
41+
} else {
42+
if (plusSeparator.equals("\\")) {
43+
return s.split("[:," + plusSeparator.repeat(2) + "]");
44+
}
45+
return s.split("[:," + plusSeparator + "]");
3946
}
4047
}
48+
4149
/**
42-
* Check input string array contains not a number. if it contains NaN,
43-
* throw IllegalArgumentException.
50+
* Check input string array contains not a number. if it contains NaN, throw IllegalArgumentException.
51+
*
4452
* @param separatedStringArr String separated by separator.
4553
*/
46-
private void hasNaN(String[] separatedStringArr){
47-
for(String s:separatedStringArr){
48-
for(char c:s.toCharArray()){
49-
if(c < 48 || c > 57)
54+
private void hasNaN(String[] separatedStringArr) {
55+
for (String s : separatedStringArr) {
56+
for (char c : s.toCharArray()) {
57+
if (c < 48 || c > 57) {
5058
throw new IllegalArgumentException();
59+
}
5160
}
5261
}
5362
}
63+
5464
/**
5565
* Sum all of them
66+
*
5667
* @param separatedStringArr clean integer string array.
5768
* @return Sum
5869
*/
59-
private int sumSeparatedStringArr(String[] separatedStringArr){
70+
private int sumSeparatedStringArr(String[] separatedStringArr) {
6071
int sum = 0;
61-
for(String separatedString:separatedStringArr){
62-
if(!separatedString.isEmpty())
72+
for (String separatedString : separatedStringArr) {
73+
if (!separatedString.isEmpty()) {
6374
sum += Integer.parseInt(separatedString);
75+
}
6476
}
6577
return sum;
6678
}

src/test/java/calculator/ApplicationTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package calculator;
22

3-
import camp.nextstep.edu.missionutils.test.NsTest;
4-
import org.junit.jupiter.api.Test;
5-
63
import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest;
74
import static org.assertj.core.api.Assertions.assertThat;
85
import static org.assertj.core.api.Assertions.assertThatThrownBy;
96

7+
import camp.nextstep.edu.missionutils.test.NsTest;
8+
import org.junit.jupiter.api.Test;
9+
1010
class ApplicationTest extends NsTest {
1111
@Test
1212
void 커스텀_구분자_사용() {
@@ -19,8 +19,8 @@ class ApplicationTest extends NsTest {
1919
@Test
2020
void 예외_테스트() {
2121
assertSimpleTest(() ->
22-
assertThatThrownBy(() -> runException("-1,2,3"))
23-
.isInstanceOf(IllegalArgumentException.class)
22+
assertThatThrownBy(() -> runException("-1,2,3"))
23+
.isInstanceOf(IllegalArgumentException.class)
2424
);
2525
}
2626

@@ -30,47 +30,47 @@ public void runMain() {
3030
}
3131

3232
@Test
33-
public void 숫자_구분자_사용(){
34-
assertSimpleTest(()->{
33+
public void 숫자_구분자_사용() {
34+
assertSimpleTest(() -> {
3535
run("//1\\n2:213");
3636
assertThat(output()).contains("결과 : 7");
3737
});
3838
}
3939

4040
@Test
41-
public void 탈출_문자_사용(){
41+
public void 탈출_문자_사용() {
4242
assertSimpleTest(() -> {
4343
assertThatThrownBy(() -> runException("1\\2:3"))
4444
.isInstanceOf(IllegalArgumentException.class);
4545
});
4646
}
4747

4848
@Test
49-
public void 탈출_문자_구분자_사용(){
49+
public void 탈출_문자_구분자_사용() {
5050
assertSimpleTest(() -> {
5151
run("//\\\\n1\\2:3,4");
5252
assertThat(output()).contains("결과 : 10");
5353
});
5454
}
5555

5656
@Test
57-
public void 마침표_구분자_사용(){
58-
assertSimpleTest(()->{
57+
public void 마침표_구분자_사용() {
58+
assertSimpleTest(() -> {
5959
run("//.\\n1.2.4:3");
6060
assertThat(output()).contains("결과 : 10");
6161
});
6262
}
6363

6464
@Test
65-
public void 따옴표_구분자_사용(){
66-
assertSimpleTest(()->{
65+
public void 따옴표_구분자_사용() {
66+
assertSimpleTest(() -> {
6767
run("//\"\\n1\"2\"4:3");
6868
assertThat(output()).contains("결과 : 10");
6969
});
7070
}
7171

7272
@Test
73-
public void 구분자_사이에_문자열_없음(){
73+
public void 구분자_사이에_문자열_없음() {
7474
assertSimpleTest(() -> {
7575
run("1,:2");
7676
assertThat(output()).contains("결과 : 3");

0 commit comments

Comments
 (0)