-
Notifications
You must be signed in to change notification settings - Fork 223
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
Changes from 11 commits
7a209bb
66e2d15
958d190
d21602b
6b98e7b
03f9aa1
d8ed15c
80c2853
16da34d
7a3eaa9
6112401
620e32e
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 |
---|---|---|
@@ -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โ) | ||
|
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 { | ||||||||||
private static final String SEPARATOR = ",|:"; | ||||||||||
private static final String DELIMITER = "//(.)\n(.*)"; | ||||||||||
|
||||||||||
public int add(final String text) { | ||||||||||
if (text == null || text.isEmpty()) return 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.
Suggested change
https://google.github.io/styleguide/javaguide.html#s4.1.1-braces-always-used ๊ตฌ๊ธ ์คํ์ผ ๊ฐ์ด๋๋ฅผ ์ฐธ๊ณ ํ์ฌ ์ค๊ดํธ๋ฅผ ์๋ตํ์ง ์๊ณ ์์ฑํด๋ณด์ธ์ ๐ |
||||||||||
try { | ||||||||||
String[] numbers = parse(text); | ||||||||||
return sumOfNumbers(numbers); | ||||||||||
} catch (Exception e) { | ||||||||||
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. ์ต์์ Exception์ catch ํ๋ค๋ฉด ์ด๋ค ๋ฌธ์ ์ ์ด ์์๊น์ ? 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. ๋ค ๋ง์ต๋๋ค ๐ |
||||||||||
throw new RuntimeException(); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
private String[] parse(String text) { | ||||||||||
Matcher m = Pattern.compile(DELIMITER).matcher(text); | ||||||||||
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. ๋งค๋ฒ |
||||||||||
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) { | ||||||||||
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. ์ด๋ฒ ๊ณผ์ ์ ์งํํ๋ฉฐ ์ต๋ํ ์์ ๋จ์์ ํด๋์ค๋ฅผ ๋์ถํด๋ณด๋๊ฒ์ ๊ถ์ฅ๋๋ฆฝ๋๋ค. |
||||||||||
try { | ||||||||||
int num = Integer.parseInt(number); | ||||||||||
if (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. ๋งค์ง๋๋ฒ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๋ ์์๋ก ์ ์ํ์ฌ ๊ฐ๋
์ฑ์ ๋ํ๋ณด์ธ์ ๐ |
||||||||||
throw new RuntimeException("์์ ์ ์๋ฅผ ์ ๋ ฅํ์ธ์."); | ||||||||||
} | ||||||||||
return num; | ||||||||||
} catch (NumberFormatException e) { | ||||||||||
throw new RuntimeException("์ซ์๋ฅผ ์ ๋ ฅํ์ธ์."); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
} |
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++; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package racingcar; | ||
|
||
@FunctionalInterface | ||
public interface MovingStrategy { | ||
boolean movable(); | ||
} |
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 { | ||
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. ์๋กญ๊ฒ ํด๋์ค๊ฐ ๋ง์์ง์ ๋ฐ๋ผ ํ
์คํธ ์ฝ๋๋ ์์ฑํด๋ณด์ธ์ ๐ |
||
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); | ||
} | ||
} | ||
|
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); | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
} | ||
} |
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.
๋ฌธ์์ด ๊ณ์ฐ๊ธฐ ๊ตฌํ ์ํด์ฃผ์ จ์ต๋๋ค ๐
ํ๊ฐ์ง ๊ฐ์ ์ฌํญ์ผ๋ก ํ์ฌ
StringCalculator
๊ฐ ๋๋ฌด ๋ง์ ์ญํ ์ ๋ด๋นํ๊ณ ์๋ค๊ณ ์๊ฐ๋ฉ๋๋ค.๋ฌธ์์ด ํ์ฑ์ ๋ด๋นํ๋ ํด๋์ค, ๊ณ์ฐ์ ๋ด๋นํ๋ ํด๋์ค ๋ฑ์ผ๋ก ์ญํ ์ ๋๋๋ ์ฐ์ต์ ํด๋ณด์ธ์ !
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.
์์ธํ ํด๋์ค ๋ถ๋ฆฌ ๋๋ฌด ์ํด์ฃผ์ จ์ต๋๋ค ๐