Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
d52adc5
기능 요구 사항
Kim-Dohun148 Sep 19, 2025
2d5e6e0
Car 클래스 생성
Kim-Dohun148 Sep 19, 2025
ae581f9
Car 클래스 Move함수 구현
Kim-Dohun148 Sep 19, 2025
4ca3b0f
Car 클래스 이름 입력 함수 구현
Kim-Dohun148 Sep 20, 2025
ee8ff0e
Racint 클래스 생성
Kim-Dohun148 Sep 20, 2025
cde6b87
Racint -> Racing
Kim-Dohun148 Sep 20, 2025
d30087b
Racing 클래스 시도 횟수 입력 함수 구현
Kim-Dohun148 Sep 20, 2025
e657adb
Racing 클래스 게임 진행 상황 출력 함수 구현
Kim-Dohun148 Sep 20, 2025
3744597
Racing 클래스 게임 종료 함수 구현
Kim-Dohun148 Sep 21, 2025
679a445
Racing 클래스 게임 진행 메인 함수 구현
Kim-Dohun148 Sep 21, 2025
e7b1003
기능 요구 사항 작성 완료
Kim-Dohun148 Sep 22, 2025
4fc7ffa
Racing 클래스 게임 종료 함수 수정
Kim-Dohun148 Sep 22, 2025
9f0c122
Application 코드 작성
Kim-Dohun148 Sep 22, 2025
f9d7fdf
Car 클래스 수정
Kim-Dohun148 Sep 22, 2025
30607b2
2주차 과제 리팩토링 사항 정리
Kim-Dohun148 Sep 24, 2025
cd45b64
docs : 2주차 과제 리팩토링 사항 정리
Kim-Dohun148 Sep 24, 2025
7b50757
feat : Interface 클래스 생성
Kim-Dohun148 Sep 24, 2025
4da2c20
Interface 클래스 생성 -> Interface 생성
Kim-Dohun148 Sep 24, 2025
656f8bc
feat : Interface Move 생성
Kim-Dohun148 Sep 24, 2025
f9873be
chore : 자바 네이밍 규칙에 따른 함수, 변수 이름 변경
Kim-Dohun148 Sep 26, 2025
1748a1e
chore : Car 클래스 이름 제한 수 상수화
Kim-Dohun148 Sep 26, 2025
5bd731c
chore : Racing 클래스 자바 네이밍 규칙에 따른 함수, 변수 이름 변경
Kim-Dohun148 Sep 26, 2025
08ebdf4
refactor : Racing 클래스 Car클래스와 상속 관계 수정 시작
Kim-Dohun148 Sep 26, 2025
d4af3bc
feat : Racing 클래스 getData 함수 구현
Kim-Dohun148 Sep 26, 2025
5a6e949
refactor : Racing 클래스 display 메소드 변수 재설정
Kim-Dohun148 Sep 26, 2025
0607e36
refactor : Racing 클래스 end 메소드 변수 재설정
Kim-Dohun148 Sep 26, 2025
6087109
refactor : Racing 클래스 생성자 구현 및 멤버 변수 car 생성
Kim-Dohun148 Sep 27, 2025
f6c0496
refactor : Racing 클래스 getData 메소드 삭제
Kim-Dohun148 Sep 27, 2025
8be2536
refactor : Car 클래스 멤버 변수 및 메소드 접근 제어자 재설정
Kim-Dohun148 Sep 27, 2025
8e49bce
refactor : Racing 클래스 메소드 Car 객체의 변수로 재설정
Kim-Dohun148 Sep 27, 2025
a695e83
refactor : Racing 클래스 생성자 매개변수 수정 및 메소드 변수 재설정
Kim-Dohun148 Sep 27, 2025
160fcdc
refactor : Racing 클래스 display 메소드 수정
Kim-Dohun148 Sep 27, 2025
a757a09
refactor : Application 메인 코드 작성
Kim-Dohun148 Sep 27, 2025
36208d2
style : Car 클래스 불필요 개행 삭제
Kim-Dohun148 Sep 27, 2025
42b4169
style : Racing 클래스 불필요 개행 삭제
Kim-Dohun148 Sep 27, 2025
b893357
docs : 2주차 과제 요구사항 정리
Kim-Dohun148 Sep 27, 2025
99a4904
chore : Interface 파일명 Move로 변경
Kim-Dohun148 Sep 27, 2025
bcb64f8
style Application 불필요 개행 삭제
Kim-Dohun148 Sep 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
[기능 요구 사항]
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
■ 자동차 전진 또는 정지
- 전진 조건: 0~9사이 무작위 값이 4 이상인 경우

■ 자동차 이름 부여
- 각 자동차 이름은 쉼표(,)를 기준으로 구분
- 이름은 5자 이하만 가능, (잘못된 값 입력시 IllegalArgumentException을 발생시킨 후 앱 종료)
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
ㄴ Car 클래스

ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
■ 경주 게임 진행 횟수 입력 힘수
- 진행 횟수는 음수일 수 없음 (잘못된 값 입력시 IllegalArgumentException을 발생시킨 후 앱 종료)

■ 실시간 경주 게임 진행 상황 출력 함수
- 전진하는 자동차 출력시 이름도 출력

■ 경주 게임 종료 함수
- 우승자 출력
- 우승자는 한 명 이상 가능
- 우승자 여러 명일 경우 쉼표(,)를 기준으로 구분
- 진행 횟수동안 가장 많이 전진한 자동차가 우승

