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

[Step2] 로또 - 2등 #7

Open
wants to merge 10 commits into
base: doy
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
6 changes: 6 additions & 0 deletions src/main/java/InputView.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ public static String askWinningNumbers() {
System.out.println("\n지난 주 당첨 번호를 입력해 주세요.");
return scanner.nextLine();
}

public static int askBonusNumber() {
scanner = new Scanner(System.in);
System.out.println("\n보너스 볼을 입력해 주세요.");
return scanner.nextInt();
}
}
4 changes: 2 additions & 2 deletions src/main/java/LottoGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public LottoGame(final List<Ticket> tickets) {
this.tickets = tickets;
}

public LottoResult matchTickets(final WinningTicket winningTicket) {
public LottoResult matchTickets(final WinningTicket winningTicket, final LottoNumber bonusNumber) {
LottoResult lottoResult = new LottoResult();
for (Ticket ticket : tickets) {
lottoResult.add(Rank.valueOf(ticket.match(winningTicket)));
lottoResult.add(Rank.valueOf(ticket.matchCount(winningTicket), ticket.contains(bonusNumber)));
}

return lottoResult;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/LottoNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static LottoNumber of(final int number) {

public LottoNumber(final int value) {
if (value < MIN_VALUE || value > MAX_VALUE) {
throw new IllegalArgumentException();
throw new IllegalArgumentException("로또 번호는 " + MIN_VALUE + "이상 " + MAX_VALUE + "이하 이어야 합니다.");
}
this.value = value;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/LottoResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public int calculatePrizeMoney() {
public String toString() {
Copy link

@Delf-Lee Delf-Lee Jan 19, 2020

Choose a reason for hiding this comment

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

극단적으로는 출력하는 책임도 분리할 수 있을것 같습니다. 소리나 이미지로 출력하는 것 처럼요. (@toString오버라이딩 전반)

(변경안 아님)

StringBuilder stringBuilder = new StringBuilder();
for (Rank rank : rankCountMap.keySet()) {
stringBuilder.append(rankCountMap.get(rank) * rank.getPrizeMoney());
stringBuilder.append(rank.toString())
.append(String.format(" - %d개", rankCountMap.get(rank)))
.append("\n");
}
return stringBuilder.toString();
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/LottoSeller.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import java.util.List;

public class LottoSeller {
private static final int PRICE = 1000;
public static final int UNIT_PRICE = 1000;

Choose a reason for hiding this comment

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

로또의 가격은 변경의 여지가 있으니, 외부에서 주입받을 수 있을 것 같습니다.


private final NumberGenerator numberGenerator;

Expand All @@ -11,8 +11,10 @@ public LottoSeller(final NumberGenerator numberGenerator) {
}

public List<Ticket> buyTickets(final int money) {
if (money < UNIT_PRICE) throw new IllegalArgumentException(UNIT_PRICE + "원 이상 구매해야 합니다");

List<Ticket> tickets = new ArrayList<>();
for (int i = 0; i < money / PRICE; i++) {
for (int i = 0; i < money / UNIT_PRICE; i++) {
tickets.add(new Ticket(LottoNumbers.of(numberGenerator.generate())));
}
return tickets;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ public class Main {
private static final int LOTTO_NUMBER_SIZE = 6;

public static void main(String[] args) {
LottoSeller seller = new LottoSeller(new RandomNumberGenerator(LOTTO_NUMBER_SIZE));

int money = InputView.askPurchasingAmount();
ResultView.printLottoCount(money / LottoSeller.UNIT_PRICE);

LottoSeller seller = new LottoSeller(new RandomNumberGenerator(LOTTO_NUMBER_SIZE));
List<Ticket> tickets = seller.buyTickets(money);
ResultView.printTickets(tickets);

WinningTicket winningTicket = new WinningTicket(LottoNumbers.of(InputView.askWinningNumbers()));

LottoGame game = new LottoGame(tickets);
LottoResult result = game.matchTickets(winningTicket);
LottoResult result = game.matchTickets(winningTicket, LottoNumber.of(InputView.askBonusNumber()));
ResultView.printStatistics(result);
ResultView.printRateOfRevenue(game.calculateRevenue(money, result));
}
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/Rank.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
public enum Rank {
FIRST(6, 2000000000),
SECOND(5, 1500000),
THIRD(4, 50000),
FOURTH(3, 5000);
FIRST(6, false, 2000000000),
SECOND(5, true, 30000000),
THIRD(5, false, 1500000),
FOURTH(4, false, 50000),
FIFTH(3, false, 5000),
MISS(0, false, 0);

private static final int WINNING_MIN_COUNT = 3;

private int countOfMatch;
private boolean isBonusMatch;
Copy link
Member

Choose a reason for hiding this comment

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

이런 전략을 하나로 추상화할 수 없을까요?
나중에 페널티 로또나, 다른 Rank 전략이 생기는 경우에 대해 확장성을 가지는 구조를 어떤 형태일까요?

private int prizeMoney;

Rank(final int countOfMatch, final int prizeMoney) {
Rank(final int countOfMatch, final boolean isBonusMatch, final int prizeMoney) {
this.countOfMatch = countOfMatch;
this.isBonusMatch = isBonusMatch;
this.prizeMoney = prizeMoney;
}

public static Rank valueOf(final int countOfMatch) {
public static Rank valueOf(final int countOfMatch, final boolean isBonusMatch) {
if (countOfMatch < WINNING_MIN_COUNT) {
return MISS;
}

for (final Rank rank : Rank.values()) {
if (rank.countOfMatch == countOfMatch) {
if (rank.countOfMatch == countOfMatch && rank.isBonusMatch == isBonusMatch) {
return rank;
}
}
Expand All @@ -26,4 +36,9 @@ public int getPrizeMoney() {
return prizeMoney;
}

@Override
public String toString() {
if (this.equals(SECOND)) return String.format("%d개 일치, 보너스 볼 일치 (%d원)", countOfMatch, prizeMoney);
Copy link
Member

Choose a reason for hiding this comment

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

한국어가 아니라 영어로 출력되는 경우에 대해 확장성있는 구조는 어떻게 가져가야할까요?

Choose a reason for hiding this comment

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

https://github.com/WeareSoft/java-lotto/pull/7/files#r368273623
제가 단 댓글과 같은 맥락인것 같군요 😄

return String.format("%d개 일치 (%d원)", countOfMatch, prizeMoney);
}
}
4 changes: 4 additions & 0 deletions src/main/java/ResultView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import java.util.List;

public class ResultView {
public static void printLottoCount(final int count) {
System.out.println(String.format("%d개를 구입했습니다.", count));
}

public static void printTickets(final List<Ticket> tickets) {
tickets.forEach(ticket -> System.out.println(ticket.toString()));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Ticket.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public Ticket(final LottoNumbers numbers) {
this.numbers = numbers;
}

public int match(final WinningTicket winningTicket) {
public int matchCount(final WinningTicket winningTicket) {
return Math.toIntExact(numbers.toList().stream().filter(winningTicket::contains).count());
}

Expand Down