diff --git a/src/main/java/ConsoleMain.java b/src/main/java/ConsoleMain.java new file mode 100644 index 0000000000..52517c9c42 --- /dev/null +++ b/src/main/java/ConsoleMain.java @@ -0,0 +1,14 @@ +import generator.AutoLadderGenerator; +import generator.LadderGenerator; +import model.Ladder; +import view.InputView; +import view.ResultView; + +public class ConsoleMain { + public static void main(String[] args) { + String participants = InputView.getParticipants(); + String maxHight = InputView.getMaxHight(); + Ladder ladder = new Ladder(participants, maxHight, new AutoLadderGenerator()); + ResultView.printResult(ladder.getParticipants(), ladder.getLines()); + } +} diff --git a/src/main/java/generator/AutoLadderGenerator.java b/src/main/java/generator/AutoLadderGenerator.java new file mode 100644 index 0000000000..52dc8a9905 --- /dev/null +++ b/src/main/java/generator/AutoLadderGenerator.java @@ -0,0 +1,24 @@ +package generator; + +import model.Line; +import model.Participant; +import model.Positive; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class AutoLadderGenerator implements LadderGenerator { + @Override + public List getLines(String height, int countOfPerson) { + ArrayList lines = new ArrayList(); + for (int i = 0; i < Positive.of(height).getNum(); i++) { + lines.add(Line.of(Positive.of(countOfPerson).getNum())); + } + + lines.forEach(Line::addAutoLines); + + return lines; + } +} diff --git a/src/main/java/generator/LadderGenerator.java b/src/main/java/generator/LadderGenerator.java new file mode 100644 index 0000000000..841eb14a66 --- /dev/null +++ b/src/main/java/generator/LadderGenerator.java @@ -0,0 +1,11 @@ +package generator; + +import model.Line; +import model.Participant; + +import java.util.ArrayList; +import java.util.List; + +public interface LadderGenerator { + public List getLines(String height, int countOfPerson); +} diff --git a/src/main/java/model/Ladder.java b/src/main/java/model/Ladder.java new file mode 100644 index 0000000000..2730d10dec --- /dev/null +++ b/src/main/java/model/Ladder.java @@ -0,0 +1,25 @@ +package model; + +import generator.LadderGenerator; + +import java.util.ArrayList; +import java.util.List; + +public class Ladder { + private List lines = new ArrayList(); + private List participants = new ArrayList(); + + + public Ladder(String names, String height, LadderGenerator ladderGenerator) { + participants = Participant.getParticipants(names); + lines = ladderGenerator.getLines(height, participants.size()); + } + + public List getLines() { + return lines; + } + + public List getParticipants() { + return participants; + } +} diff --git a/src/main/java/model/Line.java b/src/main/java/model/Line.java new file mode 100644 index 0000000000..939ec563ef --- /dev/null +++ b/src/main/java/model/Line.java @@ -0,0 +1,163 @@ +package model; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; +import java.util.stream.IntStream; + +public class Line { + private ArrayList points = new ArrayList<>(); + private int countOfPerson; + + private Line(int countOfPerson) { + this.countOfPerson = countOfPerson; + + for (int i = 0; i < countOfPerson; i++) { + addPoint(false); + } + } + + public static Line of(int countOfPerson, List indexs) { + Line line = new Line(countOfPerson); + line.addAutoLines(indexs); + + return line; + } + + public static Line of(int countOfPerson) { + return new Line(countOfPerson); + } + + public boolean hasLine(int index) { + if (index >= points.size()) { + return false; + } + + return points.get(index); + } + + /** + * 좌표 추가 + * + * @param b + */ + public void addPoint(boolean b) { + if (points.size() >= countOfPerson) { + throw new IllegalStateException("좌표의 최대값은 인원 수를 초과할 수 없습니다."); + } + + points.add(b); + } + + /** + * 선들 추가 + * + * @param indexs + */ + public void addAutoLines(List indexs) { + for (Positive idx : indexs) { + addLine(idx); + } + } + + /** + * 선 추가 + * + * @param index + */ + public void addLine(Integer index) { + addLine(Positive.of(index)); + } + + /** + * 선 만듦 + * + * @param index + */ + public void addLine(Positive index) { + if (!canAddLine(index)) { + throw new IllegalArgumentException("선 생성 불가"); + } + + points.set(index.getNum(), true); + } + + /** + * 왼쪽 좌표에 선이 있는지 여부 + * + * @param index + * @return + */ + public boolean hasLeftLine(Positive index) { + return hasLine(index.getNum() - 1); + } + + /** + * 오른쪽 좌표에 선이 있는지 여부 + * + * @param index + * @return + */ + public boolean hasRightPoint(Positive index) { + return hasLine(index.getNum() + 1); + } + + /** + * 해당 좌표에 선을 추가할 수 있는지 여부 + * + * @param index + * @return + */ + public boolean canAddLine(int index) { + return canAddLine(Positive.of(index)); + } + + /** + * 해당 좌표에 선을 추가할 수 있는지 여부 (양수) + * + * @param index + * @return + */ + public boolean canAddLine(Positive index) { + int num = index.getNum(); + if (num >= points.size() - 1) { + return false; + } + + if (hasRightPoint(index)) { + return false; + } + + if (num > 0 && hasLeftLine(index)) { + return false; + } + + return true; + } + + public List getPoints() { + return Collections.unmodifiableList(points); + } + + /** + * 자동으로 선 추가 + */ + public void addAutoLines() { + Random random = new Random(); + IntStream.rangeClosed(0, points.size()) + .filter(i -> isAddLine(random.nextBoolean(), i)) + .forEach(this::addLine); + } + + /** + * 선 추가할 건지 여부 + * + * @param isAdd + * @param i + * @return + */ + private boolean isAddLine(Boolean isAdd, int i) { + return canAddLine(i) && isAdd; + } +} diff --git a/src/main/java/model/Participant.java b/src/main/java/model/Participant.java new file mode 100644 index 0000000000..b1ef2825d9 --- /dev/null +++ b/src/main/java/model/Participant.java @@ -0,0 +1,37 @@ +package model; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Participant { + private static final String COMMA = ","; + private static final int LIMIT = 5; + private String name; + + public Participant(String name) { + if (name.length() > LIMIT) { + throw new IllegalArgumentException(); + } + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public static List getParticipants(String names) { + return Arrays.stream(split(names)) + .map(Participant::new) + .collect(Collectors.toList()); + } + + public static String[] split(String participants) { + + return participants.split(COMMA); + } +} diff --git a/src/main/java/model/Positive.java b/src/main/java/model/Positive.java new file mode 100644 index 0000000000..c8966f32c3 --- /dev/null +++ b/src/main/java/model/Positive.java @@ -0,0 +1,25 @@ +package model; + +public class Positive { + private int num; + + private Positive(int num) { + if (num < 0) { + throw new IllegalArgumentException("양수를 입력하십시오."); + } + + this.num = num; + } + + public static Positive of(String num) { + return of(Integer.valueOf(num)); + } + + public static Positive of(int num) { + return new Positive(num); + } + + public int getNum() { + return num; + } +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 0000000000..e6cf0c3771 --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,24 @@ +package view; + +import java.io.InputStream; +import java.util.Scanner; + +public class InputView { + private static final Scanner SCANNER = new Scanner(System.in); + + /** + * 참가자 가져오기 + */ + public static String getParticipants() { + System.out.println("참여할 사람 이름을 입력하세요."); + return SCANNER.next(); + } + + /** + * 최대 사다리 높이 가져오기 + */ + public static String getMaxHight() { + System.out.println("최대 사다리 높이는 몇 개인가요?"); + return SCANNER.next(); + } +} diff --git a/src/main/java/view/ResultView.java b/src/main/java/view/ResultView.java new file mode 100644 index 0000000000..2e7664eb80 --- /dev/null +++ b/src/main/java/view/ResultView.java @@ -0,0 +1,74 @@ +package view; + +import model.Line; +import model.Participant; + +import java.util.ArrayList; +import java.util.List; + +public class ResultView { + /** + * 실행 결과 출력 + * + * @param participants + * @param lines + */ + public static void printResult(List participants, List lines) { + System.out.println("실행결과"); + printParticipants(participants); + System.out.println(); + printLadder(lines); + + } + + /** + * 사다리 출력 + * + * @param lines + */ + private static void printLadder(List lines) { + for (int i = 0; i < lines.size(); i++) { + Line line = lines.get(i); + List points = line.getPoints(); + printLines(points); + } + } + + /** + * 참가자 출력 + * + * @param participants + */ + private static void printParticipants(List participants) { + for (Participant participant : participants) { + System.out.printf("%s ", participant.getName()); + } + } + + /** + * 라인들 출력 + * + * @param points + */ + private static void printLines(List points) { + System.out.print(" "); + for (int j = 0; j < points.size(); j++) { + printLine(points, j); + } + System.out.println(); + } + + /** + * 라인 출력 + * + * @param points + * @param j + */ + private static void printLine(List points, int j) { + if (points.get(j).booleanValue()) { + System.out.print("|-----"); + } else { + System.out.print("| "); + } + } +} diff --git a/src/test/java/generator/AutoLadderGeneratorTest.java b/src/test/java/generator/AutoLadderGeneratorTest.java new file mode 100644 index 0000000000..a26c06632a --- /dev/null +++ b/src/test/java/generator/AutoLadderGeneratorTest.java @@ -0,0 +1,33 @@ +package generator; + +import model.Line; +import model.Participant; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class AutoLadderGeneratorTest { + + private AutoLadderGenerator autoLadderGenerator; + + @Before + public void setUp() throws Exception { + autoLadderGenerator = new AutoLadderGenerator(); + } + + @Test + public void getParticipants() { + List participants = Participant.getParticipants("pobi,crong,papa,jk"); + assertThat(participants.size()).isEqualTo(4); + } + + @Test + public void getLines() { + List lines = autoLadderGenerator.getLines("5", 2); + assertThat(lines.size()).isEqualTo(5); + } +} \ No newline at end of file diff --git a/src/test/java/model/LadderTest.java b/src/test/java/model/LadderTest.java new file mode 100644 index 0000000000..e80afa4bcf --- /dev/null +++ b/src/test/java/model/LadderTest.java @@ -0,0 +1,27 @@ +package model; + +import generator.AutoLadderGenerator; +import generator.LadderGenerator; +import org.junit.Test; +import view.ResultView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class LadderTest { + @Test + public void asd() { + + + List participants = Participant.getParticipants("poni,honux,crong,jk"); + ArrayList lines = new ArrayList(); + + lines.add(Line.of(4, Arrays.asList(Positive.of(0), Positive.of(2)))); + lines.add(Line.of(4, Arrays.asList(Positive.of(1)))); + lines.add(Line.of(4, Arrays.asList(Positive.of(0)))); + lines.add(Line.of(4, Arrays.asList(Positive.of(1)))); + ResultView.printResult(participants, lines); + } + +} diff --git a/src/test/java/model/LineTest.java b/src/test/java/model/LineTest.java new file mode 100644 index 0000000000..fc308e9fdd --- /dev/null +++ b/src/test/java/model/LineTest.java @@ -0,0 +1,39 @@ +package model; + +import org.junit.Before; +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LineTest { + + private Line line; + private int countOfPerson; + + @Before + public void setUp() throws Exception { + countOfPerson = 4; + line = Line.of(countOfPerson); + + } + + @Test + public void hasPoint_선있을때(){ + line.addLine(Positive.of(1)); + assertThat(line.hasLine(1)).isTrue(); + } + + @Test + public void hasPoint_마지막인덱스(){ + assertThat(line.hasLine(4)).isFalse(); + } + + @Test ( expected = IllegalStateException.class) + public void addPoint_최대높이초과() { + for (int i = 0; i < countOfPerson +1; i++) { + line.addPoint(true); + } + } + + +} diff --git a/src/test/java/model/ParticipantTest.java b/src/test/java/model/ParticipantTest.java new file mode 100644 index 0000000000..4bd04c91e4 --- /dev/null +++ b/src/test/java/model/ParticipantTest.java @@ -0,0 +1,18 @@ +package model; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ParticipantTest { + @Test + public void getName_5자() { + Participant participant = new Participant("bbibb"); + assertThat(participant.getName()).isEqualTo("bbibb"); + } + + @Test(expected = IllegalArgumentException.class) + public void getName_5자초과() { + Participant participant = new Participant("bbibbb"); + } +} \ No newline at end of file