-
Notifications
You must be signed in to change notification settings - Fork 13
[0주차] 6기 객체지향 코드 연습(최세린) #7
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: main
Are you sure you want to change the base?
Changes from all commits
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,47 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Controller { | ||
| private final View view; | ||
| private final List<Member> members = new ArrayList<>(); | ||
|
|
||
| public Controller(View view) { | ||
| this.view = view; | ||
| } | ||
|
|
||
| //레이스 시작 | ||
| public void startRace() { | ||
| // 경주차 등록 | ||
| String[] names = view.getMemberNames(); | ||
| for (String name : names) { | ||
| members.add(new Member(name.trim(),0)); | ||
| } | ||
|
|
||
| // 시도 횟수 | ||
| int tryCount = view.getTryCount(); | ||
|
|
||
| // 경주 진행 | ||
| for (int i = 0; i < tryCount; i++) { | ||
| for (Member m : members) { | ||
| m.startRandom(); | ||
| } | ||
| view.printProgress(members); | ||
| } | ||
|
|
||
| // 우승자 계산 | ||
| int max = 0; | ||
| for (Member m : members) { | ||
| if (m.getGo() > max) { | ||
| max = m.getGo(); | ||
| } | ||
| } | ||
| List<Member> winners = new ArrayList<>(); | ||
| for (Member m : members) | ||
| if (m.getGo() == max) { | ||
| winners.add(m); | ||
| } | ||
| view.printWinners(winners); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package racingcar; | ||
|
|
||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.Random; | ||
|
|
||
| public class Member { | ||
| private String name; | ||
| private int go; | ||
|
Comment on lines
+8
to
+9
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 Member(String name, int go) { | ||
| validateName(name); | ||
| this.name = name; | ||
| this.go = 0; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getGo() { | ||
| return go; | ||
| } | ||
| //랜덤 뽑기 | ||
| public void startRandom() { | ||
| int randomValue = Randoms.pickNumberInRange(0, 9); | ||
| if (randomValue >= 4) { // 4 이상이면 전진 | ||
| go++; | ||
| } | ||
| } | ||
| //예외처리 | ||
| private void validateName(String name) { | ||
| if (name == null || name.isBlank() || name.length() > 5) { | ||
| throw new IllegalArgumentException("자동차 이름은 1~5자 사이여야 합니다."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package racingcar; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class View { | ||
| private final Scanner scanner; | ||
|
|
||
| public View() { | ||
| this.scanner = new Scanner(System.in); | ||
| } | ||
| //경주차 이름 입력 | ||
| public String[] getMemberNames() { | ||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
| List<String> lst = List.of(scanner.nextLine().split(",")); | ||
|
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. split을 통해 배열로도 받을 수 있는 데 List로 받으신 이유가 특별히 있나요? |
||
|
|
||
| for (String s : lst) { | ||
| String trimmed = s.trim(); | ||
|
|
||
| if (trimmed.isEmpty()) { | ||
| throw new IllegalArgumentException("자동차 이름이 공백"); | ||
| } | ||
|
|
||
| if (trimmed.length() > 5) { | ||
| throw new IllegalArgumentException("이름 오류"); | ||
| } | ||
|
Comment on lines
+20
to
+26
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. View에서 검사까지 하도록 일부로 노리신 건가요? 책임 분리 해볼까요? |
||
| } | ||
|
|
||
| return lst.stream() | ||
| .map(String::trim) | ||
| .toArray(String[]::new); | ||
| } | ||
| //시도 회수 결정 | ||
| public int getTryCount() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
| int tryNum = scanner.nextInt(); | ||
| if (tryNum <= 0) { | ||
| throw new IllegalArgumentException("시도 횟수는 이상"); | ||
| } | ||
| return tryNum; | ||
| } | ||
| //경주차 간거 표시 | ||
| public void printProgress(List<Member> members) { | ||
| for (Member m : members) { | ||
| System.out.println(m.getName() + " : " + "-".repeat(m.getGo())); | ||
|
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. member에서 get을 사용하지 않고 값을 보여주도록 해볼까요? |
||
| } | ||
| System.out.println(); | ||
| } | ||
| //우승자 발표 | ||
| public void printWinners(List<Member> winners) { | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners.stream().map(Member::getName).toList())); | ||
| } | ||
| } | ||
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.
현재는 무조건 하나의 게임만 존재하기 떄문에 이렇게 구현해도 괜찮지만, 한번 이 Controller가 상태를 갖지 않도록 해볼까요?