-
Notifications
You must be signed in to change notification settings - Fork 9
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
[문자열 덧셈 계산기] 이나영 미션 제출합니다. #572
base: main
Are you sure you want to change the base?
Changes from 1 commit
6f0da34
f2f9c42
d2b9264
fe7259c
249df5a
316cc8f
e1020e0
7dabd65
7abe52f
4ed98a0
f526e61
c2100f7
a36fc48
614215e
08968bb
3b9b3f9
a87d260
8068e41
ca88274
41788bd
bc4ca74
865940c
1560c40
62aec6b
ed58de9
ebb788a
acf275e
6975470
5578dc8
a32249e
fc27c21
7127a59
8db452d
540ce83
593ae60
a651bc2
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,63 +1,19 @@ | ||
import { getInput } from './getInput.js'; | ||
import { add } from './add.js'; | ||
import { printResult } from './printResult.js'; | ||
import { Console } from '@woowacourse/mission-utils'; | ||
|
||
class App { | ||
async run() { | ||
try { | ||
const input = await this.getInput(); | ||
const result = this.add(input); | ||
this.printResult(result); | ||
const input = await getInput(); | ||
const result = add(input); | ||
printResult(result); | ||
} catch (error) { | ||
Console.print(`${error.message}`); | ||
throw error; // 예외를 다시 던져서 테스트에서 잡히도록 처리 | ||
throw error; // 예외를 다시 던져서 테스트에서 잡히도록 처리 | ||
} | ||
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. 간결한 run 함수 너무 깔끔하네요! 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. 아...! 제가 생각치 못했던 부분이네요 ! 깔끔함과 익숙함에 취해서 '이유'를 생각하지 않은 코딩이었던 것 같아요 ㅠㅠ 2주차 미션에서는 더 고민해서 App.js 구조를 작성해보겠습니다 !! |
||
} | ||
|
||
async getInput() { | ||
const input = await Console.readLineAsync('덧셈할 문자열을 입력해 주세요.\n'); | ||
return input; | ||
} | ||
|
||
add(input) { | ||
if (input === "") { | ||
return 0; | ||
} | ||
|
||
let delimiter = /[,|:]/; // 기본 구분자 설정 | ||
|
||
if (input.startsWith("//")) { | ||
const parts = input.split("\\n"); | ||
|
||
// 커스텀 구분자만 입력하거나 숫자 미입력의 경우 대응 | ||
if (parts.length < 2 || parts[1].trim() === "") { | ||
throw new Error('[ERROR] 잘못된 입력 형식입니다.'); | ||
} | ||
|
||
// 커스텀 구분자가 /, \, \n인 경우 구분자로 설정되도록 대응 | ||
// 사용자가 커스텀 구분자와 기본 구분자를 혼용해도 연산이 이뤄지도록 수정 | ||
const customDelimiter = parts[0].slice(2).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
delimiter = RegExp(`[${customDelimiter},:]`); | ||
input = parts[1]; | ||
} | ||
|
||
const numbers = input | ||
.split(delimiter) | ||
.filter(num => num !== "") // 구분자가 중복되어도 연산되도록 필터링 | ||
.map(num => { | ||
const parsedNum = Number(num); | ||
if (isNaN(parsedNum)) { | ||
throw new Error('[ERROR] 숫자가 아닌 값이 포함되었습니다.'); | ||
} else if (parsedNum < 0) { | ||
throw new Error('[ERROR] 양수로만 이루어져야 합니다.'); | ||
} | ||
return parsedNum; | ||
}); | ||
|
||
return numbers.reduce((sum, num) => sum + num, 0); | ||
} | ||
|
||
printResult(result) { | ||
Console.print(`결과 : ${result}`); | ||
} | ||
} | ||
|
||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export function add(input) { | ||
if (input === "") { | ||
return 0; | ||
} | ||
|
||
let delimiter = /[,|:]/; // 기본 구분자 설정 | ||
|
||
if (input.startsWith("//")) { | ||
const parts = input.split("\\n"); | ||
|
||
if (parts.length < 2 || parts[1].trim() === "") { | ||
throw new Error('[ERROR] 잘못된 입력 형식입니다.'); | ||
} | ||
|
||
const customDelimiter = parts[0].slice(2).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
delimiter = RegExp(`[${customDelimiter},:]`); | ||
input = parts[1]; | ||
} | ||
|
||
const numbers = input | ||
.split(delimiter) | ||
.filter(num => num !== "") // 구분자가 중복되어도 연산되도록 필터링 | ||
.map(num => { | ||
const parsedNum = Number(num); | ||
if (isNaN(parsedNum)) { | ||
throw new Error('[ERROR] 숫자가 아닌 값이 포함되었습니다.'); | ||
} else if (parsedNum < 0) { | ||
throw new Error('[ERROR] 양수로만 이루어져야 합니다.'); | ||
} | ||
return parsedNum; | ||
}); | ||
|
||
return numbers.reduce((sum, num) => sum + num, 0); | ||
} | ||
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. 정말 좋은 지적이세요 !! 다른 분들의 코드를 봐도, 저처럼 하신 분들도 계시지만 객체지향적으로 코드를 작성하신 불들이 많으시더라고요. 파일을 둘러봐도, OOP 스럽게 쓰신 코드는 읽기 편하고 다른 케이스를 적용하기도 좋아보였어요. 다음 미션에서는 더 OOP 스러운 코드 작성을 시도하겠습니다 !! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Console } from '@woowacourse/mission-utils'; | ||
|
||
export async function getInput() { | ||
const input = await Console.readLineAsync('덧셈할 문자열을 입력해 주세요.\n'); | ||
return input; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Console } from '@woowacourse/mission-utils'; | ||
|
||
export function printResult(result) { | ||
Console.print(`결과 : ${result}`); | ||
} |
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.
저는 try-catch로 에러 핸들링하는 것을 생각 못했는데 보고 배워갑니다 ㅎㅎ
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.
제가 매 코드마다 try-catch를 재탕(?)하는 버릇이 있어서...ㅎㅎ 이 구조가 가장 보기좋다는 인식이 있어서 바로 적용할 수 있었어요 !! 칭찬해주셔서 너무 감사드립니다 🍀
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.
jaeyoung-kwon님 혹시 woowa 저장소 풀리퀘 주소 남겨주시면 제가 거기서 코드리뷰 해드릴 수 있을 것 같아요 !! 이 답글 보신다면 PR 번호나 주소 남겨주시면 맞리뷰 해드리구 싶습니다 😄