-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat: 4단계 로또 수동 #2816
feat: 4단계 로또 수동 #2816
Changes from all commits
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,8 +1,8 @@ | ||
import ui.AutoResultView; | ||
import ui.ManualResultView; | ||
import ui.common.ResultView; | ||
|
||
public class JavaLottoApplication { | ||
public static void main(String... args) { | ||
ResultView.lottoResult(new AutoResultView()); | ||
ResultView.lottoResult(new ManualResultView()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package domain.lottery; | ||
|
||
import java.util.Objects; | ||
|
||
public class LotteryNumber { | ||
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. 도메인들을 잘 추가해주셨어요! 추가로 테스트는 꼭 만들어주셔요 🙏 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 static final int MIN_NUMBER = 1; | ||
private static final int MAX_NUMBER = 45; | ||
private final int number; | ||
|
||
private LotteryNumber(int number) { | ||
this.number = number; | ||
|
||
if (number < MIN_NUMBER || number > MAX_NUMBER) { | ||
throw new IllegalArgumentException("유효하지 않은 번호입니다."); | ||
} | ||
} | ||
|
||
public static LotteryNumber of(int number) { | ||
return new LotteryNumber(number); | ||
} | ||
|
||
public int number() { | ||
return number; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
LotteryNumber that = (LotteryNumber) o; | ||
return number == that.number; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(number); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
package domain.lottery; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class LotteryTicket { | ||
private final List<Integer> ticketNumbers; | ||
private final List<LotteryNumber> ticketNumbers; | ||
|
||
public LotteryTicket(List<Integer> ticketNumbers) { | ||
private static final int COUNT_OF_LOTTERY_NUMBERS = 6; | ||
Comment on lines
+8
to
+10
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 LotteryTicket(List<LotteryNumber> ticketNumbers) { | ||
this.ticketNumbers = ticketNumbers; | ||
|
||
ticketNumbers = ticketNumbers.stream().distinct().collect(Collectors.toList()); | ||
|
||
if (ticketNumbers.size() < COUNT_OF_LOTTERY_NUMBERS) { | ||
throw new IllegalArgumentException("복권번호가 중복됩니다."); | ||
} | ||
} | ||
|
||
public List<Integer> getTicketNumbers() { | ||
public static LotteryTicket of(List<LotteryNumber> ticketNumbers) { | ||
return new LotteryTicket(Optional.ofNullable(ticketNumbers).get()); | ||
} | ||
|
||
public List<LotteryNumber> getTicketNumbers() { | ||
return ticketNumbers; | ||
} | ||
|
||
public void add(LotteryNumber lotteryNumber) { | ||
ticketNumbers.add(lotteryNumber); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package ui; | ||
|
||
import domain.lottery.*; | ||
import service.LotteryService; | ||
import ui.common.InputView; | ||
import ui.common.Result; | ||
import util.Calculator; | ||
import util.Converter; | ||
|
||
import static constant.LotteryRules.DEFAULT_LOTTERY_TICKET_PRICE; | ||
|
||
public class ManualResultView implements Result { | ||
private final String DELIMITER = ", "; | ||
private LotteryTickets lotteryTickets; | ||
private WinnerLotteryTicket winnerLotteryTicket; | ||
private RewardLotteryTickets rewardLotteryTickets; | ||
|
||
@Override | ||
public void printAll() { | ||
LotteryService lotteryService = new LotteryService(); | ||
|
||
printTicketNumbers(); | ||
printLotteryResult(lotteryService); | ||
} | ||
|
||
@Override | ||
public void printTicketNumbers() { | ||
System.out.println("구입금액을 입력해 주세요."); | ||
|
||
long amount = InputView.inputInteger(); | ||
int totalNumberOfLotteryTickets = (int) Calculator.divide(amount, DEFAULT_LOTTERY_TICKET_PRICE); | ||
|
||
System.out.println(); | ||
System.out.println("수동으로 구매할 로또 수를 입력해 주세요."); | ||
int numberOfManualLotteryTickets = InputView.inputInteger(); | ||
|
||
lotteryTickets = new LotteryTickets(totalNumberOfLotteryTickets - numberOfManualLotteryTickets); | ||
|
||
System.out.println(); | ||
System.out.println("수동으로 구매할 번호를 입력해 주세요."); | ||
for (int count = 1; count <= numberOfManualLotteryTickets; count++) { | ||
lotteryTickets.add(Converter.convertStringToLotteryTicket(InputView.inputString().split(DELIMITER))); | ||
} | ||
|
||
System.out.println(); | ||
System.out.println("수동으로 " + numberOfManualLotteryTickets + "장, 자동으로 " + (totalNumberOfLotteryTickets - numberOfManualLotteryTickets) + "개를 구매했습니다."); | ||
for (LotteryTicket lotteryTicket : lotteryTickets.getLotteryTickets()) { | ||
System.out.println(lotteryTicket.getTicketNumbers()); | ||
} | ||
|
||
System.out.println(); | ||
System.out.println("지난 주 당첨 번호를 입력해 주세요."); | ||
LotteryTicket winnerTicket = Converter.convertStringToLotteryTicket(InputView.inputString().split(DELIMITER)); | ||
|
||
System.out.println("보너스 볼을 입력해 주세요."); | ||
int bonusTicketNumber = InputView.inputInteger(); | ||
winnerLotteryTicket = new WinnerLotteryTicket(winnerTicket, LotteryNumber.of(bonusTicketNumber)); | ||
rewardLotteryTickets = new RewardLotteryTickets(lotteryTickets, winnerLotteryTicket); | ||
} | ||
|
||
@Override | ||
public void printLotteryResult(LotteryService lotteryService) { | ||
System.out.println(); | ||
System.out.println("당첨 통계"); | ||
System.out.println("---------"); | ||
|
||
System.out.println("3개 일치 (5000원)- " + lotteryService.countOfLotteryWinners(3, rewardLotteryTickets) + "개"); | ||
System.out.println("4개 일치 (50000원)- " + lotteryService.countOfLotteryWinners(4, rewardLotteryTickets) + "개"); | ||
System.out.println("5개 일치 (1500000원)- " + lotteryService.countOfLotteryWinners(5, rewardLotteryTickets) + "개"); | ||
System.out.println("5개 일치, 보너스 볼 일치(30000000원)- " + lotteryService.countOfLotteryWinners(7, rewardLotteryTickets) + "개"); | ||
System.out.println("6개 일치 (2000000000원)- " + lotteryService.countOfLotteryWinners(6, rewardLotteryTickets) + "개"); | ||
System.out.println("총 수익률은 " + Calculator.rateOfProfit((long) lotteryTickets.getLotteryTickets().size() * DEFAULT_LOTTERY_TICKET_PRICE, lotteryService.findTotalRewards(new RewardLotteryTickets(lotteryTickets, winnerLotteryTicket))) + "입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,18 @@ | ||
package util; | ||
|
||
import domain.lottery.LotteryNumber; | ||
import domain.lottery.LotteryTicket; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Converter { | ||
public static LotteryTicket convertStringToLotteryTicket(String[] numbers) { | ||
List<Integer> lotteryTicketNumbers = new ArrayList<>(); | ||
|
||
LotteryTicket lotteryTicket = LotteryTicket.of(new ArrayList<>()); | ||
for (String number : numbers) { | ||
lotteryTicketNumbers.add(Integer.parseInt(number)); | ||
lotteryTicket.add(LotteryNumber.of(Integer.parseInt(number))); | ||
} | ||
|
||
return new LotteryTicket(lotteryTicketNumbers); | ||
return lotteryTicket; | ||
} | ||
} |
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.
case문이 제거가 됐네요 훨씬 구조가 나아진거같습니다.