diff --git a/README.md b/README.md index bfeebed5..d0fc8321 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,32 @@ # java-lotto 마스터즈 멤버스 2022 로또 게임 프로젝트 - +- ✍️ from reviewer + - countNumberOfWinnings() for문 대신 stream으로 개선 + - boolean 함수형 반환 이용한 결과 리턴시 불필요한 조건문을 쓰지 않기 + - 패키지명에 복수형을 잘 쓰지 않습니다. + - Lotto 객체가 직접 Ranking 결과값을 주는 방식으로 개선해야 하지 않을까요? + - 당첨번호 리스트를 보관할 만한 객체를 설계해서 그 객체의 리스트로 전달해주면 더 좋을텐데요? + - java.util.ArrayList 에만 존재하는 메서드나 로직에 애플리케이션이 강결합되어야 할 특별한 이유가 있을까요? + 그렇지 않다면 항상 List 를 사용하는 것이 좋습니다. + - [link](https://stackoverflow.com/questions/9852831/polymorphism-why-use-list-list-new-arraylist-instead-of-arraylist-list-n) + +
+🌱 step2. 자바 로또 2단계 - 보너스 번호 추가 - pair programming (hanse, sally) #### 기능요구사항 -- [x] 로또 구입 금액을 입력하면 구입 금액에 해당하는 로또를 발급해야 한다. -- [x] 로또 1장의 가격은 1000원이다. -- [x] 당첨 번호를 입력받아서 수익률을 출력한다. -- [x] 기대 수익은 동작 예시를 참고하자. +- [x] 2등을 위해 추가 번호를 하나 더 추첨한다. 당첨 통계에 2등도 추가해야 한다. #### 프로그래밍 요구사항 -- [x] indent(인덴트, 들여쓰기) depth를 1단계로 구현한다. -- [x] depth의 경우 if문을 사용하는 경우 1단계의 depth가 증가한다. if문 안에 while문을 사용한다면 depth가 2단계가 된다. -- [x] else를 사용하지 마라. -- [x] 메소드의 크기가 최대 10라인을 넘지 않도록 구현한다. -- [x] method가 한 가지 일만 하도록 최대한 작게 만들어라. -- [x] ArrayList를 사용해서 구현한다. - +- [x] enum을 적용해 프로그래밍을 구현한다. #### 출력 결과 - \ No newline at end of file + + +
diff --git a/src/LottoApplication.java b/src/LottoApplication.java new file mode 100644 index 00000000..bc853fd7 --- /dev/null +++ b/src/LottoApplication.java @@ -0,0 +1,29 @@ +import static view.Input.*; +import static view.Output.*; + +import java.util.List; + +import domains.user.LottoMachine; +import domains.user.Lottos; +import domains.user.Tickets; +import domains.winning.Ranking; +import domains.user.UserLotto; +import view.PurchasedLotto; + +public class LottoApplication { + private static LottoMachine lottoMachine = new LottoMachine(); + private static Lottos lottos = new Lottos(); + private static UserLotto userLotto = new UserLotto(lottoMachine, lottos); + + public static void main(String[] args) { + PurchasedLotto purchasedLotto = purchaseLotto(); + + Tickets totalLottos = userLotto.getTickets(purchasedLotto); + showLottos(totalLottos); + + Ranking ranking = userLotto.getRankingFromWinningNumbers(inputWinningNumbers(), getBonusNumber()); + getResultOfLotto(ranking, purchasedLotto.getPurchaseAmount()); + + scanClose(); + } +} diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 4a76e67d..00000000 --- a/src/Main.java +++ /dev/null @@ -1,44 +0,0 @@ -import static views.Input.*; -import static views.Output.*; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import domains.LottoMachine; -import domains.Lottos; -import domains.Ranking; -import domains.Winnings; - -public class Main { - private static final int IDX_PURCHASED_AMOUNT = 0; - private static final int IDX_NUMBER_OF_TICKET = 1; - private static LottoMachine lottoMachine = new LottoMachine(); - private static Lottos lottos = new Lottos(); - - public static void main(String[] args) { - int[] purchasedLotto = purchaseLotto(); - int purchaseAmount = purchasedLotto[IDX_PURCHASED_AMOUNT]; - int numberOfTicket = purchasedLotto[IDX_NUMBER_OF_TICKET]; - - ArrayList> tickets = lottoMachine.getTicket(numberOfTicket); - List> totalLottos = lottos.getTotalLottos(tickets); - showLottos(totalLottos); - - List winningNumbers = inputWinningNumbers(); - List threeOrMore = lottos.numberOfWinningAboveThree(winningNumbers); - - getResultOfLotto(purchaseAmount, threeOrMore); - - scanClose(); - } - - private static void getResultOfLotto(int purchaseAmount, List threeOrMore) { - Winnings winnings = new Winnings(threeOrMore); - Ranking ranking = new Ranking(winnings); - - Map ranks = ranking.resultOfRanks(); - double yields = ranking.totalYields(purchaseAmount); - printResultOfLottoAndYield(ranks, yields); - } -} diff --git a/src/domains/Lotto.java b/src/domains/Lotto.java deleted file mode 100644 index d7fa304e..00000000 --- a/src/domains/Lotto.java +++ /dev/null @@ -1,40 +0,0 @@ -package domains; - -import java.util.Collections; -import java.util.List; -import java.util.Objects; - -public class Lotto { - public static final String ERROR_OF_LOTTO_PARAMS = "error of lotto params"; - private final List sixNumbers; - - public Lotto(List sixNumbers) { - if (Objects.isNull(sixNumbers)) { - throw new IllegalArgumentException(ERROR_OF_LOTTO_PARAMS); - } - if (sixNumbers.size() < 6) { - throw new IllegalArgumentException(ERROR_OF_LOTTO_PARAMS); - } - Collections.sort(sixNumbers); - this.sixNumbers = sixNumbers; - } - - public List numbers() { - return this.sixNumbers; - } - - public int numberOfWinnings(List winningNumbers){ - int count = 0; - for (Integer winningNumber : winningNumbers) { - count = getCount(count, winningNumber); - } - return count; - } - - private int getCount(int count, Integer winningNumber) { - if (sixNumbers.contains(winningNumber)) { - count++; - } - return count; - } -} diff --git a/src/domains/Lottos.java b/src/domains/Lottos.java deleted file mode 100644 index c18a58f3..00000000 --- a/src/domains/Lottos.java +++ /dev/null @@ -1,43 +0,0 @@ -package domains; - -import static java.util.stream.Collectors.*; - -import java.util.ArrayList; -import java.util.List; - -public class Lottos { - public static final int MINIMUM_NUMBER_OF_WINNING = 3; - private ArrayList lottos = new ArrayList<>(); - - public Lottos() { - this.lottos = new ArrayList<>(); - } - - public List> getTotalLottos(ArrayList> tickets) { - - for (ArrayList ticket : tickets) { - this.purchased(ticket); - } - - List> purchasedLottos = new ArrayList<>(); - for (Lotto lotto : this.lottos) { - List numbers = lotto.numbers(); - purchasedLottos.add(numbers); - } - return purchasedLottos; - } - - private void purchased(ArrayList pickedNumber){ - Lotto lotto = new Lotto(pickedNumber); - this.lottos.add(lotto); - } - - public List numberOfWinningAboveThree(List winningNumbers) { - List resultOfLottos = this.lottos.stream() - .mapToInt(lotto -> lotto.numberOfWinnings(winningNumbers)) - .filter(value -> value >= MINIMUM_NUMBER_OF_WINNING) - .boxed() - .collect(toList()); - return resultOfLottos; - } -} diff --git a/src/domains/Ranking.java b/src/domains/Ranking.java deleted file mode 100644 index b363cf27..00000000 --- a/src/domains/Ranking.java +++ /dev/null @@ -1,92 +0,0 @@ -package domains; - -import java.util.Arrays; -import java.util.LinkedHashMap; -import java.util.Map; - -public class Ranking { - public static final int TO_PERCENT = 100; - private Map ranks = new LinkedHashMap<>(); - private Winnings winnings; - - public Ranking(Winnings winnings) { - this.ranks = initializeRanks(); - this.winnings = winnings; - } - - public enum Rank { - FOURTH(3, "3개", Money.of(5000)), - THIRD(4,"4개", Money.of(50_000)), - SECOND(5,"5개", Money.of(1_500_000)), - FIRST(6,"6개", Money.of(2_000_000_000)); - - private int count; - private String textOfCount; - private Money money; - - Rank(int count, String textOfCount, Money money) { - this.count = count; - this.textOfCount = textOfCount; - this.money = money; - } - - private boolean isSame(int number) { - return count == number; - } - - private Rank of(int number) { - return Arrays.stream(values()) - .filter(ranks -> ranks.isSame(number)) - .findFirst().get(); - } - - public String getText() { - return textOfCount; - } - - public String money() { - return money.text(); - } - - public int amount() { - return this.money.amount(); - } - } - - private static Map initializeRanks() { - Map results = new LinkedHashMap<>(); - for (Rank rank : Rank.values()) { - results.put(rank, 0); - } - return results; - } - - public Map resultOfRanks() { - Map counts = this.winnings.getCounts(); - for (Integer key : counts.keySet()) { - Rank rank = findRank(key); - this.ranks.put(rank, counts.get(key)); - } - return this.ranks; - } - - private Rank findRank(Integer key) { - return Arrays.stream(Rank.values()).map(rank -> rank.of(key)).findFirst().get(); - } - - /* - 수익률(%) = 손익/투자원금 * 100 = (평가금액-투자원금)/투자원금 * 100 - */ - public double totalYields(int purchaseAmount) { - Map counts = this.winnings.getCounts(); - double totalEarningsByRank = 0; - for (Integer key : counts.keySet()) { - Rank rank = findRank(key); - int amount = rank.amount(); - totalEarningsByRank += amount * counts.get(key); - } - double profit = (totalEarningsByRank - purchaseAmount) / purchaseAmount; - double rateOfReturn = profit * TO_PERCENT; - return rateOfReturn; - } -} diff --git a/src/domains/Winnings.java b/src/domains/Winnings.java deleted file mode 100644 index 30a1c0ea..00000000 --- a/src/domains/Winnings.java +++ /dev/null @@ -1,25 +0,0 @@ -package domains; - -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -public class Winnings { - private final Map counts; - - public Winnings(List threeOrMore) { - this.counts = getNumberOfEachRanks(threeOrMore); - } - - private static Map getNumberOfEachRanks(List threeOrMore) { - Map countInfo = new LinkedHashMap<>(); - for (Integer numberOfLottos : threeOrMore) { - countInfo.put(numberOfLottos, countInfo.getOrDefault(numberOfLottos, 0) + 1); - } - return countInfo; - } - - public Map getCounts() { - return counts; - } -} diff --git a/src/domains/user/Lotto.java b/src/domains/user/Lotto.java new file mode 100644 index 00000000..a645dcb3 --- /dev/null +++ b/src/domains/user/Lotto.java @@ -0,0 +1,53 @@ +package domains.user; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; + +import domains.winning.BonusWinningNumbers; +import domains.winning.Ranking; + +public class Lotto { + public static final int MINIMUM_NUMBER_OF_WINNING = 3; + public static final String ERROR_OF_LOTTO_PARAMS = "error of lotto params"; + private final List sixNumbers; + + public Lotto(List sixNumbers) { + if (Objects.isNull(sixNumbers)) { + throw new IllegalArgumentException(ERROR_OF_LOTTO_PARAMS); + } + if (sixNumbers.size() < 6) { + throw new IllegalArgumentException(ERROR_OF_LOTTO_PARAMS); + } + Collections.sort(sixNumbers); + this.sixNumbers = sixNumbers; + } + + public void recordRanking(BonusWinningNumbers winningNumbers, Ranking ranking){ + boolean checkedBonus = false; + List winning = winningNumbers.getNumbers(); + int winningCount = countWinningNumber(winning); + + if (winningCount >= MINIMUM_NUMBER_OF_WINNING) { + checkedBonus = this.getBonus(winningNumbers.getBonus()); + } + ranking.record(checkedBonus, winningCount); + } + + private int countWinningNumber(List winning) { + return Math.toIntExact(winning.stream() + .filter(winningNumber -> this.sixNumbers.contains(winningNumber)) + .count()); + } + + private int getCount(int count, Integer winningNumber) { + if (sixNumbers.contains(winningNumber)) { + count++; + } + return count; + } + + public boolean getBonus(int bonusNumber) { + return sixNumbers.contains(bonusNumber); + } +} diff --git a/src/domains/LottoMachine.java b/src/domains/user/LottoMachine.java similarity index 50% rename from src/domains/LottoMachine.java rename to src/domains/user/LottoMachine.java index 1d457d59..f767b302 100644 --- a/src/domains/LottoMachine.java +++ b/src/domains/user/LottoMachine.java @@ -1,38 +1,40 @@ -package domains; +package domains.user; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class LottoMachine { private static final int LOTTO_START_NUMBER = 1; private static final int LOTTO_MAX_LIMITED_NUMBER = 46; private static final int RANGE_OF_AUTO_MAX = 6; - public ArrayList> getTicket(int numberOfTicket) { - ArrayList lottoNumbers = getLottoNumbers(); - ArrayList> picked = new ArrayList<>(); + public Tickets getTicket(int numberOfTicket) { + List lottoNumbers = getLottoNumbers(); + Tickets tickets = new Tickets(); + for (int i = 0; i < numberOfTicket; i++) { mixNumber(lottoNumbers); - ArrayList autoPicked = pickAutoSixNumber(lottoNumbers); - picked.add(autoPicked); + List autoPicked = pickAutoSixNumber(lottoNumbers); + tickets.takeIt(autoPicked); } - return picked; + return tickets; } - private ArrayList getLottoNumbers() { - ArrayList lottoNumbers = new ArrayList<>(); + private List getLottoNumbers() { + List lottoNumbers = new ArrayList<>(); for (int i = LOTTO_START_NUMBER; i < LOTTO_MAX_LIMITED_NUMBER; i++) { lottoNumbers.add(i); } return lottoNumbers; } - private void mixNumber(ArrayList lottoNumbers) { + private void mixNumber(List lottoNumbers) { Collections.shuffle(lottoNumbers); } - private ArrayList pickAutoSixNumber(ArrayList lottoNumbers) { - ArrayList autoPickedNumber = new ArrayList<>(); + private List pickAutoSixNumber(List lottoNumbers) { + List autoPickedNumber = new ArrayList<>(); for (int j = 0; j < RANGE_OF_AUTO_MAX; j++) { autoPickedNumber.add(lottoNumbers.get(j)); } diff --git a/src/domains/user/Lottos.java b/src/domains/user/Lottos.java new file mode 100644 index 00000000..9ec7f7e7 --- /dev/null +++ b/src/domains/user/Lottos.java @@ -0,0 +1,39 @@ +package domains.user; + +import java.util.ArrayList; +import java.util.List; + +import domains.winning.BonusWinningNumbers; +import domains.winning.Ranking; + +public class Lottos { + private ArrayList lottos = new ArrayList<>(); + + public Lottos() { + this.lottos = new ArrayList<>(); + } + + public void of(Tickets tickets) { + toLottos(tickets); + } + + private void toLottos(Tickets tickets) { + for (List ticket : tickets.getTotalLottos()) { + this.purchased(ticket); + } + } + + private void purchased(List pickedNumber){ + Lotto lotto = new Lotto(pickedNumber); + this.lottos.add(lotto); + } + + + public Ranking getNumberOfWinningAboveThree(BonusWinningNumbers winningNumbers) { + Ranking ranking = new Ranking(); + for (Lotto lotto : lottos) { + lotto.recordRanking(winningNumbers, ranking); + } + return ranking; + } +} diff --git a/src/domains/Money.java b/src/domains/user/Money.java similarity index 93% rename from src/domains/Money.java rename to src/domains/user/Money.java index 9bc148a1..7f000033 100644 --- a/src/domains/Money.java +++ b/src/domains/user/Money.java @@ -1,4 +1,4 @@ -package domains; +package domains.user; public class Money { private static final String WON = "원"; diff --git a/src/domains/user/Tickets.java b/src/domains/user/Tickets.java new file mode 100644 index 00000000..a5ad4c96 --- /dev/null +++ b/src/domains/user/Tickets.java @@ -0,0 +1,20 @@ +package domains.user; + +import java.util.ArrayList; +import java.util.List; + +public class Tickets { + private List> totalLottos = new ArrayList<>(); + + public Tickets() { + this.totalLottos = new ArrayList<>(); + } + + public void takeIt(List lottoNumbers) { + this.totalLottos.add(lottoNumbers); + } + + public List> getTotalLottos() { + return totalLottos; + } +} diff --git a/src/domains/user/UserLotto.java b/src/domains/user/UserLotto.java new file mode 100644 index 00000000..b8275eec --- /dev/null +++ b/src/domains/user/UserLotto.java @@ -0,0 +1,29 @@ +package domains.user; + +import java.util.List; + +import domains.winning.BonusWinningNumbers; +import domains.winning.Ranking; +import view.PurchasedLotto; + +public class UserLotto { + private final LottoMachine lottoMachine; + private final Lottos lottos; + + public UserLotto(LottoMachine lottoMachine, Lottos lottos) { + this.lottoMachine = lottoMachine; + this.lottos = lottos; + } + + public Tickets getTickets(PurchasedLotto purchasedLotto) { + Tickets tickets = lottoMachine.getTicket(purchasedLotto.getNumberOfTicket()); + lottos.of(tickets); + return tickets; + } + + public Ranking getRankingFromWinningNumbers(List inputValueOfWinningNumbers, int bonusNumber) { + BonusWinningNumbers winningNumbers = new BonusWinningNumbers(inputValueOfWinningNumbers, bonusNumber); + Ranking ranking = lottos.getNumberOfWinningAboveThree(winningNumbers); + return ranking; + } +} diff --git a/src/domains/winning/BonusWinningNumbers.java b/src/domains/winning/BonusWinningNumbers.java new file mode 100644 index 00000000..e8f47cd9 --- /dev/null +++ b/src/domains/winning/BonusWinningNumbers.java @@ -0,0 +1,16 @@ +package domains.winning; + +import java.util.List; + +public class BonusWinningNumbers extends WinningNumbers{ + private final int bonus; + + public BonusWinningNumbers(List winningNumbers, int bonus) { + super(winningNumbers); + this.bonus = bonus; + } + + public int getBonus() { + return bonus; + } +} diff --git a/src/domains/winning/Ranking.java b/src/domains/winning/Ranking.java new file mode 100644 index 00000000..3562d0a1 --- /dev/null +++ b/src/domains/winning/Ranking.java @@ -0,0 +1,154 @@ +package domains.winning; + +import static view.Output.*; + +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.Map; + +import domains.user.Money; + +public class Ranking { + public static final int TO_PERCENT = 100; + private Map ranks = new LinkedHashMap<>(); + + public Ranking() { + this.ranks = initializeRanks(); + } + + private enum Rank { + NONE(0, "0개", Money.of(0)), + FOURTH(3, "3개", Money.of(5000)), + THIRD(4,"4개", Money.of(50_000)), + SECOND(5,"5개", Money.of(1_500_000)), + BONUS_BALL(5, "5개 일치, 보너스 볼 일치", Money.of(30_000_000)), + FIRST(6,"6개", Money.of(2_000_000_000)); + + private int count; + private String textOfCount; + private Money money; + + Rank(int count, String textOfCount, Money money) { + this.count = count; + this.textOfCount = textOfCount; + this.money = money; + } + + private boolean isSame(int number) { + return count == number; + } + + private Rank from(int count) { + return Arrays.stream(values()) + .filter(ranks -> ranks.isSame(count)) + .findAny() + .orElse(Rank.NONE); + } + + public String getText() { + return textOfCount; + } + + public String money() { + return money.text(); + } + + public int amount() { + return this.money.amount(); + } + } + + private static Map initializeRanks() { + Map results = new LinkedHashMap<>(); + for (Rank rank : Rank.values()) { + results.put(rank, 0); + } + return results; + } + + public void record(boolean checkedBonus, int winningCount) { + if (winningCount != 4) { + includeRank(winningCount); + return; + } + toBonus(checkedBonus, winningCount); + } + + private void toBonus(boolean checkedBonus, int winningCount) { + if (checkedBonus) { + includeBonusRank(); + return; + } + includeRank(winningCount); + } + + private void includeRank(int winningCount) { + Rank rank = findRank(winningCount); + this.ranks.put(rank, this.ranks.getOrDefault(rank, 0)+1); + } + + public void includeBonusRank() { + this.ranks.put(Rank.BONUS_BALL, this.ranks.getOrDefault(Rank.BONUS_BALL, 0)+1); + } + + private Rank findRank(Integer count) { + for (Rank rank : Rank.values()) { + return rank.from(count); + } + return Rank.NONE; + } + + /* + 수익률(%) = 손익/투자원금 * 100 = (평가금액-투자원금)/투자원금 * 100 + */ + public double totalYields(int purchaseAmount) { + double totalEarningsByRank = 0; + for (Rank rank : this.ranks.keySet()) { + int amount = rank.amount(); + Integer numberOfMachedLotto = this.ranks.get(rank); + totalEarningsByRank += amount * numberOfMachedLotto; + } + double profit = (totalEarningsByRank - purchaseAmount) / purchaseAmount; + double rateOfReturn = profit * TO_PERCENT; + return rateOfReturn; + } + + public String getWinningStatistics() { + StringBuilder sb = new StringBuilder(); + sb.append(OUTPUT_WINNING_STATISTICS) + .append(System.lineSeparator()) + .append(OUTPUT_WINNING_STATISTICS_LINE) + .append(System.lineSeparator()); + rankLines(sb); + return sb.toString(); + } + + private void rankLines(StringBuilder sb) { + for (Ranking.Rank rank : ranks.keySet()) { + if (rank.NONE.isSame(rank.count)) { + continue; + } + sb.append(rank.getText()) + .append(OUTPUT_IDX_FIRST) + .append(rank.money()) + .append(OUTPUT_IDX_SECOND) + .append(ranks.get(rank)) + .append(OUTPUT_IDX_THIRD) + .append(System.lineSeparator()); + } + } + + public String getRateOfReturn(int purchaseAmount) { + double yields = totalYields(purchaseAmount); + StringBuilder sb = new StringBuilder(); + sb.append(OUTPUT_TOTAL_YIELD_MESSAGE); + DecimalFormat df = new DecimalFormat(PATTERN_ROUND_DOWN); + df.setRoundingMode(RoundingMode.DOWN); + String rateOfReturn = df.format(yields); + sb.append(rateOfReturn) + .append(OUTPUT_TOTAL_YIELD_MESSAGE_SUFFIX); + return sb.toString(); + } +} diff --git a/src/domains/winning/WinningNumbers.java b/src/domains/winning/WinningNumbers.java new file mode 100644 index 00000000..6b76429e --- /dev/null +++ b/src/domains/winning/WinningNumbers.java @@ -0,0 +1,15 @@ +package domains.winning; + +import java.util.List; + +public class WinningNumbers { + private final List winningNumbers; + + public WinningNumbers(List winningNumbers) { + this.winningNumbers = winningNumbers; + } + + public List getNumbers() { + return this.winningNumbers; + } +} diff --git a/src/views/Input.java b/src/view/Input.java similarity index 84% rename from src/views/Input.java rename to src/view/Input.java index c3a52e0d..0f34b483 100644 --- a/src/views/Input.java +++ b/src/view/Input.java @@ -1,11 +1,11 @@ -package views; +package view; import java.util.Arrays; import java.util.List; import java.util.Scanner; import static java.util.stream.Collectors.toList; -import static views.Output.println; +import static view.Output.println; public class Input { private static final Scanner scanner = new Scanner(System.in); @@ -17,6 +17,12 @@ public class Input { public Input() { } + public static int getBonusNumber(){ + println.accept("보너스 볼을 입력해주세요."); + int bonusNumber = nextInt(); + return bonusNumber; + } + public static List inputWinningNumbers(){ println.accept(OUTPUT_ASK_WINNING_NUMBER); String textNumbers = nextLine(); @@ -30,10 +36,10 @@ private static List toInteger(String textNumbers) { .collect(toList()); } - public static int[] purchaseLotto() { + public static PurchasedLotto purchaseLotto() { int purchaseAmount = getPurchaseAmount(); int numberOfTicket = getTicketAccount(purchaseAmount); - return new int[] {purchaseAmount, numberOfTicket}; + return new PurchasedLotto(purchaseAmount, numberOfTicket); } public static int getPurchaseAmount() { diff --git a/src/view/Output.java b/src/view/Output.java new file mode 100644 index 00000000..d6f491d7 --- /dev/null +++ b/src/view/Output.java @@ -0,0 +1,40 @@ +package view; + +import java.util.List; +import java.util.function.Consumer; + +import domains.user.Tickets; +import domains.winning.Ranking; + +public class Output { + public static final String PATTERN_ROUND_DOWN = "0.00"; + public static final String OUTPUT_IDX_FIRST = " 일치 ("; + public static final String OUTPUT_IDX_SECOND = ") - "; + public static final String OUTPUT_IDX_THIRD = "개"; + public static final String OUTPUT_WINNING_STATISTICS = "당첨 통계"; + public static final String OUTPUT_WINNING_STATISTICS_LINE = "--------------------"; + public static final String OUTPUT_TOTAL_YIELD_MESSAGE = "총 수익율은 "; + public static final String OUTPUT_TOTAL_YIELD_MESSAGE_SUFFIX = "%입니다"; + + public static Consumer print = (text) -> System.out.print(text); + public static Consumer println = (text) -> System.out.println(text); + public static Consumer prints = (obj) -> System.out.println(obj); + + public static void showLottos(Tickets tickets) { + List> purchasedLottos = tickets.getTotalLottos(); + for (List lotto : purchasedLottos) { + prints.accept(lotto); + } + } + public static void printResultOfLottoAndYield(String winningStatistics, String rateOfReturn) { + println.accept(winningStatistics); + println.accept(rateOfReturn); + } + + public static void getResultOfLotto(Ranking ranking, int purchaseAmount) { + String winningStatistics = ranking.getWinningStatistics(); + String rateOfReturn = ranking.getRateOfReturn(purchaseAmount); + printResultOfLottoAndYield(winningStatistics, rateOfReturn); + } + +} diff --git a/src/view/PurchasedLotto.java b/src/view/PurchasedLotto.java new file mode 100644 index 00000000..30dec928 --- /dev/null +++ b/src/view/PurchasedLotto.java @@ -0,0 +1,19 @@ +package view; + +public class PurchasedLotto { + private final int purchaseAmount; + private final int numberOfTicket; + + public PurchasedLotto(int purchaseAmount, int numberOfTicket) { + this.purchaseAmount = purchaseAmount; + this.numberOfTicket = numberOfTicket; + } + + public int getPurchaseAmount() { + return purchaseAmount; + } + + public int getNumberOfTicket() { + return numberOfTicket; + } +} diff --git a/src/views/Output.java b/src/views/Output.java deleted file mode 100644 index fcbaf975..00000000 --- a/src/views/Output.java +++ /dev/null @@ -1,70 +0,0 @@ -package views; - -import java.math.RoundingMode; -import java.text.DecimalFormat; -import java.util.List; -import java.util.Map; -import java.util.function.Consumer; - -import domains.Ranking; - -public class Output { - private static final String PATTERN_ROUND_DOWN = "0.00"; - private static final String OUTPUT_IDX_FIRST = " 일치 ("; - private static final String OUTPUT_IDX_SECOND = ") - "; - private static final String OUTPUT_IDX_THIRD = "개"; - private static final String OUTPUT_WINNING_STATISTICS = "당첨 통계"; - private static final String OUTPUT_WINNING_STATISTICS_LINE = "--------------------"; - private static final String OUTPUT_TOTAL_YIELD_MESSAGE = "총 수익율은 "; - private static final String OUTPUT_TOTAL_YIELD_MESSAGE_SUFFIX = "%입니다"; - - public static Consumer print = (text) -> System.out.print(text); - public static Consumer println = (text) -> System.out.println(text); - public static Consumer prints = (obj) -> System.out.println(obj); - - public static void showLottos(List> purchasedLottos) { - for (List lotto : purchasedLottos) { - prints.accept(lotto); - } - } - - public static void printResultOfLottoAndYield(Map ranks, double yields) { - String winningStatistics = getWinningStatistics(ranks); - String rateOfReturn = getRateOfReturn(yields); - println.accept(winningStatistics); - println.accept(rateOfReturn); - } - - private static String getWinningStatistics(Map ranks) { - StringBuilder sb = new StringBuilder(); - sb.append(OUTPUT_WINNING_STATISTICS) - .append(System.lineSeparator()) - .append(OUTPUT_WINNING_STATISTICS_LINE) - .append(System.lineSeparator()); - rankLines(ranks, sb); - return sb.toString(); - } - - private static void rankLines(Map ranks, StringBuilder sb) { - for (Ranking.Rank rank : ranks.keySet()) { - sb.append(rank.getText()) - .append(OUTPUT_IDX_FIRST) - .append(rank.money()) - .append(OUTPUT_IDX_SECOND) - .append(ranks.get(rank)) - .append(OUTPUT_IDX_THIRD) - .append(System.lineSeparator()); - } - } - - private static String getRateOfReturn(double yields) { - StringBuilder sb = new StringBuilder(); - sb.append(OUTPUT_TOTAL_YIELD_MESSAGE); - DecimalFormat df = new DecimalFormat(PATTERN_ROUND_DOWN); - df.setRoundingMode(RoundingMode.DOWN); - String rateOfReturn = df.format(yields); - sb.append(rateOfReturn) - .append(OUTPUT_TOTAL_YIELD_MESSAGE_SUFFIX); - return sb.toString(); - } -}