-
Notifications
You must be signed in to change notification settings - Fork 748
๐ 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
base: choincnp
Are you sure you want to change the base?
Changes from all commits
07dd4ec
0cdb9c6
5286c1b
9421723
9bdd075
c0f7bf5
a305a44
605a7bb
78384d2
bf5c875
d935f02
2a8c926
d9d7c82
c661e70
d5ced35
5b00ec9
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 |
---|---|---|
@@ -1,21 +1,40 @@ | ||
package nextstep; | ||
|
||
import nextstep.domain.Ladder; | ||
import nextstep.domain.Names; | ||
import nextstep.view.InputView; | ||
import nextstep.view.OutputView; | ||
|
||
import java.util.List; | ||
|
||
import nextstep.domain.Bonus; | ||
import nextstep.domain.Ladder; | ||
import nextstep.domain.LadderResult; | ||
import nextstep.domain.Name; | ||
public class Main { | ||
public static void main(String[] args) { | ||
/* I/O ์ ์ธ */ | ||
InputView in = new InputView(); | ||
OutputView out = new OutputView(); | ||
|
||
Names names = in.getName(); | ||
List<Bonus> bonus = in.getBonus(names.size()); | ||
int height = in.getHeight(); | ||
|
||
Ladder ladder = Ladder.of(names.size(), height); | ||
out.printResult(names, ladder); | ||
LadderResult result = new LadderResult(ladder); | ||
|
||
out.printLadder(names, ladder, bonus); | ||
while (true) { | ||
String target = in.readTarget().trim(); | ||
boolean isAll = "all".equals(target); | ||
if (isAll) { | ||
out.printAllResults(names, result, bonus); | ||
break; | ||
} | ||
Name n = Name.of(target); | ||
int idx = names.unmodifiableNames().indexOf(n); | ||
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. ์ ์ฒด์ ์ผ๋ก
/**
* ์ด๋ฆ ํด๋์ค
*/
public class Participant {
private static final Integer MAX_NAME_LENGTH = 5;
private final String name;
private final int position; ๊ทธ๋ฆฌ๊ณ Optional<Participant> searched = participants.findByName(participant); ์ฌ์ฉํ๋ ์ชฝ์์ 0์ด๋ -1์ ํต์์ ์ผ๋ก ํต์ฉ๋๋ ์๋ฏธ์ด๊ธด ํฉ๋๋ค๋ง ์ถ๊ฐ์ ์ผ๋ก ๋๋ฏธํฐ ๋ฒ์น์ด ์๋๋ฐ ๊ฐ์ด ํ๋ฒ ๋ณด์๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค ๐ |
||
if (idx < 0) { | ||
out.printPersonalResult(-1, result, bonus); | ||
continue; | ||
} | ||
out.printPersonalResult(idx, result, bonus); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package nextstep.domain; | ||
|
||
public class Bonus { | ||
private static final Integer MAX_NAME_LENGTH = 5; | ||
private final String bonusName; | ||
|
||
public Bonus(String bonusName) { | ||
validate(bonusName); | ||
this.bonusName = bonusName; | ||
} | ||
|
||
public static Bonus of(String bonusName) { | ||
return new Bonus(bonusName); | ||
} | ||
|
||
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 + "์๊น์ง ํ์ฉ๋ฉ๋๋ค."); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return bonusName; | ||
} | ||
} | ||
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,26 @@ | ||
package nextstep.domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LadderResult { | ||
private final Map<Integer, Integer> result; | ||
|
||
public LadderResult(Ladder ladder) { | ||
result = ladder.result(); | ||
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. ์์์๋ ์๊ฒฌ๋๋ ธ์ง๋ง
|
||
} | ||
|
||
public List<Bonus> getOrderedBonuses(List<Bonus> bonusList) { | ||
List<Bonus> ordered = new ArrayList<>(bonusList.size()); | ||
for (int i = 0; i < bonusList.size(); i++) { | ||
int bonusIndex = result.get(i); | ||
ordered.add(bonusList.get(bonusIndex)); | ||
} | ||
return ordered; | ||
} | ||
|
||
public int get(int index) { | ||
return result.get(index); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,17 +18,16 @@ public static Name of(String name) { | |
return new Name(name); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
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 + "์๊น์ง ํ์ฉ๋ฉ๋๋ค."); | ||
} | ||
if (input.equals("all")) { | ||
throw new IllegalArgumentException("์ฌ์ฉํ ์ ์๋ ์ด๋ฆ์ ๋๋ค."); | ||
} | ||
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. ์์์๋ ์๊ฒฌ ๋๋ ธ์ง๋ง, |
||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
package nextstep.view; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Scanner; | ||
import java.util.stream.Collectors; | ||
|
||
import nextstep.domain.Bonus; | ||
import nextstep.domain.Name; | ||
import nextstep.domain.Names; | ||
|
||
/** | ||
* ์ ๋ ฅ ํด๋์ค | ||
*/ | ||
public class InputView { | ||
private static final int MAX_NAME_LENGTH = 5; | ||
private final Scanner scanner = new Scanner(System.in); | ||
|
||
/** ์ฐธ์ฌ ์ธ์ ์ ๋ ฅ */ | ||
|
@@ -22,7 +26,36 @@ public Names getName() { | |
public int getHeight() { | ||
System.out.println("์ต๋ ์ฌ๋ค๋ฆฌ ๋์ด๋ ๋ช ๊ฐ์ธ๊ฐ์?"); | ||
int height = scanner.nextInt(); | ||
scanner.nextLine(); | ||
return height; | ||
} | ||
|
||
public List<Bonus> getBonus(int count) { | ||
System.out.println("์คํ ๊ฒฐ๊ณผ๋ฅผ ์ ๋ ฅํ์ธ์. (๊ฒฐ๊ณผ๋ ์ผํ(,)๋ก ๊ตฌ๋ถํ์ธ์)"); | ||
String bonus = scanner.nextLine(); | ||
return parseBonus(bonus, count); | ||
} | ||
|
||
public String readTarget() { | ||
System.out.println("๊ฒฐ๊ณผ๋ฅผ ๋ณด๊ณ ์ถ์ ์ฌ๋์?"); | ||
String target = scanner.nextLine(); | ||
return target; | ||
} | ||
|
||
/** ๊ฒฐ๊ณผ ํ์ฑ */ | ||
private List<Bonus> parseBonus(String input, int count) { | ||
List<Bonus> bonus = splitAndTrim(input); | ||
// ๋ณด๋์ค ๊ฐ์ validation | ||
if (bonus.size() != count) { | ||
throw new IllegalArgumentException("๋ณด๋์ค ๊ฐ์๋ ์ฌ๋ ์์ ์ผ์นํด์ผ ํฉ๋๋ค."); | ||
} | ||
return bonus; | ||
} | ||
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 List<Bonus> splitAndTrim(String input) { | ||
return Arrays.stream(input.split(",")) | ||
.map(String::trim) | ||
.map(Bonus::new) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
import java.util.List; | ||
|
||
import nextstep.domain.Bonus; | ||
import nextstep.domain.Ladder; | ||
import nextstep.domain.LadderResult; | ||
import nextstep.domain.Line; | ||
import nextstep.domain.Name; | ||
import nextstep.domain.Names; | ||
|
@@ -16,24 +18,59 @@ public class OutputView { | |
private static final int WIDTH = 5; | ||
|
||
/** ๊ฒฐ๊ณผ */ | ||
public void printResult(Names names, Ladder ladder) { | ||
System.out.println("์คํ ๊ฒฐ๊ณผ"); | ||
public void printLadder(Names names, Ladder ladder, List<Bonus> bonusList) { | ||
System.out.println("์ฌ๋ค๋ฆฌ ๊ฒฐ๊ณผ"); | ||
|
||
// 1) ์ด๋ฆ ์ค | ||
System.out.println(formatNameLine(names)); | ||
|
||
// 2) ์ฌ๋ค๋ฆฌ ์ค | ||
ladder.lines().forEach(line -> | ||
System.out.println(formatLadderLine(line)) | ||
); | ||
ladder.lines() | ||
.forEach(line -> System.out.println(formatLadderLine(line))); | ||
|
||
// 3) ๋ณด๋์ค | ||
System.out.println(formatBonusLine(bonusList)); | ||
} | ||
|
||
/** | ||
* ๋จ์ผ ๊ฒฐ๊ณผ | ||
*/ | ||
public void printPersonalResult(int index, LadderResult result, List<Bonus> bonusList) { | ||
System.out.println("์คํ ๊ฒฐ๊ณผ"); | ||
if (index == -1) { | ||
System.out.println("๊ฒ์์ ์ฐธ์ฌํ์ง ์์ ์ฌ๋์ ๋๋ค."); | ||
return; | ||
} | ||
System.out.println(bonusList.get(result.get(index))); | ||
} | ||
|
||
/** | ||
* all ์กฐํ ๊ฒฐ๊ณผ ์ถ๋ ฅ | ||
*/ | ||
public void printAllResults(Names names, LadderResult result, List<Bonus> bonuses) { | ||
System.out.println("์คํ ๊ฒฐ๊ณผ"); | ||
for (int i = 0; i < names.size(); i++) { | ||
Name name = names.unmodifiableNames().get(i); | ||
Bonus bonus = bonuses.get(result.get(i)); | ||
System.out.printf("%s : %s%n", name, bonus); | ||
} | ||
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. ์์์๋ ์๊ฒฌ๋๋ ธ์ง๋ง, |
||
} | ||
|
||
/** ์ด๋ฆ formatting */ | ||
private static String formatNameLine(Names names) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Name name : names.unmodifiableNames()) { | ||
sb.append(" ") | ||
.append(padLeft(name.toString())); | ||
.append(padLeft(name.toString())); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private static String formatBonusLine(List<Bonus> bonusList) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (Bonus bonus : bonusList) { | ||
sb.append(" ") | ||
.append(padLeft(bonus.toString())); | ||
} | ||
return sb.toString(); | ||
} | ||
|
@@ -53,7 +90,8 @@ private static String formatLadderLine(Line line) { | |
|
||
/** ์ข์ธก ๊ณต๋ฐฑ */ | ||
private static String padLeft(String s) { | ||
if (s.length() >= WIDTH) return s; | ||
if (s.length() >= WIDTH) | ||
return s; | ||
return " ".repeat(WIDTH - s.length()) + s; | ||
} | ||
} |
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.
target
์ด ์์๊ฐ์ฒด๋ก ํฌ์ฅ๋๋ฉด ์ด๋จ๊น์?? ๐์ด์ฐ๋ณด๋ฉด ์ฌ๋ค๋ฆฌ๊ฒ์์์ ์ฌ์ฉ๋๋ ์ปค๋งจ๋์ญํ ์ด๊ธฐ ๋๋ฌธ์ ๋๋ฉ์ธ๋ชจ๋ธ๋ก ํํ๋๋ ๊ด์ฐฎ์ ๊ฒ ๊ฐ์ต๋๋ค.
๊ทธ๋ผ ๋ง์ฝ์
exit
๋ ์ด๋ฆ์ด ์๋index
๋ฑ๋ฑ ์ปค๋งจ๋๊ด๋ จํด์ ์์ ์ฌํญ์ด ๋ฐ์ํ์ ๋ ๊ด๋ฆฌ๋ ํธํ๊ณ"all"
์ด ์๋๋ผ ์ ์ฒด๋ฅผ ๋ณด์ฌ๋ฌ๋ผ๋ ๋ช ๋ น์ด ๋ณ๊ฒฝ๋๋ ๊ธฐ์กด ์ฝ๋ ์์ ์์ดLadderGameCommand
๊ฐ์ฒด๋ง์์ ํ๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค ๐
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.
์กฐ๊ธ ๋ ํฌ์ฅํ ๊ฑธ... ์์ฝ์ต๋๋ค ใ