nextstep에서 진행하는 자바 플레이그라운드 with TDD, 클린코드 학습 내용입니다. 혼자 작성해본 연습 코드, 피드백 받은 내용을 정리합니다. 해당 코드를 더 나은 방향으로 개선할 수 있다면 댓글로 조언 꼭 부탁드립니다!
public class StringCalculatorTest {
private void customizeSystemSetIn(String inputValue) {
System.setIn(new ByteArrayInputStream(inputValue.getBytes()));
}
@ParameterizedTest
@ValueSource(strings = {"입력한 문자열"})
public void 문자열_입력_받기(String inputValue) {
customizeSystemSetIn(inputValue);
Scanner scanner = new Scanner(System.in);
assertThat(scanner.nextLine()).isEqualTo("입력한 문자열");
}
@ParameterizedTest
@ValueSource(strings = {"2 + 3 * 4 / 2"})
public void 문자열_분리하기(String inputValue) {
String[] splitInputValue = inputValue.split(" ");
assertThat(splitInputValue).containsExactly("2", "+", "3", "*", "4", "/", "2");
}
@ParameterizedTest
@ValueSource(strings = {"2 + 3 * 4 / 2"})
public void 정수와_계산기호_분리_하기(String inputValue) {
String[] splitInputValue = inputValue.split(" ");
ArrayList<Integer> integerList = new ArrayList<>();
ArrayList<String> mathematicalSignList = new ArrayList<>();
for (String s : splitInputValue) {
try {
integerList.add(Integer.parseInt(s));
} catch (NumberFormatException e) {
mathematicalSignList.add(s);
}
}
assertThat(integerList).containsExactly(2, 3, 4, 2);
assertThat(mathematicalSignList).containsExactly("+", "*", "/");
}
@Test
public void 정수_리스트와_계산_기호_리스트를_사용해_순차적_사칙연산_실행하기() {
ArrayList<Integer> integerList = new ArrayList<>(Arrays.asList(2, 3, 4, 2));
ArrayList<String> mathematicalSignList = new ArrayList<>(Arrays.asList("+", "*", "/"));
int calcResult = 0;
for (int i = 0; i < integerList.size(); i++) {
if (i == 0) {
calcResult = integerList.get(i);
continue;
}
//메소드로 빼내고 싶은 부분
calcResult = 사칙연산_기호를_입력받아_두_수의_사칙연산_수행하기(mathematicalSignList.get(i-1), calcResult, integerList.get(i));
}
assertThat(calcResult).isEqualTo(10);
}
//테스트 수행 완료 후 메소드 처리를 위해 일반 private 메소드로 변환
private int 사칙연산_기호를_입력받아_두_수의_사칙연산_수행하기(String mathematicalSign, int calcResult, int pramInt) {
if (mathematicalSign.equals("+")) {
calcResult = calcResult + pramInt;
return calcResult;
}
if (mathematicalSign.equals("-")) {
calcResult = calcResult - pramInt;
return calcResult;
}
if (mathematicalSign.equals("*")) {
calcResult = calcResult * pramInt;
return calcResult;
}
calcResult = calcResult / pramInt;
return calcResult;
}
}
public class StringCalculator {
private final String ADDITION_SIGN = "+";
private final String SUBTRACTION_SIGN = "-";
private final String MULTIPLICATION_SIGN = "*";
private final String DIVISION_SIGN = "/";
private final int INIT_INT = 0;
private String inputValue;
private ArrayList<Integer> intList = new ArrayList<>();
private ArrayList<String> mathematicalSignList = new ArrayList<>();
private int result;
public StringCalculator() {
}
public StringCalculator(String inputValue) {
this.inputValue = inputValue;
}
public int getResult() {
return result;
}
public String getInputValue() {
return inputValue;
}
public void setInputValue(String inputValue) {
this.inputValue = inputValue;
}
public void calculate() {
this.setList();
this.result = this.calculateIntList();
}
private String[] splitInputValue() {
return this.inputValue.split(" ");
}
private void setList() {
for (String s : splitInputValue()) {
try {
this.intList.add(Integer.parseInt(s));
} catch (NumberFormatException e) {
this.mathematicalSignList.add(s);
}
}
}
private int calculateTwoInt(String mathematicalSign, int calcResult, int pramInt) {
if (Objects.isNull(mathematicalSign) || mathematicalSign.equals("")) {
return calcResult;
}
if (mathematicalSign.equals(ADDITION_SIGN)) {
return calcResult + pramInt;
}
if (mathematicalSign.equals(SUBTRACTION_SIGN)) {
return calcResult - pramInt;
}
if (mathematicalSign.equals(MULTIPLICATION_SIGN)) {
return calcResult * pramInt;
}
return calcResult / pramInt;
}
private int calculateIntList() {
int calcResult = INIT_INT;
for (int i = INIT_INT; i < this.intList.size(); i++) {
if (i == INIT_INT) {
calcResult = this.intList.get(i);
continue;
}
calcResult = calculateTwoInt(this.mathematicalSignList.get(i - 1), calcResult, this.intList.get(i));
}
return calcResult;
}
}
public class calculate {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringCalculator calculator = new StringCalculator(scanner.nextLine());
calculator.calculate();
System.out.println("result : " + calculator.getResult());
}
}
와 객체지향적으로 엄청 잘짜셨네요 제가 짠거랑 비교되네요..ㅎ