-
Notifications
You must be signed in to change notification settings - Fork 26
1주차 미션 / 서버 3조 이윤희 #28
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,48 @@ | ||
| public class Ladder { | ||
|
|
||
| private final int[][] rows; | ||
| private final int[][] positions; | ||
|
|
||
| public Ladder(int row, int numberOfPerson) { | ||
| rows = new int[row][numberOfPerson]; | ||
| public Ladder(int height, int numberOfPerson) { | ||
| positions = new int[height][numberOfPerson]; | ||
| } | ||
| public void drawLine(Position pos, int direction) { | ||
| if(direction == -1){ | ||
| drawLineToLeft(pos); | ||
| } | ||
| if(direction == 1){ | ||
| drawLineToRight(pos); | ||
| } | ||
| } | ||
|
Comment on lines
+8
to
+15
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. 방향을 지정할 수 있도록 기능을 구현한 점 좋은 것 같아요. 그런데 direction이 무조건 -1 또는 1 중 하나의 값을 가져야 제대로 선을 그을텐데, 그외의 값을 가지면 선을 긋지 못할 우려가 있네요. direction이 가지는 값을 한정하기 위해서 enum을 사용해볼 수 있을 것 같아요. |
||
| public void drawLineToLeft(Position pos) { | ||
| LeftLinePossible(pos); | ||
| positions[pos.getRow()][pos.getCol()] = -1; | ||
| positions[pos.getRow()][pos.getCol()-1] = 1; | ||
| } | ||
| public void drawLineToRight(Position pos) { | ||
| RightLinePossible(pos); | ||
| positions[pos.getRow()][pos.getCol()] = 1; | ||
| positions[pos.getRow()][pos.getCol()+1] = -1; | ||
| } | ||
| public void LeftLinePossible(Position pos) { | ||
| if(pos.getCol() <= 0 || pos.getCol() > (positions[0].length-1)){ | ||
| throw new IllegalArgumentException("Index Error"); | ||
| } | ||
| if(positions[pos.getRow()][pos.getCol()-1] != 0 || positions[pos.getRow()][pos.getCol()] != 0){ | ||
| throw new IllegalArgumentException("Over Max Length of Line Error"); | ||
| } | ||
| } | ||
| public void RightLinePossible(Position pos) { | ||
| if(pos.getCol() < 0 || pos.getCol() >= (positions[0].length-1)){ | ||
| throw new IllegalArgumentException("Index Error"); | ||
| } | ||
| if(positions[pos.getRow()][pos.getCol()+1] != 0 || positions[pos.getRow()][pos.getCol()] != 0){ | ||
| throw new IllegalArgumentException("Over Max Length of Line Error"); | ||
| } | ||
| } | ||
| public int run(int pos){ | ||
| for (int i = 0; i < positions.length; i++) { // height | ||
| pos += positions[i][pos]; | ||
| } | ||
| return pos; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| public class Position { | ||
|
|
||
| private int row, col; | ||
|
|
||
| public Position(int row, int col) { | ||
| this.row = row; | ||
| this.col = col; | ||
| } | ||
|
|
||
| public int getCol() { | ||
| return col; | ||
| } | ||
| public int getRow() { | ||
| return row; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,92 @@ | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class LadderTest { | ||
|
|
||
| @Test | ||
| void Ladder_init() { | ||
| int height = 5, numberOfPerson = 5; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
| assertThat(ladder).isNotNull(); | ||
| } | ||
|
Comment on lines
+8
to
+13
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. given-when-then 패턴대로 주석을 달아놓으면 테스트를 이해하기 편할 것 같아요 |
||
| @Test | ||
| void OnePerson(){ | ||
| int height = 5, numberOfPerson = 1; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
|
|
||
| int result = ladder.run(0); | ||
| assertThat(result).isEqualTo(0); | ||
| } | ||
| @Test | ||
| void TwoPerson(){ | ||
| int height = 5, numberOfPerson = 2; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
| ladder.drawLine(new Position(0, 0), 1); | ||
| ladder.drawLine(new Position(2, 1), -1); | ||
| ladder.drawLine(new Position(3, 1), -1); | ||
|
|
||
| int result = ladder.run(0); | ||
| assertThat(result).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void ThreePerson(){ | ||
| int height = 5, numberOfPerson = 3; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
| ladder.drawLine(new Position(0, 0), 1); | ||
| ladder.drawLine(new Position(1, 1), 1); | ||
| ladder.drawLine(new Position(2, 2), -1); | ||
| //ladder.drawLine(new Position(3, 1), -1); => 0 | ||
|
|
||
| int result = ladder.run(0); | ||
| assertThat(result).isEqualTo(1); | ||
| } | ||
| @Test | ||
| void FivePerson(){ | ||
| int height = 5, numberOfPerson = 5; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
| ladder.drawLine(new Position(0, 0), 1); | ||
| ladder.drawLine(new Position(1, 1), 1); | ||
| ladder.drawLine(new Position(2, 1), -1); | ||
| ladder.drawLine(new Position(2, 3), -1); | ||
| ladder.drawLine(new Position(3, 1), 1); | ||
| ladder.drawLine(new Position(3, 4), -1); | ||
| ladder.drawLine(new Position(4, 2), 1); | ||
| ladder.drawLine(new Position(4, 0), 1); | ||
|
|
||
| int result = ladder.run(3); // 2 | ||
| assertThat(result).isEqualTo(0); // 1 | ||
| } | ||
| @Test | ||
| void IndexExceptionCode(){ | ||
| // => Test Fail : Index Error | ||
| int height = 5, numberOfPerson = 3; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
| ladder.drawLine(new Position(0, 0), 1); | ||
| ladder.drawLine(new Position(1, 1), 1); | ||
| ladder.drawLine(new Position(2, 2), 1); | ||
|
|
||
| int result = ladder.run(0); | ||
| assertThat(result).isEqualTo(1); | ||
| } | ||
|
Comment on lines
+62
to
+73
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. 테스트코드는 모두 통과되어야 합니다. 예외가 의도대로 발생하는지를 체크하려면 assertThatThrownBy()를 이용해보세요 |
||
| @Test | ||
| void MaxLengthExceptionCode(){ | ||
| // => Test Fail : Over Max Length of Line Error | ||
| int height = 5, numberOfPerson = 5; | ||
| Ladder ladder = new Ladder(height, numberOfPerson); | ||
| ladder.drawLine(new Position(0, 0), 1); | ||
| ladder.drawLine(new Position(1, 1), 1); | ||
| ladder.drawLine(new Position(2, 1), -1); | ||
| ladder.drawLine(new Position(2, 2), -1); | ||
| ladder.drawLine(new Position(2, 3), -1); | ||
| ladder.drawLine(new Position(3, 1), 1); | ||
| ladder.drawLine(new Position(3, 4), -1); | ||
| ladder.drawLine(new Position(4, 2), 1); | ||
| ladder.drawLine(new Position(4, 0), 1); | ||
|
|
||
| int result = ladder.run(3); // 2 | ||
| assertThat(result).isEqualTo(0); // 1 | ||
| } | ||
|
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. 이 테스트에서도 마찬가지로 assertThatThrownBy()를 이용해보세요 |
||
| } | ||
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.
height, numberOfPerson에 음수 값이 들어가면 문제가 생길 수 있지 않을까요..? 어떻게 하면 이 문제를 객체지향적으로 해결할 수 있지 고민해봅시다.