Skip to content
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

[문자열 덧셈 계산기] 배현아 미션 제출합니다. #583

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

27 changes: 27 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
## 구현할 기능 목록

- [x] 문자 입력 기능
- 유저에게 문자 입력 받고 변수에 저장
- [x] 구분자 확인 후 문자열 분리 기능
- 아무것도 입력하지 않았을 시 0 반환
- 기본 구분자(쉼표, 콜론)로 문자열 분리
- 커스텀 구분자('//\\n')로 문자열 분리
- [x] 문자열을 숫자로 변환하여 합을 구하는 기능
- [x] 예외 처리

## 알고리즘

1. 입력
1.1 예외 처리
2. 구분자 확인 후 문자열 분리
2.1 쉼표, 콜론 / 커텀 구분자
3. 문자열을 숫자로 변환한 후 합 구하기
4. 출력

## 예외 처리

1. 0 반환
- 아무것도 입력하지 않았을 경우
- number 타입의 문자가 없을 경우
2. 다른 문자 입력시 Error 메시지 출력
- 커스텁 구분자, 기본 구분자(쉼표, 콜론) 외에 다른 기호 입력
49 changes: 48 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
import { MissionUtils } from "@woowacourse/mission-utils";

class App {
async run() {}
async run() {
const input = await this.getUserInput();
const splitedString = this.stringSplitDelimiter(input);
this.checkInputValidation(splitedString);
this.addNumbers(splitedString);
}

async getUserInput() {
const input = await MissionUtils.Console.readLineAsync("덧셈할 문자열을 입력해 주세요.\n");
return input;
}

checkInputValidation(splitedString) {
if (splitedString.some((num) => num < 0)) {
Console.print("[ERROR]");
throw new Error("[ERROR]");
}

if (splitedString.some((val) => isNaN(val))) {
Console.print("[ERROR]");
throw new Error("[ERROR]");
}
}

stringSplitDelimiter(input) {
let str = [];

// 커스텀 구분자 사용 시
if (inputString[0] === "/" && inputString[1] === "/") {
const index = inputString.indexOf("\\n");
const delimiter = inputString.substring(2, index);
const stringToSplit = inputString.substring(index + 2);
const regex = new RegExp(`[${delimiter}:,]`);
str = stringToSplit.split(regex).map(Number);
} else {
// 기본 구분자(쉼표, 콜론) 사용 시
str = inputString.split(/,|:/).map(Number);
}

return str;
}

sumNumbers(numbers) {
const sum = numbers.reduce((acc, cur) => acc + cur, 0);
Console.print("결과 : " + sum);
}
}

export default App;