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

[사다리 - 3단계] 김성환 미션 제출합니다. #24

Open
wants to merge 2 commits into
base: fanngineer
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
5 changes: 4 additions & 1 deletion src/main/java/LadderApplication.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import controller.LadderController;
import domain.Ladder;
import domain.Participants;

public class LadderApplication {
public static void main(String[] args) {
final LadderController ladderController = new LadderController();
Ladder ladder = ladderController.createLadder();
ladderController.printResult(ladder);
Participants participants = ladderController.createParticipants(ladder.getWidth());
ladderController.getResult(ladder, participants);
ladderController.printResult(ladder, participants);
}
}
24 changes: 19 additions & 5 deletions src/main/java/controller/LadderController.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package controller;

import domain.Ladder;
import domain.Participant;
import domain.Participants;
import domain.Size;
import java.util.List;
import service.LadderService;
import view.InputView;
import view.OutputView;

public class LadderController {
private final OutputView outputView = new OutputView();
private final InputView inputView = new InputView();
private final LadderService ladderService = new LadderService();

public Ladder createLadder() {
Size width = inputView.getLadderWidth();
Size height = inputView.getLadderHeight();
return new Ladder(height, width);
int width = inputView.getLadderWidth();
int height = inputView.getLadderHeight();
return ladderService.createLadder(height, width);
}

public void printResult(Ladder ladder){
outputView.printResult();
public Participants createParticipants(int width) {
return ladderService.createParticipants(width);
}

public void getResult(Ladder ladder, Participants participants) {
ladderService.getResult(ladder, participants);
}

public void printResult(Ladder ladder, Participants participants){
outputView.printResultText();
outputView.printLadder(ladder);
outputView.printResult(participants);
}
}
33 changes: 33 additions & 0 deletions src/main/java/domain/Ladder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ public Ladder(Size ladderSize, Size lineSize) {
this.lines = generateLines(ladderSize, lineSize);
}

public Ladder(List<Line> lines) {
this.lines = lines;
}

private List<Line> generateLines(Size ladderSize, Size lineSize) {
List<Line> lines = new ArrayList<>();
for (int i = 0; i < ladderSize.getSize(); i++) {
Expand All @@ -18,7 +22,36 @@ private List<Line> generateLines(Size ladderSize, Size lineSize) {
return lines;
}

public void getResult(Participants participants){
for (Line line : lines) {
changeByLine(line, participants.getParticipants());
}
}

private void changeByLine(Line line, List<Participant> participants){
for(int i = 0; i< line.getPoints().size() ; i++){
changeByPoint(i,line.getPoints().get(i), participants);
}
}

private void changeByPoint(int idx, Point point, List<Participant> participants){
if(point.isConnected()){
move(idx, participants);
}
}

private void move(int idx, List<Participant> participants){
int temp = participants.get(idx).getEnd();
participants.get(idx).changeEnd(participants.get(idx+1).getEnd());
participants.get(idx+1).changeEnd(temp);
}

public List<Line> getLines() {
return lines;
}

public int getWidth() {
return lines.get(0).getPoints().size()+1;
}

}
23 changes: 23 additions & 0 deletions src/main/java/domain/Participant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package domain;

public class Participant {
private final int start;
private int end;

public Participant(int start) {
this.start = start;
this.end = start;
}

public int getStart() {
return start;
}

public int getEnd() {
return end;
}

public void changeEnd(int end) {
this.end = end;
}
}
15 changes: 15 additions & 0 deletions src/main/java/domain/Participants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain;

import java.util.List;

public class Participants {
private final List<Participant> participants;

public Participants(List<Participant> participants) {
this.participants = participants;
}

public List<Participant> getParticipants() {
return participants;
}
}
29 changes: 29 additions & 0 deletions src/main/java/service/LadderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package service;

import domain.Ladder;
import domain.Participant;
import domain.Participants;
import domain.Size;
import java.util.ArrayList;
import java.util.List;

public class LadderService {

public Ladder createLadder(int height, int width) {
Size heightSize = new Size(height);
Size widthSize = new Size(width);
return new Ladder(heightSize, widthSize);
}

public Participants createParticipants(int width) {
List<Participant> participants = new ArrayList<>();
for(int i = 0 ; i< width; i++){
participants.add(new Participant(i));
}
return new Participants(participants);
}

public void getResult(Ladder ladder, Participants participants) {
ladder.getResult(participants);
}
}
9 changes: 4 additions & 5 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package view;

import domain.Size;
import java.util.Scanner;

public class InputView {
private Scanner scanner = new Scanner(System.in);

public Size getLadderWidth() {
public int getLadderWidth() {
System.out.println("사다리의 넓이는 몇 개인가요?");
int width = scanner.nextInt();
scanner.nextLine();
System.out.println();
return new Size(width);
return width;
}

public Size getLadderHeight() {
public int getLadderHeight() {
System.out.println("사다리의 높이는 몇 개인가요?");
int height = scanner.nextInt();
scanner.nextLine();
System.out.println();
return new Size(height);
return height;
}
}
10 changes: 9 additions & 1 deletion src/main/java/view/OutputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import domain.Ladder;
import domain.Line;
import domain.Participant;
import domain.Participants;
import domain.Point;
import java.util.List;

public class OutputView {

public void printResult(){
public void printResultText(){
System.out.println("실행결과");
}

Expand All @@ -25,4 +28,9 @@ public void printLine(Line line){
System.out.print("|");
}

public void printResult(Participants participants){
for(Participant participant : participants.getParticipants()){
System.out.println(participant.getStart() + " -> " + participant.getEnd());
}
}
}
21 changes: 21 additions & 0 deletions src/test/java/domain/LadderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

Expand All @@ -12,4 +14,23 @@ public void creationTest() {
Ladder ladder = new Ladder(new Size(4),new Size(4));
assertThat(ladder.getLines().size()).isEqualTo(4);
}

@DisplayName("사다리 연결되었을 경우, 이웃 참가자 결과 swap 수행")
@Test
public void getResultTest() {
List<Participant> participantList = new ArrayList<Participant>();
for (int i = 0; i < 4; i++) {
participantList.add(new Participant(i));
}
Participants participants = new Participants(participantList);
List<Point> points = List.of(new Point(true), new Point(false), new Point(true));
Line line = new Line(points);
Ladder ladder = new Ladder(List.of(line));
ladder.getResult(participants);

assertThat(participants.getParticipants().get(0).getEnd()).isEqualTo(1);
assertThat(participants.getParticipants().get(1).getEnd()).isEqualTo(0);
assertThat(participants.getParticipants().get(2).getEnd()).isEqualTo(3);
assertThat(participants.getParticipants().get(3).getEnd()).isEqualTo(2);
}
}