From 78be556589dabc1a8a9a977b5da1d2bae5229e0b Mon Sep 17 00:00:00 2001 From: JhyunJung Date: Sun, 5 May 2024 11:56:00 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EB=AC=B8=EC=9E=90=EC=97=B4=20=EB=8D=A7?= =?UTF-8?q?=EC=85=88=20=EA=B3=84=EC=82=B0=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/calculator/Calculator.java | 49 ++++++++++++ src/test/java/calculator/CalculatorTest.java | 79 ++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 src/main/java/calculator/Calculator.java create mode 100644 src/test/java/calculator/CalculatorTest.java diff --git a/src/main/java/calculator/Calculator.java b/src/main/java/calculator/Calculator.java new file mode 100644 index 000000000..68c67331b --- /dev/null +++ b/src/main/java/calculator/Calculator.java @@ -0,0 +1,49 @@ +package calculator; + +import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class Calculator { + + private static final String DELIMITER_REGEX = ",|:"; + private static final int CUSTOM_DELIMITER = 1; + private static final int INPUT_STRING = 2; + public static int splitAndSum(String input) { + if (isNullOrBlank(input)) { + return 0; + } + + String[] splitedNumbers; + String regex = "//(.)\n(.*)"; + + Matcher m = Pattern.compile(regex).matcher(input); + if (m.find()) { + String delimiter = m.group(CUSTOM_DELIMITER); + splitedNumbers = m.group(INPUT_STRING).split(delimiter); + return sumNumbers(splitedNumbers); + } + + splitedNumbers = input.split(DELIMITER_REGEX); + return sumNumbers(splitedNumbers); + } + + private static boolean isNullOrBlank(String input) { + return input == null || input.isBlank(); + } + private static int sumNumbers(String[] numbers) { + return Arrays.stream(numbers) + .mapToInt(number -> { + try { + int num = Integer.parseInt(number); + if (num < 0) { + throw new IllegalArgumentException("Negative number found: " + num); + } + return num; + } catch (NumberFormatException ex) { + throw new IllegalArgumentException("Invalid number found: " + number); + } + }) + .sum(); + } +} diff --git a/src/test/java/calculator/CalculatorTest.java b/src/test/java/calculator/CalculatorTest.java new file mode 100644 index 000000000..a73e2147b --- /dev/null +++ b/src/test/java/calculator/CalculatorTest.java @@ -0,0 +1,79 @@ +package calculator; + +//요구 사항 +// 빈 문자열 또는 null 값을 입력할 경우 0을 반환해야 한다. +// 숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다. +// 숫자 두개를 컴마 구분자로 입력할 경우 두 숫자의 합을 반환한다. +// 구분자를 컴마 이외에 콜론을 사용할 수 있다. +// "\\"와 "\n" 문자 사이엥 커스텀 구분자를 지정할 수 있다. +// 음수를 전달할 경우 RuntimeException 예외가 발생 + + +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.NullSource; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class CalculatorTest { + + @ParameterizedTest + @DisplayName("빈 문자열 또는 null 값을 입력할 경우 0을 반환해야 한다.") + @NullSource + @ValueSource(strings = {"", " "}) + public void blackCheck(String input) throws Exception { + //given + int result = Calculator.splitAndSum(input); + //when + int expected = 0; + //then + assertThat(result).isEqualTo(expected); + } + + @ParameterizedTest + @DisplayName("숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다.") + @ValueSource(strings = {"1","2","10"}) + public void whenInputisSingleNumber(String input) throws Exception { + //given + int result = Calculator.splitAndSum(input); + //when + int expected = Integer.parseInt(input); + //then + assertThat(result).isEqualTo(expected); + } + + @ParameterizedTest + @DisplayName("숫자 두개를 컴마(,) 구분자로 입력할 경우 두 숫자의 합을 반환한다.") + @CsvSource(value = {"1,2:3","2,3:5","10,11:21"}, delimiter = ':') + public void whenInputisTwoNumbers(String input, String expected) throws Exception { + int result = Calculator.splitAndSum(input); + assertThat(result).isEqualTo(Integer.parseInt(expected)); + } + + @ParameterizedTest + @DisplayName("구분자로 콜론도 사용할 때") + @CsvSource(value = {"1:2,3;6","2,3:5;10"}, delimiter = ';') + public void 구분자로_콜론도_사용(String input, String expected) throws Exception { + int result = Calculator.splitAndSum(input); + assertThat(result).isEqualTo(Integer.parseInt(expected)); + } + + + @Test + @DisplayName("커스텀 구분자를 지정할 때") + public void 커스텀_구분자_사용() throws Exception { + int result = Calculator.splitAndSum("//;\n1;2;3"); + assertThat(result).isEqualTo(6); + } + + @ParameterizedTest + @DisplayName("음수_예외_처리") + @ValueSource(strings = {"-1,2,3", "1,2,-10"}) + public void 음수_예외_처리(String input) throws Exception { + assertThatThrownBy(() -> Calculator.splitAndSum(input)).isInstanceOf(IllegalArgumentException.class); + } +} From cf97347922ac3d55b23ee9d561810165e7ae1ab4 Mon Sep 17 00:00:00 2001 From: JhyunJung Date: Thu, 30 May 2024 23:44:34 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EC=9A=94=EA=B5=AC=20=EC=82=AC=ED=95=AD=20?= =?UTF-8?q?=EB=B6=84=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/README.md b/README.md index fa8f6c745..f547462b5 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,83 @@ docker compose -p kitchenpos up -d | | | | ## 모델링 + +'private static final Pattern pattern = Pattern.compile(CALCULATOR_REGEX);' + +## 요구 사항 + +- 간단한 포켓몬 게임을 구현한다. +- 메뉴 + - [ ] 메뉴 등록이 가능하다. + - [ ] 이름, 가격, 노출여부는, 메뉴그룹의 값은 필수이다. + - [ ] 가격이 없거나 0보다 작아서는 안된다. + - [ ] 메뉴 상품은 등록된 상품으로만 가능하다. + - [ ] 메뉴 상품은 수량은 0보다 작아서는 안된다. + - [ ] 메뉴의 가격이 메뉴 상품의 가격의 합보다 커서는 안된다. + - [ ] 메뉴 이름에 비속어가 들어가서는 안된다. + - [ ] 메뉴 가격은 변경 할 수 있다. + - [ ] 변경될 가격은 0보다 작아서는 안된다. + - [ ] 변경될 가격은 메뉴상품의 합보다 커서는 안된다. +- 메뉴그룹 + - [ ] 메뉴그룹 등록이 가능하다. + - [ ] 이름은 필수이다. + - [ ] 모든 메뉴 그룹을 조회 할 수 있다. +- 상품 + - [ ] 상품 등록이 가능하다. + - [ ] 가격은 팔수이고, 0보다 작아서는 안된다. + - [ ] 이름은 필수이고, 비속어가 들어가서는 안된다. + - [ ] 가격 변경이 가능하다. + - [ ] 변경될 가격은 필수이고 0보다 작아서는 안된다. + - [ ] 상품이 들어간 메뉴의 가격이 변경될 상품의 가격 총합보다 커지게 되면 해당 메뉴를 노출시키지 않는다. +- 주문 + - [ ] 주문 등록이 가능하다. + - [ ] 주문 유형은 필수이다. + -[ ] 배달, 포장, 매장식사가 있다. + - [ ] 주문 항목은 필수이다. + - [ ] 주문 유형이 식사가 아닌경우에는, 수량이 0보다 커야만 한다. + - [ ] 주문 항목에 해당하는 메뉴가 필수로 있어야 한다. + - [ ] 메뉴가 노출여부를 확인해야 한다. + - [ ] 메뉴의 가격과 주문 항목의 가격이 일치해야 한다. + - [ ] 위 조건들을 만족할 시 주문 항목 리스트에 추가한다. + - [ ] 새로운 주문을 생성시킨다. + - [ ] 주문 상태는 Waiting이어야 한다. + - [ ] 주문 받는 시간은 실시간으로 한다. + - [ ] 앞에서 만들어진 주문 항목들을 가진다. + - [ ] 주문 타입인 배달인 경우 + - [ ] 배달 주소를 필수로 한다. + - [ ] 주문 타입이 매장식사인 경우 + - [ ] 현재 사용 가능 테이블이 반드시 있어야 한다. + - [ ] 사용 가능 테이블이 있다면 해당 테이블에 배정한다. + - [ ] 주문 취소가 가능하다. + - [ ] 주문 승인이 가능하다. + - [ ] 주문 상태가 Waiting이어야 한다. + - [ ] 주문 타입이 배달인 경우, 주문 항목의 가격과 수량을 곱한 값을 합하여 배달 요청을 한다. + - [ ] 주문 상태는 ACCEPTED가 된다. + - [ ] 주문 서빙이 가능하다. + - [ ] 주문 상태는 ACCEPTED이어야 한다. + - [ ] 주문 상태가 SERVED가 된다. + - [ ] 주문 배달 시작 처리를 할 수 있다. + - [ ] 주문 상태는 DELIVERY이어야 한다. + - [ ] 주문 상태가 SERVED이어야 한다. + - [ ] 주문 상태가 DELIVERING로 변경된다. + - [ ] 주문 배달 완료 처리를 할 수 있다. + - [ ] 주문 상태가 DELIVERING이어야 한다. + - [ ] 주문 상태가 DELIVERED로 변경된다. + - [ ] 주문 완료 처리를 할 수 있다. + - [ ] 주문 상태가 SERVED이어야 한다. + - [ ] 주문 전체 조회를 할 수 있다. +- 주문테이블 + - [ ] 주문 테이블 등록이 가능하다. + - [ ] 이름은 필수이다. + - [ ] 손님 수는 0을 기본 값으로 한다. + - [ ] 비어있는 상태로 만든다. + - [ ] 주문 테이블 배치가 가능하다. + - [ ] 해당 주문 테이블을 배치 상태로 바꾼다. + - [ ] 주문 테이블 정리가 가능하다. + - [ ] 해당 주문 테이블을 비어있는 상태로 바꾼다. + - [ ] 손님 수는 0명이 된다. + - [ ] 손님 인원 수 변경이 가능하다. + - [ ] 손님 수는 0보다 작아서는 안된다. + - [ ] 주문 테이블이 비어있는 상태면 안된다. + - [ ] 해당 테이블의 손님수를 변경한다. + - [ ] 주문 테이블 전체 조회가 가능하다. \ No newline at end of file From 4e9e479340d7d16622779174a3d58151155f5e97 Mon Sep 17 00:00:00 2001 From: JhyunJung Date: Sun, 2 Jun 2024 20:09:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EB=A6=AC=EB=B7=B0=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f547462b5..63f74dd35 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ docker compose -p kitchenpos up -d - [ ] 모든 메뉴 그룹을 조회 할 수 있다. - 상품 - [ ] 상품 등록이 가능하다. - - [ ] 가격은 팔수이고, 0보다 작아서는 안된다. + - [ ] 가격은 필수이고, 0보다 작아서는 안된다. - [ ] 이름은 필수이고, 비속어가 들어가서는 안된다. - [ ] 가격 변경이 가능하다. - [ ] 변경될 가격은 필수이고 0보다 작아서는 안된다.