-
Notifications
You must be signed in to change notification settings - Fork 23
n주차 과제 #103
base: dlams
Are you sure you want to change the base?
n주차 과제 #103
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,3 @@ | ||
.idea | ||
out | ||
javaStudyWeek2.iml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,4 @@ | ||
# 2023-1-java-study | ||
SSCC 2023년 1학기 Java 기초 스터디 과제 repo입니다. | ||
스터디원 여러분은 아래 규칙에 맞추어 과제를 올려주세요. | ||
|
||
## 과제 제출법 | ||
1. SSCC Organization에 멤버 추가를 요청한다. | ||
2. Java 스터디 repo에 본인 Github 아이디로 브랜치를 생성한다. | ||
3. 스터디 repo를 fork한다. | ||
4. fork한 repo(내 계정!!!)에 과제를 업로드한다. | ||
5. SSCC-space의 본인 닉네임 브랜치로 PR을 넣는다. | ||
6. 스터디장이 Comment로 피드백을 준다. Merge되면 과제 완료! | ||
SSCC | ||
2023년 1학기 Java 기초 스터디 과제 **repo**. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="module-library"> | ||
<library name="JUnit5.8.1"> | ||
<CLASSES> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" /> | ||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
</library> | ||
</orderEntry> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import utils.Car; | ||
import utils.CarBuilder; | ||
|
||
public class DefaultCarBuilder implements CarBuilder { | ||
String name; | ||
int speed; | ||
boolean isSupercar; | ||
|
||
public DefaultCarBuilder() { | ||
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 DefaultCarBuilder name(String name) { | ||
this.name = name; | ||
return this; | ||
} | ||
|
||
public DefaultCarBuilder speed(int speed) { | ||
this.speed = speed; | ||
return this; | ||
} | ||
|
||
public DefaultCarBuilder isSupercar(int supercar) { | ||
isSupercar = supercar == 1; | ||
return this; | ||
} | ||
|
||
public Car build() { | ||
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 (isSupercar) { | ||
return new SuperCar(this.name, this.speed); | ||
} | ||
return new NormalCar(this.name, this.speed); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import utils.Car; | ||
import utils.MessageEnum; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Main { | ||
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. main함수를 담고 있는 클래스는 static한 요소들만 담고 있어야 합니다. 예를 들어, RacingApp이라는 클래스가 main함수 하나만 가지도록 클래스를 만들고, main함수는 입력클래스, 게임 로직 클래스, 출력클래스를 각각 인스턴스화 하여 실행하도록 설계 할 수 있습니다. 이런식으로 설계된다면, 입출력 클래스와 게임 로직 클래스는 의존성이 낮아져서, 게임 로직 클래스의 테스트가 쉬워집니다. |
||
private static final Scanner input = new Scanner(System.in); | ||
|
||
public static int getSizeOfCars() { | ||
MessageEnum.INIT_CARS.printMessage(); | ||
return input.nextInt(); | ||
} | ||
|
||
public static Car makeCar() { | ||
MessageEnum.INPUT_NAME.printMessage(); // [ System ] 해당 자동차의 이름을 입력해주세요. (type: String) | ||
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. Enum 정의해서 사용하는것 좋습니다! |
||
String name = input.next(); | ||
MessageEnum.INPUT_SPEED.printMessage(); // [ System ] 해당 자동차의 속도를 입력해주세요. (type: integer) | ||
int speed = input.nextInt(); | ||
MessageEnum.INPUT_IS_SPUERCAR.printMessage(); // [ System ] 해당 자동차는 SuperCar 인가요? (True = 1, False = 0) | ||
int isSupercar = input.nextInt(); | ||
|
||
return new DefaultCarBuilder() | ||
.name(name) | ||
.speed(speed) | ||
.isSupercar(isSupercar) | ||
.build(); | ||
} | ||
|
||
public static int getLoopTime() { | ||
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. 상단에서 말씀드린대로 설계한다면, static이지 않아도 되는 이 메소드들을 non-static으로 만들 수 있습니다. |
||
MessageEnum.GAME_TIME.printMessage(); | ||
return input.nextInt(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
int sizeOfCars = getSizeOfCars(); | ||
RacingGame game = new RacingGame(); | ||
|
||
for (int count = 0; count < sizeOfCars; count++) { | ||
MessageEnum.NUMBER_OF_CAR.getNumberOfCars(count + 1, sizeOfCars); // [i/n] ========== | ||
game.addRacingCar(makeCar()); | ||
} | ||
|
||
game.broadcastCars(); | ||
game.toRacing(getLoopTime()); | ||
game.broadcastResult(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import utils.Car; | ||
|
||
import java.util.Random; | ||
|
||
public class NormalCar implements Car { | ||
private String name = ""; | ||
private int speed = 0; | ||
private int score = 0; | ||
|
||
public NormalCar(String name, int speed) { | ||
this.name = name; | ||
this.speed = speed; | ||
} | ||
|
||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public int getSpeed() { | ||
return this.speed; | ||
} | ||
|
||
@Override | ||
public void describe() { | ||
System.out.print("[ " + this.name + "(Normal) ] "); | ||
System.out.print("Speed : " + this.speed); | ||
System.out.println(); | ||
} | ||
|
||
@Override | ||
public void move() { | ||
Random random = new Random(); | ||
this.score += random.nextInt(2) * this.speed; | ||
} | ||
|
||
@Override | ||
public void printResult() { | ||
System.out.print("[ " + this.name + "(Normal) ] "); | ||
System.out.print("score : " + this.score); | ||
System.out.println(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import utils.Car; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class RacingGame { | ||
private int sizeOfCars; | ||
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. 사용되지 않는 멤버변수가 있습니다. |
||
private ArrayList<Car> cars = new ArrayList<Car>(); | ||
|
||
public RacingGame() { | ||
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 int getSizeOfCars() { | ||
return cars.size(); | ||
} | ||
|
||
public void addRacingCar(Car car) { | ||
cars.add(car); | ||
} | ||
|
||
public void toRacing(int time) { | ||
for (Car car : cars) { | ||
car.go(time); | ||
} | ||
} | ||
|
||
public void broadcastCars() { | ||
for (Car car : cars) { | ||
car.describe(); | ||
} | ||
} | ||
|
||
public void broadcastResult() { | ||
for (Car car : cars) { | ||
car.printResult(); | ||
} | ||
} | ||
|
||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import utils.Car; | ||
|
||
import java.util.Random; | ||
|
||
public class SuperCar implements Car { | ||
private String name = ""; | ||
private int speed = 0; | ||
private int score = 0; | ||
private int boostCount = 0; | ||
|
||
public SuperCar(String name, int speed) { | ||
this.name = name; | ||
this.speed = speed; | ||
} | ||
|
||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public int getSpeed() { | ||
return this.speed; | ||
} | ||
|
||
@Override | ||
public void describe() { | ||
System.out.print("[ " + this.name + "(Super) ] "); | ||
System.out.print("Speed : " + this.speed); | ||
System.out.println(); | ||
} | ||
|
||
@Override | ||
public void move() { | ||
Random random = new Random(); | ||
int mainActive = random.nextInt(2); | ||
int boostMultiply = 2 - random.nextInt(2); | ||
if (mainActive == 1 && boostMultiply == 2) { | ||
boostCount++; | ||
} | ||
this.score += mainActive * boostMultiply * this.speed; | ||
} | ||
|
||
@Override | ||
public void printResult() { | ||
System.out.print("[ " + this.name + "(Super) ]"); | ||
System.out.print(" score : " + this.score); | ||
System.out.print(" active boost : " + this.boostCount); | ||
System.out.println(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils; | ||
|
||
public interface Car { | ||
String name = ""; | ||
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. 인터페이스에서 선언하는 멤버 변수는 자동으로 final이 됩니다. 여기에서 정의하지 말고, 해당 인터페이스를 구현할 클래스에서 선언해주세요. |
||
int speed = 0; | ||
|
||
public String getName(); | ||
|
||
public int getSpeed(); | ||
|
||
public void describe(); | ||
|
||
public default void go(int time) { | ||
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. NormalCar와 SuperCar가 이 경기에서 만들어 질 수 있는 모든 경우의 수 입니다. 따라서 인터페이스가 default 구현체를 가지고 있을 필요가 없습니다. 각 클래스에서 구현해주세요. |
||
for (int i = 0; i < time; i++) { | ||
this.move(); | ||
} | ||
} | ||
|
||
void move(); | ||
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을 붙이거나 삭제하거나 하시면 좋을 듯 합니다. (표기하든, 안하든 자동으로 public 처리가 되지만, readability차원에서!) |
||
|
||
public void printResult(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package utils; | ||
|
||
public interface CarBuilder { | ||
public CarBuilder name(String name); | ||
|
||
public CarBuilder speed(int speed); | ||
|
||
public CarBuilder isSupercar(int supercar); | ||
|
||
public Car build(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package utils; | ||
|
||
public enum MessageEnum { | ||
// utils.Car Initialize | ||
INIT_CARS("자동차의 갯수를 입력해주세요."), | ||
INPUT_NAME("해당 자동차의 이름을 입력해주세요. (type: String)"), | ||
INPUT_SPEED("해당 자동차의 속도를 입력해주세요. (type: integer)"), | ||
INPUT_IS_SPUERCAR("해당 자동차는 SuperCar 인가요? (True = 1, False = 0)"), | ||
NUMBER_OF_CAR(""), | ||
|
||
// utils.Car Show | ||
CAR_SHOW("\n\n\n===== : 경기 참가자 소개 : ====="), | ||
BROADCAST_WINNER("===== : 최종 결과 발표 : ====="), | ||
|
||
GAME_TIME("경기를 몇 초동안 진행시킬까요? (type: integer)"), | ||
|
||
INPUT_FAIL("입력 오류. 다시 한번 입력해주세요."); | ||
|
||
|
||
String s; | ||
String PREFIX = "[ ! ] "; | ||
|
||
MessageEnum(String s) { | ||
this.s = s; | ||
} | ||
|
||
public String getMessage() { | ||
return this.s; | ||
} | ||
|
||
public void printMessage() { | ||
System.out.println(PREFIX + this.s); | ||
} | ||
|
||
public void getNumberOfCars(int curr, int size) { | ||
if (!this.name().equals("NUMBER_OF_CAR")) { | ||
return; | ||
} // 다른 경우 예외 처리 | ||
String res = "\n[" + curr + "/" + size + "] =========="; | ||
System.out.println(res); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import org.junit.jupiter.api.*; | ||
import utils.Car; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class MainTest { | ||
private static Main main; | ||
private static Car[] cars; | ||
private static RacingGame game; | ||
|
||
|
||
@BeforeAll | ||
static void initTest() { | ||
game = new RacingGame(); | ||
cars = new Car[]{ | ||
new SuperCar("A", 1), | ||
new NormalCar("B", 2)}; | ||
} | ||
|
||
@Test | ||
@DisplayName("자동차 등록 및 크기 확") | ||
void getSizeOfCars() { | ||
game = new RacingGame(); | ||
int totalCount = cars.length; | ||
|
||
for (Car car : cars) { | ||
game.addRacingCar(car); | ||
} | ||
|
||
game.broadcastCars(); | ||
assertEquals(game.getSizeOfCars(), totalCount); | ||
} | ||
|
||
@Test | ||
@DisplayName("게임 시작 및 게임 결과 확인") | ||
void startRacing() { | ||
for (Car car : cars) { | ||
game.addRacingCar(car); | ||
} | ||
|
||
game.toRacing(10); | ||
game.broadcastResult(); | ||
} | ||
} |
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.
접근 지정자를 명시해주세요.