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

๐Ÿš€ 1๋‹จ๊ณ„ - ๋ฌธ์ž์—ด ๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ #776

Merged
merged 12 commits into from
Jan 28, 2025

Conversation

sumiini
Copy link

@sumiini sumiini commented Jan 26, 2025

์•ˆ๋…•ํ•˜์„ธ์š”.
์š”๊ตฌ์‚ฌํ•ญ๋ณ„๋กœ ์ปค๋ฐ‹ํ•˜๋ ค ํ–ˆ์œผ๋‚˜, ๋ชจ๋“  ํ…Œ์ŠคํŠธ ์กฐ๊ฑด์— ๋งŒ์กฑ์‹œ์ผœ์•ผ ํ•œ๋‹ค๋Š” ์ƒ๊ฐ์— ๋ถ„๋ฆฌํ•˜์—ฌ ์ปค๋ฐ‹ํ•˜๋Š”๊ฒŒ ์–ด๋ ค์› ์Šต๋‹ˆ๋‹ค.

add ๋‚ด ๋งค๊ฐœ๋ณ€์ˆ˜ text๋Š” ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ํ•œ ๊ฐ’์ด๋ฏ€๋กœ final ์„ ์–ธ์ด ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋‚ด๋ถ€์˜ private ํ•จ์ˆ˜๋“ค ์ค‘ text๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๊ฒƒ๋“ค๋„ ๋ชจ๋‘ final์„ ์„ ์–ธํ•ด์•ผํ•˜๋Š”์ง€? ์•„๋‹ˆ๋ฉด, ์ด๋ฏธ add ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ final text๋กœ ๋“ค์–ด์™€์„œ ์ƒ๊ด€์—†๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

@sah3122
Copy link

sah3122 commented Jan 27, 2025

add ๋‚ด ๋งค๊ฐœ๋ณ€์ˆ˜ text๋Š” ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€ํ•œ ๊ฐ’์ด๋ฏ€๋กœ final ์„ ์–ธ์ด ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.
๋‚ด๋ถ€์˜ private ํ•จ์ˆ˜๋“ค ์ค‘ text๋ฅผ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋ฐ›๋Š” ๊ฒƒ๋“ค๋„ ๋ชจ๋‘ final์„ ์„ ์–ธํ•ด์•ผํ•˜๋Š”์ง€? ์•„๋‹ˆ๋ฉด, ์ด๋ฏธ add ๋งค๊ฐœ๋ณ€์ˆ˜์—์„œ final text๋กœ ๋“ค์–ด์™€์„œ ์ƒ๊ด€์—†๋Š”์ง€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค.

private ํ•จ์ˆ˜์— ํ•œ์ •๋˜์ง€ ์•Š๊ณ  ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ’์ด ๋ถˆ๋ณ€ํ•œ์ง€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ์ง€๋งŒ ํŒ๋‹จ ํ•ด๋ณด๋ฉด ๋ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ๋ถˆ๋ณ€ํ•˜๋‹ค๋ฉด final ์„ ์–ธ์„ ํ•˜๊ณ , ์ง€์—ญ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋Š” ์ƒํ™ฉ์—์„œ๋„ ๋ถˆ๋ณ€ ๊ฐ’์„ ์œ ์ง€ํ•œ๋‹ค๋ฉด final ์„ ์–ธ์„ ํ•˜์—ฌ ๋ถˆ๋ณ€์„ฑ์„ ์œ ์ง€ ํ•˜๋Š”๊ฒƒ์ด ์˜ฌ๋ฐ”๋ฅด๋‹ค๊ณ  ์ƒ๊ฐํ•ด์š” ๐Ÿ˜„

Copy link

@sah3122 sah3122 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
๋‹ค์Œ ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „ ๋ช‡๊ฐ€์ง€ ์ƒ๊ฐ๊ฑฐ๋ฆฌ๋ฅผ ๋‚จ๊ฒจ๋‘์—ˆ์œผ๋‹ˆ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ ค์š” ๐Ÿ˜„

private static final String DELIMITER = "//(.)\n(.*)";

