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

로또 미션 제출합니다 :) #62

Open
wants to merge 6 commits into
base: jermany17
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
49 changes: 49 additions & 0 deletions src/main/java/controller/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package controller;

import java.util.*;
import view.Input;
import view.Output;

public class App {
public static void main(String[] args) {

Choose a reason for hiding this comment

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

요 메인 함수가 전체적으로 긴 느낌이 드는데요 이 부분을 적절한 기준에 따라서 함수를 쪼개보면 어떨까요?


// 모든 로또들을 넣을 리스트
List<List<String>> LottoList = new ArrayList<>();

// 구입 금액 입력한다
int total = Input.moneyInput();

// 수동으로 구매할 로또 수를 입력한다
int manual = Input.manualNumInput();

// 자동으로 구매할 로또 수를 계산한다.
int auto = total - manual;

// 수동 로또 번호를 입력한다
for(int i = 0; i < manual; i++) {
List<String> manualLotto = Input.manualLottoInput(i);
LottoList.add(manualLotto);
}

// 수동과 자동 구매 개수를 출력한다
Output.ManualAndAutoNumOutput(manual, auto);

Choose a reason for hiding this comment

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

자바 컨벤션상 메소드는 소문자로 해주시면 좋을 것 같아요!


// 자동 로또 번호를 생성 및 출력한다
for (int i = 0; i < auto; i++) {
List<String> autoLotto = Input.AutoLottoInput();
Collections.sort(autoLotto);
LottoList.add(autoLotto);
Output.LottoNumOutput(autoLotto);
}
Comment on lines +32 to +37

Choose a reason for hiding this comment

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

요 정렬까지 컨트롤러가 하게 되면 약간 컨트롤러의 책임이 많아지는 것 같은데 어떻게 생각하시나요?
컨트롤러의 책임이 많아진다는 것은 결국 수많은 코드들이 다 컨트롤러로 오고, 아무도 건드릴 수 없는 거대한 코드 덩어리가 된다는 의미이기도 합니다


// 지난 주 당첨 번호를 입력한다

Choose a reason for hiding this comment

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

주석 달아주시는 습관 아주 좋습니다

List<String> LastWeekLotto = Input.LastWeekLottoNum();

// 보너스 볼을 입력한다
String BonusBall = Input.BonusBall();

// 당첨 통계를 출력한다
Output.ResultOfLotto(LottoList, LastWeekLotto, BonusBall, total*1000);
Input.closeScanner();
}
}
12 changes: 12 additions & 0 deletions src/main/java/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package domain;

import java.util.List;

public class Lotto {

private List<String> lotto;

public Lotto(List<String> lottoNum){
this.lotto = lottoNum;
}
}
26 changes: 26 additions & 0 deletions src/main/java/domain/LottoGame.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package domain;

import java.util.List;

public class LottoGame {

public static int matchNum(List<String> Lotto, List<String> LastWeekLotto){
int matchNum = 0;
for (String n : Lotto) {
if (LastWeekLotto.contains(n)) {
matchNum++;
}
}
return matchNum;
Comment on lines +8 to +14

Choose a reason for hiding this comment

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

indent 1규칙을 지키려고 시도해보셔도 좋을 것 같아요!

}

public static int matchBonus(List<String> Lotto, String BonusBall){
for (String n : Lotto) {
if (n.equals(BonusBall)) {

Choose a reason for hiding this comment

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

요기에 *.equals 는 항상 null 이 들어올 수 있는지 정말 많이 주의해줘야 합니다
다른 언어와는 다르게 요 상황에서 npe 가 정말 많이 발생하거든요

return 1;
}
}
return 0;
}
}

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

import java.util.*;