■ 경주 게임 진행 메인 함수
- 시도 횟수 입력받기
- 시도 횟수 동안 게임 진행
- 우승자 출력
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
ㄴ Racing 클래스


ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
[2주차 과제-리팩토링]

SOLID 원칙
■ SRP : 단일책임원칙 (하나의 클래스는 하나의 기능만 담당)
- Car class : 자동차 움직임, 이름 입력 기능
- Racing class : 경주게임 기능

■ OCP - 개방폐쇠원칙 (확장 O, 수정 X)

■ LSP - 리스코프치환원칙 (하위타입은 언제나 상위타입으로 교체 가능해야함)

■ ISP - 인터페이스 분리 (목적, 용도에 적합한 인터페이스만을 제공)
- 자동차 움직임 기능을 인터페이스로 분리하여 제공

■ DIP - 의존 역전 원칙 (직접 클래스 참조하는 것이 아닌 그 대상의 상위요소로 참조)

■ 커밋 컨벤션
- feat : 새로운 기능 추가
- fix : 버그 수정
- docs : 문서의 수정
- style : (코드의 수정 없이) 스타일만 변경 (들여쓰기 같은 포맷이나 세미콜런 빼먹은 경우)
- refactor : 코드를 리팩토링
- test : Test 관련한 코드의 추가, 수정
- chore (코드의 수정 없이) 설정을 변경

■ 자바 네이밍 규칙
- 클래스 (Class)
클래스명은 파스칼을 사용한다.
클래스명은 명사로 시작한다.
인터페이스, 추상 클래스, 인터페이스를 구현하는 클래스
Ex) public abstract class Animal {}

- 메소드
메소드는 카멜 표기법으로 (첫자는 소문자)
동사로 시작
computeAge( ) , printName( )

- 변수
소문자 OR 파스칼
String userName;
Integer age

- 상수 (final)
대문자
한번 선언되고 바뀌지 않는 변수는, static final 처리

■ 수정
- Application.java car, Game 명명 수정
- Car.java start, end 명명 수정
- 메소드 형태 유지
- Racing, Car 상속 관계 수정
- 이름 예외처리 제한 글자수도 상수화
ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ
6 changes: 5 additions & 1 deletion src/main/java/racingcar/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
Car car = new Car();
car.getNames();

Racing game = new Racing(car);
game.start();
}
}
42 changes: 42 additions & 0 deletions src/main/java/racingcar/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;
import camp.nextstep.edu.missionutils.Randoms;

class Car implements Move {
private static final int MOVING_FORWARD = 4;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기도 MOVING_FORWARD와 start/end의 변수 명명을 구분하신 이유가 있으신가요?
변수와 상수의 차이에 대해 알아보시면 좋을 것 같습니다!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바 네이밍 규칙 준수하겠습니다.

private static final int START = 0;
private static final int END = 9;
private static final int MAX_LENGTH = 5;

public String[] names;
public int[] moveCount;

private void getMove() {
moveCount = new int[names.length];
}

public void move() {
for (int i = 0; i < names.length; i++) {
int random = Randoms.pickNumberInRange(START, END);
if (random >= MOVING_FORWARD) {
moveCount[i] += 1;
}
}
}

public void getNames() {
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String string = Console.readLine();
names = string.split(",");
String name;

for (int i = 0; i < names.length; i++) {
name = names[i];
if (name.length() > MAX_LENGTH) {
throw new IllegalArgumentException("이름은 5자 이하만 가능합니다.");
}
}
getMove();
}
}
5 changes: 5 additions & 0 deletions src/main/java/racingcar/Move.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package racingcar;

interface Move {
void move();
}
70 changes: 70 additions & 0 deletions src/main/java/racingcar/Racing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package racingcar;

import camp.nextstep.edu.missionutils.Console;
import java.util.ArrayList;

public class Racing {
private int round;
private Car players;

public Racing(Car car) {
players = car;
}

public void getRound() {
System.out.println("시도할 횟수는 몇회인가요?");
String input = Console.readLine();
round = Integer.parseInt(input);

if (round < 0) {
throw new IllegalArgumentException("시도 횟수는 음수일 수 없습니다.");
}
}

public void display() {
for (int i = 0; i < players.names.length; i++) {
System.out.printf("%s : ", players.names[i]);
System.out.printf("%s", "-".repeat(players.moveCount[i]));
System.out.println();
}
System.out.println();
}

public void end() {
ArrayList<String> Winner = new ArrayList<String>();
int max = players.moveCount[0];

for (int i = 0; i < players.names.length; i++) {
for (int j = i + 1; j < players.names.length; j++) {
if (max <= players.moveCount[j]) {
max = players.moveCount[j];
}
}
}
// 우승자 리스트 추가
for (int i = 0; i < players.names.length; i++) {
if (players.moveCount[i] == max) {
Winner.add(players.names[i]);
}
}
System.out.print("최종 우승자 : ");
if (Winner.size() > 1) {
String Winners = String.join(", ", Winner);
System.out.print(Winners);
}
else {
System.out.print(Winner.get(0));
}
}

public void start() {
getRound();

System.out.println("실행 결과");
for (int i = 0; i < round; i++) {
players.move();
display();
}
end();
}
}