-
Notifications
You must be signed in to change notification settings - Fork 29
원지웅_BackEnd 2주차 Race 과제 제출 #1
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
b4993b7
3d893ba
5aedd62
0bae63f
3dfd8ee
000c791
0d6a99a
95ad598
572fa69
5b0ecad
9670b88
e39b0ad
496778f
472f333
2bc7c23
ab1bc50
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,27 @@ | ||
| ## 과제 관련 기능목록 및 구현 방식 | ||
|
|
||
| 클래스 제작 : Race,Car | ||
|
|
||
| Race는 Racing의 대한 전반적인 기능들을 담당 | ||
| Car는 전반적으로 자동차 자체의 기능 및 상태를 담당 | ||
|
|
||
|
|
||
| > ()는 메서드, 아무것도 없으면 상태 및 에러 | ||
| ## Car | ||
| 1. name : 이름 | ||
| 2. stack : 자동차가 얼마나 갔는지를 확인하기 위해 | ||
| 3. action () : stack이 증가 및 유지관련 메서드 | ||
| 4. toString() : Race관련 메세지를 출력해주는 메서드 | ||
| 5. 에러 이름에 빈공간이 있는지 확인 | ||
|
|
||
| ## Race | ||
| 1. Race initCar() : 자동차 관련 세팅 메서드 | ||
| 2. Race start() : 실제 로직 | ||
| 3. Race initCount() : 몇번 돌지 등등 Race 관련 메서드 | ||
| 4. carList : list race 상황중 값을 담기 위해 | ||
| 5. final_victory : 이후 stream으로 값을 정리함 | ||
| 6. race_count : 몇번 반복할지 | ||
|
|
||
| ## Main method | ||
| 1. Race 호출 | ||
|
|
| 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.stream.IntStream; | ||
|
|
||
| public class Car { | ||
|
|
||
| private String name; | ||
| private int stack; | ||
|
|
||
| public Car(String name) { | ||
| if (name.isBlank()) throw new IllegalArgumentException("빈칸"); | ||
| if (name.length() > 5) throw new IllegalArgumentException("길이"); | ||
| if (name.contains(" ")) throw new IllegalArgumentException("이름에 공백이 포함되었습니다."); | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public int getStack() { | ||
| return stack; | ||
| } | ||
|
|
||
| public void action(){ | ||
| if (Randoms.pickNumberInRange(0,9) >= 4){ | ||
| stack++; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name+" : "+ "-".repeat(stack); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package racingcar; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
| import camp.nextstep.edu.missionutils.Randoms; | ||
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Race { | ||
|
|
||
| private List<Car> carList; | ||
| private int raceCount; | ||
|
|
||
| public Race() { | ||
| } | ||
|
|
||
| public void start(){ | ||
| initCar(); | ||
| initRace(); | ||
| } | ||
|
|
||
| private void initCar() { | ||
dnjswldnd-3513 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| carList = new ArrayList<>(); | ||
dnjswldnd-3513 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)"); | ||
|
|
||
| for(String t : Console.readLine().trim().split(",")){ | ||
| Car car = new Car(t); | ||
| if (carList.stream().anyMatch(c -> c.getName().equals(t.trim()))){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| carList.add(car); | ||
| } | ||
| } | ||
|
|
||
| private void initRace() { | ||
| System.out.println("시도할 회수는 몇회인가요?"); | ||
|
|
||
| raceCount = Integer.parseInt(camp.nextstep.edu.missionutils.Console.readLine()); | ||
| if (raceCount <= 0){ | ||
| throw new IllegalArgumentException(); | ||
| } | ||
| raceStart(); | ||
|
Collaborator
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.
Author
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. 아까 그 'raceList()' 부분에서 말씀드린것처럼 |
||
| } | ||
|
|
||
| private void raceStart() { | ||
| System.out.println("실행 결과"); | ||
|
|
||
| for (int i = 0; i < raceCount; i++) { | ||
| carList.forEach(v -> { | ||
| v.action() ; | ||
| System.out.println(v.toString()); | ||
| }); | ||
| } | ||
|
|
||
| int maxStack = carList | ||
| .stream() | ||
| .mapToInt(f -> f.getStack()) | ||
| .max() | ||
| .getAsInt(); | ||
|
|
||
| String finalVictory = carList | ||
| .stream() | ||
| .filter(f -> f.getStack() == maxStack) | ||
| .map(Car::getName) | ||
| .collect(Collectors.joining(", ")); | ||
|
|
||
| System.out.println("최종 우승자 : " + finalVictory); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.