-
Notifications
You must be signed in to change notification settings - Fork 39
[사다리 미션] 신혜빈 미션 제출합니다. #20
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: shin378378
Are you sure you want to change the base?
Changes from 12 commits
3d3d769
b85fa13
e2342f2
5c01a53
ceb91b9
b753e93
651f22b
9f1d637
42c7d00
14b4d35
210d7f3
9739d30
6422685
cd7c8a9
f2642b3
62ded33
b24b654
8b4800c
af2eec1
e3d3190
fa8e9e0
b21fef9
d0a5f26
8a15278
37a8e9d
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# java-ladder | ||
|
||
# < controller > | ||
## 1) Controller | ||
* createParticipants() //참여자 생성기능 | ||
* createResults() //참여결과 생성기능 | ||
* createLadder() //사다리 생성기능 | ||
* createLadderResult() //사다리 결과 생성기능 | ||
* playLadderGame() //사다리 게임 실행기능 | ||
|
||
--- | ||
# < model > | ||
## 1) DecideResult | ||
* moveColumn() // 열부분 움직이는 기능 | ||
* moveRow() //행부분 움직이는 기능 | ||
* decideParticipantResult() //참여자 결과 결정기능 | ||
## 2) Ladder | ||
## 3) LadderRow | ||
* LadderRow() //사다리 열부분 생성기능 | ||
* chooseTrueOrFalse() //사다리 point 정하는 기능 | ||
## 4) Participant | ||
* Participant() //참여자 생성, 참가자이름이 5글자 초과 시 에러발생 기능 | ||
## 5) Participants | ||
## 6) Results | ||
## 7) Splitter | ||
* split() //문자열 자르는 기능 | ||
|
||
--- | ||
# < view > | ||
## 1) InputView | ||
* inputParticipants() //참여할 사람이름 입력기능 | ||
* inputResults() //결과 입력기능 | ||
* inputLadderHeight() //최대 사다리 높이 입력기능 | ||
* inputParticipantToWantResult() //결과를 보고싶은 사람 입력기능 | ||
## 2) OutputView | ||
* outputParticipant() //참여자 출력기능 | ||
* outputLadderRow() //사다리행 출력기능 | ||
* outputLadder() //사다리 출력기능 | ||
* outputResults() //결과 출력기능 | ||
* outputAllParticipantsResult //모든 참여자 결과 출력기능 | ||
* outputCertainParticipantResult //특정 참여자 결과 출력기능 | ||
* outputParticipantResult //참여자 결과 출력기능 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import controller.Controller; | ||
|
||
public class Main { | ||
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. 저는 개인적으로는 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.
<질문>
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. 이제 프로젝트가 커졌을 때 프로젝트를 실행시키는 main 함수를 찾기가 힘든데요 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. Main은 무조건 하나밖에 없으니까 왜 Main이 여러개가 나온다는 건 지 몰랐는 데 하나의 어플리케이션 안에
|
||
public static void main(String[] args) { | ||
Controller controller = new Controller(); | ||
controller.playLadderGame(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package controller; | ||
|
||
import model.*; | ||
|
||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
|
||
public class Controller { | ||
InputView inputView = new InputView(); | ||
Splitter splitter = new Splitter(); | ||
OutputView outputView = new OutputView(); | ||
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. Input VIew 쪽에 달아드린 이 클래스가 static 으로 사용하지 않는 이유를 알고 싶어요! 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. 앗 딱히 이유는 없고 빼먹는 거 같습니다!! 짚어주셔서 감사합니다 :) |
||
|
||
private List<Participant> createParticipants() { | ||
String participantNamesBeforeSplit = inputView.inputParticipants(); | ||
String[] participantNames = splitter.split(participantNamesBeforeSplit); | ||
List<Participant> participantsInventory = new ArrayList<>(); | ||
for (int i = 0; i < participantNames.length; i++) { | ||
Participant participant = new Participant(i, participantNames[i]); | ||
participantsInventory.add(participant); | ||
} | ||
Participants participants = new Participants(participantsInventory); | ||
return participants.getParticipantInventory(); | ||
} | ||
|
||
private List<String> createResults() { | ||
String trialResultsBeforeSplit = inputView.inputResults(); | ||
String[] trialResults = splitter.split(trialResultsBeforeSplit); | ||
Results results = new Results(Arrays.asList(trialResults)); | ||
return results.getTrialResults(); | ||
} | ||
|
||
private Ladder createLadder(int columnSize) { | ||
int rowSize = inputView.inputLadderHeight(); | ||
Ladder ladder = new Ladder(); | ||
List<LadderRow> ladderRows = new ArrayList<>(); | ||
for (int i = 0; i < rowSize; i++) { | ||
LadderRow ladderRow = new LadderRow(columnSize); | ||
ladderRows.add(ladderRow); | ||
} | ||
ladder.settingColumns(ladderRows); | ||
return ladder; | ||
} | ||
|
||
private List<LadderRow> createLadderResult(List<Participant> participantInventory, List<String> trialResults){ | ||
Ladder ladder = createLadder(participantInventory.size()-1); | ||
List<LadderRow> ladderRows = ladder.getRows(); | ||
OutputView outputView = new OutputView(); | ||
outputView.outputLadder(participantInventory, ladderRows, trialResults); | ||
return ladder.getRows(); | ||
} | ||
|
||
public void playLadderGame(){ | ||
List<Participant> participantInventory = createParticipants(); | ||
List<String> trialResults = createResults(); | ||
List<LadderRow> ladderRows = createLadderResult(participantInventory, trialResults); | ||
DecideResult decideResult = new DecideResult(); | ||
decideResult.decideParticipantResult(participantInventory, ladderRows, trialResults); | ||
while(true) { | ||
String participantName = inputView.inputParticipantToWantResult(); | ||
outputView.outputParticipantResult(participantInventory, participantName); | ||
if(participantName.equals("all"))break; | ||
} | ||
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.
이렇게 해석할 수도 있네요! 저는 그냥 한 번만 결과를 출력하는 줄 알아서 한 번만 했거든요,,ㅎㅎ 들여쓰기를 1까지 허용한다는 조건을 만족시키기 위해서 do-while문을 사용해볼 수도 있을 것 같아요! 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. if(participantName.equals("all"))break; 저도 모르게 단순히 이 코드를 들여쓰기가 없는 코드라고 인식하고 있었네요 ㅜㅠ 짚어주셔서 감사합니다!! String participantName;
do{
participantName = inputView.inputParticipantToWantResult();
outputView.outputParticipantResult(participantInventory, participantName);
}while(!participantName.equals("all")); |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class DecideResult { | ||
private int moveColumn(List<Boolean> points, int columnPosition){ | ||
if(columnPosition!=0 && points.get(columnPosition-1)==true){ | ||
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. 오! 이 부분에 대한 책임 부여가 저랑 조금 다른 부분인 것 같아요. 저는 조건문의 내용이 List points를 인스턴스 변수로 가진 (혜빈님 클래스 이름 기준) 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. 생각해보니까 이 상희님 말씀처럼 DecideResult를 따로 둘 필요가 없을 거 같네요! |
||
return --columnPosition; | ||
} | ||
if(columnPosition!=points.size() && points.get(columnPosition)==true) { | ||
return ++columnPosition; | ||
} | ||
return columnPosition; | ||
} | ||
|
||
private int moveRow(List<LadderRow> ladderRows, int columnPosition){ | ||
for (LadderRow ladderRow : ladderRows) { | ||
List<Boolean> points = ladderRow.getPoints(); | ||
columnPosition = moveColumn(points, columnPosition); | ||
} | ||
return columnPosition; | ||
} | ||
|
||
public void decideParticipantResult(List<Participant> participantInventory, List<LadderRow> ladderRows, List<String> trialResults){ | ||
for (Participant participant : participantInventory) { | ||
int participantPosition = participant.getPosition(); | ||
int columnPosition = moveRow(ladderRows, participantPosition); | ||
String result = trialResults.get(columnPosition); | ||
participant.settingResult(result); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Ladder { | ||
private List<LadderRow> ladderRows; | ||
|
||
public void settingColumns(List<LadderRow> ladderRows) { | ||
this.ladderRows = ladderRows; | ||
} | ||
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. 생성자 이름을 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 List<LadderRow> getRows() { | ||
return ladderRows; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package model; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LadderRow { | ||
private List<Boolean> points = new ArrayList<>(); | ||
|
||
public LadderRow(int columnSize) { | ||
for (int columnPosition = 0; columnPosition < columnSize; columnPosition++) { | ||
boolean randomBoolean = chooseTrueOrFalse(this.points, columnPosition); | ||
this.points.add(columnPosition, randomBoolean); | ||
} | ||
} | ||
|
||
private boolean chooseTrueOrFalse(List<Boolean> points, int columnPosition) { | ||
if (columnPosition == 0) { | ||
return Math.random() < 0.5; | ||
} | ||
if (points.get(columnPosition - 1) == true) return false; | ||
return Math.random() < 0.5; | ||
} | ||
|
||
public List<Boolean> getPoints() { | ||
return points; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package model; | ||
|
||
public class Participant { | ||
private static final int NAME_LENGTH_LIMIT=5; | ||
private int position; | ||
private String name; | ||
private 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.
오 저도 이거 저는 세 개 중에 두 개만 넣어야 한다면 혜빈님은 어떻게 생각하시나요?? 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. 이번 기회에 저도 곰곰히 어떤 변수를 빼면 좋을까 고민을 했는 데요. 저도 상희님처럼 result결과를 빼기로 결정했습니다!! 차이점이 있다면 값을 result값을 따로 빼라고 피드백해주셔서 감사합니다! 덕분에 조금 더 깔끔한 리팩토링에 많은 도움이 된 거 같아요! |
||
|
||
public Participant(int position, String name) { | ||
if(name.length()> NAME_LENGTH_LIMIT) { | ||
throw new IllegalArgumentException("참가자 이름이 "+NAME_LENGTH_LIMIT+"자 초과입니다."); | ||
} | ||
this.position = position; | ||
this.name = name; | ||
} | ||
|
||
public void settingResult(String result){ | ||
this.result = result; | ||
} | ||
|
||
public int getPosition() { | ||
return position; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getResult() { | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Participants { | ||
private List<Participant> participantInventory; | ||
|
||
public Participants(List<Participant> participantInventory) { | ||
this.participantInventory = participantInventory; | ||
} | ||
|
||
public List<Participant> getParticipantInventory() { | ||
return participantInventory; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Results { | ||
private List<String> trialResults; | ||
|
||
public Results(List<String> trialResults) { | ||
this.trialResults = trialResults; | ||
} | ||
|
||
public List<String> getTrialResults() { | ||
return trialResults; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package model; | ||
|
||
public class Splitter { | ||
final static private String SPLIT_SIGN = ","; | ||
|
||
public String[] split(String beforeSplit) { | ||
String[] afterSplit = beforeSplit.split(SPLIT_SIGN); | ||
for (int i = 0; i < afterSplit.length; i++) { | ||
afterSplit[i] = afterSplit[i].trim(); | ||
} | ||
return afterSplit; | ||
} | ||
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. 우와 저는 공백 제거하여 분리하는 방법은 몰랐는데 알아가네요! |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package view; | ||
|
||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
Scanner scanner = new Scanner(System.in); | ||
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. 다른 부분의 구현을 정말 잘 해주신 것 같아서 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. 딱히 이유는 없습니다 ㅜㅠ 현재 스캐너와 <질문>static을 붙이는 이유가 "이 값은 객체마다의 특징을 나타내는 값이 아니다"라는 특징을 명시해주기 위해서 인가요? 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. 보통 스태틱으로 할당되는 메모리는 신경쓸 필요가 없어요 (map 에 10만개쯤 넣는 순간부터 고민해도 충분합니다) 스태틱을 쓰면 가장 간단하게는 생성자가 간단해지잖아요? 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. staic이라는 게 생각보다 메모리를 많이 잡아먹진 않군요 :) 몰랐던 지식인 데 알려주셔서 감사합니다! 생성자가 간단해진다는 생각은 못 해봤어요!! 생각해보지 못 한 관점이네요. private String name = "아무개"; 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. 만약 그렇게 된다면 큰 차이는 없을 것 같아요! 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 String inputParticipants(){ | ||
System.out.println("참여할 사람 이름을 입력하세요. (이름은 쉼표(,)로 구분하세요)"); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public String inputResults(){ | ||
System.out.println("\n"+"실행 결과를 입력하세요. (결과는 쉼표(,)로 구분하세요)"); | ||
return scanner.nextLine(); | ||
} | ||
|
||
public Integer inputLadderHeight(){ | ||
System.out.println("\n"+"최대 사다리 높이는 몇 개인가요?"); | ||
return Integer.parseInt(scanner.nextLine()); | ||
} | ||
|
||
public String inputParticipantToWantResult(){ | ||
System.out.println("\n"+"결과를 보고 싶은 사람은?"); | ||
return scanner.nextLine(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package view; | ||
|
||
import model.Participant; | ||
import model.LadderRow; | ||
|
||
import java.util.List; | ||
|
||
public class OutputView { | ||
private static final int COLUMN_LENGTH = 5; | ||
|
||
private void outputParticipant(List<Participant> participantInventory) { | ||
for (Participant participant : participantInventory) { | ||
String participantName = participant.getName(); | ||
System.out.printf("%" + COLUMN_LENGTH + "s", participantName); | ||
System.out.printf(" "); | ||
} | ||
} | ||
|
||
private void outputLadderRow(List<Boolean> points) { | ||
System.out.println(); | ||
System.out.print(" |"); | ||
for (int i = 0; i < points.size(); i++) { | ||
if (points.get(i) == true) System.out.print("-----|"); | ||
else if (points.get(i) == false) System.out.print(" |"); | ||
} | ||
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.
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. private void outputLadderColumn(Boolean bool) {
if (bool == true) System.out.print("-----|");
if (bool == false) System.out.print(" |");
} 지금은 현재 for문 안의 내용을 이렇게 따로 메소드를 만들어 빼주었습니다!! <질문>제가 이해를 잘 못 한 거 같은 데 else를 안 써도 될 거 같다는 말이 else if를 if로 바꾸는 게 좋을 거 같다는 말인가요? 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 void outputLadder(List<Participant> participantInventory, List<LadderRow> ladderRows, List<String> trialResults) { | ||
System.out.println("\n"+"사다리 결과"+"\n"); | ||
outputParticipant(participantInventory); | ||
for (int i = 0; i < ladderRows.size(); i++) { | ||
List<Boolean> points = ladderRows.get(i).getPoints(); | ||
outputLadderRow(points); | ||
} | ||
outputResults(trialResults); | ||
System.out.println(); | ||
} | ||
|
||
private void outputResults(List<String> trialResults) { | ||
System.out.println(); | ||
for (String trialResult : trialResults) { | ||
System.out.printf("%" + COLUMN_LENGTH + "s", trialResult); | ||
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. 와... 이렇게 쓸 수 있다는 것을 저도 처음봤어요 신기하네요! java 쪽에서는 printf 를 활용한 코드를 일반적으로는 많이 사용하지 않는 것 같은데요 StringBuilder result=new StrintBuilder();
for(int i=0;i<COLUMN_LENGTH;i++){
result.append("-")
} 약간 이런 느낌으로 해도 좋을 것 같은데 어떠신가요? 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. <질문>혹시.. 이런식으로 활용하라는 말씀이 맞을까용? for (String trialResult : trialResults) {
StringBuilder result=new StringBuilder();
for(int i=0;i<COLUMN_LENGTH-trialResult.length();i++){
result.append(" ");
}
result.append(trialResult+" ");
System.out.print(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. 오 좋아요 딱 이런 느낌이었어요 |
||
System.out.printf(" "); | ||
} | ||
} | ||
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. 헉 제가 출력할 때 폭을 어떻게 지정해야 하는지 몰라서 그냥 지정하지 않은 채로 냅뒀었는데 이런 방법이 있었네요! |
||
|
||
private void outputAllParticipantsResult(List<Participant> participantInventory) { | ||
for (Participant participant : participantInventory) { | ||
String participantName = participant.getName(); | ||
String participantResult = participant.getResult(); | ||
System.out.println(participantName + " : " + participantResult); | ||
} | ||
} | ||
|
||
private void outputCertainParticipantResult(List<Participant> participantInventory, String participantName) { | ||
for (Participant participant : participantInventory) { | ||
if (participantName.equals(participant.getName())) { | ||
String participantResult = participant.getResult(); | ||
System.out.println(participantName + " : " + participantResult); | ||
} | ||
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.
이 조건을 충족시키기 위해서 스트림을 사용해보는 건 어떨까요? 그리고 이름을 찾는 행위가 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. 제가 지금 우아한테크코스 프리코스를 진행 중인데요. 공동 피드백을 보면 "객체는 객체답게 사용한다."라는 부분이 있거든요? 객체는 객체답게 사용한다는 말이 getter setter만 있고 로직은 없는 객체가 있다면, 상희님의 조언을 듣고 이 말이 갑자기 떠올랐어요. 사실 객체는 객체답게 쓰라는 말이 잘 이해가 안 갔는 데 이렇게 예시를 보고 나니까 get해서 쓰지말고 객체가 스스로 처리 하도록 해야한다는 게 무슨 말인지 알겠네요 :) 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. 그리고 stream의 filter기능을 사용하면 들여쓰기를 줄일 수 있다는 점은 또 몰랐네요!! public String getCertainParticipantResult(String participantName){
return participantInventory.stream()
.filter(participant -> participantName.equals(participant.getName()))
.map(Participant::getResult)
.findFirst()
.orElse(null);
} 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. ----- 제가 결과값을 Map으로 저장하면서 지금은 사용자이름을 키값으로 사용자의 결과를 불러오는 코드로 변경되었습니다. |
||
} | ||
} | ||
|
||
public void outputParticipantResult(List<Participant> participantInventory, String participantName) { | ||
System.out.println("\n" + "실행결과"); | ||
if (participantName.equals("all")) { | ||
outputAllParticipantsResult(participantInventory); | ||
return; | ||
} | ||
outputCertainParticipantResult(participantInventory, participantName); | ||
} | ||
} |
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.
헉 리드미를 작성해주셔서 한 눈에 보기가 편하네요!