From fd3780dbaef5d1125d8bf05155da16e0e4cebd30 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 18:35:36 +0900 Subject: [PATCH 01/17] Add User class --- src/main/java/ladder/domain/User.java | 21 +++++++++++++++++++++ src/test/java/ladder/UserTest.java | 17 +++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/ladder/domain/User.java create mode 100644 src/test/java/ladder/UserTest.java diff --git a/src/main/java/ladder/domain/User.java b/src/main/java/ladder/domain/User.java new file mode 100644 index 0000000..22c0a88 --- /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 LIMITED_NUMBER_OF_CHARACTERS = 5; + + private User(String name) { + this.name = name; + } + + public static User from(String name) { + return new User(name); + } + + @Override + public String toString() { + int blankCount = LIMITED_NUMBER_OF_CHARACTERS - name.length(); + + return " ".repeat(Math.max(0, blankCount)) + name; + } +} diff --git a/src/test/java/ladder/UserTest.java b/src/test/java/ladder/UserTest.java new file mode 100644 index 0000000..94951a3 --- /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.from("john"); + + assertThat(user.toString()).isEqualTo(" john"); + } +} From 35ab8650661fe86aa586d881d10ea87aa1559472 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:23:25 +0900 Subject: [PATCH 02/17] Add Users class --- src/main/java/ladder/domain/Users.java | 27 ++++++++++++++++++++++++++ src/test/java/ladder/UserTest.java | 4 ++-- src/test/java/ladder/UsersTest.java | 17 ++++++++++++++++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ladder/domain/Users.java create mode 100644 src/test/java/ladder/UsersTest.java diff --git a/src/main/java/ladder/domain/Users.java b/src/main/java/ladder/domain/Users.java new file mode 100644 index 0000000..2538eaa --- /dev/null +++ b/src/main/java/ladder/domain/Users.java @@ -0,0 +1,27 @@ +package ladder.domain; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +public class Users { + private final List users; + + private Users(String names) { + this.users = Arrays.stream(names.split(",")).map(User::from).collect(Collectors.toList()); + } + + public static Users from(String names) { + return new Users(names); + } + + @Override + public String toString() { + StringBuilder result = new StringBuilder(); + for (User user : this.users) { + result.append(user.toString()).append(" "); + } + + return result.toString(); + } +} diff --git a/src/test/java/ladder/UserTest.java b/src/test/java/ladder/UserTest.java index 94951a3..a76683f 100644 --- a/src/test/java/ladder/UserTest.java +++ b/src/test/java/ladder/UserTest.java @@ -10,8 +10,8 @@ public class UserTest { @Test @DisplayName("toString") void toStringMethod() { - User user = User.from("john"); + User user = User.from("din"); - assertThat(user.toString()).isEqualTo(" john"); + assertThat(user.toString()).isEqualTo(" din"); } } diff --git a/src/test/java/ladder/UsersTest.java b/src/test/java/ladder/UsersTest.java new file mode 100644 index 0000000..1ae5d1d --- /dev/null +++ b/src/test/java/ladder/UsersTest.java @@ -0,0 +1,17 @@ +package ladder; + +import ladder.domain.Users; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class UsersTest { + @Test + @DisplayName("toString") + void toStringMethod() { + Users users = Users.from("john,james,tom,buddy,din"); + + assertThat(users.toString()).isEqualTo(" john james tom buddy din "); + } +} From b3cadf436cc174642b3e7d2f48b2f951a2252701 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:27:25 +0900 Subject: [PATCH 03/17] Add Input.getPlayersName() method --- src/main/java/ladder/controller/LadderGame.java | 10 ++++++++++ src/main/java/ladder/view/Input.java | 12 ++++++++++++ src/main/java/ladder/view/Output.java | 4 ++++ 3 files changed, 26 insertions(+) create mode 100644 src/main/java/ladder/controller/LadderGame.java create mode 100644 src/main/java/ladder/view/Input.java create mode 100644 src/main/java/ladder/view/Output.java diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java new file mode 100644 index 0000000..96f358b --- /dev/null +++ b/src/main/java/ladder/controller/LadderGame.java @@ -0,0 +1,10 @@ +package ladder.controller; + +import ladder.view.Input; + +public class LadderGame { + public static void main(String[] args) { + String playersName = Input.getPlayersName(); + + } +} diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java new file mode 100644 index 0000000..27cd565 --- /dev/null +++ b/src/main/java/ladder/view/Input.java @@ -0,0 +1,12 @@ +package ladder.view; + +import java.util.Scanner; + +public class Input { + public static String getPlayersName() { + System.out.println("참여할 사람의 이름을 입력하세요. 이름은 쉼표(,)로 구분합니다"); + Scanner scanner = new Scanner(System.in); + + return scanner.nextLine(); + } +} diff --git a/src/main/java/ladder/view/Output.java b/src/main/java/ladder/view/Output.java new file mode 100644 index 0000000..6f691ff --- /dev/null +++ b/src/main/java/ladder/view/Output.java @@ -0,0 +1,4 @@ +package ladder.view; + +public class Output { +} From 25d26189f85229bca9491ba136fb8e244e364cdb Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:40:24 +0900 Subject: [PATCH 04/17] Add PlayersName class to wrap primitive types --- .../java/ladder/controller/LadderGame.java | 3 +- src/main/java/ladder/domain/PlayersName.java | 29 +++++++++++++++++++ src/main/java/ladder/view/Input.java | 6 ++-- src/test/java/ladder/PlayersNameTest.java | 22 ++++++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/ladder/domain/PlayersName.java create mode 100644 src/test/java/ladder/PlayersNameTest.java diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index 96f358b..c85ff0e 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -1,10 +1,11 @@ package ladder.controller; +import ladder.domain.PlayersName; import ladder.view.Input; public class LadderGame { public static void main(String[] args) { - String playersName = Input.getPlayersName(); + PlayersName playersName = Input.getPlayersName(); } } diff --git a/src/main/java/ladder/domain/PlayersName.java b/src/main/java/ladder/domain/PlayersName.java new file mode 100644 index 0000000..0fcb8fe --- /dev/null +++ b/src/main/java/ladder/domain/PlayersName.java @@ -0,0 +1,29 @@ +package ladder.domain; + +import java.util.Optional; + +public class PlayersName { + private final String names; + + private PlayersName(String names) { + this.validator(names); + this.names = names; + } + + public static PlayersName from(String names) { + return new PlayersName(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(); + } + }; + } +} diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java index 27cd565..7f2c81d 100644 --- a/src/main/java/ladder/view/Input.java +++ b/src/main/java/ladder/view/Input.java @@ -1,12 +1,14 @@ package ladder.view; +import ladder.domain.PlayersName; + import java.util.Scanner; public class Input { - public static String getPlayersName() { + public static PlayersName getPlayersName() { System.out.println("참여할 사람의 이름을 입력하세요. 이름은 쉼표(,)로 구분합니다"); Scanner scanner = new Scanner(System.in); - return scanner.nextLine(); + return PlayersName.from(scanner.nextLine()); } } diff --git a/src/test/java/ladder/PlayersNameTest.java b/src/test/java/ladder/PlayersNameTest.java new file mode 100644 index 0000000..3894c72 --- /dev/null +++ b/src/test/java/ladder/PlayersNameTest.java @@ -0,0 +1,22 @@ +package ladder; + +import ladder.domain.PlayersName; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class PlayersNameTest { + @Test + void nullCheck() { + assertThatThrownBy(() -> { + PlayersName playersName = PlayersName.from(null); + }).isInstanceOf(IllegalArgumentException.class); + } + + @Test + void invalidNames() { + assertThatThrownBy(() -> { + PlayersName playersName = PlayersName.from("john,christina"); + }).isInstanceOf(IllegalArgumentException.class); + } +} From da6e6a4cb70a97ae06a592bd2ea094eec5b67b91 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:50:54 +0900 Subject: [PATCH 05/17] Add getUsers() to PlayersName class --- src/main/java/ladder/domain/PlayersName.java | 11 +++++++++++ src/main/java/ladder/domain/Users.java | 8 ++++---- src/test/java/ladder/PlayersNameTest.java | 11 +++++++++++ src/test/java/ladder/UsersTest.java | 3 ++- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/main/java/ladder/domain/PlayersName.java b/src/main/java/ladder/domain/PlayersName.java index 0fcb8fe..56313f4 100644 --- a/src/main/java/ladder/domain/PlayersName.java +++ b/src/main/java/ladder/domain/PlayersName.java @@ -1,6 +1,9 @@ package ladder.domain; +import java.util.Arrays; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; public class PlayersName { private final String names; @@ -26,4 +29,12 @@ private void validator(String names) { } }; } + + public Users getUsers() { + List userNames = Arrays.stream(this.names.split(",")) + .map(User::from) + .collect(Collectors.toList()); + + return Users.from(userNames); + } } diff --git a/src/main/java/ladder/domain/Users.java b/src/main/java/ladder/domain/Users.java index 2538eaa..a9f7f1b 100644 --- a/src/main/java/ladder/domain/Users.java +++ b/src/main/java/ladder/domain/Users.java @@ -7,12 +7,12 @@ public class Users { private final List users; - private Users(String names) { - this.users = Arrays.stream(names.split(",")).map(User::from).collect(Collectors.toList()); + private Users(List users) { + this.users = users; } - public static Users from(String names) { - return new Users(names); + public static Users from(List users) { + return new Users(users); } @Override diff --git a/src/test/java/ladder/PlayersNameTest.java b/src/test/java/ladder/PlayersNameTest.java index 3894c72..4dcb476 100644 --- a/src/test/java/ladder/PlayersNameTest.java +++ b/src/test/java/ladder/PlayersNameTest.java @@ -1,8 +1,10 @@ package ladder; import ladder.domain.PlayersName; +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 PlayersNameTest { @@ -19,4 +21,13 @@ void invalidNames() { PlayersName playersName = PlayersName.from("john,christina"); }).isInstanceOf(IllegalArgumentException.class); } + + @Test + void getUsers() { + PlayersName playersName = PlayersName.from("john,tom,jenny"); + + Users users = playersName.getUsers(); + + assertThat(users.toString()).isEqualTo(" john tom jenny "); + } } diff --git a/src/test/java/ladder/UsersTest.java b/src/test/java/ladder/UsersTest.java index 1ae5d1d..0ac82d4 100644 --- a/src/test/java/ladder/UsersTest.java +++ b/src/test/java/ladder/UsersTest.java @@ -1,5 +1,6 @@ package ladder; +import ladder.domain.PlayersName; import ladder.domain.Users; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -10,7 +11,7 @@ public class UsersTest { @Test @DisplayName("toString") void toStringMethod() { - Users users = Users.from("john,james,tom,buddy,din"); + Users users = PlayersName.from("john,james,tom,buddy,din").getUsers(); assertThat(users.toString()).isEqualTo(" john james tom buddy din "); } From 194b24e024b91ed86b7761f54d417484b768ca03 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:52:01 +0900 Subject: [PATCH 06/17] Modify PlayersName class name to UsersNameReader --- src/main/java/ladder/controller/LadderGame.java | 4 ++-- .../{PlayersName.java => UsersNameReader.java} | 8 ++++---- src/main/java/ladder/view/Input.java | 6 +++--- ...PlayersNameTest.java => UsersNameReaderTest.java} | 12 ++++++------ src/test/java/ladder/UsersTest.java | 4 ++-- 5 files changed, 17 insertions(+), 17 deletions(-) rename src/main/java/ladder/domain/{PlayersName.java => UsersNameReader.java} (83%) rename src/test/java/ladder/{PlayersNameTest.java => UsersNameReaderTest.java} (61%) diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index c85ff0e..f961ef9 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -1,11 +1,11 @@ package ladder.controller; -import ladder.domain.PlayersName; +import ladder.domain.UsersNameReader; import ladder.view.Input; public class LadderGame { public static void main(String[] args) { - PlayersName playersName = Input.getPlayersName(); + UsersNameReader usersNameReader = Input.getPlayersName(); } } diff --git a/src/main/java/ladder/domain/PlayersName.java b/src/main/java/ladder/domain/UsersNameReader.java similarity index 83% rename from src/main/java/ladder/domain/PlayersName.java rename to src/main/java/ladder/domain/UsersNameReader.java index 56313f4..97351d5 100644 --- a/src/main/java/ladder/domain/PlayersName.java +++ b/src/main/java/ladder/domain/UsersNameReader.java @@ -5,16 +5,16 @@ import java.util.Optional; import java.util.stream.Collectors; -public class PlayersName { +public class UsersNameReader { private final String names; - private PlayersName(String names) { + private UsersNameReader(String names) { this.validator(names); this.names = names; } - public static PlayersName from(String names) { - return new PlayersName(names); + public static UsersNameReader from(String names) { + return new UsersNameReader(names); } private void validator(String names) { diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java index 7f2c81d..161cb52 100644 --- a/src/main/java/ladder/view/Input.java +++ b/src/main/java/ladder/view/Input.java @@ -1,14 +1,14 @@ package ladder.view; -import ladder.domain.PlayersName; +import ladder.domain.UsersNameReader; import java.util.Scanner; public class Input { - public static PlayersName getPlayersName() { + public static UsersNameReader getPlayersName() { System.out.println("참여할 사람의 이름을 입력하세요. 이름은 쉼표(,)로 구분합니다"); Scanner scanner = new Scanner(System.in); - return PlayersName.from(scanner.nextLine()); + return UsersNameReader.from(scanner.nextLine()); } } diff --git a/src/test/java/ladder/PlayersNameTest.java b/src/test/java/ladder/UsersNameReaderTest.java similarity index 61% rename from src/test/java/ladder/PlayersNameTest.java rename to src/test/java/ladder/UsersNameReaderTest.java index 4dcb476..ecd2d6b 100644 --- a/src/test/java/ladder/PlayersNameTest.java +++ b/src/test/java/ladder/UsersNameReaderTest.java @@ -1,32 +1,32 @@ package ladder; -import ladder.domain.PlayersName; +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 PlayersNameTest { +public class UsersNameReaderTest { @Test void nullCheck() { assertThatThrownBy(() -> { - PlayersName playersName = PlayersName.from(null); + UsersNameReader usersNameReader = UsersNameReader.from(null); }).isInstanceOf(IllegalArgumentException.class); } @Test void invalidNames() { assertThatThrownBy(() -> { - PlayersName playersName = PlayersName.from("john,christina"); + UsersNameReader usersNameReader = UsersNameReader.from("john,christina"); }).isInstanceOf(IllegalArgumentException.class); } @Test void getUsers() { - PlayersName playersName = PlayersName.from("john,tom,jenny"); + UsersNameReader usersNameReader = UsersNameReader.from("john,tom,jenny"); - Users users = playersName.getUsers(); + Users users = usersNameReader.getUsers(); assertThat(users.toString()).isEqualTo(" john tom jenny "); } diff --git a/src/test/java/ladder/UsersTest.java b/src/test/java/ladder/UsersTest.java index 0ac82d4..ad7e2ff 100644 --- a/src/test/java/ladder/UsersTest.java +++ b/src/test/java/ladder/UsersTest.java @@ -1,6 +1,6 @@ package ladder; -import ladder.domain.PlayersName; +import ladder.domain.UsersNameReader; import ladder.domain.Users; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -11,7 +11,7 @@ public class UsersTest { @Test @DisplayName("toString") void toStringMethod() { - Users users = PlayersName.from("john,james,tom,buddy,din").getUsers(); + Users users = UsersNameReader.from("john,james,tom,buddy,din").getUsers(); assertThat(users.toString()).isEqualTo(" john james tom buddy din "); } From e7ba28b7d858cf4d6f901344ce671dcd7a62ee0f Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:53:18 +0900 Subject: [PATCH 07/17] Modify getPlayersName method name to getUsersName --- src/main/java/ladder/controller/LadderGame.java | 2 +- src/main/java/ladder/view/Input.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index f961ef9..8b77787 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -5,7 +5,7 @@ public class LadderGame { public static void main(String[] args) { - UsersNameReader usersNameReader = Input.getPlayersName(); + UsersNameReader usersNameReader = Input.getUsersName(); } } diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java index 161cb52..3362b71 100644 --- a/src/main/java/ladder/view/Input.java +++ b/src/main/java/ladder/view/Input.java @@ -5,7 +5,7 @@ import java.util.Scanner; public class Input { - public static UsersNameReader getPlayersName() { + public static UsersNameReader getUsersName() { System.out.println("참여할 사람의 이름을 입력하세요. 이름은 쉼표(,)로 구분합니다"); Scanner scanner = new Scanner(System.in); From 8c6eb20ae02e5411eb3a9bf372c88286c931c106 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 19:55:00 +0900 Subject: [PATCH 08/17] Modifiy getUsers method name to convertNamesToUser --- src/main/java/ladder/domain/UsersNameReader.java | 2 +- src/test/java/ladder/UsersNameReaderTest.java | 2 +- src/test/java/ladder/UsersTest.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/ladder/domain/UsersNameReader.java b/src/main/java/ladder/domain/UsersNameReader.java index 97351d5..dc6da9e 100644 --- a/src/main/java/ladder/domain/UsersNameReader.java +++ b/src/main/java/ladder/domain/UsersNameReader.java @@ -30,7 +30,7 @@ private void validator(String names) { }; } - public Users getUsers() { + public Users convertNamesToUsers() { List userNames = Arrays.stream(this.names.split(",")) .map(User::from) .collect(Collectors.toList()); diff --git a/src/test/java/ladder/UsersNameReaderTest.java b/src/test/java/ladder/UsersNameReaderTest.java index ecd2d6b..5a9bd1e 100644 --- a/src/test/java/ladder/UsersNameReaderTest.java +++ b/src/test/java/ladder/UsersNameReaderTest.java @@ -26,7 +26,7 @@ void invalidNames() { void getUsers() { UsersNameReader usersNameReader = UsersNameReader.from("john,tom,jenny"); - Users users = usersNameReader.getUsers(); + 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 index ad7e2ff..1dc1cb0 100644 --- a/src/test/java/ladder/UsersTest.java +++ b/src/test/java/ladder/UsersTest.java @@ -11,7 +11,7 @@ public class UsersTest { @Test @DisplayName("toString") void toStringMethod() { - Users users = UsersNameReader.from("john,james,tom,buddy,din").getUsers(); + Users users = UsersNameReader.from("john,james,tom,buddy,din").convertNamesToUsers(); assertThat(users.toString()).isEqualTo(" john james tom buddy din "); } From 213fa986d35e3799666613c8fc8ed04b10b54604 Mon Sep 17 00:00:00 2001 From: woomin Date: Wed, 1 Jul 2020 20:21:36 +0900 Subject: [PATCH 09/17] Modify from() factory method name to newInstance --- src/main/java/ladder/domain/User.java | 2 +- src/main/java/ladder/domain/Users.java | 2 +- src/main/java/ladder/domain/UsersNameReader.java | 6 +++--- src/main/java/ladder/view/Input.java | 2 +- src/test/java/ladder/UserTest.java | 2 +- src/test/java/ladder/UsersNameReaderTest.java | 6 +++--- src/test/java/ladder/UsersTest.java | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/java/ladder/domain/User.java b/src/main/java/ladder/domain/User.java index 22c0a88..30cccef 100644 --- a/src/main/java/ladder/domain/User.java +++ b/src/main/java/ladder/domain/User.java @@ -8,7 +8,7 @@ private User(String name) { this.name = name; } - public static User from(String name) { + public static User newInstance(String name) { return new User(name); } diff --git a/src/main/java/ladder/domain/Users.java b/src/main/java/ladder/domain/Users.java index a9f7f1b..38c34f2 100644 --- a/src/main/java/ladder/domain/Users.java +++ b/src/main/java/ladder/domain/Users.java @@ -11,7 +11,7 @@ private Users(List users) { this.users = users; } - public static Users from(List users) { + public static Users newInstance(List users) { return new Users(users); } diff --git a/src/main/java/ladder/domain/UsersNameReader.java b/src/main/java/ladder/domain/UsersNameReader.java index dc6da9e..edfc7bc 100644 --- a/src/main/java/ladder/domain/UsersNameReader.java +++ b/src/main/java/ladder/domain/UsersNameReader.java @@ -13,7 +13,7 @@ private UsersNameReader(String names) { this.names = names; } - public static UsersNameReader from(String names) { + public static UsersNameReader newInstace(String names) { return new UsersNameReader(names); } @@ -32,9 +32,9 @@ private void validator(String names) { public Users convertNamesToUsers() { List userNames = Arrays.stream(this.names.split(",")) - .map(User::from) + .map(User::newInstance) .collect(Collectors.toList()); - return Users.from(userNames); + return Users.newInstance(userNames); } } diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java index 3362b71..39be917 100644 --- a/src/main/java/ladder/view/Input.java +++ b/src/main/java/ladder/view/Input.java @@ -9,6 +9,6 @@ public static UsersNameReader getUsersName() { System.out.println("참여할 사람의 이름을 입력하세요. 이름은 쉼표(,)로 구분합니다"); Scanner scanner = new Scanner(System.in); - return UsersNameReader.from(scanner.nextLine()); + return UsersNameReader.newInstace(scanner.nextLine()); } } diff --git a/src/test/java/ladder/UserTest.java b/src/test/java/ladder/UserTest.java index a76683f..cf0aa43 100644 --- a/src/test/java/ladder/UserTest.java +++ b/src/test/java/ladder/UserTest.java @@ -10,7 +10,7 @@ public class UserTest { @Test @DisplayName("toString") void toStringMethod() { - User user = User.from("din"); + 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 index 5a9bd1e..31ae042 100644 --- a/src/test/java/ladder/UsersNameReaderTest.java +++ b/src/test/java/ladder/UsersNameReaderTest.java @@ -11,20 +11,20 @@ public class UsersNameReaderTest { @Test void nullCheck() { assertThatThrownBy(() -> { - UsersNameReader usersNameReader = UsersNameReader.from(null); + UsersNameReader usersNameReader = UsersNameReader.newInstace(null); }).isInstanceOf(IllegalArgumentException.class); } @Test void invalidNames() { assertThatThrownBy(() -> { - UsersNameReader usersNameReader = UsersNameReader.from("john,christina"); + UsersNameReader usersNameReader = UsersNameReader.newInstace("john,christina"); }).isInstanceOf(IllegalArgumentException.class); } @Test void getUsers() { - UsersNameReader usersNameReader = UsersNameReader.from("john,tom,jenny"); + UsersNameReader usersNameReader = UsersNameReader.newInstace("john,tom,jenny"); Users users = usersNameReader.convertNamesToUsers(); diff --git a/src/test/java/ladder/UsersTest.java b/src/test/java/ladder/UsersTest.java index 1dc1cb0..a092e96 100644 --- a/src/test/java/ladder/UsersTest.java +++ b/src/test/java/ladder/UsersTest.java @@ -11,7 +11,7 @@ public class UsersTest { @Test @DisplayName("toString") void toStringMethod() { - Users users = UsersNameReader.from("john,james,tom,buddy,din").convertNamesToUsers(); + Users users = UsersNameReader.newInstace("john,james,tom,buddy,din").convertNamesToUsers(); assertThat(users.toString()).isEqualTo(" john james tom buddy din "); } From a1017f9cff8cf51afc6ebb791e986b3aa8f8d372 Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 10:57:20 +0900 Subject: [PATCH 10/17] Modify the name of constant --- src/main/java/ladder/domain/User.java | 4 ++-- src/main/java/ladder/domain/Users.java | 2 -- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/ladder/domain/User.java b/src/main/java/ladder/domain/User.java index 30cccef..176a350 100644 --- a/src/main/java/ladder/domain/User.java +++ b/src/main/java/ladder/domain/User.java @@ -2,7 +2,7 @@ public class User { private final String name; - private final int LIMITED_NUMBER_OF_CHARACTERS = 5; + private final int MAX_NUMBER_OF_CHARACTERS = 5; private User(String name) { this.name = name; @@ -14,7 +14,7 @@ public static User newInstance(String name) { @Override public String toString() { - int blankCount = LIMITED_NUMBER_OF_CHARACTERS - name.length(); + 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 index 38c34f2..f96ec0c 100644 --- a/src/main/java/ladder/domain/Users.java +++ b/src/main/java/ladder/domain/Users.java @@ -1,8 +1,6 @@ package ladder.domain; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; public class Users { private final List users; From 0d79d0300612802994cc3fffe463efb28a563f77 Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 11:05:36 +0900 Subject: [PATCH 11/17] Add getNumberOfUsers() method to Users class --- src/main/java/ladder/controller/LadderGame.java | 2 ++ src/main/java/ladder/domain/Users.java | 4 ++++ src/test/java/ladder/UsersTest.java | 14 ++++++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index 8b77787..0c27a95 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -1,11 +1,13 @@ package ladder.controller; +import ladder.domain.Users; import ladder.domain.UsersNameReader; import ladder.view.Input; public class LadderGame { public static void main(String[] args) { UsersNameReader usersNameReader = Input.getUsersName(); + Users users = usersNameReader.convertNamesToUsers(); } } diff --git a/src/main/java/ladder/domain/Users.java b/src/main/java/ladder/domain/Users.java index f96ec0c..9a8915c 100644 --- a/src/main/java/ladder/domain/Users.java +++ b/src/main/java/ladder/domain/Users.java @@ -22,4 +22,8 @@ public String toString() { return result.toString(); } + + public int getNumberOfUsers() { + return users.size(); + } } diff --git a/src/test/java/ladder/UsersTest.java b/src/test/java/ladder/UsersTest.java index a092e96..cfe4417 100644 --- a/src/test/java/ladder/UsersTest.java +++ b/src/test/java/ladder/UsersTest.java @@ -2,17 +2,27 @@ 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() { - Users users = UsersNameReader.newInstace("john,james,tom,buddy,din").convertNamesToUsers(); - assertThat(users.toString()).isEqualTo(" john james tom buddy din "); } + + @Test + void getNumberOfUsers() { + assertThat(users.getNumberOfUsers()).isEqualTo(5); + } } From bc04bfe1b920194c060238eeb53e2f989412ab00 Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 11:38:08 +0900 Subject: [PATCH 12/17] Add Line class --- src/main/java/ladder/domain/Line.java | 42 +++++++++++++++++++++++++++ src/test/java/ladder/LineTest.java | 21 ++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/main/java/ladder/domain/Line.java create mode 100644 src/test/java/ladder/LineTest.java diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java new file mode 100644 index 0000000..184ba67 --- /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 (int 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, int state) { + if (state == 1) { + result.append("----"); + } + + if (state == 0) { + result.append(" "); + } + } +} diff --git a/src/test/java/ladder/LineTest.java b/src/test/java/ladder/LineTest.java new file mode 100644 index 0000000..b3fbb0a --- /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(1, 0); + + Line line = Line.newInstance(bridgeStates); + + assertThat(line.toString()).isEqualTo("|----| |"); + } +} From 6ebb7299ba956d952bd8ead3e75a142d8f6f89cb Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 16:01:03 +0900 Subject: [PATCH 13/17] Add getHeigthOfLadder() in Input class --- src/main/java/ladder/controller/LadderGame.java | 1 + src/main/java/ladder/view/Input.java | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index 0c27a95..f78bb50 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -9,5 +9,6 @@ public static void main(String[] args) { UsersNameReader usersNameReader = Input.getUsersName(); Users users = usersNameReader.convertNamesToUsers(); + } } diff --git a/src/main/java/ladder/view/Input.java b/src/main/java/ladder/view/Input.java index 39be917..cbc35bd 100644 --- a/src/main/java/ladder/view/Input.java +++ b/src/main/java/ladder/view/Input.java @@ -11,4 +11,11 @@ public static UsersNameReader getUsersName() { return UsersNameReader.newInstace(scanner.nextLine()); } + + public static int getHeightOfLadder() { + System.out.println("최대 사다리 높이는 몇 개 인가요?"); + Scanner scanner = new Scanner(System.in); + + return scanner.nextInt(); + } } From c8e297c9a30e762651983a461622512b989a1fb6 Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 16:16:40 +0900 Subject: [PATCH 14/17] Modify Line constructor parameter --- src/main/java/ladder/domain/Line.java | 14 +++++++------- src/test/java/ladder/LineTest.java | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java index 184ba67..ee0e451 100644 --- a/src/main/java/ladder/domain/Line.java +++ b/src/main/java/ladder/domain/Line.java @@ -3,13 +3,13 @@ import java.util.List; public class Line { - private final List bridgeStates; + private final List bridgeStates; - public static Line newInstance(List bridgeStates) { + public static Line newInstance(List bridgeStates) { return new Line(bridgeStates); } - public Line(List bridgeStates) { + public Line(List bridgeStates) { this.bridgeStates = bridgeStates; } @@ -17,7 +17,7 @@ public Line(List bridgeStates) { public String toString() { StringBuilder result = new StringBuilder(); - for (int state : bridgeStates) { + for (Boolean state : bridgeStates) { appendColumn(result); appendBridge(result, state); } @@ -30,12 +30,12 @@ private void appendColumn(StringBuilder result) { result.append("|"); } - private void appendBridge(StringBuilder result, int state) { - if (state == 1) { + private void appendBridge(StringBuilder result, Boolean state) { + if (state) { result.append("----"); } - if (state == 0) { + if (!state) { result.append(" "); } } diff --git a/src/test/java/ladder/LineTest.java b/src/test/java/ladder/LineTest.java index b3fbb0a..e90562c 100644 --- a/src/test/java/ladder/LineTest.java +++ b/src/test/java/ladder/LineTest.java @@ -12,7 +12,7 @@ public class LineTest { @Test @DisplayName("toString") void toStringMethod() { - List bridgeStates = List.of(1, 0); + List bridgeStates = List.of(true, false); Line line = Line.newInstance(bridgeStates); From a9e2f360b31b8297b214c1cd6d3e58e50b24688f Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 16:34:46 +0900 Subject: [PATCH 15/17] Add BridgeGenerator class --- .../java/ladder/controller/LadderGame.java | 2 +- .../java/ladder/domain/BridgeGenerator.java | 29 +++++++++++++++++++ src/test/java/ladder/BridgeGeneratorTest.java | 23 +++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/main/java/ladder/domain/BridgeGenerator.java create mode 100644 src/test/java/ladder/BridgeGeneratorTest.java diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index f78bb50..688d514 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -9,6 +9,6 @@ public static void main(String[] args) { UsersNameReader usersNameReader = Input.getUsersName(); Users users = usersNameReader.convertNamesToUsers(); - + int time = Input.getHeightOfLadder(); } } diff --git a/src/main/java/ladder/domain/BridgeGenerator.java b/src/main/java/ladder/domain/BridgeGenerator.java new file mode 100644 index 0000000..dc48096 --- /dev/null +++ b/src/main/java/ladder/domain/BridgeGenerator.java @@ -0,0 +1,29 @@ +package ladder.domain; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +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 add(List states) { + Line line = Line.newInstance(states); + this.bridgeStates.add(line); + } + + public List getBridgeStates() { + return Collections.unmodifiableList(this.bridgeStates); + } +} diff --git a/src/test/java/ladder/BridgeGeneratorTest.java b/src/test/java/ladder/BridgeGeneratorTest.java new file mode 100644 index 0000000..34793c0 --- /dev/null +++ b/src/test/java/ladder/BridgeGeneratorTest.java @@ -0,0 +1,23 @@ +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.add(List.of(true, false, true, false)); + bridgeGenerator.add(List.of(true, false, false, true)); + bridgeGenerator.add(List.of(false, true, false, true)); + + assertThat(bridgeGenerator.getBridgeStates()).hasSize(3); + } +} From ce18bd71fa200d27680a13f62a8cf50e5e0a513e Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 16:46:26 +0900 Subject: [PATCH 16/17] Finish step2 --- src/main/java/ladder/controller/LadderGame.java | 14 ++++++++++++++ src/main/java/ladder/domain/Line.java | 6 +++--- src/main/java/ladder/view/Output.java | 12 ++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index 688d514..1e1bce4 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -1,8 +1,12 @@ package ladder.controller; +import ladder.domain.BridgeGenerator; import ladder.domain.Users; import ladder.domain.UsersNameReader; import ladder.view.Input; +import ladder.view.Output; + +import java.util.List; public class LadderGame { public static void main(String[] args) { @@ -10,5 +14,15 @@ public static void main(String[] args) { Users users = usersNameReader.convertNamesToUsers(); int time = Input.getHeightOfLadder(); + BridgeGenerator bridgeGenerator = BridgeGenerator.newInstance(users.getNumberOfUsers(), time); + + bridgeGenerator.add(List.of(true, false, true)); + bridgeGenerator.add(List.of(false, true, false)); + bridgeGenerator.add(List.of(true, false, false)); + bridgeGenerator.add(List.of(false, true, false)); + bridgeGenerator.add(List.of(true, false, true)); + + Output.showUserNames(users); + Output.showResult(bridgeGenerator.getBridgeStates()); } } diff --git a/src/main/java/ladder/domain/Line.java b/src/main/java/ladder/domain/Line.java index ee0e451..ff4a65b 100644 --- a/src/main/java/ladder/domain/Line.java +++ b/src/main/java/ladder/domain/Line.java @@ -23,7 +23,7 @@ public String toString() { } appendColumn(result); - return String.valueOf(result); + return " " + String.valueOf(result); } private void appendColumn(StringBuilder result) { @@ -32,11 +32,11 @@ private void appendColumn(StringBuilder result) { private void appendBridge(StringBuilder result, Boolean state) { if (state) { - result.append("----"); + result.append("------"); } if (!state) { - result.append(" "); + result.append(" "); } } } diff --git a/src/main/java/ladder/view/Output.java b/src/main/java/ladder/view/Output.java index 6f691ff..0dfd715 100644 --- a/src/main/java/ladder/view/Output.java +++ b/src/main/java/ladder/view/Output.java @@ -1,4 +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())); + } } From 097f9b794d93797d7a9787878bed6f736d749241 Mon Sep 17 00:00:00 2001 From: woomin Date: Fri, 3 Jul 2020 18:21:05 +0900 Subject: [PATCH 17/17] Add create() to create random bridges --- .../java/ladder/controller/LadderGame.java | 12 ++----- .../java/ladder/domain/BridgeGenerator.java | 34 +++++++++++++++++-- src/test/java/ladder/BridgeGeneratorTest.java | 4 +-- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/main/java/ladder/controller/LadderGame.java b/src/main/java/ladder/controller/LadderGame.java index 1e1bce4..832d8a6 100644 --- a/src/main/java/ladder/controller/LadderGame.java +++ b/src/main/java/ladder/controller/LadderGame.java @@ -6,21 +6,15 @@ import ladder.view.Input; import ladder.view.Output; -import java.util.List; - public class LadderGame { public static void main(String[] args) { UsersNameReader usersNameReader = Input.getUsersName(); Users users = usersNameReader.convertNamesToUsers(); - int time = Input.getHeightOfLadder(); - BridgeGenerator bridgeGenerator = BridgeGenerator.newInstance(users.getNumberOfUsers(), time); + BridgeGenerator bridgeGenerator = + BridgeGenerator.newInstance(users.getNumberOfUsers(), Input.getHeightOfLadder()); - bridgeGenerator.add(List.of(true, false, true)); - bridgeGenerator.add(List.of(false, true, false)); - bridgeGenerator.add(List.of(true, false, false)); - bridgeGenerator.add(List.of(false, true, false)); - bridgeGenerator.add(List.of(true, false, true)); + 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 index dc48096..bf31480 100644 --- a/src/main/java/ladder/domain/BridgeGenerator.java +++ b/src/main/java/ladder/domain/BridgeGenerator.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Random; public class BridgeGenerator { private final int heigthOfLadder; @@ -18,9 +19,36 @@ public static BridgeGenerator newInstance(int numberOfUsers, int heightOfLadder) return new BridgeGenerator(numberOfUsers, heightOfLadder); } - public void add(List states) { - Line line = Line.newInstance(states); - this.bridgeStates.add(line); + 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() { diff --git a/src/test/java/ladder/BridgeGeneratorTest.java b/src/test/java/ladder/BridgeGeneratorTest.java index 34793c0..7a06205 100644 --- a/src/test/java/ladder/BridgeGeneratorTest.java +++ b/src/test/java/ladder/BridgeGeneratorTest.java @@ -14,9 +14,7 @@ void add() { int heightOfLadder = 3; BridgeGenerator bridgeGenerator = BridgeGenerator.newInstance(numberOfUsers, heightOfLadder); - bridgeGenerator.add(List.of(true, false, true, false)); - bridgeGenerator.add(List.of(true, false, false, true)); - bridgeGenerator.add(List.of(false, true, false, true)); + bridgeGenerator.create(); assertThat(bridgeGenerator.getBridgeStates()).hasSize(3); }