Skip to content

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

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Empty file removed src/main/java/.gitkeep
Empty file.
56 changes: 56 additions & 0 deletions src/main/java/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import java.util.Arrays;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringAddCalculator {

private static final String DELIMITER = "[,:;]";
private static final String DELIMITER_REGEX = "//(.)\n(.*)";
private static final Pattern PATTERN = Pattern.compile(DELIMITER_REGEX);

public static int splitAndSum(String text) {
if (Objects.isNull(text) || text.isBlank()) {
return 0;
}
return sum(toInt(split(text)));
}

private static String[] split(String text) {
Matcher matcher = PATTERN.matcher(text);
if (!matcher.find()) {
return text.split(DELIMITER);
}

String customDelimiter = matcher.group(1);
String delimitedText = matcher.group(2);
return delimitedText.split(customDelimiter);
}
Copy link

Choose a reason for hiding this comment

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

์ด๋ฒˆ ๋ฏธ์…˜์—์„œ ์ค‘์š”ํ•œ ๊ธฐ๋Šฅ์„ ๋‹ด๋‹นํ•˜๋Š” split ๋ฉ”์„œ๋“œ์—์„œ ๋‘๊ฐ€์ง€์˜ ํ–‰์œ„๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๊ฒƒ ๊ฐ™์•„์š”!

  1. ๊ตฌ๋ถ„์ž๋ฅผ ์ฐพ๊ธฐ
  2. ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌ

split ์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋Š” 2๋ฒˆ์— ํ•ด๋‹นํ•˜๋Š” ํ–‰์œ„๋งŒ ์กด์žฌํ•˜๊ฒŒ ๋ฆฌํŒฉํ† ๋ง ํ•˜๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”? ๐Ÿค”

    private static String[] split(String text, String delimiter) {
        return text.split(delimiter);
    }

์œ„ ์ฒ˜๋Ÿผ ๋ฆฌํŒฉํ† ๋ง์ด ๋˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฉ”์„œ๋“œ๋กœ ๋‚˜๋ˆ„๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์„๊ฒƒ ๊ฐ™์•„์š” :)

  1. ๊ตฌ๋ถ„์ž๋ฅผ ์ฐพ๊ธฐ
  2. ๊ตฌ๋ถ„์ž๋กœ ๋ถ„ํ• ํ•  text ์ถ”์ถœ