public int add(final String text) {
if (text == null || text.isEmpty()) return 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (text == null || text.isEmpty()) return 0;
if (text == null || text.isEmpty()) {
return 0;
}

https://google.github.io/styleguide/javaguide.html#s4.1.1-braces-always-used

๊ตฌ๊ธ€ ์Šคํƒ€์ผ ๊ฐ€์ด๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ค‘๊ด„ํ˜ธ๋ฅผ ์ƒ๋žตํ•˜์ง€ ์•Š๊ณ  ์ž‘์„ฑํ•ด๋ณด์„ธ์š” ๐Ÿ˜„

try {
String[] numbers = parse(text);
return sumOfNumbers(numbers);
} catch (Exception e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ตœ์ƒ์œ„ Exception์„ catch ํ•œ๋‹ค๋ฉด ์–ด๋–ค ๋ฌธ์ œ์ ์ด ์žˆ์„๊นŒ์š” ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–ด๋–ค ์˜ˆ์™ธ์ธ์ง€ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋„ค ๋งž์Šต๋‹ˆ๋‹ค ๐Ÿ˜„
์–ด๋–ค ์˜ˆ์™ธ์ธ์ง€ ์•Œ์ˆ˜ ์—†์„ ๋ฟ๋”๋ผ ํ•˜์œ„ Exception์„ ๋ฌด์‹œํ•ด๋ฒ„๋ฆฌ๋Š” ์ƒํ™ฉ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์˜ˆ์™ธ ์ฒ˜๋ฆฌ์— ๊ด€๋ จํ•ด์„œ ์•„๋ž˜ ๋ฌธ์„œ๊ฐ€ ๋„์›€์ด ๋˜์‹ค๊ฑฐ์—์š” ๐Ÿ˜„
https://jojoldu.tistory.com/734

}

private String[] parse(String text) {
Matcher m = Pattern.compile(DELIMITER).matcher(text);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งค๋ฒˆ Pattern.compile(DELIMITER)์„ ํ†ตํ•ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ณ  ์žฌ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š” ?

.sum();
}

private int validate(String number) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด๋ฒˆ ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๋ฉฐ ์ตœ๋Œ€ํ•œ ์ž‘์€ ๋‹จ์œ„์˜ ํด๋ž˜์Šค๋ฅผ ๋„์ถœํ•ด๋ณด๋Š”๊ฒƒ์„ ๊ถŒ์žฅ๋“œ๋ฆฝ๋‹ˆ๋‹ค.
์–‘์ˆ˜๋ฅผ ๋ณด์žฅํ• ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์„ธ์š” ๐Ÿ˜„

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringCalculator {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ ๊ตฌํ˜„ ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
ํ•œ๊ฐ€์ง€ ๊ฐœ์„ ์‚ฌํ•ญ์œผ๋กœ ํ˜„์žฌ StringCalculator ๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์€ ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ์ƒ๊ฐ๋ฉ๋‹ˆ๋‹ค.
๋ฌธ์ž์—ด ํŒŒ์‹ฑ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค, ๊ณ„์‚ฐ์„ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค ๋“ฑ์œผ๋กœ ์—ญํ• ์„ ๋‚˜๋ˆ„๋Š” ์—ฐ์Šต์„ ํ•ด๋ณด์„ธ์š” !

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒ์„ธํ•œ ํด๋ž˜์Šค ๋ถ„๋ฆฌ ๋„ˆ๋ฌด ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘

private int validate(String number) {
try {
int num = Integer.parseInt(number);
if (num < 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋งค์ง๋„˜๋ฒ„๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๋Š” ์ƒ์ˆ˜๋กœ ์ •์˜ํ•˜์—ฌ ๊ฐ€๋…์„ฑ์„ ๋†’ํ˜€๋ณด์„ธ์š” ๐Ÿ˜„
https://www.slipp.net/questions/356

Copy link
Author

@sumiini sumiini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•ด์ฃผ์‹  ํ”ผ๋“œ๋ฐฑ ๋ฐ”ํƒ•์œผ๋กœ ๋ฆฌํŒฉํ† ๋งํ•˜์˜€์Šต๋‹ˆ๋‹ค !
์ƒ์„ธํžˆ ํ”ผ๋“œ๋ฐฑ ํ•ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜Š

1, ๋ฌธ์ž์—ด ํŒŒ์‹ฑ ํด๋ž˜์Šค, ๊ณ„์‚ฐ ๋‹ด๋‹น ํด๋ž˜์Šค ์—ญํ•  ๋ถ„์‚ฐ
2. ์–‘์ˆ˜ ๋ณด์žฅ ํด๋ž˜์Šค ์ƒ์„ฑ
3. ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์„ธ๋ถ„ํ™”
4. Pattern ์žฌ์‚ฌ์šฉ

์ƒˆํ•ด ๋ณต ๋งŽ์ด ๋ฐ›์œผ์„ธ์š” ๐Ÿค—

Copy link

@sah3122 sah3122 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘
์‚ฌ์†Œํ•œ ์ฝ”๋ฉ˜ํŠธ ๋ช‡๊ฐ€์ง€ ๋‚จ๊ฒจ๋‘์—ˆ๋Š”๋ฐ ํ™•์ธ ๋ถ€ํƒ ๋“œ๋ฆฌ๋ฉฐ ๋‹ค์Œ ๋ฏธ์…˜๋„ ํ™”์ดํŒ… ํ•ด์ฃผ์„ธ์š” !


}

class PositiveNumber {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์–‘์ˆ˜๋ฅผ ๋ณด์žฅํ•˜๋Š” ํด๋ž˜์Šค ์„ ์–ธ ๐Ÿ‘

Comment on lines +34 to +43
public int getValue() {
return value;
}

private PositiveNumber(int value) {
if (value < 0) {
throw new RuntimeException("์–‘์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
}
this.value = value;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๏ฟฝ์„ ์–ธ ์ˆœ์„œ๊ฐ€ ์ž˜๋ชป ๋˜์–ด ์žˆ๋Š”๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ˜„

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get๋ฉ”์„œ๋“œ๋ณด๋‹ค ์ƒ์„ฑ์ž๊ฐ€ ๋” ์šฐ์„ ์œผ๋กœ ์„ ์–ธ๋˜์–ด์•ผ ํ•˜๊ตฐ์š” !
์ฃผ์˜ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜Š

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringCalculator {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒ์„ธํ•œ ํด๋ž˜์Šค ๋ถ„๋ฆฌ ๋„ˆ๋ฌด ์ž˜ํ•ด์ฃผ์…จ์Šต๋‹ˆ๋‹ค ๐Ÿ‘

import org.junit.jupiter.params.provider.ValueSource;


class StringCalculatorTest {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒˆ๋กญ๊ฒŒ ํด๋ž˜์Šค๊ฐ€ ๋งŽ์•„์ง์— ๋”ฐ๋ผ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋„ ์ž‘์„ฑํ•ด๋ณด์„ธ์š” ๐Ÿ˜„
์ด๋ฒˆ ๊ณผ์ •์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ๋‹จ์œ„ ํ…Œ์ŠคํŠธ ์ž‘์„ฑ์— ๋Œ€ํ•œ ์—ฐ์Šต์„ ํ•ด๋ณด๋Š”๊ฒƒ์„ ๊ถŒ์žฅ ๋“œ๋ฆฝ๋‹ˆ๋‹ค !

@sah3122 sah3122 merged commit 0682104 into next-step:sumiini Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants