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
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit Hold shift + click to select a range
7a209bb
feat(racingcar):Car ์ƒ์„ฑ
Jan 21, 2025
66e2d15
feat(racingcar): move ๊ธฐ๋Šฅ ์ถ”๊ฐ€
Jan 21, 2025
958d190
feat(racingcar): ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
Jan 21, 2025
d21602b
docs : ์š”๊ตฌ์‚ฌํ•ญ ์ •์˜
Jan 25, 2025
6b98e7b
feat : ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ์— ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’ ๋˜๋Š” ์Œ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ RuntimeException ์˜ˆ์™ธ ์ƒ์„ฑ
Jan 25, 2025
03f9aa1
docs : ์š”๊ตฌ์‚ฌํ•ญ ์ˆ˜์ •
Jan 25, 2025
d8ed15c
refactor : StringCalculator add ํ•จ์ˆ˜ ๋ณ€๊ฒฝ
Jan 26, 2025
80c2853
feat : ๋นˆ ๋ฌธ์ž์—ด ๋˜๋Š” null์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜ & ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜
Jan 26, 2025
16da34d
feat : ์ˆซ์ž ๋‘๊ฐœ๋ฅผ ์‰ผํ‘œ(,) ,(:} ๊ตฌ๋ถ„์ž๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ๋‘ ์ˆซ์ž์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜
Jan 26, 2025
7a3eaa9
feat : //์™€ \\n ๋ฌธ์ž ์‚ฌ์ด์— ์ปค์Šคํ…€ ๊ตฌ๋ถ„์ž ์ถ”๊ฐ€
Jan 26, 2025
6112401
feat : ๋ฌธ์ž์—ด ํŒŒ์‹ฑ ํ•จ์ˆ˜ํ™”
Jan 26, 2025
620e32e
refactor : step1 ํ”ผ๋“œ๋ฐฑ ๋ฐ˜์˜ (ํด๋ž˜์Šค ์„ธ๋ถ„ํ™”,์–‘์ˆ˜ ๋ณด์žฅ ํด๋ž˜์Šค ์ƒ์„ฑ)
Jan 28, 2025
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
10 changes: 10 additions & 0 deletions src/main/java/calculator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## ๋ฌธ์ž์—ด ๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐโž•
### ์š”๊ตฌ์‚ฌํ•ญ
- [ ] ๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ์— ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’ ๋˜๋Š” ์Œ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ `RuntimeException` ์˜ˆ์™ธ๋ฅผ throw ํ•œ๋‹ค.
- [ ] ์‰ผํ‘œ(,) ๋˜๋Š” ์ฝœ๋ก (:)์„ ๊ตฌ๋ถ„์ž๋กœ ๊ฐ€์ง€๋Š” ๋ฌธ์ž์—ด์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ ๊ตฌ๋ถ„์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ถ„๋ฆฌํ•œ ๊ฐ ์ˆซ์ž์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜
- (์˜ˆ: โ€œโ€ => 0, "1,2" => 3, "1,2,3" => 6, โ€œ1,2:3โ€ => 6)
- [ ] "//"์™€ "\n" ๋ฌธ์ž ์‚ฌ์ด์— ์ปค์Šคํ…€ ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
- (์˜ˆ : โ€œ//;\n1;2;3โ€ => 6)
- [ ] ๋นˆ ๋ฌธ์ž์—ด ๋˜๋Š” null์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜
- [ ] ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.(์˜ˆ : โ€œ1โ€)

49 changes: 49 additions & 0 deletions src/main/java/calculator/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package calculator;

import java.util.Arrays;
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 static final String SEPARATOR = ",|:";
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

throw new RuntimeException();
}
}

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)์„ ํ†ตํ•ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š๊ณ  ์žฌ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋„๋ก ๋ณ€๊ฒฝํ•ด๋ณด๋Š”๊ฑด ์–ด๋–จ๊นŒ์š” ?

if (m.find()) {
String customDelimiter = m.group(1);
return m.group(2).split(Pattern.quote(customDelimiter));
} else {
return text.split(SEPARATOR);
}
}

private int sumOfNumbers(String[] numbers) {
return Arrays.stream(numbers)
.mapToInt(this::validate)
.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.

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

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

throw new RuntimeException("์–‘์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
}
return num;
} catch (NumberFormatException e) {
throw new RuntimeException("์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
}
}

}
25 changes: 25 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package racingcar;

import java.util.Random;

