Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Step2 ladder create #4

Open
wants to merge 17 commits into
base: woomin-Jeon
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions src/main/java/ladder/controller/LadderGame.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
57 changes: 57 additions & 0 deletions src/main/java/ladder/domain/BridgeGenerator.java
Original file line number Diff line number Diff line change
@@ -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<Line> 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<Boolean> makeRandomStates() {
Random random = new Random();

List<Boolean> states = new ArrayList<>();
for (int i = 0; i < numberOfUsers - 1; i += 1) {
addState(random, states, i);
}

return states;
}

private void addState(Random random, List<Boolean> 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<Line> getBridgeStates() {
return Collections.unmodifiableList(this.bridgeStates);
}
}
42 changes: 42 additions & 0 deletions src/main/java/ladder/domain/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ladder.domain;

import java.util.List;

public class Line {
private final List<Boolean> bridgeStates;

public static Line newInstance(List<Boolean> bridgeStates) {
return new Line(bridgeStates);
}

public Line(List<Boolean> 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(" ");
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/ladder/domain/User.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
29 changes: 29 additions & 0 deletions src/main/java/ladder/domain/Users.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ladder.domain;

import java.util.List;

public class Users {
private final List<User> users;

private Users(List<User> users) {
this.users = users;
}

public static Users newInstance(List<User> 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();
}
}
40 changes: 40 additions & 0 deletions src/main/java/ladder/domain/UsersNameReader.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<User> userNames = Arrays.stream(this.names.split(","))
.map(User::newInstance)
.collect(Collectors.toList());

return Users.newInstance(userNames);
}
}
21 changes: 21 additions & 0 deletions src/main/java/ladder/view/Input.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
16 changes: 16 additions & 0 deletions src/main/java/ladder/view/Output.java
Original file line number Diff line number Diff line change
@@ -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<Line> bridgeStates) {
bridgeStates.forEach(v -> System.out.println(v.toString()));
}
}
21 changes: 21 additions & 0 deletions src/test/java/ladder/BridgeGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
21 changes: 21 additions & 0 deletions src/test/java/ladder/LineTest.java
Original file line number Diff line number Diff line change
@@ -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<Boolean> bridgeStates = List.of(true, false);

Line line = Line.newInstance(bridgeStates);

assertThat(line.toString()).isEqualTo("|----| |");
}
}
17 changes: 17 additions & 0 deletions src/test/java/ladder/UserTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
33 changes: 33 additions & 0 deletions src/test/java/ladder/UsersNameReaderTest.java
Original file line number Diff line number Diff line change
@@ -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 ");
}
}
28 changes: 28 additions & 0 deletions src/test/java/ladder/UsersTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}