Skip to content

Commit

Permalink
refactor:Separator 클래스 생성 및 로직 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
whrjsgml0000 committed Oct 20, 2024
1 parent 831a58f commit c145fc0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
22 changes: 22 additions & 0 deletions src/main/java/calculator/dto/CheckSeparatorDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package calculator.dto;

public class CheckSeparatorDto {
private String inputString;
private String customSeparator = null;

public String getInputString() {
return inputString;
}

public void setInputString(String inputString) {
this.inputString = inputString;
}

public String getCustomSeparator() {
return customSeparator;
}

public void setCustomSeparator(String customSeparator) {
this.customSeparator = customSeparator;
}
}
43 changes: 8 additions & 35 deletions src/main/java/calculator/service/CalculatorService.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,27 @@
package calculator.service;

import calculator.dto.CheckSeparatorDto;
import calculator.service.separator.Separator;
import calculator.service.validator.Validator;

public class CalculatorService {
private final Validator validator = new Validator();
private String inputString;
private String customSeparator;
private final Separator separator = new Separator();


/**
* 덧셈 로직을 순차적으로 진행한다.
*/
public int add(String inputString) {
this.inputString = inputString;
boolean hasCustomSeparator = checkCustomSeparator();
String[] separatedString = separate(hasCustomSeparator);
CheckSeparatorDto checkSeparatorDto = new CheckSeparatorDto();
checkSeparatorDto.setInputString(inputString);
separator.checkCustomSeparator(checkSeparatorDto);
String[] separatedString = separator.separate(checkSeparatorDto);

validator.hasNaN(separatedString);
return sumSeparatedStringArr(separatedString);
}

/**
* 커스텀 구분자가 있는지 확인한다.
*
* @return 만약 커스텀 구분자가 있다면 true, 없다면 false 를 반환한다.
*/
private boolean checkCustomSeparator() {
if (inputString.length() >= 5 && inputString.startsWith("//") && inputString.startsWith("\\n", 3)) {
customSeparator = String.valueOf(inputString.charAt(2));
inputString = inputString.substring(5);
return true;
}
return false;
}

/**
* 받은 문자열을 구분자를 기준으로 나눈다. 만약 커스텀 구분자에 역슬래시가 있다면 문제가 생기지 않도록 한다.
*
* @param hasCustomSeparator 커스텀 구분자가 있다면 true, 없다면 false 를 입력한다.
* @return 분리된 문자열을 반환한다. 이 문자열에는 반드시 숫자만 있는 것은 아니다.
*/
private String[] separate(boolean hasCustomSeparator) {
if (!hasCustomSeparator) {
return inputString.split("[:,]");
}
if (customSeparator.equals("\\")) {
return inputString.split("[:," + customSeparator.repeat(2) + "]");
}
return inputString.split("[:," + customSeparator + "]");
}

/**
* 문자열을 int 형으로 바꾼 뒤 합한다.
*
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/calculator/service/separator/Separator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package calculator.service.separator;

import calculator.constant.CalConst;
import calculator.dto.CheckSeparatorDto;

public class Separator {

public void checkCustomSeparator(CheckSeparatorDto checkSeparatorDto) {
String inputString = checkSeparatorDto.getInputString();
String customSeparator;
if (inputString.length() >= CalConst.MINIMUM_LENGTH_IF_EXIST_CUSTOM_SEPARATOR.value()
&& inputString.startsWith("//")
&& inputString.startsWith("\\n", CalConst.END_INDEX_OF_CUSTOM_SEPARATOR.value())) {
customSeparator = String.valueOf(inputString.charAt(CalConst.INDEX_OF_CUSTOM_SEPARATOR.value()));
inputString = inputString.substring(CalConst.START_INDEX_OF_CALCULATING.value());

checkSeparatorDto.setCustomSeparator(customSeparator);
checkSeparatorDto.setInputString(inputString);
}
}

public String[] separate(CheckSeparatorDto checkSeparatorDto) {
String customSeparator = checkSeparatorDto.getCustomSeparator();
String inputString = checkSeparatorDto.getInputString();
if (customSeparator==null) {
return inputString.split("[:,]");
}
if (customSeparator.equals("\\")) {
return inputString.split("[:," + customSeparator.repeat(2) + "]");
}
return inputString.split("[:," + customSeparator + "]");
}
}

0 comments on commit c145fc0

Please sign in to comment.