-
Notifications
You must be signed in to change notification settings - Fork 476
Practice study #4
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
base: main
Are you sure you want to change the base?
Changes from all commits
b2fc40c
6117125
b370c4d
6f611ff
6f4d162
ccbc144
3291894
ecea326
71910d2
fe3c63b
0dc695c
2221336
369d5c2
6f0687c
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 |
|---|---|---|
| @@ -1 +1,82 @@ | ||
| # javascript-calculator-precourse | ||
| # javascript-calculator-precourse | ||
|
|
||
| **과제 진행 요구 사항** | ||
|
|
||
| - 미션은 [문자열 덧셈 계산기](https://github.com/woowacourse-precourse/javascript-calculator-7) 저장소를 포크하고 클론하는 것으로 시작한다. | ||
| - **기능을 구현하기 전 `README.md`에 구현할 기능 목록을 정리**해 추가한다. | ||
| - Git의 커밋 단위는 앞 단계에서 `README.md`에 정리한 기능 목록 단위로 추가한다. | ||
| - [AngularJS Git Commit Message Conventions](https://gist.github.com/stephenparish/9941e89d80e2bc58a153)을 참고해 커밋 메시지를 작성한다. | ||
| - 자세한 과제 진행 방법은 프리코스 진행 가이드 문서를 참고한다. | ||
|
|
||
| ## **기능 요구 사항 체크리스트** | ||
|
|
||
| 입력한 문자열에서 숫자를 추출하여 더하는 계산기를 구현한다. | ||
|
|
||
| 입력 | ||
|
|
||
| - [X] 사용자에게 쉼표(,) 또는 콜론(:)을 구분자로 가지는 문자열을 입력 받는다. | ||
| - 입력 메세지: ‘덧셈할 문자열을 입력해 주세요.’ | ||
|
|
||
| 내부 기능 | ||
| 1) 일반적인 경우 | ||
| - [X] 쉼표(,) 또는 콜론(:)으로 숫자를 구분한다. | ||
| - [X] 구분자를 기준으로 각 숫자들을 분리 한다. | ||
| - [X] 각 숫자의 합을 반환한다. | ||
| - 예: "" => 0, "1,2" => 3, "1,2,3" => 6, "1,2:3" => 6 | ||
|
|
||
|
|
||
| //;\n1;2;3 | ||
|
|
||
| 2) 커스텀 경우 | ||
| - 앞의 기본 구분자(쉼표, 콜론) 외에 커스텀 구분자를 지정할 수 있다. | ||
| - [X] 문자열 앞부분의 "//"와 "\n" 사이에 위치하는 문자를 커스텀 구분자로 인지한다. | ||
| - 예를 들어 "//;\n1;2;3"과 같이 값을 입력할 경우 커스텀 구분자는 세미콜론(;)이며, 결과 값은 6이 반환되어야 한다. | ||
| - [X] 구분자를 기준으로 각 숫자들을 분리 한다. | ||
| - [X] 각 숫자의 합을 반환한다. | ||
|
|
||
| 출력 | ||
| - [X] 결과를 반환한다. | ||
| - 출력 메시지: '결과 : ${6}' | ||
|
|
||
| 예외 처리 | ||
| - [X] | ||
| - 사용자가 잘못된 값을 입력할 경우 "[ERROR]"로 시작하는 메시지와 함께 `Error`를 발생시킨 후 애플리케이션은 종료되어야 한다. | ||
|
|
||
| ### **입출력 요구 사항** | ||
|
|
||
| ### **입력** | ||
|
|
||
| - 구분자와 양수로 구성된 문자열 | ||
|
|
||
| ### **출력** | ||
|
|
||
| - 덧셈 결과 | ||
|
|
||
| ``` | ||
| 결과 : 6 | ||
|
|
||
| ``` | ||
|
|
||
| ### **실행 결과 예시** | ||
|
|
||
| ``` | ||
| 덧셈할 문자열을 입력해 주세요. | ||
| 1,2:3 | ||
| 결과 : 6 | ||
|
|
||
| ``` | ||
|
|
||
| ## **프로그래밍 요구 사항** | ||
|
|
||
| - [X] Node.js 20.17.0 버전에서 실행 가능해야 한다. | ||
| - [X] 프로그램 실행의 시작점은 `App.js`의 `run()`이다. | ||
| - [X] `package.json` 파일은 변경할 수 없으며, **제공된 라이브러리와 스타일 라이브러리 이외의 외부 라이브러리는 사용하지 않는다.** | ||
| - 프로그램 종료 시 `process.exit()`를 호출하지 않는다. | ||
| - 프로그래밍 요구 사항에서 달리 명시하지 않는 한 파일, 패키지 등의 이름을 바꾸거나 이동하지 않는다. | ||
| - 자바스크립트 코드 컨벤션을 지키면서 프로그래밍한다. | ||
| - 기본적으로 [JavaScript Style Guide](https://github.com/woowacourse/woowacourse-docs/tree/main/styleguide/javascript)를 원칙으로 한다. | ||
|
|
||
| ### **라이브러리** | ||
|
|
||
| - `@woowacourse/mission-utils`에서 제공하는 `Console` API를 사용하여 구현해야 한다. | ||
| - 사용자의 값을 입력 및 출력하려면 `Console.readLineAsync()`와 `Console.print()`를 활용한다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| import { MainController } from './Controller/mainController.js'; | ||
|
|
||
| class App { | ||
| async run() {} | ||
| async run() { | ||
| const controller = new MainController(); | ||
| await controller.startProgram(); | ||
| } | ||
| } | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| export const DELIMITER = { | ||
| COMMA: ',', | ||
| COLON: ':', | ||
| }; | ||
|
|
||
| export const DELIMITER_MAKER = { | ||
| STRING_FIRST: '/', | ||
| STRING_SECOND: '/', | ||
| STRING_THIRD: '\\', | ||
| STRING_FOURTH: 'n', | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| const ERROR_PREFIX = '[ERROR]'; | ||
|
|
||
| export const ERROR_MESSAGES = { | ||
| WRONG_INPUT: `${ERROR_PREFIX} 잘못된 입력입니다.`, | ||
| WRONG_NORMAL_INPUT: `${ERROR_PREFIX} 컴마나 클론을 기준으로 숫자를 입력하지 않으셨습니다.`, | ||
| WRONG_CUSTOM_INPUT: `${ERROR_PREFIX}커스텀 숫자를 기준으로 숫자를 입력하지 않으셨습니다.`, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| export const INPUT_MESSAGES = { | ||
| STRING_TO_ADD: `덧셈할 문자열을 입력해 주세요.\n`, | ||
| }; | ||
|
|
||
| export const OUTPUT_MESSAGES = { | ||
| RESULT: `결과`, | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { InputHandler } from '../View/inputView.js'; | ||
| import { OutputHandler } from '../View/outputView.js'; | ||
|
|
||
| import { NumbersParserHandler } from '../Model/numbersParser.js'; | ||
| import { addNumbers } from '../Utils/calculatorToAdd.js'; | ||
| import { ValidationHandler } from '../Validation/validateStringToAdd.js'; | ||
|
|
||
| export class MainController { | ||
| constructor() { | ||
| this.input = new InputHandler(); | ||
| this.output = new OutputHandler(); | ||
| } | ||
|
|
||
| async startProgram() { | ||
| const stringToAdd = await this.input.getStringToAddInput(); | ||
| new ValidationHandler().checkValidation(stringToAdd); | ||
|
|
||
| const parsedNumbers = new NumbersParserHandler().parseNumbers(stringToAdd); | ||
| const resultAddedNumbers = addNumbers(parsedNumbers); | ||
|
|
||
| this.output.printResultOfSum(resultAddedNumbers); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { DELIMITER, DELIMITER_MAKER } from '../Constant/constant.js'; | ||
|
|
||
| export class NumbersParserHandler { | ||
| parseNumbers(inputString) { | ||
| this.inputString = inputString; | ||
| this.specifyCaseValue(); | ||
|
|
||
| if (this.NORMAL_CASE_CHECK) { | ||
| return this.parseNumbersForNormal(); | ||
| } | ||
|
|
||
| if (this.CUSTOM_CASE_CHECK) { | ||
| return this.parseNumbersForCustom(); | ||
| } | ||
| } | ||
|
|
||
| specifyCaseValue() { | ||
| this.NORMAL_CASE_CHECK = | ||
| this.inputString[1] === DELIMITER.COMMA || | ||
| this.inputString[1] === DELIMITER.COLON; | ||
|
|
||
| this.CUSTOM_CASE_CHECK = | ||
| this.inputString[0] === DELIMITER_MAKER.STRING_FIRST && | ||
| this.inputString[1] === DELIMITER_MAKER.STRING_SECOND && | ||
| this.inputString[3] === DELIMITER_MAKER.STRING_THIRD && | ||
| this.inputString[4] === DELIMITER_MAKER.STRING_FOURTH; | ||
|
Comment on lines
+23
to
+26
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. 구분자가 1자리가 아니거나 위치가 무작위일 경우에는 |
||
| } | ||
|
|
||
| // TODO: 리펙토링: 정규식 표현에도 상수화 처리하여 콤마, 클론 넣기 | ||
| parseNumbersForNormal() { | ||
| if ( | ||
| this.inputString[1] === DELIMITER.COMMA || | ||
| this.inputString[1] === DELIMITER.COLON | ||
| ) { | ||
| return this.inputString | ||
| .split(/,|:/) | ||
| .map((number) => number.trim()) | ||
| .map(Number); | ||
| } | ||
| } | ||
|
|
||
| parseNumbersForCustom() { | ||
| const onlyNumbersAndDelimiter = this.inputString.substr(5); | ||
| const customDelimiter = this.inputString[2]; | ||
|
|
||
| return onlyNumbersAndDelimiter | ||
| .split(customDelimiter) | ||
| .map((number) => number.trim()) | ||
| .map(Number); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export const addNumbers = (parsedNumbers) => { | ||
| let sum = 0; | ||
| parsedNumbers.forEach((number) => { | ||
| sum += number; | ||
| }); | ||
|
|
||
| return sum; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { DELIMITER, DELIMITER_MAKER } from '../Constant/constant.js'; | ||
| import { ERROR_MESSAGES } from '../Constant/errorMessages.js'; | ||
|
|
||
| export class ValidationHandler { | ||
| checkValidation(inputString) { | ||
| this.inputString = [...inputString]; | ||
|
|
||
| this.specifyCaseValue(); | ||
| this.checkBasicError(); | ||
| this.checkNormalOrCustom(); | ||
| } | ||
|
|
||
| specifyCaseValue() { | ||
| this.NORMAL_CASE_CHECK = | ||
| this.inputString[1] === DELIMITER.COMMA || | ||
| this.inputString[1] === DELIMITER.COLON; | ||
|
Comment on lines
+15
to
+16
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. 현재 코드를 보면 1,2;3 이 통과가 안 되어야 하는데 통과가 될 것 같아요! 🤔 |
||
|
|
||
| this.CUSTOM_CASE_CHECK = | ||
| this.inputString[0] === DELIMITER_MAKER.STRING_FIRST && | ||
| this.inputString[1] === DELIMITER_MAKER.STRING_SECOND && | ||
| this.inputString[3] === DELIMITER_MAKER.STRING_THIRD && | ||
| this.inputString[4] === DELIMITER_MAKER.STRING_FOURTH; | ||
| } | ||
|
|
||
| checkBasicError() { | ||
| if (!(this.NORMAL_CASE_CHECK || this.CUSTOM_CASE_CHECK)) { | ||
| throw new Error(ERROR_MESSAGES.WRONG_INPUT); | ||
| } | ||
| } | ||
|
|
||
| checkNormalOrCustom = () => { | ||
| if (this.NORMAL_CASE_CHECK) { | ||
| this.validateNormalCase(); | ||
| } | ||
|
|
||
| if (this.CUSTOM_CASE_CHECK) { | ||
| this.validateCustomCase(); | ||
| } | ||
| }; | ||
|
|
||
| // TODO: 리펙토링 - forEach를 사용해서 짝수 인덱스만 돌게하는 것 찾기 | ||
| validateNormalCase() { | ||
| const onlyNumberInArray = []; | ||
| for (let i = 0; i < this.inputString.length; i += 2) { | ||
| onlyNumberInArray.push(this.inputString[i]); | ||
| } | ||
|
|
||
| onlyNumberInArray.forEach((number) => { | ||
| if (Number.isNaN(Number(number))) { | ||
| throw new Error(ERROR_MESSAGES.WRONG_NORMAL_INPUT); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| validateCustomCase() { | ||
| const onlyNumberInArray = []; | ||
| for (let i = 5; i < this.inputString.length; i += 2) { | ||
| onlyNumberInArray.push(this.inputString[i]); | ||
| } | ||
|
|
||
| onlyNumberInArray.forEach((number) => { | ||
| if (Number.isNaN(Number(number))) { | ||
| throw new Error(ERROR_MESSAGES.WRONG_CUSTOM_INPUT); | ||
| } | ||
| }); | ||
|
Comment on lines
+56
to
+65
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. 이 부분이 위에 validateNormalCase()와 로직이 겹치는 부분이 많아서 다음과 같이 재사용 할 수도 있을 것 같아요! 🤔 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { Console } from '@woowacourse/mission-utils'; | ||
| import { INPUT_MESSAGES } from '../Constant/messages.js'; | ||
|
|
||
| // TODO: 공부 - return 뒤에 await 없어도 실행 가능한 이유 | ||
| export class InputHandler { | ||
| async getStringToAddInput() { | ||
| return Console.readLineAsync(INPUT_MESSAGES.STRING_TO_ADD); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { Console } from '@woowacourse/mission-utils'; | ||
| import { OUTPUT_MESSAGES } from '../Constant/messages.js'; | ||
|
|
||
| export class OutputHandler { | ||
| async printResultOfSum(resultAddedNumbers) { | ||
|
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. printResultOfSum 함수는 비동기를 처리하지 않아서 async가 안 붙어도 될 것 같아요! ㅎㅎ |
||
| const { RESULT } = OUTPUT_MESSAGES; | ||
|
|
||
| return Console.print(`${RESULT} : ${resultAddedNumbers}`); | ||
|
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. Console.print()는 단순히 console.log(message)라 반환하는 값이 없어서 return 문을 빼도 괜찮을 것 같아요! ㅎㅎ 👍 |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import App from "./App.js"; | ||
| import App from './App.js'; | ||
|
|
||
| const app = new App(); | ||
| await app.run(); |
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.
상수 파일들을 에러 메시지와 일반 메시지까지 꼼꼼하게 잘 나누신 것 같아요 👍
너무 잘 나눠서 가독성이 좋은 것 같습니다 👍