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

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

Merged
merged 3 commits into from
Mar 14, 2025
Merged
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
Empty file removed src/main/java/.gitkeep
Empty file.
64 changes: 64 additions & 0 deletions src/main/java/StringAddCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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;
}

String delimiter = findDelimiter(text);
String textToSplit = findTextToSplit(text);
return sum(toInts(split(textToSplit, delimiter)));
Comment on lines +17 to +19
Copy link

Choose a reason for hiding this comment

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

๐Ÿ’ฏ

}

private static String findDelimiter(String text) {
Matcher matcher = PATTERN.matcher(text);
if (!matcher.find()) {
return DELIMITER;
}
return matcher.group(1);
}

private static String findTextToSplit(String text) {
Matcher matcher = PATTERN.matcher(text);
if (!matcher.find()) {
return text;
}
return matcher.group(2);
}

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

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

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

private static void validate(int value) {
Copy link

Choose a reason for hiding this comment

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

๐Ÿ‘

if (value < 0) {
throw new RuntimeException();
}
}

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);
}
}