Skip to content

๐Ÿš€ 3๋‹จ๊ณ„ - ์‚ฌ๋‹ค๋ฆฌ(๊ฒŒ์ž„ ์‹คํ–‰) #2418

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

Merged
merged 18 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
07dd4ec
refactor : refactored anonymous class to Lambda expression
choincnp Apr 22, 2025
0cdb9c6
refactor : add functional interface and refactored to use anonymous cโ€ฆ
choincnp Apr 22, 2025
5286c1b
refactor : replace Anonymous class to lambda expression
choincnp Apr 22, 2025
9421723
refactor : implement stream methods
choincnp Apr 22, 2025
9bdd075
refactor : using optional method to filter results - 1
choincnp Apr 22, 2025
c0f7bf5
refactor : using optional method to filter results - 2
choincnp Apr 22, 2025
a305a44
refactor : using optional method to filter results - 3
choincnp Apr 22, 2025
605a7bb
test : Add domain - Line and some invariants test
choincnp Apr 22, 2025
78384d2
test : Add domain - Ladder and add prototype test
choincnp Apr 22, 2025
bf5c875
refactor : split into small entity
choincnp Apr 23, 2025
d935f02
feat : Add I/O class / method
choincnp Apr 23, 2025
2a8c926
feat : Refactor I/O class/method
choincnp Apr 24, 2025
d9d7c82
refactor : change outputs into fixed type, use strategy pattern
choincnp Apr 24, 2025
c661e70
Merge branch 'step2' into step3
choincnp Apr 24, 2025
d5ced35
refactor : change I/O views and modified some logics
choincnp Apr 24, 2025
5b00ec9
Merge branch 'choincnp' into step3
choincnp Apr 24, 2025
ac7dfaf
refactor : 1์ฐจ ๋ฆฌํŒฉํ† ๋ง, index ๊ตฌ์กฐ์—์„œ ๊ฐ์ฒดํ˜•์‹์œผ๋กœ
choincnp May 5, 2025
672fac7
refactor : ์ปค๋งจ๋“œ ๋ถ„๋ฆฌ์— ๋”ฐ๋ฅธ validation ๋ณ€๊ฒฝ
choincnp May 5, 2025
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
23 changes: 19 additions & 4 deletions src/main/java/nextstep/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package nextstep;

import nextstep.domain.Bonuses;
import nextstep.domain.Command;
import nextstep.domain.Ladder;
import nextstep.domain.Names;
import nextstep.domain.LadderGame;
import nextstep.domain.LadderResult;
import nextstep.domain.Participants;
import nextstep.view.InputView;
import nextstep.view.OutputView;

Expand All @@ -11,11 +15,22 @@ public static void main(String[] args) {
InputView in = new InputView();
OutputView out = new OutputView();

Names names = in.getName();
Participants participants = in.getName();
Bonuses bonus = in.getBonus(participants.size());
int height = in.getHeight();

Ladder ladder = Ladder.of(names.size(), height);
out.printResult(names, ladder);
Ladder ladder = Ladder.of(participants.size(), height);
LadderGame game = new LadderGame(bonus, participants, ladder);
LadderResult result = game.play();

out.printLadder(participants, ladder, bonus);
while (true) {
Command command = Command.of(in.readTarget());
if (command.isAll()) {
out.printAllResults(result);
break;
}
out.printPersonalResult(result, command.getValue());
}
}
}
42 changes: 42 additions & 0 deletions src/main/java/nextstep/domain/Bonus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nextstep.domain;

