-
Notifications
You must be signed in to change notification settings - Fork 52
[황명하_BackEnd] 2주차 과제 제출합니다 #57
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,50 +3,62 @@ | |
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
| import camp.nextstep.edu.missionutils.Console; | ||
|
|
||
| public class Application | ||
| { | ||
| public static void main(String[] args) | ||
| { | ||
| // TODO: 프로그램 구현 | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| Scanner sc = 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. 여기서는 굳이 scanner가 필요하지 않을 것 같아요 |
||
| String[] names = sc.nextLine().split(","); | ||
|
|
||
| List<Car> cars = new ArrayList<>(); | ||
| List<Car> cars = getCars(sc); | ||
| int count = getTrialCount(sc); | ||
|
|
||
| for (String name : names) | ||
| { | ||
| cars.add(new Car(name)); | ||
| raceCars(cars, count); | ||
| List<String> winners = getWinners(cars); | ||
|
|
||
| printWinners(winners); | ||
| } | ||
|
|
||
| private static List<Car> getCars(Scanner sc) { | ||
|
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.print("차 이름들을 입력하세요 (쉼표로 구분): "); | ||
| String[] names = Console.readLine().split(","); | ||
|
|
||
| List<Car> cars = new ArrayList<>(); | ||
| for (String name : names) { | ||
| cars.add(new Car(name.trim())); | ||
| } | ||
| return cars; | ||
| } | ||
|
|
||
| int count=sc.nextInt(); | ||
| private static int getTrialCount(Scanner sc) { | ||
| System.out.print("시도할 횟수를 입력하세요: "); | ||
| return sc.nextInt(); | ||
| } | ||
|
|
||
| private static void raceCars(List<Car> cars, int count) { | ||
| for (int i = 0; i < count; i++) { | ||
| for (Car car : cars) { | ||
| car.movement(); | ||
| System.out.println(car.getName()+ " : " + "-".repeat(car.getPosition())); | ||
| System.out.println(car.getName() + " : " + "-".repeat(car.getPosition())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| int win=0; | ||
|
|
||
| for(Car car:cars) | ||
| { | ||
| if(car.getPosition()>win) { | ||
| win = car.getPosition(); | ||
| } | ||
| private static List<String> getWinners(List<Car> cars) { | ||
| int maxPosition = 0; | ||
| for (Car car : cars) { | ||
| maxPosition = Math.max(maxPosition, car.getPosition()); | ||
| } | ||
| List<String> winCars = new ArrayList<>(); | ||
|
|
||
| for(Car car: cars) | ||
| { | ||
| if(car.getPosition() == win) | ||
| { | ||
| winCars.add(car.getName()); | ||
| List<String> winners = new ArrayList<>(); | ||
| for (Car car : cars) { | ||
|
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 (car.getPosition() == maxPosition) { | ||
| winners.add(car.getName()); | ||
| } | ||
| } | ||
| System.out.print("winner"+String.join(", ",winCars)); | ||
| return winners; | ||
| } | ||
|
|
||
| private static void printWinners(List<String> winners) { | ||
| System.out.println("최종 우승자 : " + String.join(", ", winners)); | ||
| } | ||
| } | ||
| } | ||
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.
클래스가 여러 책임을 가지고 있어서 더 분리할 수 있을 것 같아요