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

[로또 미션] 신예린 미션 제출합니다. #19

Open
wants to merge 32 commits into
base: nyeroni
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
958541e
feat: 로또 구매할 금액 입력
nyeroni May 1, 2024
8627586
feat: 구매한 로또 출력
nyeroni May 1, 2024
c086150
feat: 로또 랜덤 번호로 생성
nyeroni May 1, 2024
d435b8e
feat: 로또 생성
nyeroni May 1, 2024
07b7bc7
feat: 총 구매한 로또를 list로 생성
nyeroni May 1, 2024
5ed6fea
feat: 구매할 가격 입력 후 해당 가격 만큼 로또 생성 및 출력
nyeroni May 1, 2024
2fd7c31
feat: 실행
nyeroni May 1, 2024
ebbd2b3
feat: 랜덤 숫자 생성 변경
nyeroni May 1, 2024
2a199f1
feat: 정렬 코드 삭제
nyeroni May 1, 2024
2d59e3d
feat: 로또 랜덤 생성 중복 방지 추가
nyeroni May 2, 2024
6b2269c
feat: 당첨 번호 입력 및 당첨 개수, 수익률 계산 및 출력
nyeroni May 2, 2024
7052bdd
feat: 메서드 명 변경 및 로또 생성 오류 해결
nyeroni May 3, 2024
4375301
Refactor: 불필요한 코드 정리
nyeroni May 4, 2024
e2860da
feat: 요구사항 정리
nyeroni May 6, 2024
49773c8
feat: 출력 문구 enum으로 설정
nyeroni May 6, 2024
9c28320
feat: 출력 문구 수정 및 로또 출력 방식 변경
nyeroni May 6, 2024
2fe5105
feat: 변수명 변경 및 보너스 번호 매치 메서드 생성
nyeroni May 6, 2024
4595b55
feat: 수동 로또 생성
nyeroni May 6, 2024
561a3b3
feat: 수동 로또 생성 및 로또 목록 출력 방식 변경
nyeroni May 6, 2024
0e387fb
feat: enum타입 수정
nyeroni May 6, 2024
cfe53f4
feat: 투입 금액 및 로또 구매 개수 예외 처리
nyeroni May 6, 2024
ef9bdab
feat: 수동 개수 예외 처리 및 설정
nyeroni May 6, 2024
3d69130
feat: 보너스넘버 예외 처리 및 설정
nyeroni May 6, 2024
f1804d5
feat: 로또 예외 처리
nyeroni May 6, 2024
652c085
feat: 예외 처리 및 수정
nyeroni May 6, 2024
7beec8d
feat: 결과 출력 수정
nyeroni May 6, 2024
48b8f00
feat: 예외 문구 수정
nyeroni May 6, 2024
04d0f96
feat: 예외 문구 수정
nyeroni May 6, 2024
2fd040c
feat: 생성자 추가
nyeroni May 6, 2024
4f9977c
feat: 생성자 추가
nyeroni May 6, 2024
2f51b48
test: 테스트코드 작성
nyeroni May 6, 2024
36e8947
test: 테스트코드 작성
nyeroni May 6, 2024
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
10 changes: 10 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import controller.LottoController;

import java.io.IOException;

public class Application {

public static void main(String [] args) throws IOException {
LottoController lottoController = new LottoController();
}
}
86 changes: 86 additions & 0 deletions src/main/java/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package controller;

import model.AutoLotto;
import model.Lotto;
import model.LottoList;
import view.InputView;
import view.OutputView;

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