public class Bonus {
private static final Integer MAX_NAME_LENGTH = 5;
private final String bonusName;
private final Integer position;

public Bonus(String bonusName, Integer position) {
validate(bonusName);
this.bonusName = bonusName;
this.position = position;
}

public static Bonus of(String bonusName) {
return new Bonus(bonusName, 0);
}

public static Bonus of(String bonusName, int position) {
return new Bonus(bonusName, position);
}

private void validate(String input) {
if (input == null || input.trim().isEmpty()) {
throw new IllegalArgumentException("์ด๋ฆ„์€ ๋นˆ ๋ฌธ์ž์—ด์ด ๋  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
if (input.length() > MAX_NAME_LENGTH) {
throw new IllegalArgumentException("์ด๋ฆ„์€ ์ตœ๋Œ€ " + MAX_NAME_LENGTH + "์ž๊นŒ์ง€ ํ—ˆ์šฉ๋ฉ๋‹ˆ๋‹ค.");
}
}

public int position() {
return position;
}

public boolean isMatch(int index){
return position.equals(index);
}

public String getName() {
return bonusName;
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์›์‹œ๊ฐ์ฒด ํฌ์žฅ ์ข‹์Šต๋‹ˆ๋‹ค ๐Ÿ‘

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

import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
* Bonus์˜ ์ผ๊ธ‰์ปฌ๋ ‰์…˜
*/
public class Bonuses {
private List<Bonus> bonuses;

public Bonuses(String input, int count) {
List<Bonus> bonuses = splitAndTrim(input);
if (count != bonuses.size()) {
throw new IllegalArgumentException("๋ณด๋„ˆ์Šค ๊ฐœ์ˆ˜์™€ ์‚ฌ๋žŒ ์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}
this.bonuses = bonuses;
}

private List<Bonus> splitAndTrim(String input) {
String[] names = input.split(",");
return IntStream.range(0, names.length)
.mapToObj(i -> Bonus.of(names[i].trim(), i))
.collect(Collectors.toList());
}

public Optional<Bonus> getBonusByPosition(int index) {
if (index < 0 || index >= bonuses.size()) {
throw new IndexOutOfBoundsException("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ์ˆซ์ž์ž…๋‹ˆ๋‹ค.");
}
return bonuses.stream()
.filter(bonus -> bonus.isMatch(index))
.findFirst();
}

public String getNameByPosition(int index) {
return getBonusByPosition(index)
.map(Bonus::getName)
.orElseThrow(NoSuchElementException::new);
}

public List<Bonus> unmodifiableNames() {
return Collections.unmodifiableList(bonuses);
}
}
48 changes: 48 additions & 0 deletions src/main/java/nextstep/domain/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package nextstep.domain;

import static nextstep.domain.CommandEnum.*;

import java.util.Objects;

/**
* ์‚ฌ๋‹ค๋ฆฌ๊ฒŒ์ž„ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ์‹ถ์€ ์ปค๋งจ๋“œ
*/
public class Command {
private final String command;

private Command(String command) {
this.command = validate(command.trim());
}

public static Command of(String command) {
return new Command(command);
}

public boolean isAll() {
return ALL.equals(command);
}

public String validate(String input) {
if (input == null || input.isEmpty()) {
throw new IllegalArgumentException("๋นˆ ๋ฌธ์ž์—ด์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
}
return input;
}

public String getValue() {
return command;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
Command command1 = (Command)o;
return Objects.equals(command, command1.command);
}

@Override
public int hashCode() {
return Objects.hashCode(command);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๊น”๋”ํ•˜๊ฒŒ ๊ฐœ์„ ํ•ด์ฃผ์…จ๋„ค์š” ๐Ÿ‘ ์ปค๋งจ๋“œ ๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ๊ฐ์ฒด๋“ค์ด ์ˆ˜์ •๋ ์ผ์ด ์—†์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ‘

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

public enum CommandEnum {
ALL("all");

private final String command;

CommandEnum(String command) {
this.command = command;
}

public boolean equals(String command) {
return this.command.equals(command);
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ™•์žฅ์„ฑ์„ ๊ณ ๋ คํ•œ Enum ๊นŒ์ง€ ๐Ÿ‘

9 changes: 9 additions & 0 deletions src/main/java/nextstep/domain/Ladder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package nextstep.domain;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -39,4 +41,11 @@ public int height() {
public List<Line> lines() {
return Collections.unmodifiableList(lines);
}

public Map<Integer, Integer> result() {
Map<Integer, Integer> result = new HashMap<>();
IntStream.range(0, lines.size())
.forEach(i -> result.put(i, play(i)));
return result;
}
}
33 changes: 33 additions & 0 deletions src/main/java/nextstep/domain/LadderGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package nextstep.domain;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class LadderGame {
private final Bonuses bonuses;
private final Participants participants;
private final Ladder ladder;

public LadderGame(Bonuses bonuses, Participants participants, Ladder ladder) {
this.bonuses = bonuses;
this.participants = participants;
this.ladder = ladder;
}

/**
* ๋ชจ๋“  ์ฐธ๊ฐ€์ž์˜ ๊ฒŒ์ž„ ๊ฒฐ๊ณผ๋ฅผ ๊ณ„์‚ฐํ•˜์—ฌ LadderResult๋กœ ๋ฐ˜ํ™˜
*/
public LadderResult play() {
// insertion order๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด LinkedHashMap ์‚ฌ์šฉ
Map<Participant, Bonus> resultMap = participants.unmodifiableNames().stream()
.collect(Collectors.toMap(
p -> p,
p -> bonuses.getBonusByPosition(ladder.play(p.getPosition())).orElseThrow(IllegalArgumentException::new),
(existing, replacement) -> existing,
LinkedHashMap::new
));
return new LadderResult(resultMap);
}

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

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class LadderResult {
private final Map<Participant, Bonus> result;

public LadderResult(Map<Participant, Bonus> result) {
this.result = result;
}

public String getBonusName(Participant participant) {
return Optional.ofNullable(result.get(participant))
.map(Bonus::getName)
.orElseThrow(IllegalArgumentException::new);
}

public String findBonusByParticipantName(String name) {
return Optional.ofNullable(getAllResult().get(name))
.orElseThrow(IllegalArgumentException::new);
}

/* ํ—ฌํผ ๋ฉ”์„œ๋“œ */
public Map<String, String> getAllResult() {
return Collections.unmodifiableMap(
result.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey().getName(),
e -> e.getValue().getName(),
(a, b) -> a, LinkedHashMap::new
))
);
}
}
47 changes: 0 additions & 47 deletions src/main/java/nextstep/domain/Names.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@
/**
* ์ด๋ฆ„ ํด๋ž˜์Šค
*/
public class Name {
public class Participant {
private static final Integer MAX_NAME_LENGTH = 5;
private final String name;
private final Integer position;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ด ์ผ€์ด์Šค์˜ ๊ฒฝ์šฐ ๊ทธ๋ƒฅ intํ˜•์œผ๋กœ ์‚ฌ์šฉํ•˜์‹œ๊ฑฐ๋‚˜ position๋„ ์›์‹œ๊ฐ์ฒด๋กœ ํฌ์žฅ๋˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ˜„
๋ฐ•์‹ฑ์„ ํ•˜๋ฉด ์‹ค์ œ ์—ฐ์‚ฐ์„ ํ•  ๋•Œ ๊ท€์ฐฎ์•„์งˆ ์ˆ˜๋„ ์žˆ๊ณ , ์ง€๊ธˆ ์ƒํ™ฉ์—์„œ๋Š” null์ผ ์—ผ๋ ค๋„ ์—†์œผ๋‹ˆ
๋ช…์‹œ์ ์œผ๋กœ intํ˜•์„ ์‚ฌ์šฉํ•˜๋Š”๊ฑธ ์ถ”์ฒœ ๋“œ๋ฆฝ๋‹ˆ๋‹ค ๐Ÿ™
์š” ๋‚ด์šฉ๋„ ํ•จ๊ป˜ ์ •๋ฆฌํ•˜๊ณ  ๊ฐ€์‹œ์ฃ  ๐Ÿ™‡

https://coding-factory.tistory.com/547


private Name(String name) {
public Participant(String name, int Position) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public Participant(String name, int Position) {
public Participant(String name, int position) {

validate(name);
this.name = name;
}

public static Name of(String name) {
return new Name(name);
}

public String getName() {
return name;
this.position = Position;
}

private void validate(String input) {
Expand All @@ -31,17 +25,24 @@ private void validate(String input) {
}
}

@Override
public String toString() {
public int getPosition() {
return position;
}

public boolean isSameName(String name) {
return this.name.equals(name);
}

public String getName() {
return name;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
Name name1 = (Name)o;
return Objects.equals(name, name1.name);
Participant participant1 = (Participant)o;
return Objects.equals(name, participant1.name);
}

@Override
Expand Down
Loading