Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
51 changes: 51 additions & 0 deletions src/main/java/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import domain.LottoMachine;
import domain.Profit;
import domain.WinningStatus;
import domain.lastweekwinninglotto.LastWeekWinningBonusBall;
import domain.lastweekwinninglotto.LastWeekWinningLotto;
import domain.lottoticket.LottoTicket;
import domain.lottoticket.Lottos;
import domain.lottoticket.NumberOfLottoTicket;
import domain.strategy.RandomLottoNumberStrategy;
import ui.Printer;
import ui.Receiver;

import java.util.ArrayList;
import java.util.List;

public class LottoApplication {
private final Printer printer = new Printer();
private final Receiver receiver = new Receiver();
private static LottoMachine lottoMachine = new LottoMachine();
private final RandomLottoNumberStrategy randomLottoNumberStrategy = new RandomLottoNumberStrategy();

public void run() {
printer.requestPurchaseAmount();
NumberOfLottoTicket numberOfLottoTicket = new NumberOfLottoTicket(receiver.receiveLottoTotalAmount());
Lottos lottos = makeLottos(numberOfLottoTicket);
printer.printAllLotto(lottos);
printer.requestLastWeekLottoWinningNumber();
List<Integer> LastWeekLottoWinningNumbers = receiver.receiveLastWeekLottoWinningNumbers();
LastWeekWinningLotto lastWeekWinningLotto = new LastWeekWinningLotto(LastWeekLottoWinningNumbers);
printer.requestLottoBonusBallNumber();
LastWeekWinningBonusBall lastWeekWinningBonusBall = new LastWeekWinningBonusBall(receiver.receiveLottoBonusBallNumber());
ArrayList<WinningStatus> getLottoPrices = lottoMachine.Discriminator(lottos, lastWeekWinningLotto, lastWeekWinningBonusBall, numberOfLottoTicket);
Profit profit = new Profit(getLottoPrices);
printer.printLottoProfit(profit.getCalculatedProfit(numberOfLottoTicket));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
List<Integer> LastWeekLottoWinningNumbers = receiver.receiveLastWeekLottoWinningNumbers();
LastWeekWinningLotto lastWeekWinningLotto = new LastWeekWinningLotto(LastWeekLottoWinningNumbers);
printer.requestLottoBonusBallNumber();
LastWeekWinningBonusBall lastWeekWinningBonusBall = new LastWeekWinningBonusBall(receiver.receiveLottoBonusBallNumber());
ArrayList<WinningStatus> getLottoPrices = lottoMachine.Discriminator(lottos, lastWeekWinningLotto, lastWeekWinningBonusBall, numberOfLottoTicket);
Profit profit = new Profit(getLottoPrices);
printer.printLottoProfit(profit.getCalculatedProfit(numberOfLottoTicket));
List<Integer> LastWeekLottoWinningNumbers = receiver.receiveLastWeekLottoWinningNumbers();
LastWeekWinningLotto lastWeekWinningLotto = new LastWeekWinningLotto(LastWeekLottoWinningNumbers);
printer.requestLottoBonusBallNumber();
LastWeekWinningBonusBall lastWeekWinningBonusBall = new LastWeekWinningBonusBall(receiver.receiveLottoBonusBallNumber());
List<WinningStatus> getLottoPrices = lottoMachine.Discriminator(lottos, lastWeekWinningLotto, lastWeekWinningBonusBall, numberOfLottoTicket);
Profit profit = new Profit(getLottoPrices);
printer.printLottoProfit(profit.getCalculatedProfit(numberOfLottoTicket));

일전에 리뷰받은 내용입니다!
자바의 유명한 규칙중엔 program to interfaces not implementations 라는 부분이 있습니다.

객체 지향 기초 이야기 3, 유연함(Flexibility)

위 글 참고해보시면 좋을것 같아요

}
Copy link
Member

Choose a reason for hiding this comment

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

공백을 통해 좀 더 읽기 쉽게 배치해 보는 것은 어떨까요?


private Lottos makeLottos(NumberOfLottoTicket numberOfLottoTicket) {
ArrayList<LottoTicket> lottoDummy = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ArrayList<LottoTicket> lottoDummy = new ArrayList<>();
List<LottoTicket> lottoDummy = new ArrayList<>();

Interface!

for (int i = 0; i < numberOfLottoTicket.getNumberOfLottoTicket(); i++) {
LottoTicket lotto = new LottoTicket(randomLottoNumberStrategy.getRandomLottoNumbers());
lottoDummy.add(lotto);
}
Lottos lottos = new Lottos(lottoDummy);
return lottos;
}

public static void main(String[] args) {
LottoApplication app = new LottoApplication();
app.run();
}
}
59 changes: 59 additions & 0 deletions src/main/java/domain/EnumWinningStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package domain;

