-
Notifications
You must be signed in to change notification settings - Fork 0
#step2 PR #5
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
base: rudiamoon_study
Are you sure you want to change the base?
#step2 PR #5
Changes from 17 commits
50533b8
c2427fc
3eabb9e
acd2e92
5a1f9c9
b0eef9b
62c9f54
ed1f7b4
0b54be4
a920066
6092c51
b3e6bc1
47776fc
113231c
e80745c
2ffd68d
3e3740e
2d1c563
928efca
4cec062
560ebfb
ee14b60
7bda094
7c044ee
a459186
301eb83
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #Fri Jul 05 20:59:43 KST 2019 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-bin.zip | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package nextstep.main; | ||
|
|
||
| import nextstep.main.view.InputView; | ||
| import nextstep.main.view.ResultView; | ||
| import nextstep.main.vo.Ladder; | ||
| import nextstep.main.vo.Persons; | ||
|
|
||
| public class ConsoleMain { | ||
| public static void main(String []args){ | ||
| String playersName = InputView.playerNames(); | ||
| String playerResults = InputView.ladderResultPreDiction(); | ||
|
|
||
| int ladderHeight = InputView.maxLadderHeight(); | ||
|
|
||
| Persons persons = Persons.generate(playersName); | ||
| Persons results = Persons.generate(playerResults); | ||
|
|
||
| Ladder ladder = new Ladder(); | ||
| ladder.generateLadder(ladderHeight, persons.getPersonCount()); | ||
|
|
||
| ResultView.executeNames(persons); | ||
| ResultView.drawLadder(ladder); | ||
| ResultView.executeResults(results); | ||
|
|
||
|
|
||
| persons.settingResult(ladder, results); | ||
| String personName = InputView.personResult(); | ||
| ResultView.textResultPerson(persons.getLadderResult(personName)); | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package nextstep.main.view; | ||
|
|
||
| import java.util.Scanner; | ||
|
|
||
| public class InputView { | ||
| public static String playerNames() { | ||
| System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static String ladderResultPreDiction(){ | ||
| System.out.println("실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static int maxLadderHeight() { | ||
| System.out.println("최대 사다리 높이는 몇 개인가요?"); | ||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextInt(); | ||
| } | ||
|
|
||
| public static String personResult(){ | ||
| System.out.println("결과를 보고 싶은 사람은?\n"); | ||
|
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. 여기 앞쪽에 라인피드
|
||
| Scanner scanner = new Scanner(System.in); | ||
| return scanner.nextLine(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package nextstep.main.view; | ||
|
|
||
| import nextstep.main.vo.Ladder; | ||
| import nextstep.main.vo.Persons; | ||
|
|
||
| public class ResultView { | ||
| public static void executeNames(Persons persons){ | ||
| System.out.println("사다리 결과"); | ||
|
|
||
| persons.getPersons() | ||
| .forEach(System.out::print); | ||
|
|
||
|
|
||
| System.out.println(); | ||
| } | ||
|
|
||
| public static void drawLadder(Ladder ladder) { | ||
| ladder.getLines() | ||
| .forEach(System.out::println); | ||
| } | ||
|
|
||
| public static void executeResults(Persons results){ | ||
| results.getPersons() | ||
| .forEach(System.out::print); | ||
| } | ||
|
|
||
| public static void textResultPerson(String result){ | ||
| System.out.println("실행 결과"); | ||
| System.out.println(result); | ||
| } | ||
|
|
||
| public static void textResultAll(){ | ||
| System.out.println("실행 결과"); | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package nextstep.main.vo; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Ladder { | ||
| private List<Line> lines; | ||
|
|
||
| public Ladder() { | ||
| this.lines = new ArrayList<>(); | ||
| } | ||
|
|
||
| public int generateLadder(int ladderHeight, int playerCount) { | ||
| IntStream.range(0, ladderHeight) | ||
| .mapToObj(item -> new Line(playerCount)) | ||
| .forEach(line -> this.lines.add(line)); | ||
|
|
||
| return this.lines.size(); | ||
| } | ||
|
|
||
| public List<Line> getLines() { | ||
| return this.lines; | ||
| } | ||
|
|
||
| public int getResult(int position) { | ||
| int currentPosition = position; | ||
|
|
||
| for(int i = 0; i< lines.size(); i++) { | ||
| currentPosition = lines.get(i).getPlusMinusPosition(currentPosition); | ||
| } | ||
|
|
||
| return currentPosition; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package nextstep.main.vo; | ||
|
|
||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Line { | ||
| private static final int START_POINT = 0; | ||
| public static final String VERTICAL = "|"; | ||
| public static final int HORIZONTAL_SIZE = 5; | ||
| public static final String DASH = "-"; | ||
| public static final String EMPTY = " "; | ||
|
|
||
| private List<Boolean> points; | ||
|
impactrudia marked this conversation as resolved.
|
||
|
|
||
| Line(int countOfPerson) { | ||
| generateLine(countOfPerson); | ||
| } | ||
|
|
||
| private int generateLine(int countOfPerson) { | ||
| points = new ArrayList<>(); | ||
|
|
||
| IntStream.range(0, numberOfPoints(countOfPerson)) | ||
| .forEach(i -> points.add(generateCurrentPoint(i))); | ||
|
|
||
| return points.size(); | ||
| } | ||
|
|
||
| private static int numberOfPoints(int countOfPerson) { | ||
| return countOfPerson - 1; | ||
| } | ||
|
|
||
| private boolean generateCurrentPoint(int newPosition) { | ||
| boolean point = new Random().nextBoolean(); | ||
|
|
||
| if (isOverLapped(newPosition, point)) { | ||
| point = !point; | ||
| } | ||
|
|
||
| return point; | ||
| } | ||
|
|
||
| private boolean isOverLapped(int currPosition, boolean newPoint) { | ||
| if (currPosition == START_POINT) | ||
| return false; | ||
|
|
||
| return isEqual(points.get(currPosition - 1), newPoint); | ||
| } | ||
|
|
||
| static boolean isEqual(boolean prevPoint, boolean newPoint) { | ||
| return prevPoint == newPoint; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| final StringBuilder str = new StringBuilder(" |"); | ||
| points.forEach(b -> str.append(pointToString(b))); | ||
|
|
||
| return str.toString(); | ||
| } | ||
|
|
||
| private String pointToString(boolean b) { | ||
| if (b) { | ||
| return StringUtils.leftPad(VERTICAL, HORIZONTAL_SIZE, DASH); | ||
| } | ||
| return StringUtils.leftPad(VERTICAL, HORIZONTAL_SIZE, EMPTY); | ||
| } | ||
|
|
||
| public List<Boolean> getPoints() { | ||
| return points; | ||
| } | ||
|
|
||
| public int getPlusMinusPosition(int currentPosition) { | ||
| if (currentPosition == 0) { | ||
| return points.get(0) ? currentPosition + 1 : currentPosition; | ||
| } | ||
|
|
||
| if (currentPosition == points.size()) { | ||
| return points.get(currentPosition - 1) ? currentPosition - 1 : currentPosition; | ||
| } | ||
|
|
||
| if (points.get(currentPosition)) { | ||
| return currentPosition + 1; | ||
| } | ||
|
|
||
|
|
||
| if(points.get(currentPosition-1)) { | ||
| return currentPosition -1; | ||
| } | ||
|
|
||
| return currentPosition; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package nextstep.main.vo; | ||
|
|
||
| import java.util.stream.Collectors; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class Person { | ||
| private static final int MAX_NAME_COUNT = 5; | ||
|
|
||
| private String name; | ||
| private String result; | ||
|
|
||
| public Person(String name) { | ||
| if (name.length() > MAX_NAME_COUNT) | ||
| throw new IllegalArgumentException(); | ||
|
|
||
| this.name = name; | ||
| } | ||
|
|
||
| public Person(String name, String result) { | ||
|
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. 사용되는곳이 없대! |
||
| this.name = name; | ||
| this.result = result; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| String str = IntStream.range(0, MAX_NAME_COUNT - this.name.length()) | ||
| .mapToObj(i -> " ") | ||
| .collect(Collectors.joining("", this.name, " ")); | ||
|
|
||
| return str; | ||
| } | ||
|
|
||
| public boolean isMatch(String personName) { | ||
| return name.equals(personName); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package nextstep.main.vo; | ||
|
|
||
| import nextstep.main.view.ResultView; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Persons { | ||
| private static final String NAME_SEPARATOR = ","; | ||
|
|
||
| private List<Person> persons; | ||
| private List<Person> results; | ||
|
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 Persons(List<Person> players) { | ||
| this.persons = Collections.unmodifiableList(players); | ||
| this.results = new ArrayList<>(); | ||
| } | ||
|
|
||
| public static Persons generate(String names) { | ||
| List<Person> persons = Arrays.stream(split(names)) | ||
| .map(Person::new) | ||
| .collect(Collectors.toList()); | ||
| return new Persons(persons); | ||
| } | ||
|
|
||
| private static String[] split(String value) { | ||
| return value.split(NAME_SEPARATOR); | ||
| } | ||
|
|
||
| public int getPersonCount() { | ||
| return this.persons.size(); | ||
| } | ||
|
|
||
| public List<Person> getPersons() { | ||
| return this.persons; | ||
| } | ||
|
|
||
| public int getStartPosition(String personName) { | ||
|
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. 사용되는곳이 없는거 같아 |
||
| int index = 0; | ||
| for (Person person : persons) { | ||
| if (person.isMatch(personName)) { | ||
| return index; | ||
| } | ||
| index++; | ||
| } | ||
| throw new IllegalArgumentException("잘못된 참여자 이름입니다."); | ||
| } | ||
|
|
||
| public void settingResult(Ladder ladder, Persons results) { | ||
| for (int i = 0 ; i < persons.size() ; i++ ) { | ||
| int resultPosition = ladder.getResult(i); | ||
| this.results.add(results.getPersons().get(resultPosition)); | ||
| } | ||
| } | ||
|
|
||
| public String getLadderResult(String personName) { | ||
| if(personName.equals("all")){ | ||
| StringBuilder sb = new StringBuilder(); | ||
| for(int i = 0 ; i< persons.size() ; i++){ | ||
| sb.append(persons.get(i).toString() + " : " + results.get(i).toString()); | ||
| sb.append("\n"); | ||
| } | ||
| return sb.toString(); | ||
| } | ||
| int resultIdx = 0; | ||
| for(int i = 0 ; i< persons.size() ; i++){ | ||
| if(personName.equals(persons.get(i))){ | ||
| resultIdx = i; | ||
| break; | ||
| } | ||
| } | ||
| return results.get(resultIdx).toString(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package nextstep.main; | ||
|
|
||
| import static org.junit.Assert.*; | ||
|
|
||
| public class ConsoleMainTest { | ||
|
|
||
| } |
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.
all 이 아닌경우에는 반복해서 결과볼 수 있게 바꿔보면 좋을거 같아ㅎㅎ
요구사항에 반복하라는 얘기는 없어보이는데 all일때만 종료되는것 처럼 예시가 나와있어서
all일때만 종료되도록 바꿔보는것도 좋을거 같아!