From 4339152bf5f88f07e7d9165de2797c90779e96a5 Mon Sep 17 00:00:00 2001 From: JeongBeanHyun Date: Fri, 7 Mar 2025 23:12:28 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EC=82=AC=EB=8B=A4=EB=A6=AC=201?= =?UTF-8?q?=EB=8B=A8=EA=B3=84=20=EB=AF=B8=EC=85=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Application.java | 8 +++ .../java/controller/LadderController.java | 30 +++++++++++ src/main/java/model/Ladder.java | 24 +++++++++ src/main/java/model/Line.java | 39 ++++++++++++++ src/main/java/model/Point.java | 26 ++++++++++ src/main/java/model/Size.java | 20 +++++++ src/main/java/view/LineCharacter.java | 17 ++++++ src/main/java/view/ResultView.java | 52 +++++++++++++++++++ 8 files changed, 216 insertions(+) create mode 100644 src/main/java/Application.java create mode 100644 src/main/java/controller/LadderController.java create mode 100644 src/main/java/model/Ladder.java create mode 100644 src/main/java/model/Line.java create mode 100644 src/main/java/model/Point.java create mode 100644 src/main/java/model/Size.java create mode 100644 src/main/java/view/LineCharacter.java create mode 100644 src/main/java/view/ResultView.java diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 0000000..ab27858 --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,8 @@ +import controller.LadderController; + +public class Application { + public static void main(String[] args) { + LadderController ladderController = new LadderController(); + ladderController.startLadder(); + } +} diff --git a/src/main/java/controller/LadderController.java b/src/main/java/controller/LadderController.java new file mode 100644 index 0000000..397715b --- /dev/null +++ b/src/main/java/controller/LadderController.java @@ -0,0 +1,30 @@ +package controller; + +import model.Ladder; +import model.Line; +import model.Point; +import model.Size; +import view.ResultView; + +import java.util.ArrayList; +import java.util.List; + +public class LadderController { + + private static final int LADDER_SIZE = 4; + private final ResultView resultView = new ResultView(); + + public void startLadder() { + Ladder ladder = new Ladder(new Size(LADDER_SIZE), new Size(LADDER_SIZE)); + List points = convertPointsToStringList(ladder.getLines()); + resultView.printLadder(points); + } + + private List convertPointsToStringList(List lines) { + List points = new ArrayList<>(); + for (Line line : lines) { + points.add(Point.toStringUsingMap(line.getPoints())); + } + return List.copyOf(points); + } +} diff --git a/src/main/java/model/Ladder.java b/src/main/java/model/Ladder.java new file mode 100644 index 0000000..856f499 --- /dev/null +++ b/src/main/java/model/Ladder.java @@ -0,0 +1,24 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public class Ladder { + private final List lines; + + public Ladder(Size width, Size height) { + this.lines = generateLines(width, height); + } + + private List generateLines(Size width, Size height) { + List lines = new ArrayList<>(); + for (int i = 0; i < height.getSize(); i++) { + lines.add(new Line(width)); + } + return List.copyOf(lines); + } + + public List getLines() { + return List.copyOf(lines); + } +} diff --git a/src/main/java/model/Line.java b/src/main/java/model/Line.java new file mode 100644 index 0000000..61c37c4 --- /dev/null +++ b/src/main/java/model/Line.java @@ -0,0 +1,39 @@ +package model; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class Line { + private final List points; + private final Random random = new Random(); + + public Line(Size width) { + this.points = createLinePoints(width); + } + + private List createLinePoints(Size width) { + List points = new ArrayList<>(); + boolean isPreviousLine = false; + + for (int i = 1; i < width.getSize(); i++) { + Point point = createPoint(isPreviousLine); + points.add(point); + isPreviousLine = (point == Point.HAS_POINT); + } + return List.copyOf(points); + } + + private Point createPoint(boolean isPreviousLine) { + List points = List.of(Point.NO_POINT, Point.HAS_POINT); + if (isPreviousLine) { + return Point.NO_POINT; + } + int randomIndex = random.nextInt(points.size()); + return points.get(randomIndex); + } + + public List getPoints() { + return List.copyOf(points); + } +} diff --git a/src/main/java/model/Point.java b/src/main/java/model/Point.java new file mode 100644 index 0000000..07335b1 --- /dev/null +++ b/src/main/java/model/Point.java @@ -0,0 +1,26 @@ +package model; + +import java.util.List; +import java.util.Map; + +public enum Point { + HAS_POINT(true), + NO_POINT(false); + + private final boolean point; + + Point(boolean point) { + this.point = point; + } + + public static String toStringUsingMap(List points) { + Map pointToStringMap = Map.of( + HAS_POINT, "HAS_POINT", + NO_POINT, "NO_POINT" + ); + + return points.stream() + .map(point -> pointToStringMap.getOrDefault(point, "UNKNOWN")) + .toList().toString(); + } +} diff --git a/src/main/java/model/Size.java b/src/main/java/model/Size.java new file mode 100644 index 0000000..cefa54c --- /dev/null +++ b/src/main/java/model/Size.java @@ -0,0 +1,20 @@ +package model; + +public class Size { + private final int size; + + public Size(int size) { + validateSize(size); + this.size = size; + } + + public int getSize() { + return size; + } + + private void validateSize(int size) { + if (size < 0) { + throw new IllegalArgumentException("사다리 사이즈가 0보다 작을 수는 없습니다."); + } + } +} diff --git a/src/main/java/view/LineCharacter.java b/src/main/java/view/LineCharacter.java new file mode 100644 index 0000000..e4cd8cd --- /dev/null +++ b/src/main/java/view/LineCharacter.java @@ -0,0 +1,17 @@ +package view; + +public enum LineCharacter { + VERTICAL("|"), + CONNECTED("-----"), + DISCONNECTED(" "); + + private final String symbol; + + LineCharacter(String symbol) { + this.symbol = symbol; + } + + public String getSymbol() { + return symbol; + } +} diff --git a/src/main/java/view/ResultView.java b/src/main/java/view/ResultView.java new file mode 100644 index 0000000..3bf5656 --- /dev/null +++ b/src/main/java/view/ResultView.java @@ -0,0 +1,52 @@ +package view; + +import java.util.List; + +public class ResultView { + + public void printLadder(List ladderLines) { + printResultHeader(); + StringBuilder resultBuilder = new StringBuilder(); + processLadderLines(resultBuilder, ladderLines); + outputResult(resultBuilder); + } + + private void printResultHeader() { + System.out.println("실행결과"); + System.out.println(); + } + + private void processLadderLines(StringBuilder resultBuilder, List ladderLines) { + for (String line : ladderLines) { + String[] points = parseLine(line); + resultBuilder.append(LineCharacter.VERTICAL.getSymbol()); + appendSymbolsForPoints(points, resultBuilder); + resultBuilder.append("\n"); + } + } + + private String[] parseLine(String line) { + return line.replaceAll("[\\[\\] ]", "").split(","); + } + + private void appendSymbolsForPoints(String[] points, StringBuilder resultBuilder) { + for (String point : points) { + appendSymbolToBuilder(resultBuilder, point); + resultBuilder.append(LineCharacter.VERTICAL.getSymbol()); + } + } + + private void appendSymbolToBuilder(StringBuilder resultBuilder, String point) { + if (point.equals("HAS_POINT")) { + resultBuilder.append(LineCharacter.CONNECTED.getSymbol()); + } + //else if 수정 + if (point.equals("NO_POINT")) { + resultBuilder.append(LineCharacter.DISCONNECTED.getSymbol()); + } + } + + private void outputResult(StringBuilder resultBuilder) { + System.out.println(resultBuilder); + } +} From 1afb92b721f2bdbe253ea04c198d153e033631e3 Mon Sep 17 00:00:00 2001 From: JeongBeanHyun Date: Fri, 7 Mar 2025 23:29:49 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=EC=82=AC=EC=86=8C=ED=95=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/view/ResultView.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/view/ResultView.java b/src/main/java/view/ResultView.java index 3bf5656..394b50b 100644 --- a/src/main/java/view/ResultView.java +++ b/src/main/java/view/ResultView.java @@ -40,7 +40,6 @@ private void appendSymbolToBuilder(StringBuilder resultBuilder, String point) { if (point.equals("HAS_POINT")) { resultBuilder.append(LineCharacter.CONNECTED.getSymbol()); } - //else if 수정 if (point.equals("NO_POINT")) { resultBuilder.append(LineCharacter.DISCONNECTED.getSymbol()); }