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

[로또 미션] 안정현 미션 제출합니다. #11

Open
wants to merge 5 commits into
base: anhye0n
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
38 changes: 38 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import domain.Input;
import domain.GenerateLotto;
import domain.WinningLotto;
import exception.Lotto;
import exception.Money;
import view.Print;

import java.util.ArrayList;

public class Main {
public static void main(String[] args) {
GenerateLotto lotto = new GenerateLotto();
WinningLotto winningLotto = new WinningLotto();
Print print = new Print();
Input input = new Input();

int purchaseAmount = input.setPurchaseAmount();

int manualCount = input.setManualCount();

if (manualCount > 0) {
lotto.getManualLotto(purchaseAmount, manualCount);
}

ArrayList<ArrayList<Integer>> totalLottos = lotto.getLotto(lotto.getRemainingMoney(purchaseAmount, manualCount));

print.printPurchasedLottoCount(manualCount, totalLottos);

ArrayList<Integer> winningNumbers = input.setWinningNumber();

int bonusNumber = input.setBonusNumber(winningNumbers);

winningLotto.totalCheckLotto(totalLottos, winningNumbers, bonusNumber);

print.printWinningCount(winningLotto.winningCount, winningLotto.calculateRate(purchaseAmount));

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

import exception.Lotto;
import exception.ManualNumber;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class GenerateLotto {
ArrayList<ArrayList<Integer>> totalLotto = new ArrayList<>();

Comment on lines +11 to +13

Choose a reason for hiding this comment

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

ArrayList<ArrayList> totalLotto 형태로 코드를 작성하셨습니다.
Lotto라는 객체를 하나 만들어서
ArrayList totalLotto 형태로 바꿔주신다면 코드를 읽는것도 수월하고 무엇보다 확장성 및 유지보수에
좋다라고 생각해요.

만약에 프로그램을 만들어논 상태에서 로또의 색깔을 추가해야 된다는 상황이 발생할 경우
Lotto객체를 만들지 않은 프로그램은 GenerateLotto에서 직접 함수를 추가해야되서
GenerateLotto의 역할이 너무 많아진다고 생각해요.
Lotto객체가 있으면 Lotto객체의 멤버 변수를 만들어서 그 부분만 추가할 수 있어 편리하다고 생각합니다.

private final int LOTTO_PRICE = 1000;

private ArrayList<Integer> getOne() {
GenerateRandom random = new GenerateRandom();

Set<Integer> set = new HashSet<>();

while (set.size() != 6) {
set.add(random.generateRandom());
}
ArrayList<Integer> lotto = new ArrayList<>(set);


Collections.sort(lotto);

return lotto;
}

Comment on lines +16 to +31

Choose a reason for hiding this comment

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

하나의 정수를 1~45번중 랜덤으로 가져와서 6개의 크기가 채워지면
lotto리스트에 넣는 함수로 보입니다.

로또의 번호가 중복이 될 수있어서 contains() 함수를 이용하여
로또 번호가 이미 존재하는지 안하는지를 판별하면 좋을거 같습니다

public ArrayList<ArrayList<Integer>> getLotto(int money) {
for (int i = 0; i < money; i += LOTTO_PRICE) {
totalLotto.add(this.getOne());
}

return totalLotto;
}

public ArrayList<Integer> getManualOne() {

Input input = new Input();

ArrayList<Integer> manualOne = input.setManualNumber();

Collections.sort(manualOne);

return new Lotto(manualOne).lotto();
}

public void getManualLotto(int purchaseAmount, int manualCount) {
// TODO
ManualNumber manualNumber = new ManualNumber(purchaseAmount, manualCount);

System.out.println("\n수동으로 구매할 번호를 입력해 주세요.");

for (int i = 0; i < manualNumber.count(); i++) {
totalLotto.add(this.getManualOne());
}
}

public int getRemainingMoney(int money, int manualCount) {
return money - (LOTTO_PRICE * manualCount);
}

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

import java.util.Random;

public class GenerateRandom {
public int generateRandom() {
Random random = new Random();

return random.nextInt(45) + 1;
}

}
Comment on lines +1 to +12

Choose a reason for hiding this comment

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

따로 랜덤 객체를 만든건 너무 좋은 습관 같습니다.
이렇게 따로 랜덤 객체를 만들어 놓으면
랜덤 값을 받는 로또 객체를 Test할 때 더욱 쉽게 Test할수 있다고 생각해요

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

import exception.BonusNumber;
import exception.Lotto;
import exception.Money;

import java.util.ArrayList;
import java.util.Scanner;

public record Input() {

Comment on lines +10 to +11

Choose a reason for hiding this comment

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

Input 객체를 record라고 선언하신 이유를 알고싶어요 :)

public int setPurchaseAmount() {
System.out.println("구입금액을 입력해주세요.");
Scanner scanner = new Scanner(System.in);

return new Money(scanner.nextInt()).money();
}

public ArrayList<Integer> setWinningNumber() {
System.out.println("\n지난 주 당첨 번호를 입력해 주세요.");
Scanner scanner = new Scanner(System.in);

String line = scanner.nextLine();

return new Lotto(separateNumber(line)).lotto();
}

public int setBonusNumber(ArrayList<Integer> winningNumbers) {
System.out.println("\n보너스 볼을 입력해 주세요");
Scanner scanner = new Scanner(System.in);
int bonusNumber = scanner.nextInt();

winningNumbers.add(bonusNumber);

return new BonusNumber(winningNumbers, bonusNumber).bonusNumber();
}

public int setManualCount() {
System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요.");
Scanner scanner = new Scanner(System.in);

return scanner.nextInt();

}

public ArrayList<Integer> setManualNumber() {
Scanner scanner = new Scanner(System.in);

String line = scanner.nextLine();

return separateNumber(line);
}

public ArrayList<Integer> separateNumber(String raw) {
String[] numberStrings = raw.split(", ");
ArrayList<Integer> numbers = new ArrayList<>();

for (String numberString : numberStrings) {
numbers.add(Integer.parseInt(numberString));
}

return numbers;
}


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

import java.util.ArrayList;
import java.util.Arrays;

public class WinningLotto {
public ArrayList<Integer> winningCount = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0));

private int checkLotto(ArrayList<Integer> lotto, ArrayList<Integer> winningNumbers) {
int count = 0;

for (int winningNumber : winningNumbers) {

if (lotto.contains(winningNumber)) {
count++;
}
}

return count;
}

private void addWinningCount(ArrayList<Integer> totalLotto, int count, int bonusNumber) {

if (count == 3) {
winningCount.set(0, winningCount.get(0) + 1);
return;
}
if (count == 4) {
winningCount.set(1, winningCount.get(1) + 1);
return;
}
if (count == 5) {
addBonusCount(totalLotto, bonusNumber);
return;
}
if (count == 6) {
winningCount.set(4, winningCount.get(4) + 1);
}
}

private void addBonusCount(ArrayList<Integer> lotto, int bonusNumber) {
if (lotto.contains(bonusNumber)) {
winningCount.set(3, winningCount.get(3) + 1);
return;
}

winningCount.set(2, winningCount.get(2) + 1);
}

public void totalCheckLotto(ArrayList<ArrayList<Integer>> totalLottes, ArrayList<Integer> winningNumbers, int bonusNumber) {
for (ArrayList<Integer> totalLotto : totalLottes) {
addWinningCount(totalLotto, checkLotto(totalLotto, winningNumbers), bonusNumber);
}
}

public double calculateRate(int money) {
return (double) (WinningNumbers.findByIndex(0) * winningCount.get(0) + WinningNumbers.findByIndex(1) * winningCount.get(1) + WinningNumbers.findByIndex(2) * winningCount.get(2) + WinningNumbers.findByIndex(3) * winningCount.get(3) + WinningNumbers.findByIndex(4) * winningCount.get(4)) / money;
}
Comment on lines +56 to +58

Choose a reason for hiding this comment

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

이 부분은 for 문을 사용해서 계산하는게 더 좋을거 같습니다.

}

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

public enum WinningNumbers {
FIFTH(5000),
FOURTH(50000),
THIRD(1500000),
SECOND(30000000),
FIRST(2000000000);

private final int amount;

WinningNumbers(int amount) {
this.amount = amount;
}

public int amount() {
return amount;
}

public static int findByIndex(int index) {
if (index >= 0 && index < values().length) {
return values()[index].amount();
}

throw new IllegalArgumentException();
}

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

import java.util.ArrayList;

public record BonusNumber(ArrayList<Integer> winningNumbers, int bonusNumber) {

public BonusNumber {
validateDuplication(winningNumbers);
}

private static void validateDuplication(ArrayList<Integer> winningNumbers) {
if (winningNumbers.stream().distinct().count() != 7) {
throw new IllegalArgumentException("숫자는 중복 불가능합니다.");
}
}
}
Comment on lines +5 to +16

Choose a reason for hiding this comment

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

record 를 이용하여 validate구문을 함께 사용해 예외 처리를 잘 진행 하신걸로 보여요 :)

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

import java.util.ArrayList;

public record Lotto(ArrayList<Integer> lotto) {

public Lotto {
validateLottoRange(lotto);
validateDuplication(lotto);
validateLottoLength(lotto);
}
Comment on lines +6 to +11

Choose a reason for hiding this comment

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

:) 더 깔끔해 지셨어요!!


private void validateLottoRange(ArrayList<Integer> lotto) {
if (lotto.stream().allMatch(number -> number < 1 || number > 45)) {
throw new IllegalArgumentException("1 ~ 45사이의 값만 입력할 수 있습니다.");
}
}

private static void validateDuplication(ArrayList<Integer> winningNumbers) {
if (winningNumbers.stream().distinct().count() != 6) {
throw new IllegalArgumentException("로또 번호 중복 입력은 불가능합니다.");
}
}

private void validateLottoLength(ArrayList<Integer> lotto) {
if (lotto.size() != 6) {
throw new IllegalArgumentException("길이가 맞지 않습니다.");
}
}

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

import java.util.ArrayList;

public record ManualNumber(int money, int count) {
private static final int LOTTO_PRICE = 1000;

public ManualNumber {
validateNegativeNumber(count);
checkRemainingMoney(money, count);
}

private void validateNegativeNumber(int number) {
if (number < 0) {
throw new IllegalArgumentException("음수는 입력될 수 없습니다.");
}
}
public void validateLottoRange(ArrayList<Integer> lotto) {
if (lotto.stream().allMatch(number -> number >= 1 && number <= 45)) {
throw new IllegalArgumentException("1 ~ 45사이의 값만 입력할 수 있습니다.");
}
}

public void checkRemainingMoney(int money, int manualCount) {
if (money < manualCount * LOTTO_PRICE) {
throw new IllegalArgumentException("구입 금액을 넘은 갯수 입니다.");
}
}
}
Comment on lines +7 to +29

Choose a reason for hiding this comment

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

validateLottoRange를 빼먹은거 같아요

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

public record Money(int money) {

public Money {
validateNegativeNumber(money);
}

private void validateNegativeNumber(int money) {
if (money < 0) {
throw new IllegalArgumentException("구입 금액이 음수일 수 없습니다.");
}
}

}
Loading