public class Input {

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

public static int moneyInput(){
System.out.println("구입금액을 입력해 주세요.");
int money = scanner.nextInt();
System.out.println();
return money/1000;

Choose a reason for hiding this comment

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

요 부분은 비즈니스 로직이 아닐까요? money /1000 부분이요!
view 와 비즈니스 로직은 최대한 분리해보셔도 좋을 것 같아요

}

public static int manualNumInput(){
System.out.println("수동으로 구매할 로또 수를 입력해 주세요.");
int manual = scanner.nextInt();
System.out.println();
scanner.nextLine();
return manual;
}

public static List<String> manualLottoInput(int n) {
if (n == 0)
System.out.println("수동으로 구매할 번호를 입력해 주세요.");
List<String> manualLotto = Arrays.asList(scanner.nextLine().split(","));
for(int i = 0; i < manualLotto.size(); i++){
manualLotto.set(i, manualLotto.get(i).trim());
}
return manualLotto;
}

public static List<String> AutoLottoInput() {
List<String> lottoRange = new ArrayList<>();
for (int i = 1; i <= 45; i++) {
lottoRange.add(String.valueOf(i));

Choose a reason for hiding this comment

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

로또 숫자가 String 이 된 이유가 있을까요? 로또 숫자는 숫자다보니 정수 타입이 적절할 것 같아서요
만약 추가적인 비즈니스 룰이 있다면 값객체로 만들어봐도 좋을 것 같아요
https://velog.io/@cjh8746/%EC%97%94%ED%8B%B0%ED%8B%B0entity%EC%99%80-%EA%B0%92%EA%B0%9D%EC%B2%B4value-object%EC%97%90-%EB%8C%80%ED%95%B4%EC%84%9C

}
Collections.shuffle(lottoRange);
return lottoRange.subList(0, 6);
}

public static List<String> LastWeekLottoNum(){
System.out.println("\n지난 주 당첨 번호를 입력해 주세요.");
List<String> LastWeekLotto = Arrays.asList(scanner.nextLine().split(","));
for(int i = 0; i < LastWeekLotto.size(); i++){
LastWeekLotto.set(i, LastWeekLotto.get(i).trim());
}
return LastWeekLotto;
}

public static String BonusBall(){
System.out.println("\n보너스 볼을 입력해 주세요.");
return scanner.nextLine();
}

public static void closeScanner() {
if (scanner != null) {
scanner.close();
Comment on lines +57 to +59

Choose a reason for hiding this comment

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

여기서 쓰라는 것은 아니지만, Autocloseable 도 한번 보시면 좋을 것 같아요!
만약 적용한다면 어떻게 될지도 봐도 재밌겠네요

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

import java.util.*;
import domain.LottoGame;

public class Output {

public static void ManualAndAutoNumOutput(int manual, int auto){
System.out.println();
System.out.println("수동으로 " + manual + "장, 자동으로 " + auto + "개를 구매했습니다.");
}

public static void LottoNumOutput(List<String> Lotto){
System.out.print("[");
for(int i = 0; i < Lotto.size(); i++){
System.out.print(Lotto.get(i));
if(i != Lotto.size() - 1){
System.out.print(", ");
}
}
System.out.println("]");
}

public static void ResultOfLotto(List<List<String>> LottoList, List<String> LastWeekLotto, String BonusBall, int moneyInput){
int result3 = 0, result4 = 0, result5 = 0, result6 = 0, result5Bonus = 0;
int matchNum = 0, matchBonus = 0;
int resultMoney = 0;
double rate = 0;

for (List<String> Lotto : LottoList) {
matchNum = LottoGame.matchNum(Lotto, LastWeekLotto);
matchBonus = LottoGame.matchBonus(Lotto, BonusBall);
if (matchNum == 3) {
result3++;
resultMoney += 5000;
}
if (matchNum == 4) {
result4++;
resultMoney += 50000;
}
if (matchNum == 5 && matchBonus == 0) {
result5++;
resultMoney += 1500000;
}
if (matchNum == 5 && matchBonus == 1) {
result5Bonus++;
resultMoney += 30000000;
}
if (matchNum == 6) {
result6++;
resultMoney += 2000000000;
}
}
Comment on lines +24 to +53

Choose a reason for hiding this comment

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

요 부분은 비즈니스가 아닐까요?


System.out.println("\n당첨 통계");
System.out.println("---------");
System.out.println("3개 일치 (5000원)- " + result3 + "개");
System.out.println("4개 일치 (50000원)- " + result4 + "개");
System.out.println("5개 일치 (1500000원)- " + result5 + "개");
System.out.println("5개 일치, 보너스 볼 일치(30000000)원 - " + result5Bonus + "개");
System.out.println("6개 일치 (2000000000원)-" + result6 + "개");
rate = (double) resultMoney / moneyInput;
System.out.println("총 수익률은 " + String.format("%.2f", rate) + "입니다.");
}
}
54 changes: 54 additions & 0 deletions src/test/java/LottoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import domain.LottoGame;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;

public class LottoTest {

@Test
@DisplayName("로또번호_6개_일치여부를_확인한다")
public void 로또번호_6개_일치여부를_확인한다() {
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "6");
List<String> lastWeekLotto = Arrays.asList("6", "5", "4", "3", "2", "1");
int result = LottoGame.matchNum(lotto, lastWeekLotto);
assertEquals(6, result);
}

@Test
@DisplayName("로또번호_3개_일치여부를_확인한다")
public void 로또번호_3개_일치여부를_확인한다() {
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "6");
List<String> lastWeekLotto = Arrays.asList("10", "9", "8", "3", "2", "1");
int result = LottoGame.matchNum(lotto, lastWeekLotto);
assertEquals(3, result);
}

@Test
@DisplayName("로또번호_0개_일치여부를_확인한다")
public void 로또번호_0개_일치여부를_확인한다() {
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "6");
List<String> lastWeekLotto = Arrays.asList("7", "8", "9", "10", "11", "12");
int result = LottoGame.matchNum(lotto, lastWeekLotto);
assertEquals(0, result);
}

@Test
@DisplayName("보너스번호_일치여부를_확인한다")
public void 보너스번호_일치여부를_확인한다() {
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "7");
String bonusBall = "7";
int result = LottoGame.matchBonus(lotto, bonusBall);
assertEquals(1, result); // 보너스볼이 일치함
}

@Test
@DisplayName("보너스번호_불일치여부를_확인한다")
public void 보너스번호_불일치여부를_확인한다() {
List<String> lotto = Arrays.asList("1", "2", "3", "4", "5", "7");
String bonusBall = "8";
int result = LottoGame.matchBonus(lotto, bonusBall);
assertEquals(0, result); // 보너스볼이 일치하지 않음
}
}
Comment on lines +10 to +54

Choose a reason for hiding this comment

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

테스트 너무 잘 해주셨어요