From 5922baf9960b55e918c8e553d4b145b89b31c848 Mon Sep 17 00:00:00 2001 From: minwoo Date: Sat, 27 May 2023 23:36:42 +0900 Subject: [PATCH] Add some files --- .gitignore | 3 +++ README.md | 11 ++------ javastudy.iml | 28 ++++++++++++++++++++ src/DefaultCarBuilder.java | 33 ++++++++++++++++++++++++ src/Main.java | 47 ++++++++++++++++++++++++++++++++++ src/NormalCar.java | 45 +++++++++++++++++++++++++++++++++ src/RacingGame.java | 39 ++++++++++++++++++++++++++++ src/SuperCar.java | 52 ++++++++++++++++++++++++++++++++++++++ src/utils/Car.java | 22 ++++++++++++++++ src/utils/CarBuilder.java | 11 ++++++++ src/utils/MessageEnum.java | 42 ++++++++++++++++++++++++++++++ test/MainTest.java | 44 ++++++++++++++++++++++++++++++++ 12 files changed, 368 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 javastudy.iml create mode 100644 src/DefaultCarBuilder.java create mode 100644 src/Main.java create mode 100644 src/NormalCar.java create mode 100644 src/RacingGame.java create mode 100644 src/SuperCar.java create mode 100644 src/utils/Car.java create mode 100644 src/utils/CarBuilder.java create mode 100644 src/utils/MessageEnum.java create mode 100644 test/MainTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e996746 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +out +javaStudyWeek2.iml diff --git a/README.md b/README.md index 6b1f44f..c2714e1 100644 --- a/README.md +++ b/README.md @@ -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**. diff --git a/javastudy.iml b/javastudy.iml new file mode 100644 index 0000000..ee3eb20 --- /dev/null +++ b/javastudy.iml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/DefaultCarBuilder.java b/src/DefaultCarBuilder.java new file mode 100644 index 0000000..89bad64 --- /dev/null +++ b/src/DefaultCarBuilder.java @@ -0,0 +1,33 @@ +import utils.Car; +import utils.CarBuilder; + +public class DefaultCarBuilder implements CarBuilder { + String name; + int speed; + boolean isSupercar; + + public DefaultCarBuilder() { + } + + 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() { + if (isSupercar) { + return new SuperCar(this.name, this.speed); + } + return new NormalCar(this.name, this.speed); + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..496f8fd --- /dev/null +++ b/src/Main.java @@ -0,0 +1,47 @@ +import utils.Car; +import utils.MessageEnum; + +import java.util.Scanner; + +public class 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) + 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() { + 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(); + } +} \ No newline at end of file diff --git a/src/NormalCar.java b/src/NormalCar.java new file mode 100644 index 0000000..e7cbcab --- /dev/null +++ b/src/NormalCar.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/RacingGame.java b/src/RacingGame.java new file mode 100644 index 0000000..4b9a257 --- /dev/null +++ b/src/RacingGame.java @@ -0,0 +1,39 @@ +import utils.Car; + +import java.util.ArrayList; + +public class RacingGame { + private int sizeOfCars; + private ArrayList cars = new ArrayList(); + + public RacingGame() { + } + + 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(); + } + } + + +} diff --git a/src/SuperCar.java b/src/SuperCar.java new file mode 100644 index 0000000..3fd32e1 --- /dev/null +++ b/src/SuperCar.java @@ -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(); + } +} \ No newline at end of file diff --git a/src/utils/Car.java b/src/utils/Car.java new file mode 100644 index 0000000..d7e8945 --- /dev/null +++ b/src/utils/Car.java @@ -0,0 +1,22 @@ +package utils; + +public interface Car { + String name = ""; + int speed = 0; + + public String getName(); + + public int getSpeed(); + + public void describe(); + + public default void go(int time) { + for (int i = 0; i < time; i++) { + this.move(); + } + } + + void move(); + + public void printResult(); +} diff --git a/src/utils/CarBuilder.java b/src/utils/CarBuilder.java new file mode 100644 index 0000000..1fa845c --- /dev/null +++ b/src/utils/CarBuilder.java @@ -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(); +} diff --git a/src/utils/MessageEnum.java b/src/utils/MessageEnum.java new file mode 100644 index 0000000..2eeccee --- /dev/null +++ b/src/utils/MessageEnum.java @@ -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); + } +} diff --git a/test/MainTest.java b/test/MainTest.java new file mode 100644 index 0000000..bd735f8 --- /dev/null +++ b/test/MainTest.java @@ -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(); + } +} \ No newline at end of file