-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Sy step2 #3
Changes from all commits
348c451
6364a5c
317c849
771d457
d46daab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -37,6 +40,7 @@ public static ArrayList<ArrayList<Integer>> makeLotto(int lottoCount) { | |
|
||
System.out.println(lotto.get(i).toString()); | ||
} | ||
System.out.println(); | ||
return lotto; | ||
} | ||
|
||
|
@@ -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) { | ||
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); | ||
|
||
|
@@ -65,8 +83,8 @@ public static ArrayList<Integer> countResults(ArrayList<Integer> winList, ArrayL | |
|
||
public static int countMatchedNumbers(ArrayList<Integer> winList, ArrayList<Integer> lotto) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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){ | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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); | ||
|
||
|
||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2등 로또 당첨은, 그냥
(전체 로또를 순회하면서)
(숫자가 겹치는지 확인)
(만약에 보너스 숫자가 일치하면 flag를 true로 변경)
(겹치는 숫자의 수와 flag 확인)
이 더 자연스러울 것 같아요!