Skip to content

Commit

Permalink
refactor: toString 메소드 사용 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
brorae committed May 1, 2022
1 parent 1d62f32 commit 7bcec61
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/main/java/chess/dao/BoardDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void save(ChessBoard chessBoard, int gameId) {
String sql = "insert into board (position, symbol, color, game_id) values (?, ?, ?, ?)";

List<Object[]> board = chessBoard.getPieces().entrySet().stream()
.map(entry -> new Object[]{entry.getKey().toString(), entry.getValue().getSymbol().name(),
.map(entry -> new Object[]{entry.getKey().getValue(), entry.getValue().getSymbol().name(),
entry.getValue().getColor().name(), gameId})
.collect(Collectors.toList());

Expand All @@ -48,7 +48,7 @@ public ChessBoard findById(int id) {
@Override
public int update(Position position, Piece piece, int gameId) {
String sql = "update board set symbol = (?), color = (?) where game_id = (?) and position = (?)";
return jdbcTemplate.update(sql, piece.getSymbol().name(), piece.getColor().name(), gameId, position.toString());
return jdbcTemplate.update(sql, piece.getSymbol().name(), piece.getColor().name(), gameId, position.getValue());
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/chess/domain/chessboard/ChessBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ public Map<Position, Piece> getPieces() {
public Map<String, Piece> toMap() {
return pieces.entrySet()
.stream()
.collect(Collectors.toMap(m -> m.getKey().toString(), Map.Entry::getValue));
.collect(Collectors.toMap(m -> m.getKey().getValue(), Map.Entry::getValue));
}
}
5 changes: 0 additions & 5 deletions src/main/java/chess/domain/position/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,4 @@ public Column move(final int value) {
public int getValue() {
return value;
}

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}
}
9 changes: 8 additions & 1 deletion src/main/java/chess/domain/position/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,15 @@ public int hashCode() {
return Objects.hash(column, row);
}

public String getValue() {
return column.name().toLowerCase(Locale.ROOT) + row.getValue();
}

@Override
public String toString() {
return column.toString() + row.toString();
return "Position{" +
"column=" + column +
", row=" + row +
'}';
}
}
5 changes: 0 additions & 5 deletions src/main/java/chess/domain/position/Row.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ public Row move(final int value) {
public int getValue() {
return value;
}

@Override
public String toString() {
return Integer.toString(value);
}
}
4 changes: 2 additions & 2 deletions src/main/java/chess/domain/state/BlackRunning.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private boolean isBlackTurn(final ChessBoard chessBoard, final GameCommand gameC
}

@Override
public String toString() {
return "BlackRunning";
public String getValue() {
return "BlackTurn";
}
}
3 changes: 1 addition & 2 deletions src/main/java/chess/domain/state/Finish.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public boolean isFinished() {
return true;
}

@Override
public String toString() {
public String getValue() {
return "Finish";
}
}
2 changes: 1 addition & 1 deletion src/main/java/chess/domain/state/Ready.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean isFinished() {
}

@Override
public String toString() {
public String getValue() {
return "Ready";
}
}
6 changes: 4 additions & 2 deletions src/main/java/chess/domain/state/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ public interface State {
State proceed(final ChessBoard chessBoard, final GameCommand gameCommand);

static State of(String value) {
if (value.equals("BlackRunning")) {
if (value.equals("BlackTurn")) {
return new BlackRunning();
}
if (value.equals("WhiteRunning")) {
if (value.equals("WhiteTurn")) {
return new WhiteRunning();
}
if (value.equals("Finish")) {
Expand All @@ -21,4 +21,6 @@ static State of(String value) {
}

boolean isFinished();

String getValue();
}
4 changes: 2 additions & 2 deletions src/main/java/chess/domain/state/WhiteRunning.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private boolean isWhiteTurn(final ChessBoard chessBoard, final GameCommand gameC
}

@Override
public String toString() {
return "WhiteRunning";
public String getValue() {
return "WhiteTurn";
}
}
12 changes: 6 additions & 6 deletions src/main/java/chess/service/ChessService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public int insertGame(RoomDto roomDto, ChessBoard chessBoard) {
ChessGame chessGame = new ChessGame(chessBoard);
chessGame.playGameByCommand(GameCommand.of("start"));

int id = gameDao.save(roomDto.getTitle(), roomDto.getPassword(), chessGame.getState().toString());
int id = gameDao.save(roomDto.getTitle(), roomDto.getPassword(), chessGame.getState().getValue());
boardDao.save(chessGame.getChessBoard(), id);
return id;
}
Expand All @@ -52,7 +52,7 @@ public BoardDto selectBoard(int id) {
Map<Position, Piece> pieces = chessBoard.getPieces();
Map<String, PieceDto> board = new HashMap<>();
for (Position position : pieces.keySet()) {
String key = position.toString();
String key = position.getValue();
Piece piece = pieces.get(Position.of(key));
PieceDto pieceDto = new PieceDto(piece.getSymbol().name(), piece.getColor().name());
board.put(key, pieceDto);
Expand All @@ -72,7 +72,7 @@ public String selectWinner(int gameId) {
}

public String selectState(int gameId) {
return gameDao.findState(gameId).toString();
return gameDao.findState(gameId).getValue();
}

public StatusDto selectStatus(int gameId) {
Expand All @@ -87,7 +87,7 @@ public void movePiece(int gameId, String from, String to) {
ChessGame chessGame = new ChessGame(gameDao.findState(gameId), boardDao.findById(gameId));
chessGame.playGameByCommand(GameCommand.of("move", from, to));
chessGame.isEndGameByPiece();
gameDao.update(chessGame.getState().toString(), gameId);
gameDao.update(chessGame.getState().getValue(), gameId);

Map<String, Piece> pieces = chessGame.getChessBoard().toMap();
boardDao.update(Position.of(to), pieces.get(to), gameId);
Expand All @@ -98,7 +98,7 @@ public void movePiece(int gameId, String from, String to) {
public void endGame(int gameId) {
ChessGame chessGame = new ChessGame(gameDao.findState(gameId), boardDao.findById(gameId));
chessGame.playGameByCommand(GameCommand.of("end"));
gameDao.update(chessGame.getState().toString(), gameId);
gameDao.update(chessGame.getState().getValue(), gameId);
}

@Transactional
Expand All @@ -122,7 +122,7 @@ private void validateState(State state) {
@Transactional
public void restartGame(int gameId) {
ChessBoard chessBoard = new ChessBoard(new NormalPiecesGenerator());
int id = gameDao.update("WhiteRunning", gameId);
int id = gameDao.update("WhiteTurn", gameId);
boardDao.delete(gameId);
boardDao.save(chessBoard, id);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/chess/dao/BoardDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BoardDaoTest {

@BeforeEach
void setUp() {
gameId = gameDao.save("title", "password", "WhiteRunning");
gameId = gameDao.save("title", "password", "WhiteTurn");
boardDao.save(new ChessBoard(new NormalPiecesGenerator()), gameId);
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/chess/dao/FakeBoardDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void save(ChessBoard chessBoard, int gameId) {
Map<Position, Piece> pieces = chessBoard.getPieces();
Map<String, PieceDto> board = new HashMap<>();
for (Position position : pieces.keySet()) {
String key = position.toString();
String key = position.getValue();
Piece piece = pieces.get(Position.of(key));
PieceDto pieceDto = new PieceDto(piece.getSymbol().name(), piece.getColor().name());
board.put(key, pieceDto);
Expand All @@ -43,7 +43,7 @@ public ChessBoard findById(int id) {
@Override
public int update(Position position, Piece piece, int gameId) {
BoardDto boardDto = board.get(gameId);
String value = position.toString();
String value = position.getValue();
PieceDto pieceDto = new PieceDto(piece.getSymbol().name(), piece.getColor().name());
Map<String, PieceDto> board = boardDto.getBoard();
board.put(value, pieceDto);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/chess/dao/GameDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class GameDaoTest {

@BeforeEach
void setUp() {
gameId = gameDao.save("title", "password", "WhiteRunning");
gameId = gameDao.save("title", "password", "WhiteTurn");
}

@AfterEach
Expand All @@ -43,9 +43,9 @@ void findAll() {
@Test
@DisplayName("게임의 상태를 조회할 수 있다.")
void findState() {
String state = gameDao.findState(gameId).toString();
String state = gameDao.findState(gameId).getValue();

assertThat(state).isEqualTo("WhiteRunning");
assertThat(state).isEqualTo("WhiteTurn");
}

@Test
Expand All @@ -67,9 +67,9 @@ void findGameCount() {
@Test
@DisplayName("게임의 상태를 업데이트할 수 있다.")
void update() {
gameDao.update("BlackRunning", gameId);
String state = gameDao.findState(gameId).toString();
gameDao.update("BlackTurn", gameId);
String state = gameDao.findState(gameId).getValue();

assertThat(state).isEqualTo("BlackRunning");
assertThat(state).isEqualTo("BlackTurn");
}
}
8 changes: 4 additions & 4 deletions src/test/java/chess/service/ChessServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void updateBoard() {
void selectState() {
String state = chessService.selectState(id);

assertThat(state).isEqualTo("WhiteRunning");
assertThat(state).isEqualTo("WhiteTurn");
}

@Test
Expand All @@ -120,7 +120,7 @@ void selectStatus() {
void endGame() {
chessService.endGame(id);

String state = gameDao.findState(id).toString();
String state = gameDao.findState(id).getValue();

assertThat(state).isEqualTo("Finish");
}
Expand Down Expand Up @@ -158,8 +158,8 @@ void deleteGame() {
void restartGame() {
chessService.endGame(id);
chessService.restartGame(id);
String state = gameDao.findState(id).toString();
String state = gameDao.findState(id).getValue();

assertThat(state).isEqualTo("WhiteRunning");
assertThat(state).isEqualTo("WhiteTurn");
}
}

0 comments on commit 7bcec61

Please sign in to comment.