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

[문자열 덧셈 계산기] 은상현 미션 제출합니다. #1890

Open
wants to merge 14 commits into
base: main
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
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,32 @@
# java-calculator-precourse
# java-calculator-precourse

## 기능 목록
- [X] 입력된 문자열에서 숫자를 추출할 수 있다.
- [X] 구분자를 지정할 수 있다.
- [X] 구분자가 빈 문자열일 경우 예외를 발생시킨다.
- [X] 구분자에 숫자를 포함한 경우 예외를 발생시킨다.
- [X] 생성된 구분자들을 관리할 수 있다.
- [X] 구분자들은 고유한 값을 가진다.
- [X] 커스텀 문자열을 지정할 수 있다.
- [X] `//` 와 `\n` 사이에 위치하는 문자를 커스텀 구분자로 지정한다.
- [X] 커스텀 구분자가 숫자인 경우 예외를 발생시킨다.
- [X] 입력된 문자열을 구분자를 기준으로 분리할 수 있다.
- [X] 구분자, 커스텀 구분자를 기준으로 문자열을 분리할 수 있다.
- [X] 분리한 문자가 양수인지 확인할 수 있다.
- [X] 분리한 문자가 숫자가 아닌 경우 예외를 발생시킨다.
- [X] 분리한 문자가 음수나 0인 경우 예외를 발생시킨다.
- [X] 추출된 숫자의 합을 확인할 수 있다.

## 기능 요구 사항
입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다.

쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환한다.
```
예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6
```

앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 사용한다.
```
예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다.
```
사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료되어야 한다.
5 changes: 4 additions & 1 deletion src/main/java/calculator/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package calculator;

import calculator.controller.CalculatorController;

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
CalculatorController controller = new CalculatorController();
controller.run();
}
}
77 changes: 77 additions & 0 deletions src/main/java/calculator/controller/CalculatorController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package calculator.controller;

import calculator.domain.Delimiter;
import calculator.domain.DelimiterManager;
import calculator.domain.Number;
import calculator.domain.Numbers;
import calculator.view.InputView;
import calculator.view.OutputView;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class CalculatorController {

private static final String SETUP_SYMBOL_COMMA = ",";
private static final String SETUP_SYMBOL_COLON = ":";

private static final Pattern DELIMITER_PATTERN = Pattern.compile("//(.*?)\\n");
private final InputView inputView = new InputView();
private final OutputView outputView = new OutputView();
private final DelimiterManager delimiterManager;

public CalculatorController() {
delimiterManager = new DelimiterManager(List.of(
new Delimiter(SETUP_SYMBOL_COMMA)
,new Delimiter(SETUP_SYMBOL_COLON)
));
}

public void run() {
try {
startCalculator();
} catch (IllegalArgumentException exception){
outputView.printException(exception);
}
}

public void startCalculator(){
String inputString = inputView.createString();
DelimiterManager delimiterManager = extractCustomDelimiters(inputString);
Numbers number = createNumbers(inputString, delimiterManager);
outputView.printResult(number.calculateSum());
}

private DelimiterManager extractCustomDelimiters(String input) {
input = input.replace("\\n", "\n");
Matcher matcher = DELIMITER_PATTERN.matcher(input);

while (matcher.find()) {
String delimiterBlock = matcher.group(1);
delimiterManager.addDelimiter(delimiterBlock);
}

return delimiterManager;
}

private Numbers createNumbers(String inputString, DelimiterManager delimiterManager) {
String numberPart = removeDelimiterDefinition(inputString);

String delimiterPattern = delimiterManager.getDelimiters().stream()
.map(Delimiter::getSymbol)
.reduce((a, b) -> a + "|" + b)
.orElse(",");

return new Numbers(Arrays.stream(numberPart.split(delimiterPattern))
.map(String::trim)
.map(Number::new)
.collect(Collectors.toList()));
}

private String removeDelimiterDefinition(String inputString) {
inputString = inputString.replace("\\n", "\n");
return inputString.replaceAll("//.*?\\n", "");
}
}
46 changes: 46 additions & 0 deletions src/main/java/calculator/domain/Delimiter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package calculator.domain;

import java.util.Objects;

public class Delimiter {
private final String symbol;

public Delimiter(String symbol) {
validateNotNullOrEmpty(symbol);
validateNoDigits(symbol);
this.symbol = symbol;
}

private void validateNotNullOrEmpty(String symbol) {
if(symbol == null || symbol.isBlank()){
throw new IllegalArgumentException("[ERROR] 구분자가 빈 문자열일 수 없습니다.");
}
}

private void validateNoDigits(String symbol) {
if (symbol.matches(".*\\d.*")) {
throw new IllegalArgumentException("[ERROR] 구분자에 숫자가 포함될 수 없습니다.");
}
}

public String getSymbol() {
return symbol;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Delimiter delimiter = (Delimiter) o;
return Objects.equals(symbol, delimiter.symbol);
}

@Override
public int hashCode() {
return Objects.hash(symbol);
}
}
29 changes: 29 additions & 0 deletions src/main/java/calculator/domain/DelimiterManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package calculator.domain;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class DelimiterManager {
private List<Delimiter> delimiters;

public DelimiterManager(List<Delimiter> delimiters) {
this.delimiters = uniqueDelimiters(delimiters);
}

private List<Delimiter> uniqueDelimiters(List<Delimiter> delimiters) {
return delimiters.stream()
.distinct()
.collect(Collectors.toList());
}

public void addDelimiter(String delimiter) {
delimiters.add(new Delimiter(delimiter));
delimiters = uniqueDelimiters(delimiters);
}

public List<Delimiter> getDelimiters() {
return delimiters;
}
}
26 changes: 26 additions & 0 deletions src/main/java/calculator/domain/Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package calculator.domain;

