Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ repositories {
}

dependencies {
testCompile('org.assertj:assertj-core:3.9.0')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
3 changes: 2 additions & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Sat Apr 13 15:17:25 KST 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10-all.zip
25 changes: 25 additions & 0 deletions src/main/java/LadderController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import domain.ladder.Ladder;
import domain.ladder.ladderFactory.RandomLadderFactory;
import dto.UserInputDto;
import view.InputView;
import view.OutputView;

public class LadderController {

private static InputView inputView = new InputView(System.in);
private static OutputView outputView;

public static void main(String[] args) {
UserInputDto inputDto = inputView.getInputDto();

RandomLadderFactory factory = new RandomLadderFactory(inputDto);

Ladder ladder = factory.createLadder();

outputView = new OutputView(ladder);

outputView.showLadder();
}


}
2 changes: 0 additions & 2 deletions src/main/java/TestObj.java

This file was deleted.

31 changes: 31 additions & 0 deletions src/main/java/domain/direction/Direction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package domain.direction;

import domain.ladder.Point;

import java.util.Random;

public enum Direction {

RIGHT,
DOWN,
LEFT;

public boolean isRight() {
return this == Direction.RIGHT;
}

public static Direction getRightOrDown() {
return new Random().nextBoolean() ? RIGHT : DOWN;
}

public static Direction createMidDirection(Point point) {
Comment thread
pci2676 marked this conversation as resolved.
if (point.isRight()) {
return Direction.LEFT;
}
return Direction.getRightOrDown();
}

public static Direction createLastDirection(Point point) {
return point.isRight() ? LEFT : DOWN;
}
}
35 changes: 35 additions & 0 deletions src/main/java/domain/ladder/Ladder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package domain.ladder;

import domain.direction.Direction;

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

public class Ladder {

private List<Pillar> pillars;
private int height;

public Ladder(List<Pillar> pillars, int height) {
this.pillars = pillars;
this.height = height;
}

public Direction getDirection(int x, int y) {
return pillars.stream()
.filter(pillar -> pillar.hasXY(x, y))
.map(pillar -> pillar.getPointDirection(x, y))
.findFirst()
.orElseThrow(RuntimeException::new);
}

public int getHeight() {
return this.height;
}

public List<String> getNames() {
return pillars.stream()
.map(Pillar::getUserName)
.collect(Collectors.toList());
}
}
39 changes: 39 additions & 0 deletions src/main/java/domain/ladder/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package domain.ladder;

public class Location {

private int x;
private int y;

public Location(int x, int y) {
validate(x, y);
this.x = x;
this.y = y;
}

private void validate(int x, int y) {
if (x < 0) {
throw new IllegalArgumentException("x error");
}
if (y < 0) {
throw new IllegalArgumentException("y error");
}
}

public Location(Location location) {
this.x = location.getX() + 1;
this.y = location.getY();
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public boolean isEqual(int x, int y) {
return this.x == x && this.y == y;
}
}
50 changes: 50 additions & 0 deletions src/main/java/domain/ladder/Pillar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package domain.ladder;

import domain.direction.Direction;

import java.util.List;

public class Pillar {

private User user;
private List<Point> points;

public Pillar(User user, List<Point> points) {
this.user = user;
this.points = points;
}

public String getUserName() {
return user.getName();
}

public Direction getPointDirection(int x, int y) {
return getPointByXY(x, y)
.getDirection();
}

private Point getPointByXY(int x, int y) {
return points.stream()
.filter(point -> point.isPresentXY(x, y))
.findFirst()
.orElseThrow(RuntimeException::new);
}

public boolean hasXY(int x, int y) {
return points.stream()
.filter(point -> point.isPresentXY(x, y))
.count() > 0;
}

public static Pillar createFirst(String name, int height) {
return new Pillar(new User(name), PointGenerator.createFirst(height));
}

public static Pillar createMiddle(String name, Pillar before) {
return new Pillar(new User(name), PointGenerator.createMiddle(before.points));
}

public static Pillar createLast(String name, Pillar before) {
return new Pillar(new User(name), PointGenerator.createLast(before.points));
}
}
30 changes: 30 additions & 0 deletions src/main/java/domain/ladder/Point.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package domain.ladder;

import domain.direction.Direction;

public class Point {
private Location location;
private Direction direction;

public Point(Location location, Direction direction) {
this.location = location;
this.direction = direction;
}

public Direction getDirection() {
return this.direction;
}

public boolean isRight() {
return this.direction == Direction.RIGHT;
}

public boolean isPresentXY(int x, int y) {
return this.location.isEqual(x, y);
}

public static Point createNextPoint(Point before, Direction direction) {
Location nextLocation = new Location(before.location);
return new Point(nextLocation, direction);
}
}
30 changes: 30 additions & 0 deletions src/main/java/domain/ladder/PointGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package domain.ladder;

import domain.direction.Direction;

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

public class PointGenerator {

public static List<Point> createFirst(int height) {
return IntStream.range(0, height)
.mapToObj(y -> new Location(0, y))
.map(location -> new Point(location, Direction.getRightOrDown()))
.collect(Collectors.toList());
}

public static List<Point> createMiddle(List<Point> before) {
return before.stream()
.map(point -> Point.createNextPoint(point, Direction.createMidDirection(point)))
.collect(Collectors.toList());
}

public static List<Point> createLast(List<Point> points) {
return points.stream()
.map(point -> Point.createNextPoint(point, Direction.createLastDirection(point)))
.collect(Collectors.toList());
}

}
14 changes: 14 additions & 0 deletions src/main/java/domain/ladder/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package domain.ladder;

public class User {

private String name;

public User(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
77 changes: 77 additions & 0 deletions src/main/java/domain/ladder/ladderFactory/FixedLadderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package domain.ladder.ladderFactory;

import domain.direction.Direction;
import domain.ladder.*;
import dto.UserInputDto;

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

public class FixedLadderFactory implements LadderFactory {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이쪽 로직이 너무 몰려있고 복잡해서 의도 파악하기가 힘들어 한번설명들어야 알수 있을거같아


private List<String> names;
private int height;

public FixedLadderFactory(UserInputDto inputDto) {
this.names = inputDto.getNames();
this.height = inputDto.getHeight();
}

public List<Pillar> createPillars() {
List<Pillar> pillars = new ArrayList<>();
pillars.add(createFixedFirstPillar());
addMid(pillars);
pillars.add(Pillar.createLast(names.get(getLastIndex()), pillars.get(getLastIndex() - 1)));
return pillars;
}

public Pillar createFixedFirstPillar() {
List<Location> locations = getLocations();
List<Point> points = new ArrayList<>();
points.addAll(getRightPoints(locations));
points.addAll(getDownPoints(locations));
points.addAll(getLeftPoints(locations));
return new Pillar(new User(names.get(0)), points);
}

private List<Location> getLocations() {
return IntStream.range(0, height)
.mapToObj(y -> new Location(0, y))
.collect(Collectors.toList());
}

private List<Point> getRightPoints(List<Location> locations) {
return IntStream.range(0, height / 3)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

이건 왜하는거야?? 의도를모르겠어

.mapToObj(idx -> new Point(locations.get(idx), Direction.RIGHT))
.collect(Collectors.toList());
}

private List<Point> getDownPoints(List<Location> locations) {
return IntStream.range(height / 3, (height / 3) * 2)
.mapToObj(idx -> new Point(locations.get(idx), Direction.DOWN))
.collect(Collectors.toList());
}

private List<Point> getLeftPoints(List<Location> locations) {
return IntStream.range((height / 3) * 2, height)
.mapToObj(idx -> new Point(locations.get(idx), Direction.LEFT))
.collect(Collectors.toList());
}

public void addMid(List<Pillar> pillars) {
for (int i = 1; i < names.size() - 1; i++) {
Pillar pillar = Pillar.createMiddle(names.get(i), pillars.get(i - 1));
pillars.add(pillar);
}
}

private int getLastIndex() {
return names.size() - 1;
}

public Ladder createLadder() {
return new Ladder(createPillars(), height);
}
}
9 changes: 9 additions & 0 deletions src/main/java/domain/ladder/ladderFactory/LadderFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain.ladder.ladderFactory;

import domain.ladder.Pillar;

import java.util.List;

public interface LadderFactory {
List<Pillar> createPillars();
}
Loading