Skip to content

Commit c3410f0

Browse files
committed
fix: lab02
1 parent e3de03d commit c3410f0

File tree

11 files changed

+703
-0
lines changed

11 files changed

+703
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
2+
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
3+
public class Main {
4+
public static void main(String[] args) {
5+
6+
}
7+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package lab02.handout;
2+
3+
import java.util.Random;
4+
5+
6+
public class Car implements Comparable<Car>{
7+
private final String carName;
8+
private int moveCount;
9+
10+
public Car(String carName) {
11+
this.carName = carName;
12+
this.moveCount = 0;
13+
}
14+
15+
public int randomNum() {
16+
Random random = new Random();
17+
return random.nextInt(9);
18+
} // 자동차의 움직임 여부 판단을 위한 난수 생성
19+
20+
public boolean checkAhead(int num) {
21+
return num >= 4;
22+
} // 자동차의 움직임 여부 판단
23+
24+
public void increaseCount() {
25+
int check = randomNum();
26+
if (checkAhead(check)) {
27+
moveCount++;
28+
}
29+
}
30+
31+
public void increaseCount(int check) {
32+
if(checkAhead(check)) {
33+
moveCount++;
34+
}
35+
}
36+
@Override
37+
public String toString() {
38+
return carName + " : " + "-".repeat(moveCount);
39+
}
40+
41+
@Override
42+
public int compareTo(Car car) {
43+
return Integer.compare(this.moveCount, car.moveCount);
44+
}
45+
46+
public void setMoveCount(int moveCount) {
47+
this.moveCount = moveCount;
48+
}
49+
50+
public String getCarName() {
51+
return carName;
52+
}
53+
54+
public int getMoveCount() {
55+
return moveCount;
56+
}
57+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package lab02.handout;
2+
3+
import lab02.solution.Car;
4+
import lab02.solution.Referee;
5+
6+
import java.io.IOException;
7+
import java.util.ArrayList;
8+
import java.util.Scanner;
9+
import java.util.StringTokenizer;
10+
11+
public class RacingCarApplication {
12+
13+
private static final Scanner scanner = new Scanner(System.in);
14+
private static final ArrayList<Car> cars = new ArrayList<>();
15+
16+
private static int repeatCount;
17+
private static boolean isValidCount = false;
18+
19+
20+
public static void main(String[] args) throws IOException {
21+
do {
22+
carName();
23+
} while(cars.isEmpty());
24+
25+
while (!isValidCount) {
26+
System.out.println("시도할 횟수는 몇 회인가요?");
27+
String input = scanner.nextLine();
28+
29+
if (input == null || input.isEmpty()) {
30+
System.out.println("입력된 값이 없습니다. 다시 입력해 주세요.");
31+
continue;
32+
} else if (!isNumeric(input)) {
33+
System.out.println("반복 횟수는 숫자만 입력해 주세요. 다시 입력해 주세요.");
34+
continue;
35+
}
36+
repeatCount = Integer.parseInt(input);
37+
if (repeatCount > 0) {
38+
isValidCount = true;
39+
} else {
40+
System.out.println("반복 횟수는 1 이상의 정수여야 합니다. 다시 입력해 주세요.");
41+
}
42+
}
43+
while (repeatCount > 0) {
44+
repeatIncrease();
45+
repeatCount--;
46+
}
47+
Referee referee = new Referee(cars);
48+
referee.decideTheGame();
49+
referee.printResult();
50+
}
51+
52+
public static void repeatIncrease() {
53+
for (Car car : cars) {
54+
car.increaseCount();
55+
System.out.println(car);
56+
}
57+
System.out.println();
58+
}
59+
60+
public static void carName() {
61+
StringTokenizer st = null;
62+
do {
63+
System.out.println("경주할 자동차 이름을 입력하세요 (쉼표(,)로 구분).");
64+
String input = scanner.nextLine();
65+
if (input == null || input.trim().isEmpty()) {
66+
System.out.println("자동차 이름을 반드시 입력해야 합니다.");
67+
continue;
68+
}
69+
st = new StringTokenizer(input, ",");
70+
if(st.countTokens() < 2) {
71+
System.out.println("최소 2대 이상의 자동차를 쉼표(,)로 구분해서 입력해야 합니다.");
72+
continue;
73+
}
74+
break;
75+
} while(true);
76+
77+
while (st.hasMoreElements()) {
78+
String name = st.nextToken().strip();
79+
if (!name.isEmpty() && name.length() <= 5) {
80+
cars.add(new Car(name));
81+
}
82+
}
83+
}
84+
85+
/**
86+
* 입력된 문자열이 숫자로만 구성되어 있는지 확인합니다.
87+
*/
88+
private static boolean isNumeric(String str) {
89+
if (str == null || str.isEmpty()) {
90+
return false;
91+
}
92+
for (char c : str.toCharArray()) {
93+
if (!Character.isDigit(c)) {
94+
return false;
95+
}
96+
}
97+
return true;
98+
}
99+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package lab02.handout;
2+
3+
import lab02.solution.Car;
4+
5+
import java.util.ArrayList;
6+
7+
public class Referee {
8+
private ArrayList<lab02.solution.Car> cars;
9+
10+
private static ArrayList<lab02.solution.Car> victoryCars;
11+
12+
public Referee() {}
13+
public Referee(ArrayList<lab02.solution.Car> cars) {
14+
this.cars = cars;
15+
}
16+
17+
public ArrayList<lab02.solution.Car> getVictoryCars() {
18+
return victoryCars;
19+
}
20+
21+
public void addCar(lab02.solution.Car car) {
22+
cars.add(car);
23+
}
24+
public void decideTheGame() {
25+
initVictoryCar();
26+
for (lab02.solution.Car car : cars) {
27+
faster(car);
28+
}
29+
} // 우승자 판별
30+
31+
public void printResult() {
32+
StringBuilder result = new StringBuilder();
33+
for (lab02.solution.Car victoryCar : victoryCars) {
34+
result.append(victoryCar.getCarName())
35+
.append(", ");
36+
}
37+
result = new StringBuilder(result.substring(0, result.length() - 2));
38+
result.append("가 최종우승했습니다.");
39+
System.out.println(result);
40+
} // 우승 결과 출력
41+
42+
public void initVictoryCar() {
43+
ArrayList<lab02.solution.Car> list = new ArrayList<>();
44+
if (!cars.isEmpty()) {
45+
list.add(cars.get(0));
46+
}
47+
victoryCars = list;
48+
} // 우승자를 판별할 초기값
49+
50+
public void faster(lab02.solution.Car cmp) {
51+
Car tmp= victoryCars.get(0);
52+
if(tmp.equals(cmp)) return;
53+
if(tmp.compareTo(cmp) > 0){
54+
return;
55+
}
56+
if(tmp.compareTo(cmp) == 0) {
57+
victoryCars.add(cmp);
58+
return;
59+
}
60+
victoryCars.clear();
61+
victoryCars.add(cmp);
62+
} // 우상자 판별을 위한 거리 비교
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package lab02.solution;
2+
3+
import java.util.Random;
4+
5+
6+
public class Car implements Comparable<Car>{
7+
private final String carName;
8+
private int moveCount;
9+
10+
public Car(String carName) {
11+
this.carName = carName;
12+
this.moveCount = 0;
13+
}
14+
15+
public int randomNum() {
16+
Random random = new Random();
17+
return random.nextInt(9);
18+
} // 자동차의 움직임 여부 판단을 위한 난수 생성
19+
20+
public boolean checkAhead(int num) {
21+
return num >= 4;
22+
} // 자동차의 움직임 여부 판단
23+
24+
public void increaseCount() {
25+
int check = randomNum();
26+
if (checkAhead(check)) {
27+
moveCount++;
28+
}
29+
}
30+
31+
public void increaseCount(int check) {
32+
if(checkAhead(check)) {
33+
moveCount++;
34+
}
35+
}
36+
@Override
37+
public String toString() {
38+
return carName + " : " + "-".repeat(moveCount);
39+
}
40+
41+
@Override
42+
public int compareTo(Car car) {
43+
return Integer.compare(this.moveCount, car.moveCount);
44+
}
45+
46+
public void setMoveCount(int moveCount) {
47+
this.moveCount = moveCount;
48+
}
49+
50+
public String getCarName() {
51+
return carName;
52+
}
53+
54+
public int getMoveCount() {
55+
return moveCount;
56+
}
57+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package lab02.solution;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.ArrayList;
7+
import java.util.StringTokenizer;
8+
9+
public class RacingCarApplication {
10+
private static final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
11+
private static final ArrayList<Car> cars = new ArrayList<>();
12+
private static int repeatCount;
13+
14+
public static void main(String[] args){
15+
try {
16+
carName();
17+
repeatCount();
18+
} catch (IOException e) {
19+
System.out.println("입력된 값이 없습니다");
20+
e.getStackTrace();
21+
} catch (NumberFormatException e) {
22+
System.out.println("반복횟수는 int 타입 입니다.");
23+
} catch (Exception e) {
24+
System.out.println("경주할 자동차 이름의 구분은 (,)를 기준으로 구분 해야합니다.");
25+
}
26+
27+
while (repeatCount > 0) {
28+
repeatIncrease();
29+
repeatCount--;
30+
}
31+
32+
Referee referee = new Referee(cars);
33+
referee.decideTheGame();
34+
referee.printResult();
35+
}
36+
37+
public static void repeatIncrease() {
38+
for (Car car : cars) {
39+
car.increaseCount();
40+
System.out.println(car);
41+
}
42+
System.out.println();
43+
}
44+
45+
public static void carName() throws IOException {
46+
System.out.println("경주할 자동차 이름을 입력하세요(이름은 쉽표(,)를 기준으로 구분.");
47+
StringTokenizer st = new StringTokenizer(bufferedReader.readLine(),",");
48+
49+
while (st.hasMoreElements()) {
50+
String name = st.nextToken().strip();
51+
if(name.length() <= 5) {
52+
cars.add(new Car(name));
53+
}
54+
}
55+
}
56+
57+
public static void repeatCount() throws IOException {
58+
System.out.println("시도할 횟수는 몇회인가요?");
59+
repeatCount = Integer.parseInt(bufferedReader.readLine());
60+
}
61+
}

0 commit comments

Comments
 (0)