diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java new file mode 100644 index 0000000..832d8a6 --- /dev/null +++ b/src/main/java/ladder/controller/LadderGame.java @@ -0,0 +1,22 @@ +package ladder.controller; + +import ladder.domain.BridgeGenerator; +import ladder.domain.Users; +import ladder.domain.UsersNameReader; +import ladder.view.Input; +import ladder.view.Output; + +public class LadderGame { + public static void main(String[] args) { + UsersNameReader usersNameReader = Input.getUsersName(); + Users users = usersNameReader.convertNamesToUsers(); + + BridgeGenerator bridgeGenerator = + BridgeGenerator.newInstance(users.getNumberOfUsers(), Input.getHeightOfLadder()); + + bridgeGenerator.create(); + + Output.showUserNames(users); + Output.showResult(bridgeGenerator.getBridgeStates()); + } +} diff --git a/src/main/java/ladder/domain/BridgeGenerator.java b/src/main/java/ladder/domain/BridgeGenerator.java new file mode 100644 index 0000000..bf31480 --- /dev/null +++ b/src/main/java/ladder/domain/BridgeGenerator.java @@ -0,0 +1,57 @@ +package ladder.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Random; + +public class BridgeGenerator { + private final int heigthOfLadder; + private final int numberOfUsers; + private final List bridgeStates = new ArrayList<>(); + + public BridgeGenerator(int numberOfUsers, int heightOfLadder) { + this.heigthOfLadder = heightOfLadder; + this.numberOfUsers = numberOfUsers; + } + + public static BridgeGenerator newInstance(int numberOfUsers, int heightOfLadder) { + return new BridgeGenerator(numberOfUsers, heightOfLadder); + } + + public void create() { + for (int i = 0; i < heigthOfLadder; i += 1) { + Line line = Line.newInstance(makeRandomStates()); + bridgeStates.add(line); + } + } + + private List makeRandomStates() { + Random random = new Random(); + + List states = new ArrayList<>(); + for (int i = 0; i < numberOfUsers - 1; i += 1) { + addState(random, states, i); + } + + return states; + } + + private void addState(Random random, List states, int index) { + if (states.size() == 0) { + states.add(random.nextInt() % 2 == 0); + return; + } + + if (!states.get(index - 1)) { + states.add(random.nextInt() % 2 == 0); + return; + } + + states.add(false); + } + + public List getBridgeStates() { + return Collections.unmodifiableList(this.bridgeStates); + } +} diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java new file mode 100644 index 0000000..ff4a65b --- /dev/null +++ b/src/main/java/ladder/domain/Line.java @@ -0,0 +1,42 @@ +package ladder.domain; + +import java.util.List; + +public class Line { + private final List bridgeStates; + + public static Line newInstance(List bridgeStates) { + return new Line(bridgeStates); + } + + public Line(List bridgeStates) { + this.bridgeStates = bridgeStates; + } + + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + + for (Boolean state : bridgeStates) { + appendColumn(result); + appendBridge(result, state); + } + appendColumn(result); + + return " " + String.valueOf(result); + } + + private void appendColumn(StringBuilder result) { + result.append("|"); + } + + private void appendBridge(StringBuilder result, Boolean state) { + if (state) { + result.append("------"); + } + + if (!state) { + result.append(" "); + } + } +} diff --git a/src/main/java/ladder/domain/User.java b/src/main/java/ladder/domain/User.java new file mode 100644 index 0000000..176a350 --- /dev/null +++ b/src/main/java/ladder/domain/User.java @@ -0,0 +1,21 @@ +package ladder.domain; + +public class User { + private final String name; + private final int MAX_NUMBER_OF_CHARACTERS = 5; + + private User(String name) { + this.name = name; + } + + public static User newInstance(String name) { + return new User(name); + } + + @Override + public String toString() { + int blankCount = MAX_NUMBER_OF_CHARACTERS - name.length(); + + return " ".repeat(Math.max(0, blankCount)) + name; + } +} diff --git a/src/main/java/ladder/domain/Users.java b/src/main/java/ladder/domain/Users.java new file mode 100644 index 0000000..9a8915c --- /dev/null +++ b/src/main/java/ladder/domain/Users.java @@ -0,0 +1,29 @@ +package ladder.domain; + +import java.util.List; + +public class Users { + private final List users; + + private Users(List users) { + this.users = users; + } + + public static Users newInstance(List users) { + return new Users(users); + } + + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + for (User user : this.users) { + result.append(user.toString()).append(" "); + } + + return result.toString(); + } + + public int getNumberOfUsers() { + return users.size(); + } +} diff --git a/src/main/java/ladder/domain/UsersNameReader.java b/src/main/java/ladder/domain/UsersNameReader.java new file mode 100644 index 0000000..edfc7bc --- /dev/null +++ b/src/main/java/ladder/domain/UsersNameReader.java @@ -0,0 +1,40 @@ +package ladder.domain; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +public class UsersNameReader { + private final String names; + + private UsersNameReader(String names) { + this.validator(names); + this.names = names; + } + + public static UsersNameReader newInstace(String names) { + return new UsersNameReader(names); + } + + private void validator(String names) { + Optional optNames = Optional.ofNullable(names); + if (optNames.isEmpty()) { + throw new IllegalArgumentException(); + } + + for (String name : names.split(",")) { + if (name.length() > 5 || name.length() < 1) { + throw new IllegalArgumentException(); + } + }; + } + + public Users convertNamesToUsers() { + List userNames = Arrays.stream(this.names.split(",")) + .map(User::newInstance) + .collect(Collectors.toList()); + + return Users.newInstance(userNames); + } +} diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java new file mode 100644 index 0000000..cbc35bd --- /dev/null +++ b/src/main/java/ladder/view/Input.java @@ -0,0 +1,21 @@ +package ladder.view; + +import ladder.domain.UsersNameReader; + +import java.util.Scanner; + +public class Input { + public static UsersNameReader getUsersName() { + System.out.println("참여할 사람의 이름을 입력하세요. 이름은 쉼표(,)로 구분합니다"); + Scanner scanner = new Scanner(System.in); + + return UsersNameReader.newInstace(scanner.nextLine()); + } + + public static int getHeightOfLadder() { + System.out.println("최대 사다리 높이는 몇 개 인가요?"); + Scanner scanner = new Scanner(System.in); + + return scanner.nextInt(); + } +} diff --git a/src/main/java/ladder/view/Output.java b/src/main/java/ladder/view/Output.java new file mode 100644 index 0000000..0dfd715 --- /dev/null +++ b/src/main/java/ladder/view/Output.java @@ -0,0 +1,16 @@ +package ladder.view; + +import ladder.domain.Line; +import ladder.domain.Users; + +import java.util.List; + +public class Output { + public static void showUserNames(Users users) { + System.out.println(users.toString()); + } + + public static void showResult(List bridgeStates) { + bridgeStates.forEach(v -> System.out.println(v.toString())); + } +} diff --git a/src/test/java/ladder/BridgeGeneratorTest.java b/src/test/java/ladder/BridgeGeneratorTest.java new file mode 100644 index 0000000..7a06205 --- /dev/null +++ b/src/test/java/ladder/BridgeGeneratorTest.java @@ -0,0 +1,21 @@ +package ladder; + +import ladder.domain.BridgeGenerator; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BridgeGeneratorTest { + @Test + void add() { + int numberOfUsers = 4; + int heightOfLadder = 3; + BridgeGenerator bridgeGenerator = BridgeGenerator.newInstance(numberOfUsers, heightOfLadder); + + bridgeGenerator.create(); + + assertThat(bridgeGenerator.getBridgeStates()).hasSize(3); + } +} diff --git a/src/test/java/ladder/LineTest.java b/src/test/java/ladder/LineTest.java new file mode 100644 index 0000000..e90562c --- /dev/null +++ b/src/test/java/ladder/LineTest.java @@ -0,0 +1,21 @@ +package ladder; + +import ladder.domain.Line; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LineTest { + @Test + @DisplayName("toString") + void toStringMethod() { + List bridgeStates = List.of(true, false); + + Line line = Line.newInstance(bridgeStates); + + assertThat(line.toString()).isEqualTo("|----| |"); + } +} diff --git a/src/test/java/ladder/UserTest.java b/src/test/java/ladder/UserTest.java new file mode 100644 index 0000000..cf0aa43 --- /dev/null +++ b/src/test/java/ladder/UserTest.java @@ -0,0 +1,17 @@ +package ladder; + +import ladder.domain.User; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UserTest { + @Test + @DisplayName("toString") + void toStringMethod() { + User user = User.newInstance("din"); + + assertThat(user.toString()).isEqualTo(" din"); + } +} diff --git a/src/test/java/ladder/UsersNameReaderTest.java b/src/test/java/ladder/UsersNameReaderTest.java new file mode 100644 index 0000000..31ae042 --- /dev/null +++ b/src/test/java/ladder/UsersNameReaderTest.java @@ -0,0 +1,33 @@ +package ladder; + +import ladder.domain.UsersNameReader; +import ladder.domain.Users; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class UsersNameReaderTest { + @Test + void nullCheck() { + assertThatThrownBy(() -> { + UsersNameReader usersNameReader = UsersNameReader.newInstace(null); + }).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void invalidNames() { + assertThatThrownBy(() -> { + UsersNameReader usersNameReader = UsersNameReader.newInstace("john,christina"); + }).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void getUsers() { + UsersNameReader usersNameReader = UsersNameReader.newInstace("john,tom,jenny"); + + Users users = usersNameReader.convertNamesToUsers(); + + assertThat(users.toString()).isEqualTo(" john tom jenny "); + } +} diff --git a/src/test/java/ladder/UsersTest.java b/src/test/java/ladder/UsersTest.java new file mode 100644 index 0000000..cfe4417 --- /dev/null +++ b/src/test/java/ladder/UsersTest.java @@ -0,0 +1,28 @@ +package ladder; + +import ladder.domain.UsersNameReader; +import ladder.domain.Users; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UsersTest { + Users users; + @BeforeEach + void init() { + users = UsersNameReader.newInstace("john,james,tom,buddy,din").convertNamesToUsers(); + } + + @Test + @DisplayName("toString") + void toStringMethod() { + assertThat(users.toString()).isEqualTo(" john james tom buddy din "); + } + + @Test + void getNumberOfUsers() { + assertThat(users.getNumberOfUsers()).isEqualTo(5); + } +}