Skip to content

Commit 1fba16d

Browse files
authored
고생하셨습니다.
🎉 PR 머지 완료! 🎉
1 parent fa13b03 commit 1fba16d

File tree

10 files changed

+364
-0
lines changed

10 files changed

+364
-0
lines changed

src/main/java/LadderSimulator.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.util.List;
2+
3+
import model.Ladder;
4+
import model.Person;
5+
import util.InputParser;
6+
import view.InputView;
7+
import view.OutPutView;
8+
9+
public class LadderSimulator {
10+
11+
private Ladder ladder;
12+
private int height;
13+
private int width;
14+
15+
public void start() {
16+
processLadderInput();
17+
processLadderPersonMove();
18+
processLadderResult();
19+
}
20+
21+
private void processLadderInput() {
22+
List<Person> persons = InputParser.parsePersons(InputView.inputPersons());
23+
List<String> results = InputParser.parseResults(InputView.inputResults());
24+
height = InputParser.parseLadderHeight(InputView.inputLadderHeight());
25+
width = persons.size() - 1;
26+
27+
this.ladder = new Ladder(height, width, persons, results);
28+
OutPutView.printLadder(ladder);
29+
}
30+
31+
private void processLadderPersonMove() {
32+
for (Person person : ladder.getPersons()) {
33+
PersonMovement(person);
34+
}
35+
}
36+
37+
private void processLadderResult() {
38+
while (true) {
39+
String resultPersonName = InputView.inputResultPerson();
40+
41+
if (resultPersonName.equals("all")) { // all 입력시 모든 사람의 결과 출력
42+
OutPutView.printLadderResult(ladder);
43+
return;
44+
}
45+
46+
Person resultPerson = ladder.getPersonByName(resultPersonName);
47+
OutPutView.printPersonResult(resultPerson, ladder);
48+
}
49+
}
50+
51+
// 우측우선이동임
52+
private void personMovement(Person person) {
53+
for (int nowHeight = 0; nowHeight < height; nowHeight++) {
54+
if (canMoveRight(person, ladder, nowHeight)) {
55+
person.moveRight();
56+
continue;
57+
}
58+
59+
if (canMoveLeft(person, ladder, nowHeight)) {
60+
person.moveLeft();
61+
continue;
62+
}
63+
}
64+
}
65+
66+
private boolean canMoveLeft(Person person, Ladder ladder, int nowHeight) {
67+
return person.getPosition() > 0 && ladder.isPoint(nowHeight, person.getPosition() - 1);
68+
}
69+
70+
private boolean canMoveRight(Person person, Ladder ladder, int nowHeight) {
71+
return person.getPosition() < width && ladder.isPoint(nowHeight, person.getPosition());
72+
}
73+
74+
}

src/main/java/Main.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
LadderSimulator simulator = new LadderSimulator();
4+
simulator.start();
5+
}
6+
}

src/main/java/model/Ladder.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package model;
2+
3+
import java.util.List;
4+
import java.util.stream.Stream;
5+
6+
public class Ladder {
7+
8+
private final List<Line> lines;
9+
private List<Person> persons;
10+
private List<String> results;
11+
12+
public Ladder(int height, int width, List<Person> persons, List<String> results) {
13+
this.lines = Stream.generate(() -> new Line(width))
14+
.limit(height)
15+
.toList();
16+
this.persons = persons;
17+
this.results = results;
18+
}
19+
20+
public List<Line> getLines() {
21+
return lines;
22+
}
23+
24+
public List<Person> getPersons() {
25+
return persons;
26+
}
27+
28+
public List<String> getResults() {
29+
return results;
30+
}
31+
32+
public boolean isPoint(int lineIndex, int pointIndex) {
33+
if (lineIndex < 0 || lineIndex >= lines.size()) {
34+
throw new IndexOutOfBoundsException("인덱스 범위 초과");
35+
}
36+
return lines.get(lineIndex).isPoint(pointIndex);
37+
}
38+
39+
public Person getPersonByName(String name) {
40+
return persons.stream()
41+
.filter(person -> person.getName().equals(name))
42+
.findFirst()
43+
.orElseThrow(() -> new IllegalArgumentException("해당 이름의 사람이 없습니다: "));
44+
}
45+
46+
}

src/main/java/model/Line.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package model;
2+
3+
import java.util.List;
4+
5+
import util.LadderRandomGenerator;
6+
7+
public class Line {
8+
9+
private final List<Boolean> points;
10+
11+
public Line(int length) {
12+
this.points = LadderRandomGenerator.generateRandomLine(length);
13+
}
14+
15+
public List<Boolean> getPoints() {
16+
return points;
17+
}
18+
19+
public boolean isPoint(int index) {
20+
if (index < 0 || index >= points.size()) {
21+
throw new IndexOutOfBoundsException("인덱스 범위 초과");
22+
}
23+
return points.get(index);
24+
}
25+
26+
@Override
27+
public String toString() {
28+
StringBuilder sb = new StringBuilder();
29+
for (Boolean point : points) {
30+
sb.append("|");
31+
sb.append(point ? "-----" : " ");
32+
}
33+
sb.append("|");
34+
return sb.toString();
35+
}
36+
}

