-
Notifications
You must be signed in to change notification settings - Fork 708
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
base: jimbaemon
Are you sure you want to change the base?
2단계 - 사다리(생성) #1601
Changes from all commits
650c60d
df26b5a
5042230
a08e9c3
ddf92f0
76e6b3a
769a9ce
581332a
49aa28c
294206f
38370f1
a43e229
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,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); | ||
} | ||
} |
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; | ||
} | ||
} |
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; | ||
|
||
public Line(final boolean hasLine) { | ||
this.hasLine = hasLine; | ||
} | ||
|
||
public static Line create(final Line prevLine) { | ||
if (prevLine == null || !prevLine.hasLine) { | ||
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 메서드로 뽑아서 이름을 부여한다면 코드를 읽기 더 좋을 것 같아요!! |
||
SecureRandom secureRandom = new SecureRandom(); | ||
return new Line(secureRandom.nextBoolean()); | ||
Comment on lines
+15
to
+16
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. 라인 생성 전략을 사용하지 않고 코드가 직접 대입되어 있는 것 같아요!! |
||
} | ||
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 " |"; | ||
} | ||
} |
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); | ||
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. PREV_INDEX가 0이면 해당 로직이 조금 이상할 것 같아요!! |
||
} | ||
|
||
public int size() { | ||
return lines.size(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return lines.stream() | ||
.map(Line::toString) | ||
.collect(Collectors.joining()); | ||
} | ||
} |
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
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. 상수와 변수 사이에는 공백을 두면 가독성을 높일 수 있어요!! |
||
|
||
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
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.
|
||
} |
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 { | ||
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. 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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package nextstep.ladder.strategy; | ||
|
||
public interface LineStrategy { | ||
boolean hasLine(); | ||
} |
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
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. SecureRandom 클래스는 처음 보는데 덕분에 좋은 정보를 알게 된 것 같아요!! 👍🏼 |
||
} | ||
} |
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(); | ||
} | ||
|
||
} |
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(); | ||
} | ||
} | ||
} |
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; | ||
} | ||
} | ||
} |
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.
해당 부분은 Wrapper 타입을 꼭 사용할 필요는 없을 것 같아요!!