public class Number {

private static final String POSITIVE_NUMBER_PATTERN = "^[1-9]\\d*$";
private final int value;

public Number(String name) {
validate(name);
this.value = parseInt(name);
}

private void validate(String name) {
if (!name.matches(POSITIVE_NUMBER_PATTERN)) {
throw new IllegalArgumentException("[ERROR] 양의 정수가 아닙니다");
}
}

private int parseInt(String name) {
return Integer.parseInt(name);
}

public int getValue() {
return value;
}
}
22 changes: 22 additions & 0 deletions src/main/java/calculator/domain/Numbers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package calculator.domain;

import java.util.List;

public class Numbers {

private final List<Number> numbers;

public Numbers(List<Number> numbers) {
this.numbers = numbers;
}

public int calculateSum() {
return numbers.stream()
.mapToInt(Number::getValue)
.sum();
}

public List<Number> getNumbers() {
return numbers;
}
}
12 changes: 12 additions & 0 deletions src/main/java/calculator/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package calculator.view;

import camp.nextstep.edu.missionutils.Console;

public class InputView {
private static final String INPUT_STRING_TO_ADD = "덧셈할 문자열을 입력해 주세요.";

public static String createString() {
System.out.println(INPUT_STRING_TO_ADD);
return Console.readLine();
}
}
15 changes: 15 additions & 0 deletions src/main/java/calculator/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package calculator.view;


import static java.lang.StringTemplate.STR;

public class OutputView {

public static void printResult(int value) {
String result = STR."결과는: ${value}";
System.out.println(result);
}
public static void printException(Exception exception) {
System.out.println(exception.getMessage());
}
}
53 changes: 53 additions & 0 deletions src/test/java/calculator/domain/DelimiterManagerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package calculator.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;

import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;

class DelimiterManagerTest {

@DisplayName("constructor() : 생성된 구분자들을 관리할 수 있다.")
@ParameterizedTest
@CsvSource({"',', ':', ';'"})
void constructor_delimiterManager_success(
String symbolComma
, String symbolColon
, String symbolSemiColon) throws Exception {
//given & when & then
assertThatCode(() -> createDelimiterManager(symbolComma, symbolColon, symbolSemiColon))
.doesNotThrowAnyException();
}

@DisplayName("uniqueDelimiters() : 구분자들은 고유한 값을 가진다.")
@ParameterizedTest
@CsvSource({"',',:,:", "',',;,;"})
void uniqueDelimiters_delimiterManager_success(
String symbolComma
, String duplicationSymbol1
, String duplicationSymbol2) throws Exception {
//given
DelimiterManager delimiterManager = createDelimiterManager(symbolComma, duplicationSymbol1, duplicationSymbol2);
System.out.println(delimiterManager.getDelimiters().size());;
// when
List<Delimiter> uniqueDelimiters = delimiterManager.getDelimiters();
System.out.println(uniqueDelimiters.size());

// then
assertThat(uniqueDelimiters)
.hasSize(2)
.extracting(Delimiter::getSymbol)
.containsExactlyInAnyOrder(symbolComma, duplicationSymbol1);
}

private static DelimiterManager createDelimiterManager(String symbol1, String symbol2, String symbol3) {
return new DelimiterManager(List.of(new Delimiter(symbol1)
, new Delimiter(symbol2)
, new Delimiter(symbol3)));
}
}
49 changes: 49 additions & 0 deletions src/test/java/calculator/domain/DelimiterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package calculator.domain;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class DelimiterTest {

@DisplayName("constructor() : 구분자를 정상적으로 입력한 경우")
@ParameterizedTest
@ValueSource(strings = {";", "-", "/"})
void constructor_delimiter_success(String symbol) throws Exception {
//given & when & then
assertThatCode(() -> new Delimiter(symbol))
.doesNotThrowAnyException();;
}

@DisplayName("validateNotNullOrEmpty() : 구분자가 빈 문자열인 경우")
@ParameterizedTest
@ValueSource(strings = {"", " ", " "})
void validateNotNullOrEmpty_delimiter_fail(String symbol) throws Exception{
//given
String errorMessage = "[ERROR] 구분자가 빈 문자열일 수 없습니다.";

//when & then
assertThatThrownBy(() -> new Delimiter(symbol))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(errorMessage);
}

@DisplayName("validateNoDigits() : 구분자에 숫자가 포함된 경우")
@ParameterizedTest
@ValueSource(strings = {"1,", ",2", "33", "3,4"})
void validateNoDigits_delimiter_fail(String symbol) throws Exception{
//given
String errorMessage = "[ERROR] 구분자에 숫자가 포함될 수 없습니다.";

//when & then
assertThatThrownBy(() -> new Delimiter(symbol))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(errorMessage);
}
}