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단계 - 사다리(생성) #1601

Open
wants to merge 12 commits into
base: jimbaemon
Choose a base branch
from
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,19 @@
* 모든 피드백을 완료하면 다음 단계를 도전하고 앞의 과정을 반복한다.

## 온라인 코드 리뷰 과정
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview)
* [텍스트와 이미지로 살펴보는 온라인 코드 리뷰 과정](https://github.com/nextstep-step/nextstep-docs/tree/master/codereview)

## 기능정의 2단계 - 사다리(생성)
* 참여자
* 참여자의 이름은 5자를 넘지 않는다.

* 라인
* 라인은 가로로 겹쳐지지 않는다.
* 이전 값을 조회하여 라인이 있다면 라인은 만들지 말고 없다면 랜덤으로 생성한다.

* 라인들
* 라인들은 인원수 -1 만큼 존재한다.

* 사다리
* 사다리의 높이만큼 사다리의 갯수를 생성한다.

25 changes: 25 additions & 0 deletions src/main/java/nextstep/ladder/controller/LadderGameController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package nextstep.ladder.controller;

import nextstep.ladder.model.Ladder;
import nextstep.ladder.model.Lines;
import nextstep.ladder.model.Player;
import nextstep.ladder.model.Players;
import nextstep.ladder.view.Input;
import nextstep.ladder.view.Output;

import java.util.Arrays;
import java.util.stream.Collectors;

public class LadderGameController {

public static void main(String[] args) {
String[] strings = Input.inputPlayers();
Players players = Arrays.stream(strings)
.map(Player::new)
.collect(Collectors.collectingAndThen(Collectors.toList(), Players::new));

Ladder ladder = new Ladder(Input.inputLadderHeights(), players.number());

Output.outputResult(ladder, players);
}
}
24 changes: 24 additions & 0 deletions src/main/java/nextstep/ladder/model/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nextstep.ladder.model;

import java.util.ArrayList;
import java.util.List;

public class Ladder {
private final List<Lines> lines;

public Ladder(final int numberOfFloors, final int numberOfPlayers) {
List<Lines> result = new ArrayList<>();
for (int i = 0; i < numberOfFloors; i++) {
result.add(new Lines(numberOfPlayers));
}
this.lines = result;
}

public int numberOfFloors() {
return lines.size();
}

public List<Lines> getLines() {
return lines;
}
}
45 changes: 45 additions & 0 deletions src/main/java/nextstep/ladder/model/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package nextstep.ladder.model;

import java.security.SecureRandom;
import java.util.Objects;

public class Line {
private final Boolean hasLine;
Copy link

Choose a reason for hiding this comment

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

해당 부분은 Wrapper 타입을 꼭 사용할 필요는 없을 것 같아요!!


public Line(final boolean hasLine) {
this.hasLine = hasLine;
}

public static Line create(final Line prevLine) {
if (prevLine == null || !prevLine.hasLine) {
Copy link

Choose a reason for hiding this comment

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

간단한 조건문이지만 private 메서드로 뽑아서 이름을 부여한다면 코드를 읽기 더 좋을 것 같아요!!

SecureRandom secureRandom = new SecureRandom();
return new Line(secureRandom.nextBoolean());
Comment on lines +15 to +16
Copy link

Choose a reason for hiding this comment

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

라인 생성 전략을 사용하지 않고 코드가 직접 대입되어 있는 것 같아요!!

}
return new Line(false);
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Line line = (Line)o;
return Objects.equals(hasLine, line.hasLine);
}

@Override
public int hashCode() {
return Objects.hash(hasLine);
}

@Override
public String toString() {
if (hasLine) {
return "-----|";
}
return " |";
}
}
38 changes: 38 additions & 0 deletions src/main/java/nextstep/ladder/model/Lines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package nextstep.ladder.model;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class Lines {
private final static int FIRST_INDEX = 0;
private final static int PREV_INDEX = 0;

private final List<Line> lines;

public Lines(final int countOfPlayer) {
List<Line> result = new ArrayList<>();
for (int i = 0; i < countOfPlayer - 1; i++) {
result.add(Line.create(getPrevLine(result, i)));
}
this.lines = result;
}

private Line getPrevLine(final List<Line> result, final int index) {
if (index == 0) {
return null;
}
return result.get(index - PREV_INDEX);
Copy link

Choose a reason for hiding this comment

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

PREV_INDEX가 0이면 해당 로직이 조금 이상할 것 같아요!!

}

public int size() {
return lines.size();
}

@Override
public String toString() {
return lines.stream()
.map(Line::toString)
.collect(Collectors.joining());
}
}
22 changes: 22 additions & 0 deletions src/main/java/nextstep/ladder/model/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nextstep.ladder.model;

public class Player {
private final static int MAX_NAME_LENGTH = 5;
private final String name;
Comment on lines +4 to +5
Copy link

Choose a reason for hiding this comment

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

상수와 변수 사이에는 공백을 두면 가독성을 높일 수 있어요!!


public Player(final String name) {
validate(name);
this.name = name;
}

private void validate(final String name) {
if (name.length() > MAX_NAME_LENGTH) {
throw new IllegalArgumentException("사용자의 이름은 5자를 넘을 수 없습니다");
}
}

@Override
public String toString() {
return String.format("%6s", name);
}
Comment on lines +18 to +21
Copy link

Choose a reason for hiding this comment

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

toString 메서드의 의미와 사용 용도에 대해서 한번 알아보면 좋을 것 같아요!! 현재는 출력을 위한 용도로 사용되고 있는데 toString의 올바른 용도에 맞게 사용되지 못한 것 같아요!! 아래 글을 참고해보시면 좋을 것 같습니다 👍🏼

https://hudi.blog/java-correct-purpose-of-tostring/

}
23 changes: 23 additions & 0 deletions src/main/java/nextstep/ladder/model/Players.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nextstep.ladder.model;

import java.util.List;
import java.util.stream.Collectors;

public class Players {
Copy link

Choose a reason for hiding this comment

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

toString을 올바르게 사용한다면 현재 Players 일급컬렉션은 큰 의미를 가지기가 어려울 것 같아요!!

private List<Player> players;

public Players(List<Player> players) {
this.players = players;
}

public int number() {
return players.size();
}

@Override
public String toString() {
return players.stream()
.map(Player::toString)
.collect(Collectors.joining());
}
}
5 changes: 5 additions & 0 deletions src/main/java/nextstep/ladder/strategy/LineStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package nextstep.ladder.strategy;

public interface LineStrategy {
boolean hasLine();
}
11 changes: 11 additions & 0 deletions src/main/java/nextstep/ladder/strategy/RandomLineStrategy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package nextstep.ladder.strategy;

import java.security.SecureRandom;

public class RandomLineStrategy implements LineStrategy {
@Override
public boolean hasLine() {
SecureRandom secureRandom = new SecureRandom();
return secureRandom.nextBoolean();
Comment on lines +8 to +9
Copy link

Choose a reason for hiding this comment

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

SecureRandom 클래스는 처음 보는데 덕분에 좋은 정보를 알게 된 것 같아요!! 👍🏼

}
}
31 changes: 31 additions & 0 deletions src/main/java/nextstep/ladder/view/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package nextstep.ladder.view;

import java.util.List;
import java.util.Scanner;

public class Input {
private final static Scanner SCANNER = new Scanner(System.in);
private final static String SEPARATOR = ",";

private Input() {
}

public static String[] inputPlayers() {
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
String players = nextLine();
System.out.println();
return players.split(SEPARATOR);
}

public static int inputLadderHeights() {
System.out.println("최대 사다리 높이는 몇 개인가요?");
String height = nextLine();
System.out.println();
return Integer.parseInt(height);
}

private static String nextLine() {
return SCANNER.nextLine();
}

}
23 changes: 23 additions & 0 deletions src/main/java/nextstep/ladder/view/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nextstep.ladder.view;

import nextstep.ladder.model.Ladder;
import nextstep.ladder.model.Line;
import nextstep.ladder.model.Lines;
import nextstep.ladder.model.Players;

import java.util.List;

public class Output {
private Output() {
}

public static void outputResult(Ladder ladder, Players players) {
System.out.println(players);
List<Lines> lines = ladder.getLines();
for (Lines line : lines) {
System.out.print(" |");
System.out.print(line);
System.out.println();
}
}
}
54 changes: 27 additions & 27 deletions src/main/java/nextstep/optional/Computer.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package nextstep.optional;

public class Computer {
private Soundcard soundcard;
private Soundcard soundcard;

public Computer(Soundcard soundcard) {
this.soundcard = soundcard;
}
public Computer(Soundcard soundcard) {
this.soundcard = soundcard;
}

public Soundcard getSoundcard() {
return soundcard;
}
public Soundcard getSoundcard() {
return soundcard;
}

public static class Soundcard {
private USB usb;
public static class Soundcard {
private USB usb;

public Soundcard(USB usb) {
super();
this.usb = usb;
}
public Soundcard(USB usb) {
super();
this.usb = usb;
}

public USB getUsb() {
return usb;
}
}
public USB getUsb() {
return usb;
}
}

public static class USB {
private String version;
public static class USB {
private String version;

public USB(String version) {
super();
this.version = version;
}
public USB(String version) {
super();
this.version = version;
}

public String getVersion() {
return this.version;
}
}
public String getVersion() {
return this.version;
}
}
}
34 changes: 17 additions & 17 deletions src/main/java/nextstep/optional/ComputerStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import nextstep.optional.Computer.USB;

public class ComputerStore {
public static final String UNKNOWN_VERSION = "UNKNOWN";
public static final String UNKNOWN_VERSION = "UNKNOWN";

public static String getVersion(Computer computer) {
String version = UNKNOWN_VERSION;
if (computer != null) {
Soundcard soundcard = computer.getSoundcard();
if (soundcard != null) {
USB usb = soundcard.getUsb();
if (usb != null) {
version = usb.getVersion();
}
}
}
return version;
}
public static String getVersion(Computer computer) {
String version = UNKNOWN_VERSION;
if (computer != null) {
Soundcard soundcard = computer.getSoundcard();
if (soundcard != null) {
USB usb = soundcard.getUsb();
if (usb != null) {
version = usb.getVersion();
}
}
}
return version;
}

public static String getVersionOptional(Computer computer) {
return null;
}
public static String getVersionOptional(Computer computer) {
return null;
}
}
Loading