-
Notifications
You must be signed in to change notification settings - Fork 745
๐ 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
Changes from all commits
07dd4ec
0cdb9c6
5286c1b
9421723
9bdd075
c0f7bf5
a305a44
605a7bb
78384d2
bf5c875
d935f02
2a8c926
d9d7c82
c661e70
d5ced35
5b00ec9
ac7dfaf
672fac7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} | ||
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); | ||
} | ||
} |
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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ๊น๋ํ๊ฒ ๊ฐ์ ํด์ฃผ์ จ๋ค์ ๐ ์ปค๋งจ๋ ๋๋ฌธ์ ๋ค๋ฅธ ๊ฐ์ฒด๋ค์ด ์์ ๋ ์ผ์ด ์์ ๊ฒ ๊ฐ์ต๋๋ค ๐ |
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); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ํ์ฅ์ฑ์ ๊ณ ๋ คํ |
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); | ||
} | ||
|
||
} |
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 | ||
)) | ||
); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ์ด ์ผ์ด์ค์ ๊ฒฝ์ฐ ๊ทธ๋ฅ |
||||||
|
||||||
private Name(String name) { | ||||||
public Participant(String name, int Position) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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) { | ||||||
|
@@ -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 | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์์๊ฐ์ฒด ํฌ์ฅ ์ข์ต๋๋ค ๐