public class Car {
private final String name;
private int position;

public Car(final String name) {
if (name.length() > 5) {
throw new IllegalArgumentException();
}
this.name = name;
}

public int getPosition() {
return position;
}

public void move(final MovingStrategy movingStrategy) {
if (movingStrategy.movable()) {
position++;
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/racingcar/MovingStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package racingcar;

@FunctionalInterface
public interface MovingStrategy {
boolean movable();
}
70 changes: 70 additions & 0 deletions src/test/java/calculator/StringCalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package calculator;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
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.

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

private StringCalculator calculator;

@BeforeEach
void setUp() {
calculator = new StringCalculator();
}

@DisplayName(value = "๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ์— ์Œ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ RuntimeException ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.")
@Test
void negative() {
Assertions.assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> calculator.add("-1"));
}

@DisplayName(value = "๋ฌธ์ž์—ด ๊ณ„์‚ฐ๊ธฐ์— ์ˆซ์ž ์ด์™ธ์˜ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ RuntimeException ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.")
@Test
void wrong() {
Assertions.assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> calculator.add("text"));
}


@DisplayName(value = "๋นˆ ๋ฌธ์ž์—ด ๋˜๋Š” null ๊ฐ’์„ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ 0์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.")
@ParameterizedTest
@NullAndEmptySource
void emptyOrNull(final String text) {
Assertions.assertThat(calculator.add(text)).isZero();
}

@DisplayName(value = "์ˆซ์ž ํ•˜๋‚˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.")
@ParameterizedTest
@ValueSource(strings = {"1"})
void oneNumber(final String text) {
Assertions.assertThat(calculator.add(text)).isSameAs(Integer.parseInt(text));
}

@DisplayName(value = "์ˆซ์ž ๋‘๊ฐœ๋ฅผ ์‰ผํ‘œ(,) ๊ตฌ๋ถ„์ž๋กœ ์ž…๋ ฅํ•  ๊ฒฝ์šฐ ๋‘ ์ˆซ์ž์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.")
@ParameterizedTest
@ValueSource(strings = {"1,2"})
void twoNumbers(final String text) {
Assertions.assertThat(calculator.add(text)).isSameAs(3);
}

@DisplayName(value = "๊ตฌ๋ถ„์ž๋ฅผ ์‰ผํ‘œ(,) ์ด์™ธ์— ์ฝœ๋ก (:)์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.")
@ParameterizedTest
@ValueSource(strings = {"1,2:3"})
void colons(final String text) {
Assertions.assertThat(calculator.add(text)).isSameAs(6);
}

@DisplayName(value = "//์™€ \\n ๋ฌธ์ž ์‚ฌ์ด์— ์ปค์Šคํ…€ ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.")
@ParameterizedTest
@ValueSource(strings = {"//;\n1;2;3"})
void customDelimiter(final String text) {
Assertions.assertThat(calculator.add(text)).isSameAs(6);
}
}

34 changes: 34 additions & 0 deletions src/test/java/racingcar/CarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package racingcar;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.assertj.core.api.FactoryBasedNavigableListAssert.assertThat;

class CarTest {
@DisplayName(("์ž๋™์ฐจ์˜ ์ด๋ฆ„์€ 5๊ธ€์ž ์ดํ•˜๋‹ค"))
@Test
void constructor() {
Assertions.assertThatIllegalArgumentException()
.isThrownBy(() -> new Car("๋™ํ•ด๋ฌผ๊ณผ๋ฐฑ๋‘์‚ฐ์ด"));
}

@DisplayName(("์ž๋™์ฐจ๋Š” ์›€์ง์ธ๋‹ค"))
@Test
void move() {
final var car = new Car("sumin");
car.move(() -> true);
Assertions.assertThat(car.getPosition()).isEqualTo(1);
}

@DisplayName(("์ž๋™์ฐจ๋Š” ์ •์ง€ํ•œ๋‹ค"))
@Test
void stop() {
final var car = new Car("sumin");
car.move(() -> false);
Assertions.assertThat(car.getPosition()).isEqualTo(0);
}
}
8 changes: 8 additions & 0 deletions src/test/java/racingcar/ForwardStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package racingcar;

public class ForwardStrategy implements MovingStrategy {
@Override
public boolean movable() {
return false;
}
}
8 changes: 8 additions & 0 deletions src/test/java/racingcar/StopStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package racingcar;

public class StopStrategy implements MovingStrategy {
@Override
public boolean movable() {
return false;
}
}