String delimiter = findDelimiter(text);
String textToSplit = findTextToSplit(text);
sum(toInt(split(textToSplit, delimiter);

Copy link
Author

Choose a reason for hiding this comment

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

๋„ต ๋ง์”€ํ•ด์ฃผ์‹ ๋Œ€๋กœ 1, 2 ๊ฐ๊ฐ์˜ ํ–‰์œ„๋งŒ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!


private static int[] toInt(String[] texts) {
return Arrays.stream(texts)
.mapToInt(StringAddCalculator::toIntOrThrow)
.toArray();
}

private static int toIntOrThrow(String text) {
try {
int parsedValue = toInt(text);
if (parsedValue < 0) {
throw new NumberFormatException();
}
Copy link

Choose a reason for hiding this comment

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

NumberFormatException ์€ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๊ฒฝ์šฐ์ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ์™ธ์ž…๋‹ˆ๋‹ค :)
์Œ์ˆ˜๋Š” ์ˆซ์ž์ด๊ธฐ ๋•Œ๋ฌธ์— ์˜ฌ๋ฐ”๋ฅธ ์˜ˆ์™ธ๋Š” ์•„๋‹๊ฒƒ ๊ฐ™์•„์š”! ๐Ÿค”

์Œ์ˆ˜๋ฅผ ์ „๋‹ฌํ•  ๊ฒฝ์šฐ RuntimeException ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผ ํ•œ๋‹ค. (์˜ˆ : โ€œ-1,2,3โ€) ์ด ์š”๊ตฌ์‚ฌํ•ญ์„ ๋งŒ์กฑํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ”๋กœ ์˜ˆ์™ธ๋ฅผ ์ „ํŒŒํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์–ด๋–จ๊นŒ์š”?

Suggested change
if (parsedValue < 0) {
throw new NumberFormatException();
}
if (parsedValue < 0) {
throw new RuntimeException();
}

Copy link
Author

Choose a reason for hiding this comment

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

๋„ต ํ•ด๋‹น ์กฐ๊ฑด์— ์ ์ ˆํ•œ ์˜ˆ์™ธ์ธ์ง€์— ๋Œ€ํ•œ ํŒ๋‹จ์„ ๋†“์นœ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋ง์”€ํ•ด์ฃผ์‹ ๋Œ€๋กœ ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

return parsedValue;
} catch (NumberFormatException e) {
throw new RuntimeException();
}
Copy link

Choose a reason for hiding this comment

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

๋ณ„๋„ try/catch ๋ฌธ์„ ํ•  ํ•„์š” ์—†์„๊ฒƒ ๊ฐ™์•„์š”!

NumberFormatException ์€ unchecked exception ์ด๊ธฐ๋„ ํ•˜๊ตฌ์š”!

java.lang.Object
  โ””โ”€โ”€ java.lang.Throwable
        โ””โ”€โ”€ java.lang.Exception
              โ””โ”€โ”€ java.lang.RuntimeException
                    โ””โ”€โ”€ java.lang.IllegalArgumentException
                          โ””โ”€โ”€ java.lang.NumberFormatException

Copy link

Choose a reason for hiding this comment

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

์•„! @hyoojuu ๋‹˜

๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ์— ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’ ๋˜๋Š” ์Œ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ RuntimeException ์˜ˆ์™ธ๋ฅผ throwํ•œ๋‹ค.
์ด ์š”๊ตฌ์‚ฌํ•ญ์„ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด catch ํ•˜๊ณ  ๋‹ค์‹œ ๋ถ€๋ชจ ์˜ˆ์™ธ๋กœ ์ „ํŒŒํ•˜์‹ ๊ฑธ๊นŒ์š”? ๐Ÿ˜…

NumberFormatException ์€ Runtime ์„ ์ƒ์†ํ•˜๊ณ  ์žˆ๋Š” ์˜ˆ์™ธ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ์˜ˆ์™ธ๋ฅผ ์ „ํŒŒํ•  ํ•„์š”๊ฐ€ ์—†์„๊ฒƒ ๊ฐ™์•„๋ณด์—ฌ์„œ ํ”ผ๋“œ๋ฐฑ ๋‚จ๊ฒผ์Šต๋‹ˆ๋‹น ๐Ÿ˜„
์ด๋ฏธ ์•Œ๊ณ  ๊ณ„์…จ๋˜ ๋‚ด์šฉ์ด๋ผ๋ฉด ์Šค์œฝ~ ์ง€๋‚˜๊ฐ€์‹œ๋ฉด ๋ ๊ฒƒ ๊ฐ™์•„์š” ๐Ÿ™ƒ

Copy link
Author

Choose a reason for hiding this comment

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

์•— ๋„ต,, ์ƒ์†ํ•˜๊ณ  ์žˆ๋Š” ๊ฒƒ์€ ์•Œ๊ณ  ์žˆ์—ˆ๋Š”๋ฐ
Unchecked Exception์€ ๋ช…์‹œ์ ์œผ๋กœ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค๋Š” ์ ์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ๋ถ€์กฑํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

}

private static int toInt(String text) {
return Integer.parseInt(text);
}

private static int sum(int[] values) {
return Arrays.stream(values)
.sum();
}
}
6 changes: 2 additions & 4 deletions src/test/java/SetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ void setUp() {

@Test
void ์ปฌ๋ ‰์…˜_ํฌ๊ธฐ_๊ตฌํ•˜๊ธฐ() {
int size = this.numbers.size();

assertThat(size).isEqualTo(3);
assertThat(this.numbers).hasSize(3);
Copy link

Choose a reason for hiding this comment

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

๐Ÿ‘

}

@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void ์ˆซ์ž_ํฌํ•จ์—ฌ๋ถ€_ํ™•์ธ(int value) {
assertThat(this.numbers.contains(value)).isTrue();
assertThat(this.numbers).contains(value);
Copy link

Choose a reason for hiding this comment

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

๐Ÿ‘

}

@ParameterizedTest
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/StringAddCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class StringAddCalculatorTest {

@Test
public void splitAndSum_null_๋˜๋Š”_๋นˆ๋ฌธ์ž() {
int result = StringAddCalculator.splitAndSum(null);
assertThat(result).isEqualTo(0);

result = StringAddCalculator.splitAndSum("");
assertThat(result).isEqualTo(0);
}

@Test
public void splitAndSum_์ˆซ์žํ•˜๋‚˜() throws Exception {
int result = StringAddCalculator.splitAndSum("1");
assertThat(result).isEqualTo(1);
}

@Test
public void splitAndSum_์‰ผํ‘œ๊ตฌ๋ถ„์ž() throws Exception {
int result = StringAddCalculator.splitAndSum("1,2");
assertThat(result).isEqualTo(3);
}

@Test
public void splitAndSum_์‰ผํ‘œ_๋˜๋Š”_์ฝœ๋ก _๊ตฌ๋ถ„์ž() throws Exception {
int result = StringAddCalculator.splitAndSum("1,2:3");
assertThat(result).isEqualTo(6);
}

@Test
public void splitAndSum_custom_๊ตฌ๋ถ„์ž() throws Exception {
int result = StringAddCalculator.splitAndSum("//;\n1;2;3");
assertThat(result).isEqualTo(6);
}

@Test
public void splitAndSum_negative() throws Exception {
assertThatThrownBy(() -> StringAddCalculator.splitAndSum("-1,2,3"))
.isInstanceOf(RuntimeException.class);
}
}