public class LottoController {
private InputView inputView = new InputView();
private OutputView outputView = new OutputView();
private LottoList lottoList;

public LottoController(){
lottoStart();
}
public void lottoStart() {
int price = inputMoney();
viewLotto(price);
Lotto winningLotto = winnerNumberToInteger();
int[] match = match(lottoList, winningLotto);
double rate = calculateRate(match, price);
outputView.resultPrint(match, rate);
}

public int inputMoney(){
return toIntegerPrice(inputView.inputPrice());
}
public int toIntegerPrice(String price){
return Integer.parseInt(price);
}
public void viewLotto(int price){
int count = price/1000;
outputView.countPrint(count);
lottoList = makeLottolist(count);
}
public LottoList makeLottolist(int count){
for(int i=0; i<count; i++){
lottoList = new LottoList(makeLotto());
}
return lottoList;
}
public Lotto makeLotto(){
List<Integer> lotto = new ArrayList<>();
AutoLotto autoLotto = new AutoLotto();
lotto = autoLotto.getAutoLotto();
outputView.lottoPrint(lotto);
return new Lotto(lotto);
}

public Lotto winnerNumberToInteger(){
String winnerNumsStr = inputView.inputWinnerNumber();
Lotto winningLotto = new Lotto();
return winningLotto.toIntegerWinner(winnerNumsStr);
}


public int [] match(LottoList lottoList, Lotto winningLotto){
int [] arr = new int[4];
for(Lotto lotto : lottoList.getLottoList()) {
int cnt = lotto.winningCalculate(winningLotto);
if(cnt == 3){
arr[0] ++;
}
else if (cnt == 4){
arr[1] ++;
}
else if(cnt == 5){
arr[2] ++;
}
else if(cnt == 6){
arr[3] ++;
}
}
return arr;
}
public double calculateRate(int arr[], int price){
int total = arr[0] * 5000 + arr[1] * 50000 + arr[2] * 1500000 + arr[3] * 2000000000;
Copy link

Choose a reason for hiding this comment

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

정의해두신 enum을 활용할 수 있지 않을까요?

double rate = (double) total / (double) price;
return rate;
}

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

import java.util.*;
import java.util.stream.IntStream;

public class AutoLotto {
private static final int MIN_LOTTO_NUMBER = 1;
private static final int MAX_LOTTO_NUMBER = 45;
private static final int CNT_LOTTO_NUMBER = 6;

private static List<Integer> lottoNums = new ArrayList<>();
public AutoLotto(){
createAutoLotto();
}
public void createAutoLotto(){

List<Integer> numbers = new ArrayList<>();
for (int i = MIN_LOTTO_NUMBER; i <= MAX_LOTTO_NUMBER; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
lottoNums = new ArrayList<>();
for (int i = 0; i < CNT_LOTTO_NUMBER; i++) {
int uniqueNumber = numbers.get(i);
while (lottoNums.contains(uniqueNumber)) {
Copy link

Choose a reason for hiding this comment

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

궁금한점이 있습니다. numbers에는 이미 유니크한 숫자들이 shuffle되어있는 것 아닌가요? 이미 중복되지 않는 것은 보장된 것 같은데 한번 더 중복을 체크하시는 이유가 궁금합니다.

// 중복된 번호라면 다시 랜덤하게 선택
Collections.shuffle(numbers);
uniqueNumber = numbers.get(i);
}
lottoNums.add(uniqueNumber);
}
Collections.sort(lottoNums);
}
public List<Integer> getAutoLotto(){
return lottoNums;
}

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

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

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

public Lotto(List<Integer> numbers) {
this.numbers = numbers;
}
public Lotto(){

}
public Lotto toIntegerWinner(String winningNumbersStr) {
String[] str = winningNumbersStr.split(",\\s*");
List<Integer> winningLottoList = new ArrayList<>();
for (String s : str) {
winningLottoList.add(Integer.parseInt(s));
}
Lotto winningLotto = new Lotto(winningLottoList);
return winningLotto;
}
public List<Integer>getNumbers(){
return numbers;
}

public int winningCalculate(Lotto winningLotto) {
int cnt = 0;
List<Integer> winningLottoList = winningLotto.getNumbers();
for (Integer integer : numbers) {
if(numbers.contains(integer)){
cnt ++;
}
}
return cnt;
}
}
22 changes: 22 additions & 0 deletions src/main/java/model/LottoList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package model;

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

public class LottoList {
List<Lotto> lottoList = new ArrayList<>();
private int totalPrice;

public LottoList(Lotto lotto){
lottoList.add(lotto);
}
public List<Lotto> getLottoList(){
return lottoList;
}
public void setTotalPrice(int reward){
this.totalPrice+=reward;
}
public int getTotalPrice(){
return totalPrice;
}
}
23 changes: 23 additions & 0 deletions src/main/java/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package view;

import java.util.Scanner;

public class InputView {

private final static String INPUT_PRICE = "구입금액을 입력해 주세요.";
private final static String INPUT_WINNER_NUMBER = "지난 주 당첨 번호를 입력해 주세요.";

private static final Scanner scanner = new Scanner(System.in);


public String inputPrice(){
System.out.println(INPUT_PRICE);
return scanner.nextLine();
}

public String inputWinnerNumber(){
System.out.println();
System.out.println(INPUT_WINNER_NUMBER);
return scanner.nextLine();
}
}
33 changes: 33 additions & 0 deletions src/main/java/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package view;

import java.util.List;

public class OutputView {

private final static String BUY_MESSAGE = "개를 구매했습니다.";
private final static String THREE_MATCHES = "3개 일치 (5000원)- ";
private final static String FOUR_MATCHES = "4개 일치 (50000원)- ";
private final static String FIVE_MATCHES = "5개 일치 (1500000원)- ";
private final static String SIX_MATCHES = "6개 일치 (2000000000원)- ";
private final static String COUNT = "개";

public void countPrint (int count){
System.out.println("\n" + count + BUY_MESSAGE);

}
public void lottoPrint(List<Integer> numbers) {
System.out.println(numbers);
}

public void resultPrint(int [] arr, double rate){
System.out.println();
System.out.println("당첨 통계");
System.out.println("---------");
System.out.println(THREE_MATCHES + arr[0] + COUNT);
System.out.println(FOUR_MATCHES + arr[1] + COUNT);
System.out.println(FIVE_MATCHES + arr[2] + COUNT);
System.out.println(SIX_MATCHES + arr[3] + COUNT);

System.out.println("총 수익률은 " + rate + " 입니다.(기준이 1이기 때문에 결과적으로 손해라는 의미임)");
}
}