src/main/java/model/Person.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package model;
2+
3+
import java.util.List;
4+
5+
public class Person {
6+
7+
private final String name;
8+
private int position;
9+
10+
public Person(String name, int position) {
11+
this.name = name;
12+
this.position = position;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public int getPosition() {
20+
return position;
21+
}
22+
23+
public void moveLeft() {
24+
position--;
25+
}
26+
27+
public void moveRight() {
28+
position++;
29+
}
30+
31+
public static List<Person> fromNames(List<String> names) {
32+
return names.stream()
33+
.map(name -> new Person(name, names.indexOf(name)))
34+
.toList();
35+
36+
}
37+
}

src/main/java/util/InputParser.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package util;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import model.Person;
7+
8+
public class InputParser {
9+
10+
public static int parseLadderHeight(String input) {
11+
LadderVaildator.validateLadderHeight(input);
12+
return Integer.parseInt(input);
13+
}
14+
15+
public static int parseLadderWidth(String input) {
16+
LadderVaildator.validateLadderWidth(input);
17+
return Integer.parseInt(input);
18+
}
19+
20+
public static List<Person> parsePersons(String input) {
21+
LadderVaildator.validatePersons(input);
22+
String[] names = input.split(",");
23+
return Person.fromNames(Arrays.asList(names));
24+
}
25+
26+
public static List<String> parseResults(String input) {
27+
LadderVaildator.validateResults(input);
28+
return Arrays.asList(input.split(","));
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
package util;
3+
4+
import java.util.List;
5+
import java.util.stream.Stream;
6+
7+
public class LadderRandomGenerator {
8+
9+
public static boolean generateRandomBoolean() {
10+
return Math.random() < 0.5;
11+
}
12+
13+
public static List<Boolean> generateRandomLine(int length) {
14+
return Stream.generate(LadderRandomGenerator::generateRandomBoolean)
15+
.limit(length)
16+
.toList();
17+
}
18+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
package util;
3+
4+
public class LadderVaildator {
5+
6+
public static void validateLadderHeight(String height) {
7+
if (height == null || height.isEmpty()) {
8+
throw new IllegalArgumentException("사다리 높이는 비어있을 수 없습니다.");
9+
}
10+
11+
if (height.matches("\\D+")) {
12+
throw new IllegalArgumentException("사다리 높이는 숫자로 입력해야 합니다.");
13+
}
14+
15+
int heightValue = Integer.parseInt(height);
16+
17+
if (heightValue <= 0) {
18+
throw new IllegalArgumentException("사다리 높이는 0보다 큰 숫자여야 합니다.");
19+
}
20+
21+
}
22+
23+
public static void validateLadderWidth(String width) {
24+
if (width == null || width.isEmpty()) {
25+
throw new IllegalArgumentException("사다리 넓이는 비어있을 수 없습니다.");
26+
}
27+
28+
if (width.matches("\\D+")) {
29+
throw new IllegalArgumentException("사다리 넓이는 숫자로 입력해야 합니다.");
30+
}
31+
32+
int widthValue = Integer.parseInt(width);
33+
34+
if (widthValue <= 0) {
35+
throw new IllegalArgumentException("사다리 넓이는 0보다 큰 숫자여야 합니다.");
36+
}
37+
38+
}
39+
40+
public static void validatePersons(String persons) {
41+
if (persons == null || persons.isEmpty()) {
42+
throw new IllegalArgumentException("참여자 이름은 비어있을 수 없습니다.");
43+
}
44+
}
45+
46+
public static void validateResults(String results) {
47+
if (results == null || results.isEmpty()) {
48+
throw new IllegalArgumentException("실행 결과는 비어있을 수 없습니다.");
49+
}
50+
}
51+
}

src/main/java/view/InputView.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package view;
2+
3+
import java.util.Scanner;
4+
5+
public class InputView {
6+
7+
private static final Scanner scanner = new Scanner(System.in);
8+
9+
public static String inputLadderHeight() {
10+
System.out.print("사다리의 높이는 몇 개인가요?");
11+
return scanner.nextLine();
12+
}
13+
14+
public static String inputPersons() {
15+
System.out.print("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)");
16+
return scanner.nextLine();
17+
}
18+
19+
public static String inputResults() {
20+
System.out.print("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)");
21+
return scanner.nextLine();
22+
}
23+
24+
public static String inputResultPerson() {
25+
System.out.print("결과를 보고 싶은 사람은?");
26+
return scanner.nextLine();
27+
}
28+
29+
}

src/main/java/view/OutPutView.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package view;
2+
3+
import java.util.List;
4+
5+
import model.Ladder;
6+
import model.Line;
7+
import model.Person;
8+
9+
public class OutPutView {
10+
11+
public static void printLadder(Ladder ladder) {
12+
List<Line> lines = ladder.getLines();
13+
System.out.println("사다리 결과");
14+
for (Person person : ladder.getPersons()) {
15+
System.out.print(person.getName() + " ");
16+
}
17+
System.out.println();
18+
for (Line line : lines) {
19+
System.out.println(line);
20+
}
21+
for (String result : ladder.getResults()) {
22+
System.out.print(result + " ");
23+
}
24+
System.out.println();
25+
}
26+
27+
public static void printLadderResult(Ladder ladder) {
28+
System.out.println("실행 결과");
29+
for (Person person : ladder.getPersons()) {
30+
System.out.println(person.getName() + " : " + ladder.getResults().get(person.getPosition()));
31+
}
32+
}
33+
34+
public static void printPersonResult(Person person, Ladder ladder) {
35+
System.out.println(ladder.getResults().get(person.getPosition()));
36+
}
37+
}

0 commit comments

Comments
 (0)