From 0a8fbead65cf53ef298f6a74441fc365e665f21b Mon Sep 17 00:00:00 2001 From: naheenosaur Date: Mon, 24 Feb 2020 22:44:00 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20Number=20Class=20=EC=A0=95?= =?UTF-8?q?=EC=A0=81=ED=8C=A9=ED=86=A0=EB=A6=AC=EB=A1=9C=20=ED=91=9C?= =?UTF-8?q?=ED=98=84,=20=EB=A9=94=EB=AA=A8=EB=A6=AC=EC=97=90=20=EC=A0=80?= =?UTF-8?q?=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Number.java | 52 +++++++++++++++++++ src/main/java/calculator/PositiveNumber.java | 41 --------------- .../java/calculator/StringCalculator.java | 5 +- ...ositiveNumberTest.java => NumberTest.java} | 7 ++- .../MenuGroupRestControllerTest.java | 5 -- 5 files changed, 60 insertions(+), 50 deletions(-) create mode 100644 src/main/java/calculator/Number.java delete mode 100644 src/main/java/calculator/PositiveNumber.java rename src/test/java/calculator/{PositiveNumberTest.java => NumberTest.java} (83%) diff --git a/src/main/java/calculator/Number.java b/src/main/java/calculator/Number.java new file mode 100644 index 000000000..ee15192a2 --- /dev/null +++ b/src/main/java/calculator/Number.java @@ -0,0 +1,52 @@ +package calculator; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Number { + private static final Pattern NUMERIC_PATTERN = Pattern.compile("[+]?\\d+"); + private static final Map positiveNumbers = new HashMap<>(); + private int value; + + private Number() { + this.value = 0; + } + + private Number(int num) { + this.value = num; + } + + public static Number sumOf(List strings) { + Number number = new Number(); + for (String string : strings) { + number.add(ofPositive(string).value); + } + return number; + } + + private static Number ofPositive(String stringNumber) { + int num = parsePositiveInt(stringNumber); + Number number = positiveNumbers.getOrDefault(num, new Number(num)); + positiveNumbers.put(num, number); + return number; + } + + private static int parsePositiveInt(String string) { + Matcher isNumeric = NUMERIC_PATTERN.matcher(string); + if (isNumeric.matches()) { + return Integer.parseInt(string); + } + throw new IllegalArgumentException(); + } + + public int getValue() { + return this.value; + } + + private void add(int number) { + this.value += number; + } +} diff --git a/src/main/java/calculator/PositiveNumber.java b/src/main/java/calculator/PositiveNumber.java deleted file mode 100644 index 841e97462..000000000 --- a/src/main/java/calculator/PositiveNumber.java +++ /dev/null @@ -1,41 +0,0 @@ -package calculator; - -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -public class PositiveNumber { - private static final Pattern NUMERIC_PATTERN = Pattern.compile("[+]?\\d+"); - private int value; - - PositiveNumber(List strings) { - this.value = sum(strings); - } - - private PositiveNumber(String stringNumber) { - this.value = parsePositiveInt(stringNumber); - } - - public int getValue() { - return this.value; - } - - private int sum(List strings) { - for (String string : strings) { - add(new PositiveNumber(string)); - } - return this.value; - } - - private void add(PositiveNumber added) { - this.value += added.value; - } - - private Integer parsePositiveInt(String string) { - Matcher isNumeric = NUMERIC_PATTERN.matcher(string); - if (isNumeric.matches()) { - return Integer.parseInt(string); - } - throw new IllegalArgumentException(); - } -} diff --git a/src/main/java/calculator/StringCalculator.java b/src/main/java/calculator/StringCalculator.java index a58d41da1..6698e1b1f 100644 --- a/src/main/java/calculator/StringCalculator.java +++ b/src/main/java/calculator/StringCalculator.java @@ -3,6 +3,7 @@ import org.apache.logging.log4j.util.Strings; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -18,8 +19,8 @@ public int calculate(String input) { return 0; } List inputs = splitInputs(input); - PositiveNumber positiveNumber = new PositiveNumber(inputs); - return positiveNumber.getValue(); + Number number = Number.sumOf(Collections.unmodifiableList(inputs)); + return number.getValue(); } private List splitInputs(String input) { diff --git a/src/test/java/calculator/PositiveNumberTest.java b/src/test/java/calculator/NumberTest.java similarity index 83% rename from src/test/java/calculator/PositiveNumberTest.java rename to src/test/java/calculator/NumberTest.java index 375fb3bd2..3c0fbc374 100644 --- a/src/test/java/calculator/PositiveNumberTest.java +++ b/src/test/java/calculator/NumberTest.java @@ -6,13 +6,14 @@ import org.junit.jupiter.params.provider.MethodSource; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.params.provider.Arguments.arguments; -class PositiveNumberTest { +class NumberTest { private static final Stream throwWithNonNumericExceptionArguments() { return Stream.of( arguments(Arrays.asList("-1", "2", "3")), @@ -24,6 +25,8 @@ private static final Stream throwWithNonNumericExceptionArguments() { @MethodSource("throwWithNonNumericExceptionArguments") @DisplayName("숫자 이외의 값 또는 음수는 RuntimeException을 발생한다.") void throwWithNonNumericException(List inputs) { - assertThrows(IllegalArgumentException.class, () -> new PositiveNumber(inputs)); + assertThrows(IllegalArgumentException.class, + () -> Number.sumOf(Collections.unmodifiableList(inputs)) + ); } } diff --git a/src/test/java/kitchenpos/controller/MenuGroupRestControllerTest.java b/src/test/java/kitchenpos/controller/MenuGroupRestControllerTest.java index bd2eac1a6..2e5fe02ae 100644 --- a/src/test/java/kitchenpos/controller/MenuGroupRestControllerTest.java +++ b/src/test/java/kitchenpos/controller/MenuGroupRestControllerTest.java @@ -2,7 +2,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import kitchenpos.bo.MenuGroupBo; -import kitchenpos.model.MenuGroupTest; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -10,13 +9,9 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;