-
Notifications
You must be signed in to change notification settings - Fork 223
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
1단계 - 문자열 덧셈 계산기 #764
1단계 - 문자열 덧셈 계산기 #764
Changes from 9 commits
fc77bc2
1691d89
6084c23
cad2807
2f012a4
5083359
26dd278
a90a0a8
2c26197
4b07905
9fdde3f
92c4c36
807448f
7924a83
97fc530
e57738e
9fc8804
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 4 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
max_line_length = 120 | ||
tab_width = 4 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
## 1단계 요구사항 | ||
|
||
- [x] 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 전달하는 경우 구분자를 기준으로 분리한 각 숫자의 합을 반환 | ||
- (예: “” => 0, "1,2" => 3, "1,2,3" => 6, “1,2:3” => 6) | ||
|
||
- [x] 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. 커스텀 구분자는 문자열 앞부분의 “//”와 “\n” 사이에 위치하는 문자를 커스텀 구분자로 사용한다. | ||
- 예를 들어 “//;\n1;2;3”과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. | ||
|
||
- [x] 문자열 계산기에 숫자 이외의 값 또는 음수를 전달하는 경우 RuntimeException 예외를 throw 한다. | ||
|
||
### 힌트(= 추가 요구 사항) | ||
|
||
- [x] 빈 문자열 또는 null을 입력할 경우 0을 반환해야 한다. (예 : “” => 0, null => 0) | ||
|
||
- [x] 숫자 하나를 문자열로 입력할 경우 해당 숫자를 반환한다.(예 : “1”) | ||
|
||
- [x] 숫자 두개를 컴마(,) 구분자로 입력할 경우 두 숫자의 합을 반환한다. (예 : “1,2”) | ||
|
||
- [x] 구분자를 컴마(,) 이외에 콜론(:)을 사용할 수 있다. (예 : “1,2:3” => 6) | ||
|
||
- [x] "//"와 "\n" 문자 사이에 커스텀 구분자를 지정할 수 있다. (예 : “//;\n1;2;3” => 6 | ||
|
||
- [x] 음수를 전달할 경우 RuntimeException 예외가 발생해야 한다. (예 : “-1,2,3”) | ||
|
||
## 커밋 컨벤션 | ||
|
||
- 커밋 메시지 작성법은 [AngularJS Git Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) 을 | ||
원칙으로 작성한다. | ||
|
||
```markdown | ||
feat (feature) | ||
fix (bug fix) | ||
docs (documentation) | ||
style (formatting, missing semi colons, …) | ||
refactor | ||
test (when adding missing tests) | ||
chore (maintain) | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
name: kitchenpos | ||
services: | ||
db: | ||
image: mysql:8.0.30 | ||
platform: linux/x86_64 | ||
restart: always | ||
ports: | ||
- "33306:3306" | ||
environment: | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_DATABASE: kitchenpos | ||
MYSQL_USER: user | ||
MYSQL_PASSWORD: password | ||
TZ: Asia/Seoul | ||
volumes: | ||
- ./db/mysql/data:/var/lib/mysql | ||
- ./db/mysql/config:/etc/mysql/conf.d | ||
- ./db/mysql/init:/docker-entrypoint-initdb.d | ||
db: | ||
image: mysql:8.0.30 | ||
platform: linux/x86_64 | ||
restart: always | ||
ports: | ||
- "33306:3306" | ||
environment: | ||
MYSQL_ROOT_PASSWORD: root | ||
MYSQL_DATABASE: kitchenpos | ||
MYSQL_USER: user | ||
MYSQL_PASSWORD: password | ||
TZ: Asia/Seoul | ||
volumes: | ||
- ./db/mysql/data:/var/lib/mysql | ||
- ./db/mysql/config:/etc/mysql/conf.d | ||
- ./db/mysql/init:/docker-entrypoint-initdb.d |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ POST {{host}}/api/menu-groups | |
Content-Type: application/json | ||
|
||
{ | ||
"name": "추천메뉴" | ||
"name": "추천메뉴" | ||
} | ||
|
||
### | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"local": { | ||
"host": "localhost:8080" | ||
} | ||
"local": { | ||
"host": "localhost:8080" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package calculator; | ||
|
||
import calculator.exception.InvalidNumberFormatException; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사용하지않는 Import 문은 제거해도될것같아요 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반영했습니다!~ |
||
import calculator.exception.NegativeNumberException; | ||
|
||
public class StringAdditionCalculator { | ||
|
||
private String text; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클래스 변수에도 final을 붙여주는것은 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분 수정했습니다! |
||
|
||
public StringAdditionCalculator(final String text) { | ||
this.text = text; | ||
} | ||
|
||
public int add() { | ||
if (text == null || text.isEmpty()) { | ||
return 0; | ||
} | ||
|
||
String[] numbers = StringSplitter.split(text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 문자열 계산기가 과도한 책임을 가지고 있는 것처럼 보입니다. 객체지향적인 코드를 위해, 문자열 계산기가 처리하는 숫자를 객체로 추상화해보는 것은 어떨까요? 그렇다면 문자열 계산기가 가지고 있는 책임을 나눠가질 수 있을 것 같습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 그래서 숫자를 검증하고 정수를 변환하는 역할을 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
배열 대신 콜랙션을 사용해보는것은 어떨까요? 아니면 따로 배열을 사용하신 이유가 있을까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 코드에서 확장성을 고려하지 못한 것 같습니다. |
||
int sum = 0; | ||
|
||
for (String number : numbers) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 일급 콜랙션을 사용해보는것은 어떨까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵! 반영했습니다! |
||
if (!isNumeric(number)) { | ||
throw new InvalidNumberFormatException("Invalid input: Non-numeric value found: " + number); | ||
} | ||
|
||
int parsedNumber = Integer.parseInt(number); | ||
|
||
if (parsedNumber < 0) { | ||
throw new NegativeNumberException("Negative numbers are not allowed: " + number); | ||
} | ||
sum += parsedNumber; | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
private boolean isNumeric(final String text) { | ||
try { | ||
Integer.parseInt(text); | ||
return true; | ||
} catch (NumberFormatException e) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,24 @@ | ||||||||
package calculator; | ||||||||
|
||||||||
public class StringSplitter { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생성자의 접근 제한자를 private으로 설정하면 유틸리티 클래스가 인스턴스화되는 것을 방지할 수 있습니다.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 유틸리티 클래스를 명확히 표현하기 위해 접근 제한자 부분까지 신경써야 한다는 점을 잊고 있었네요 ! 꼼꼼한 피드백 감사드려요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아래와 같이 생성자를 private로 반영했어요 ! private StringSplitter() {
throw new UnsupportedOperationException("StringSplitter is a utility class and cannot be instantiated.");
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아하 제가 코드 확인을 못한 것 같습니다. 잘 반영해주셨네요 👍 |
||||||||
|
||||||||
private static final String DEFAULT_DELIMITERS = ",|:"; | ||||||||
public static final String CUSTOM_DELIMITER_PREFIX = "//"; | ||||||||
public static final String CUSTOM_DELIMITER_SUFFIX = "\n"; | ||||||||
|
||||||||
public static String[] split(final String inputText) { | ||||||||
|
||||||||
// check for custom delimiter | ||||||||
if (inputText.startsWith(CUSTOM_DELIMITER_PREFIX)) { | ||||||||
int customDelimiterEndIndex = inputText.indexOf(CUSTOM_DELIMITER_SUFFIX); | ||||||||
String customDelimiter = inputText.substring(CUSTOM_DELIMITER_PREFIX.length(), customDelimiterEndIndex); | ||||||||
String numberSection = inputText.substring(customDelimiterEndIndex + CUSTOM_DELIMITER_SUFFIX.length()); | ||||||||
return numberSection.split(customDelimiter); | ||||||||
} | ||||||||
|
||||||||
// default delimiters | ||||||||
return inputText.split(DEFAULT_DELIMITERS); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package calculator.exception; | ||
|
||
public class InvalidNumberFormatException extends RuntimeException { | ||
|
||
public InvalidNumberFormatException(final String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package calculator.exception; | ||
|
||
public class NegativeNumberException extends RuntimeException { | ||
public NegativeNumberException(final String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package racingcar; | ||
|
||
public class Car { | ||
|
||
private final String name; | ||
private int position; | ||
|
||
public Car(String name) { | ||
if (name.length() > 5) { | ||
throw new IllegalArgumentException(); | ||
} | ||
this.name = name; | ||
} | ||
|
||
public void move(final MovingStrategy moveingStrategy) { | ||
if (moveingStrategy.movable()) { | ||
position++; | ||
} | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package racingcar; | ||
|
||
@FunctionalInterface | ||
public interface MovingStrategy { | ||
boolean movable(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