Skip to content
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

Implement the StringAddCalculator #6

Open
wants to merge 1 commit into
base: master
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package StringAddCalculator.controller;

import StringAddCalculator.domain.Computation;
import StringAddCalculator.view.InputView;


public class StringAddCalculator {
final static private Computation computation = new Computation();
public static void main(String[] args){
InputView inputView = new InputView();
computation.getNumber(inputView.askAddNumber());
}
}
30 changes: 30 additions & 0 deletions src/main/java/StringAddCalculator/domain/Computation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package StringAddCalculator.domain;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Computation {
public int getNumber(String s) {

String sum = s;
if(s == null || s.isEmpty()) {
return 0;
}

Matcher m = Pattern.compile("//(.)\\\\n(.*)").matcher(s);
if(m.find()) {
String customDelimiter = m.group(1);
String[] tokens = m.group(2).split(customDelimiter);

sum = String.valueOf(addNumber(tokens));
return Integer.parseInt(String.valueOf(sum));
}

return addNumber(s.split(",|:"));
}

static int addNumber(String[] arr) {
return Arrays.stream(arr).map(item -> Integer.parseInt(item)).reduce(0, (a, b) -> a+b);
}
}
14 changes: 14 additions & 0 deletions src/main/java/StringAddCalculator/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package StringAddCalculator.view;

import java.util.Scanner;

public class InputView {
Scanner scanner = new Scanner(System.in);

public String askAddNumber() {
System.out.println("입력해주세요");
String AddText = scanner.nextLine();
return AddText;
}

}
58 changes: 58 additions & 0 deletions src/test/java/StringAddCalculator/StringCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package StringAddCalculator;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;


import StringAddCalculator.controller.StringAddCalculator;
import StringAddCalculator.domain.Computation;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class StringCalculatorTest {
private Computation computation;

@BeforeEach
void setUp() {
computation = new Computation();
}

@DisplayName("널이나 빈값체크")
@Test
public void splitAndSum_null_또는_빈문자() {
int result = computation.getNumber(null);
assertEquals(0, result);

result = computation.getNumber("");
assertEquals(0, result);
}

@DisplayName("숫자 하나 체크")
@Test
public void splitAndSum_숫자하나() throws Exception {
int result = computation.getNumber("1");
assertThat(result).isEqualTo(1);
}

@DisplayName("쉼표 구분문자 체크")
@Test
public void splitAndSum_쉼표구분자() throws Exception {
int result = computation.getNumber("1,2");
assertThat(result).isEqualTo(3);
}

@DisplayName("쉼표 또는 콜론 구분자")
@Test
public void splitAndSum_쉼표_또는_콜론_구분자() throws Exception {
int result = computation.getNumber("1,2:3");
assertThat(result).isEqualTo(6);
}

@DisplayName("구분자")
@Test
public void splitAndSum_custom_구분자() throws Exception {
int result = computation.getNumber("//;\\n1;2;3");
assertThat(result).isEqualTo(6);
}
}