diff --git a/README.md b/README.md index 8a4f22ed0..001b68043 100644 --- a/README.md +++ b/README.md @@ -1 +1,22 @@ # java-planetlotto-precourse + +# 개요 +행성 로또 추첨기 + +# 요구사항 +### 로또 번호 +1. 로또는 1개당 5개의 중복되지 않는 숫자를 가집니다. + + +### 사용자(입력) +1. 로또 구입 금액을 입력받습니다. + 1. 로또 1장의 가격은 500원입니다. + 2. 500원 단위가 아니라면 예외입니다. + 3. 양의 정수여야합니다 +2. 사용자에게 당첨 번호(5개)와 보너스 번호 1개를 입력받습니다. + 1. 사용자는 당첨 번호와 보너스 번호를 모두 중복되지않게 입력합니다 + 2. 범위는 1~30까지의 수를 입력합니다. + 3. + +### 출력 +1. 당첨 통계를 보여줍니다. diff --git a/src/main/java/planetlotto/Application.java b/src/main/java/planetlotto/Application.java index 27d0a8f96..1be3184a6 100644 --- a/src/main/java/planetlotto/Application.java +++ b/src/main/java/planetlotto/Application.java @@ -1,7 +1,11 @@ package planetlotto; +import planetlotto.domain.LottoMachine; +import planetlotto.domain.LottoResult; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + + new LottoGame(new LottoMachine(), new LottoResult()).start(); } } diff --git a/src/main/java/planetlotto/LottoGame.java b/src/main/java/planetlotto/LottoGame.java new file mode 100644 index 000000000..9eb3d2014 --- /dev/null +++ b/src/main/java/planetlotto/LottoGame.java @@ -0,0 +1,81 @@ +package planetlotto; + + +import planetlotto.domain.LottoMachine; +import planetlotto.domain.LottoNum; +import planetlotto.domain.LottoResult; +import planetlotto.domain.WinningLotto; +import planetlotto.view.InputView; +import planetlotto.view.OutputView; + +import java.util.function.Predicate; + + +public class LottoGame { + + private final LottoMachine lottoMachine; + private final LottoResult lottoResult; + + private static int ONE_LOTTO_PRICE = 500; + + public LottoGame(final LottoMachine lottoMachine, + final LottoResult lottoResult){ + this.lottoMachine = lottoMachine; + this.lottoResult = lottoResult; + } + + public void start(){ + // todo: 500범위에 맞게 벨리데이션 추가 + // todo 타입이 int라서 해당 자료형의 범위에 맞게 값 벨리데이션 + int amount; + int repeat; + + while(true){ + try{ + amount = InputView.askAmount(); + break; + } catch(IllegalArgumentException e){ + System.out.println("[ERROR]: "+ e.getMessage()); + } + } + + repeat = division(amount); + + + // todo: 분리하기 + lottoMachine.makeLotto(repeat); + + LottoNum lottoNum = new LottoNum(lottoMachine.getLottoList()); + + OutputView.printPurchasedLottos(lottoMachine.getLottoList()); + + WinningLotto winningLotto; + while(true) { + try{ + winningLotto = new WinningLotto(InputView.askWinningLotto().stream().sorted().toList()); + break; + } catch(IllegalArgumentException e){ + System.out.println("[ERROR]: "+ e.getMessage()); + } + } + // todo: 범위, 갯수 벨리데이션 + + int bonus; + while(true) { + try{ + bonus = InputView.askBonusNumber(); + break; + } catch(IllegalArgumentException e){ + System.out.println("[ERROR]: "+ e.getMessage()); + } + } + + lottoResult.matchResult(lottoNum ,winningLotto, bonus); + + OutputView.printResult(lottoResult.getResult()); + } + + private int division(int amount){ + return amount/ONE_LOTTO_PRICE; + } +} diff --git a/src/main/java/planetlotto/domain/LottoMachine.java b/src/main/java/planetlotto/domain/LottoMachine.java new file mode 100644 index 000000000..6e55f32fe --- /dev/null +++ b/src/main/java/planetlotto/domain/LottoMachine.java @@ -0,0 +1,25 @@ +package planetlotto.domain; + +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class LottoMachine { + + public List> lottoList = new ArrayList<>(); + + public void makeLotto(int purchaseLottoSize){ + for(int i=0; i> getLottoList(){ + return lottoList; + } +} diff --git a/src/main/java/planetlotto/domain/LottoNum.java b/src/main/java/planetlotto/domain/LottoNum.java new file mode 100644 index 000000000..53ecd0dbd --- /dev/null +++ b/src/main/java/planetlotto/domain/LottoNum.java @@ -0,0 +1,16 @@ +package planetlotto.domain; + +import java.util.List; + +public class LottoNum { + + private final List> lottoList; + + public LottoNum(List> lottoNum){ + this.lottoList = lottoNum; + } + + public List> getLottoList(){ + return lottoList; + } +} diff --git a/src/main/java/planetlotto/domain/LottoResult.java b/src/main/java/planetlotto/domain/LottoResult.java new file mode 100644 index 000000000..8eb073955 --- /dev/null +++ b/src/main/java/planetlotto/domain/LottoResult.java @@ -0,0 +1,65 @@ +package planetlotto.domain; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Predicate; + +public class LottoResult { + + public Map result = new HashMap<>(); + + + // todo: 헤당 로직에서 숫자가 거꾸로 순위가 들어가는중 + public void matchResult(final LottoNum lottoNum, final WinningLotto winningLotto, int bonus){ + + List> lottoNumList = lottoNum.getLottoList(); + + List winningLottoNum = winningLotto.getWinningLottoNumList(); + + List resultLotto; + + for (List lotto : lottoNumList) { + resultLotto = winningLottoNum.stream() + .filter( + l -> lotto + .stream() + .anyMatch(Predicate.isEqual(l))) + .toList(); + + boolean isBonus = matchBonus(bonus, lotto); + int key = resultLotto.size(); + + key = rank(key, isBonus); + + result.put(key, result.getOrDefault(key, 0)+1); + } + } + + private boolean matchBonus(int bonus, List lottoList) { + return lottoList.stream().anyMatch(lottoNum -> bonus == lottoNum); + } + + private int rank(int key, boolean isBonus){ + switch(key){ + case 2: key = 5; break; + case 3: key = 4; break; + case 4: key = 2; break; + case 5: key = 1; break; + } + + return isBonus(isBonus, key); + } + + private int isBonus(boolean isBonus, int key){ + if(key == 2 && !isBonus) return 3; + else if(key == 4 && !isBonus) return 0; + else if(key == 5 && !isBonus) return 0; + return key; + } + + public Map getResult(){ + return result; + } + +} diff --git a/src/main/java/planetlotto/domain/WinningLotto.java b/src/main/java/planetlotto/domain/WinningLotto.java new file mode 100644 index 000000000..f3f877c81 --- /dev/null +++ b/src/main/java/planetlotto/domain/WinningLotto.java @@ -0,0 +1,16 @@ +package planetlotto.domain; + +import java.util.List; + +public class WinningLotto { + + private final List winningLottoNumList; + + public WinningLotto(List winningNumList){ + winningLottoNumList = winningNumList; + } + + public List getWinningLottoNumList(){ + return winningLottoNumList; + } +}