import domain.lottoticket.NumberOfLottoTicket;
import domain.lottowinningresult.LottoWinningBonusBallResult;
import domain.lottowinningresult.LottoWinningResult;
import ui.Printer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class EnumWinningStatus {
private static final int NOT_MATCH_LOTTO = 0;
private ArrayList<WinningStatus> lottoPrices = new ArrayList<>();
private Map<WinningStatus, Integer> mappingLottoWithBonusBall = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

HashMap을 통해서 관리하셨군요!
좋은 방법인 것 같아요.

private Printer printer = new Printer();

public ArrayList<WinningStatus> getLottoPrices(LottoFactory lottoFactory, NumberOfLottoTicket numberOfLottoTicket){
LottoWinningResult lottoWinningResults = lottoFactory.getLottoWinningResult();
LottoWinningBonusBallResult lottoWinningBonusBallResult = lottoFactory.getLottoWinningBonusBallResult();

for (int i = 0; i < numberOfLottoTicket.getNumberOfLottoTicket(); i++) {
int lottoMatchedNumber = lottoWinningResults.getLottoWinningResult().get(i);
Boolean lottoBonusBallNumber = lottoWinningBonusBallResult.getLottoWinningBonusBallResult().get(i);
getWinningLottoTicketPrices(lottoMatchedNumber, lottoBonusBallNumber);
}
printer.printAllMatchedLottoResult(getMappingLottoWithBonusBall());
return lottoPrices;
}

private void getWinningLottoTicketPrices(int lottoMatchedNumber, boolean lottoBonusBallNumber) {
for(WinningStatus winningStatus: WinningStatus.values()){
int matchedWinningNumberCount = winningStatus.getMatchCount();
boolean isMatchedBonusBallCount = winningStatus.hasBonusBall();
makeWinningLottoTicket(lottoMatchedNumber, matchedWinningNumberCount, isMatchedBonusBallCount, lottoBonusBallNumber, winningStatus);
}
}

private void makeWinningLottoTicket(
int lottoMatchedNumber,
int matchedWinningNumberCount,
boolean isMatchedBonusBallCount,
boolean lottoBonusBallNumber,
WinningStatus winningStatus
){
if((lottoMatchedNumber == matchedWinningNumberCount) && (isMatchedBonusBallCount == lottoBonusBallNumber)){
lottoPrices.add(winningStatus);
}
}

private Map<WinningStatus, Integer> getMappingLottoWithBonusBall() {
for (WinningStatus key: lottoPrices
) {
mappingLottoWithBonusBall.put(key, mappingLottoWithBonusBall.getOrDefault(key, 0)+1);
}
return mappingLottoWithBonusBall;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private Map<WinningStatus, Integer> getMappingLottoWithBonusBall() {
for (WinningStatus key: lottoPrices
) {
mappingLottoWithBonusBall.put(key, mappingLottoWithBonusBall.getOrDefault(key, 0)+1);
}
return mappingLottoWithBonusBall;
}
private Map<WinningStatus, Integer> getMappingLottoWithBonusBall() {
for (WinningStatus key: lottoPrices) {
mappingLottoWithBonusBall.put(key, mappingLottoWithBonusBall.getOrDefault(key, 0)+1);
}
return mappingLottoWithBonusBall;
}

컨벤션 오타가 있네요


}
23 changes: 23 additions & 0 deletions src/main/java/domain/LottoFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package domain;

import domain.lottowinningresult.LottoWinningBonusBallResult;
import domain.lottowinningresult.LottoWinningResult;

public class LottoFactory {
private LottoWinningResult lottoWinningResult;
private LottoWinningBonusBallResult lottoWinningBonusBallResult;

public LottoFactory(LottoWinningResult lottoWinningResults, LottoWinningBonusBallResult lottoWinningBonusBallResults){
this.lottoWinningResult = lottoWinningResults;
this.lottoWinningBonusBallResult = lottoWinningBonusBallResults;
}

public LottoWinningResult getLottoWinningResult(){
return this.lottoWinningResult;
}

public LottoWinningBonusBallResult getLottoWinningBonusBallResult(){
return this.lottoWinningBonusBallResult;
}

}
68 changes: 68 additions & 0 deletions src/main/java/domain/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package domain;

import domain.lastweekwinninglotto.LastWeekWinningBonusBall;
import domain.lastweekwinninglotto.LastWeekWinningLotto;
import domain.lottoticket.LottoTicket;
import domain.lottoticket.Lottos;
import domain.lottoticket.NumberOfLottoTicket;
import domain.lottowinningresult.LottoWinningBonusBallResult;
import domain.lottowinningresult.LottoWinningResult;

import java.util.ArrayList;
import java.util.List;

public class LottoMachine {
EnumWinningStatus enumWinningStatus = new EnumWinningStatus();
ArrayList<Integer> lottoWinningResults = new ArrayList<>();
ArrayList<Boolean> lottoWinningBonusBallResults = new ArrayList<>();

public ArrayList<WinningStatus> Discriminator(
Comment on lines +16 to +19
Copy link
Member

Choose a reason for hiding this comment

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

여기도 숨어있군요

Lottos lottos,
LastWeekWinningLotto lastWeekWinningLotto,
LastWeekWinningBonusBall lastWeekWinningBonusBall,
NumberOfLottoTicket numberOfLottoTicket
){
Comment on lines +20 to +24
Copy link
Member

Choose a reason for hiding this comment

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

Discriminator라는 함수가 받는 인자가 너무 많은 것 같아요.
numberOfLottoTicket의 경우 lottos에 포함되어 있을 수 있지 않을까요?

for (LottoTicket lotto : lottos.getLottos())
{
List<Boolean> lottoMatchedResult = getMatchLottoNumber(lotto, lastWeekWinningLotto);
Copy link
Member

Choose a reason for hiding this comment

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

로또 매칭 결과값을 List로 따로 관리하셨군요!

사실 저도 이번 미션 진행하면서 결과 값을 관리하는 것에 대해 고민을 많이 했는데 결국 추가적으로 관리하는 것은 이번 미션 목적에 비해 오버 엔지니어링이라는 생각에 결과값 리스트를 따로 관리하지 않았습니다.

이렇게 결과값 리스트 따로 관리하면 이후에 추가적인 로직으로 판단하는 데에 있어서 장점이 있을까요?

Boolean lottoMatchedBonusBallResult = getMatchLottoBonusBallNumber(lotto, lastWeekWinningBonusBall);
int matchLottoNumberCount = getMatchLottoNumberCount(lottoMatchedResult);
lottoWinningResults.add(matchLottoNumberCount);
lottoWinningBonusBallResults.add(lottoMatchedBonusBallResult);
}
LottoFactory lottoFactory = createLottoFactory(lottoWinningResults, lottoWinningBonusBallResults);
return enumWinningStatus.getLottoPrices(lottoFactory, numberOfLottoTicket);
}

private LottoFactory createLottoFactory(ArrayList<Integer> lottoWinningResults, ArrayList<Boolean> lottoWinningBonusBallResults) {
LottoWinningBonusBallResult lottoWinningBonusBallResult = new LottoWinningBonusBallResult(lottoWinningBonusBallResults);
LottoWinningResult winningResult = new LottoWinningResult(lottoWinningResults);

LottoFactory lottoFactory = new LottoFactory(winningResult, lottoWinningBonusBallResult);
return lottoFactory;
}

private int getMatchLottoNumberCount(List<Boolean> lottoMatchedResult){
return (int) lottoMatchedResult.stream().filter(index-> (index)).count();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return (int) lottoMatchedResult.stream().filter(index-> (index)).count();
return (int)lottoMatchedResult.stream().filter(index-> (index)).count();

}

private List<Boolean> getMatchLottoNumber(LottoTicket lotto, LastWeekWinningLotto lastWeekWinningLotto){
List<Integer> lottoList = lotto.getLotto();
List<Integer> lastWeekWinningLottos = lastWeekWinningLotto.getLotto();
ArrayList<Boolean> lottoMatchedResult = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

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

Interface!


for (int winningLottoNumber = 0; winningLottoNumber < lastWeekWinningLottos.size(); winningLottoNumber++) {
Boolean isMatchLottoNumber = lottoList.contains(lastWeekWinningLottos.get(winningLottoNumber));
lottoMatchedResult.add(isMatchLottoNumber);
}
return lottoMatchedResult;
}

private Boolean getMatchLottoBonusBallNumber(LottoTicket lotto, LastWeekWinningBonusBall lastWeekWinningBonusBall) {
List<Integer> lottoBonusBalls = lotto.getLotto();
int lastWeekWinningBonusBalls = lastWeekWinningBonusBall.getLastWeekWinningBonusBall();
Boolean isMatchLottoNumber = lottoBonusBalls.contains(lastWeekWinningBonusBalls);
return isMatchLottoNumber;

}
}
21 changes: 21 additions & 0 deletions src/main/java/domain/Profit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package domain;

import domain.lottoticket.NumberOfLottoTicket;

import java.util.List;

public class Profit {
private static int totalLottoPrice;
public Profit(List<WinningStatus> lottoPrices) {
int sumLottoPrices = sumLottoPrices(lottoPrices);
totalLottoPrice = sumLottoPrices;
}

private int sumLottoPrices(List<WinningStatus> lottoPrices){
return lottoPrices.stream().mapToInt(WinningStatus::getWinningMoney).sum();
}

public float getCalculatedProfit(NumberOfLottoTicket numberOfLottoTicket) {
return (float) totalLottoPrice / numberOfLottoTicket.getMoney();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return (float) totalLottoPrice / numberOfLottoTicket.getMoney();
return (float)totalLottoPrice / numberOfLottoTicket.getMoney();

컨벤션에 의하면 캐스팅괄호 뒤에 공백이 없어야 합니다!
8.6 좋은 예 참조

}
}
31 changes: 31 additions & 0 deletions src/main/java/domain/WinningStatus.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package domain;

public enum WinningStatus {
SIX_MATCH(6, false, 2000000000),
FIVE_MATCH_WITH_BONUS_BALL(5, true, 30000000),
FIVE_MATCH(5, false, 1500000),
FOUR_MATCH(4, false, 50000),
THREE_MATCH(3, false, 5000);

private final int matchCount;
private final int winningMoney;
private final boolean BonusBall;

WinningStatus(int matchCount, boolean BonusBall, int winningMoney){
this.matchCount = matchCount;
this.winningMoney = winningMoney;
this.BonusBall = BonusBall;
}

public int getMatchCount() {
return this.matchCount;
}

public boolean hasBonusBall() {
return this.BonusBall;
}

public int getWinningMoney() {
return this.winningMoney;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package domain.exception;

public class NotValidLottoLengthException extends RuntimeException {
private static final String MESSAGE = "번호는 6개를 입력해야 합니다.";

public NotValidLottoLengthException() {
super(MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package domain.lastweekwinninglotto;

public class LastWeekWinningBonusBall {

private final int bonusBall;
public LastWeekWinningBonusBall(int bonusBall){
this.bonusBall = bonusBall;
}

public int getLastWeekWinningBonusBall(){
return this.bonusBall;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package domain.lastweekwinninglotto;

import domain.lottoticket.Lotto;

import java.util.List;

public class LastWeekWinningLotto implements Lotto {
private final List<Integer> winningLotto;

public LastWeekWinningLotto(List<Integer> winningLotto){
this.winningLotto = winningLotto;
}

@Override
public List<Integer> getLotto(){
return this.winningLotto;
}
}
7 changes: 7 additions & 0 deletions src/main/java/domain/lottoticket/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package domain.lottoticket;

import java.util.List;

public interface Lotto {
public List<Integer> getLotto();
}
25 changes: 25 additions & 0 deletions src/main/java/domain/lottoticket/LottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package domain.lottoticket;

import domain.exception.*;
import java.util.List;

public class LottoTicket implements Lotto{
private static final int LOTTO_NUMBER_COUNT = 6;
private final List<Integer> numbers;

public LottoTicket(List<Integer> numbers ) {
checkLottoLength(numbers);
this.numbers = numbers;
}
Copy link
Member

Choose a reason for hiding this comment

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

불필요한 공백이 있네요


private void checkLottoLength(List<Integer> numbers){
if(numbers.size() !=LOTTO_NUMBER_COUNT){
throw new NotValidLottoLengthException();
}
}
@Override
public List<Integer> getLotto(){
return this.numbers;
}

}
15 changes: 15 additions & 0 deletions src/main/java/domain/lottoticket/Lottos.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain.lottoticket;

import java.util.List;

public class Lottos {
private final List<LottoTicket> lotto;

public Lottos(List<LottoTicket> lotto ) {
this.lotto = lotto;
}

public List<LottoTicket> getLottos() {
return this.lotto;
}
}
25 changes: 25 additions & 0 deletions src/main/java/domain/lottoticket/NumberOfLottoTicket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package domain.lottoticket;

public class NumberOfLottoTicket {
private final static int LOTTO_PRICE = 1000;
private final int tickets;
private final int money;

public NumberOfLottoTicket(int money) {
this.tickets = setNumberOfLottoTickets(money);
this.money = money;
}

// naming 바꾸기
private int setNumberOfLottoTickets(int money) {
return money / LOTTO_PRICE;
}
Comment on lines +13 to +16
Copy link
Member

Choose a reason for hiding this comment

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

이번 미션에서는 setter의 사용을 제한하지 않았지만
setter를 사용하여 tickets를 저장하고 그 이후에 getter로 받아 사용하는 것이 그렇지 않을 때에 비해 어떤 장단점이 있을까요?


public int getNumberOfLottoTicket() {
return this.tickets;
}

public int getMoney() {
return this.money;
}
}
Loading