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

Sy step2 #3

Open
wants to merge 5 commits into
base: master
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
10 changes: 9 additions & 1 deletion src/main/java/com/poolc/javapractice/lotto/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ public int getBuyAmount() {
public ArrayList<Integer> getWinNumbers() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
String win = sc.nextLine();
return Step1.winNumbers(win);
return Lotto.winNumbers(win);
}

public int getBonusNumber(){
System.out.println("보너스 볼을 입력해 주세요.");
int bonusNumber = sc.nextInt();
sc.nextLine();
System.out.println();
return bonusNumber;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@
import java.text.DecimalFormat;
import java.util.*;

public class Step1 {
public class Lotto {
public static void main(String[] args) {
Input input = new Input();
Output output = new Output();

int buyAmount = input.getBuyAmount();
int lottoCount = buyAmount / 1000;
output.printLottoCount(lottoCount);

ArrayList<ArrayList<Integer>> lotto = makeLotto(lottoCount);
ArrayList<Integer> winList = input.getWinNumbers();
ArrayList<Integer> lottoResults = result(countResults(winList, lotto, lottoCount));
int bonusNumber = input.getBonusNumber();
ArrayList<Integer> countedList = countResults(winList, lotto, lottoCount);
ArrayList<Integer> lottoResults = result(countedList);

Output output = new Output();
output.printLottoCount(lottoCount);
output.printWinStatistics(lottoResults);
output.printEarningRate(earningRate(lottoResults, buyAmount));
}
int count2ndLotto = find2ndLotto(lotto, countedList, lottoResults, bonusNumber);

output.printWinStatistics(lottoResults, count2ndLotto);
output.printEarningRate(earningRate(lottoResults, buyAmount, count2ndLotto));
}

public static ArrayList<ArrayList<Integer>> makeLotto(int lottoCount) {
ArrayList<ArrayList<Integer>> lotto = new ArrayList<>(lottoCount);
Expand All @@ -37,6 +40,7 @@ public static ArrayList<ArrayList<Integer>> makeLotto(int lottoCount) {

System.out.println(lotto.get(i).toString());
}
System.out.println();
return lotto;
}

Expand All @@ -52,6 +56,20 @@ public static ArrayList<Integer> winNumbers(String winNumbers) {
return winArrayList;
}

public static int find2ndLotto(ArrayList<ArrayList<Integer>> lotto, ArrayList<Integer> countedList, ArrayList<Integer> lottoResults, int bonusNumber) {
Copy link
Member

Choose a reason for hiding this comment

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

2등 로또 당첨은, 그냥
(전체 로또를 순회하면서)
(숫자가 겹치는지 확인)
(만약에 보너스 숫자가 일치하면 flag를 true로 변경)
(겹치는 숫자의 수와 flag 확인)
이 더 자연스러울 것 같아요!

int indexFiveMatchedLotto = countedList.indexOf(5);
int count2ndLotto = 0;

while(indexFiveMatchedLotto != -1){
BonusMatch bonusMatch = BonusMatch.isMatched(lotto.get(indexFiveMatchedLotto), bonusNumber);
count2ndLotto += bonusMatch.run(lottoResults);
ArrayList<Integer> tempList = new ArrayList<>(countedList.subList(indexFiveMatchedLotto + 1, countedList.size()));
indexFiveMatchedLotto = tempList.indexOf(5);
}

return count2ndLotto;
}

public static ArrayList<Integer> countResults(ArrayList<Integer> winList, ArrayList<ArrayList<Integer>> lotto, int lottoCount) {
ArrayList<Integer> countedList = new ArrayList<>(lottoCount);

Expand All @@ -65,8 +83,8 @@ public static ArrayList<Integer> countResults(ArrayList<Integer> winList, ArrayL

public static int countMatchedNumbers(ArrayList<Integer> winList, ArrayList<Integer> lotto) {
Copy link
Member

Choose a reason for hiding this comment

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

그냥 long으로 처리해도 되지 않을까요?

return (int) lotto.stream()
.filter(winList::contains)
.count();
.filter(winList::contains)
.count();
}

public static ArrayList<Integer> result(ArrayList<Integer> countedList){
Expand All @@ -79,12 +97,47 @@ public static ArrayList<Integer> result(ArrayList<Integer> countedList){
return lottoResults;
}

public static double earningRate(ArrayList<Integer> lottoResults, int buyAmount) {
public static double earningRate(ArrayList<Integer> lottoResults, int buyAmount, int count2ndLotto) {

double totalMoney = 5000 * lottoResults.get(0) + 50000 * lottoResults.get(1)
+ 1500000 * lottoResults.get(2) + 2000000000 * lottoResults.get(3);
+ 1500000 * lottoResults.get(2) + 30000000 * count2ndLotto+ 2000000000 * lottoResults.get(3);
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.DOWN);
return Double.parseDouble(df.format(((totalMoney - buyAmount) / buyAmount * 100)));
}

enum BonusMatch {
Copy link
Member

Choose a reason for hiding this comment

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

파일이 너무 길어지니, Enum은 분리하는게 좋을 것 같아요.

NOTMATCH(false){
@Override
int run(ArrayList<Integer> lottoResults) {
return 0;
}
},
MATCH(true){
@Override
int run(ArrayList<Integer> lottoResults) {
Copy link
Member

Choose a reason for hiding this comment

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

Enum 안에 기능을 넣는 것 보단, Enum을 받아서 처리하는 외부 메서드를 만들어서 관리하는게 더 좋을 것 같아요.
Enum은 쉽게 꺼내쓸 수 있는 이름표 정도로 생각하는게 의미론적 관점에서 더 좋습니다.

lottoResults.set(2, lottoResults.get(2) - 1);
return 1;
}
};

private boolean matchedBonus;

BonusMatch(boolean matchedBonus){
this.matchedBonus = matchedBonus;
}

public static BonusMatch isMatched(ArrayList<Integer> lotto, int bonusNumber) {
if(lotto.contains(bonusNumber)){
return BonusMatch.MATCH;
}
return BonusMatch.NOTMATCH;
}

abstract int run(ArrayList<Integer> countedList);



}

}
13 changes: 7 additions & 6 deletions src/main/java/com/poolc/javapractice/lotto/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ public void printLottoCount(int lottoCount) {
System.out.println(lottoCount + "개를 구매했습니다.");
}

public void printWinStatistics(ArrayList<Integer> resultList) {
public void printWinStatistics(ArrayList<Integer> lottoResults, int count2ndLotto) {
System.out.println("당첨 통계");
System.out.println("---------");

System.out.println("3개 일치 (5000원) - " + resultList.get(0) + "개");
System.out.println("4개 일치 (50000원) - " + resultList.get(1) + "개");
System.out.println("5개 일치 (1500000원) - " + resultList.get(2) + "개");
System.out.println("6개 일치 (2000000000원) - " + resultList.get(3) + "개");
System.out.println("3개 일치 (5000원) - " + lottoResults.get(0) + "개");
System.out.println("4개 일치 (50000원) - " + lottoResults.get(1) + "개");
System.out.println("5개 일치 (1500000원) - " + lottoResults.get(2) + "개");
System.out.println("5개 일치, 보너스 볼 일치(30000000원) - " + count2ndLotto +"개");
System.out.println("6개 일치 (2000000000원) - " + lottoResults.get(3) + "개");
}

public void printEarningRate(double earningRate) {
System.out.printf("총 수익률은 %f%%입니다.", earningRate);
System.out.printf("총 수익률은 %.2f%%입니다.", earningRate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Array;
import java.util.*;
import java.text.DecimalFormat;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

public class Step1Test {
public class LottoTest {

@Test
public void makeLottoTest(){
Step1 step1 = new Step1();
Lotto step1 = new Lotto();
ArrayList<ArrayList<Integer>> lotto = step1.makeLotto(100);
for (int i = 0; i < 10; i++){
for (int j = 0; j < lotto.get(i).size(); j++){
Expand All @@ -24,7 +22,7 @@ public void makeLottoTest(){

@Test
public void winNumbersTest(){
Step1 step1 = new Step1();
Lotto step1 = new Lotto();
ArrayList<Integer> actualList = step1.winNumbers("1, 2,3, 4, 5,6");
ArrayList<Integer> expectedList = new ArrayList<>();
for (int i = 1; i < 7; i++){
Expand All @@ -35,7 +33,7 @@ public void winNumbersTest(){

@Test
public void countResultsTest(){
Step1 step1 = new Step1();
Lotto step1 = new Lotto();
ArrayList<Integer> winListTest = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
ArrayList<ArrayList<Integer>> lottoTest = new ArrayList<>(2);
lottoTest.add(new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7, 8)));
Expand All @@ -50,7 +48,7 @@ public void countResultsTest(){

@Test
public void resultTest(){
Step1 step1 = new Step1();
Lotto step1 = new Lotto();
ArrayList<Integer> lottoResultsTest = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 3, 4, 4));
ArrayList<Integer> actual = step1.result(lottoResultsTest);

Expand All @@ -65,10 +63,10 @@ public void resultTest(){

@Test
public void earningRateTest(){
Step1 step1 = new Step1();
Lotto step1 = new Lotto();
ArrayList<Integer> lottoResultsTest = new ArrayList<>();
lottoResultsTest.add(3);
double actual = step1.earningRate(lottoResultsTest,14000);
double actual = step1.earningRate(lottoResultsTest, 2,14000);

double expected = -64.28;

Expand Down