-
Notifications
You must be signed in to change notification settings - Fork 47
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
[로또 미션] - 오채현 미션 제출합니다. #3
base: che7371
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
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. 메소드 순서대로 아래에 구현돼있어서 읽기 편했어요:) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package controller; | ||
|
||
import model.AutoLotto; | ||
import model.Lotto; | ||
import model.LottoList; | ||
import model.LottoService; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LottoController { | ||
private InputView inputView = new InputView(); | ||
private OutputView outputView = new OutputView(); | ||
private AutoLotto autoLotto; | ||
private LottoList lottoList=new LottoList(); | ||
|
||
private LottoService lottoService=new LottoService(); | ||
|
||
public LottoController(){ | ||
lottoStart(); | ||
} | ||
public void lottoStart() { | ||
int price = inputMoney(); | ||
int manualPrice= inputManual(); | ||
viewLotto(price,manualPrice); | ||
List<Integer> winningNums=inputView.inputWinningNumbers(); | ||
lottoList.setTotalPrice(price);//넣어야함 | ||
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. 꼭 필요한 주석이 아닌 것 같아 지우셔도 될 것 같아요 |
||
int bonusBall=inputView.inputBonusBall(); | ||
lottoService.calculateWinningNumbers(winningNums,lottoList,bonusBall); | ||
} | ||
|
||
public int inputMoney(){ | ||
return inputView.inputPrice(); | ||
} | ||
public int inputManual(){ | ||
return inputView.inputManualNum(); | ||
} | ||
public void viewLotto(int price,int manualPrice){ | ||
int count = price/1000-manualPrice; | ||
lottoList = generateLottoList(count,manualPrice); | ||
outputView.countPrint(manualPrice,count); | ||
printLottoList(lottoList);//생성된 후 출력 | ||
} | ||
|
||
public void printLottoList(LottoList lottoList){ | ||
for(Lotto lotto:lottoList.getLottoList()) | ||
System.out.println(lotto.getNumbers()); | ||
} | ||
|
||
public Lotto makeLotto(){ | ||
List<Integer> lotto = new ArrayList<>(); | ||
AutoLotto autoLotto = new AutoLotto(); | ||
lotto = autoLotto.getAutoLotto(); | ||
return new Lotto(lotto); | ||
} | ||
public LottoList generateLottoList(int count,int manualPrice){ | ||
inputView.InputManualLottoStart(); | ||
for(int i=0;i<manualPrice;i++){ | ||
List<Integer> lottoNums=inputView.inputManualLottoNum(); | ||
Lotto manualLotto=new Lotto(lottoNums); | ||
lottoList.setLottoList(manualLotto); | ||
} | ||
for(int j=0; j<count; j++){ | ||
lottoList.setLottoList(makeLotto()); | ||
} | ||
return lottoList; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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)) { | ||
Collections.shuffle(numbers); | ||
uniqueNumber = numbers.get(i); | ||
} | ||
lottoNums.add(uniqueNumber); | ||
} | ||
Collections.sort(lottoNums); | ||
Comment on lines
+21
to
+30
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. 랜덤으로 숫자를 뽑으실때 이렇게 하시면 |
||
} | ||
public List<Integer> getAutoLotto(){ | ||
return lottoNums; | ||
} | ||
|
||
} |
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. Lotto의 numbers를 List로 구현하셨는데, 4단계 힌트에서 나와있는 것처럼 로또 숫자 하나를 표현하는 Integer를 포장한 LottoNumber 객체를 구현하시면 예외처리할때 도움이 될 것 같아요! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package model; | ||
|
||
import java.util.List; | ||
|
||
public class Lotto { | ||
private static final int BONUS_NUMBER_INDEX = 6; | ||
private final List<Integer> numbers; | ||
|
||
public Lotto(List<Integer> numbers) { | ||
this.numbers = numbers; | ||
} | ||
|
||
public List<Integer>getNumbers(){ | ||
return numbers; | ||
} | ||
} |
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. 딱 필요한 함수들과 변수들로 깔끔하게 잘 짜신 것 같아요! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package model; | ||
|
||
import view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class LottoList { | ||
List<Lotto> lottoList = new ArrayList<>(); | ||
private int totalPrice; | ||
|
||
public LottoList(){ | ||
|
||
} | ||
public void setLottoList(Lotto lotto){ | ||
lottoList.add(lotto); | ||
} | ||
|
||
public List<Lotto> getLottoList(){ | ||
return lottoList; | ||
} | ||
public void setTotalPrice(int reward){ | ||
this.totalPrice+=reward; | ||
} | ||
public int getTotalPrice(){ | ||
return totalPrice; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package model; | ||
|
||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.sql.SQLOutput; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class LottoService { | ||
|
||
|
||
private OutputView outputView = new OutputView(); | ||
private Prize prize=new Prize(); | ||
|
||
public void calculateWinningNumbers(List<Integer> winningNumbers,LottoList lottoList,int bonusBall){ | ||
Map<Integer,Integer> stats=new HashMap<>(); | ||
for(Lotto lotto:lottoList.getLottoList()){ | ||
int matches=countMatchNumbers(lotto.getNumbers(),winningNumbers); | ||
int bonusMatches=calculateBonusBallMatch(lotto,matches,bonusBall); | ||
putPrize(matches,bonusMatches,stats); | ||
} | ||
double revenue=calculateRevenue(stats,lottoList); | ||
outputView.printWinningResult(stats,revenue,prize); | ||
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. model인 LottoService에서 OutputView를 호출하는 것보다 |
||
} | ||
|
||
public void putPrize(int matches,int bonusMatches,Map<Integer,Integer> stats){ | ||
if(bonusMatches==1) | ||
matches=7; | ||
stats.put(matches,stats.getOrDefault(matches,0)+1); | ||
} | ||
public int calculateBonusBallMatch(Lotto lotto,int matches,int bonusBall){ | ||
return matches==Match.MATCH_5.getMatch() && lotto.getNumbers().contains(bonusBall) ? 1 : 0; | ||
} | ||
Comment on lines
+33
to
+35
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. 프로그래밍 요구사항에 삼항 연산자를 쓰지 말라고 적혀있더라고요. |
||
public int countMatchNumbers(List<Integer> lottoNums,List<Integer> winningNums){ | ||
int matches=0; | ||
for(Integer num:lottoNums){ | ||
if(winningNums.contains(num)) | ||
matches++; | ||
} | ||
return matches; | ||
} | ||
|
||
public double calculateRevenue(Map<Integer,Integer> stats,LottoList lottoList){ | ||
int totalPrice= lottoList.getTotalPrice(); | ||
int totalRevenue=stats.entrySet().stream().mapToInt(entry-> entry.getValue()*prize.getPrize(entry.getKey())).sum(); | ||
double revenue=(double) totalRevenue/totalPrice; | ||
return revenue; | ||
} | ||
} |
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을 활용하는데 어려움을 겪었는데, 필요한 부분에 깔끔하게 잘 쓰신 것 같아요:) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package model; | ||
|
||
public enum Match { | ||
MATCH_3(3, 0), // 3개 일치 | ||
MATCH_4(4, 0), // 4개 일치 | ||
MATCH_5(5, 0), // 5개 일치 | ||
MATCH_5_BONUS(5, 1), // 5개 일치 + 보너스 볼 일치 | ||
MATCH_6(6, 0); // 6개 일치 | ||
|
||
private final int match; // 로또 번호 일치 개수 | ||
private final int bonusMatch; // 보너스 볼 일치 여부 (0: 불일치, 1: 일치) | ||
|
||
Match(int match, int bonusMatch) { | ||
this.match = match; | ||
this.bonusMatch = bonusMatch; | ||
} | ||
|
||
public int getMatch() { | ||
return match; | ||
} | ||
|
||
public int getBonusMatch() { | ||
return 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. Map 사용한 Prize로 구현하신것도 괜찮게 잘 짜신 것 같아요. ENUM에서 내부 매소드를 이용해서 enum의 정보를 이용하는 방법도 있어요. 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 Match 클래스 짜놓고 활용 방법이 안 떠올라서 결국 Prize 클래스를 이용했는데 그런 방법이 있군요..! 감사합니다. 관련해서 더 찾아봐야겠어요. 아직 자바 문법 지식이 많이 부족하네요. 열심히 노력하겠습니다 ! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package model; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Prize { | ||
private final Map<Integer, Integer> prizeMap; | ||
|
||
public Prize() { | ||
prizeMap = initializePrizeMap(); | ||
} | ||
|
||
private Map<Integer, Integer> initializePrizeMap() { | ||
Map<Integer, Integer> map = new HashMap<>(); | ||
map.put(3, 5000); | ||
map.put(4, 50000); | ||
map.put(5, 1500000); | ||
map.put(6, 2000000000); | ||
map.put(7,30000000); | ||
return map; | ||
} | ||
|
||
|
||
public int getPrize(int matches) { | ||
return prizeMap.getOrDefault(matches, 0); | ||
} | ||
} |
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. view는 너무 이쁘게 잘 짜신 것 같아요:) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package view; | ||
|
||
import javax.swing.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Scanner; | ||
|
||
public class InputView { | ||
|
||
private final static String INPUT_WINNING_NUMBERS="지난 주 당첨 번호를 입력해 주세요."; | ||
private final static String INPUT_PRICE = "구입금액을 입력해 주세요."; | ||
private static final String INPUT_MANUAL_NUM="수동으로 구매할 로또 수를 입력해 주세요."; | ||
private static final String INPUT_MANUAL_LOTTO_NUM="수동으로 구매할 번호를 입력해 주세요."; | ||
private static final String INPUT_BONUS_BALL="보너스 볼을 입력해 주세요."; | ||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public int inputPrice(){ | ||
System.out.println(INPUT_PRICE); | ||
int price = scanner.nextInt(); | ||
scanner.nextLine(); | ||
return price; | ||
} | ||
|
||
public List<Integer> inputWinningNumbers(){ | ||
System.out.println(); | ||
System.out.println(INPUT_WINNING_NUMBERS); | ||
List<Integer> winningNumbers=new ArrayList<>(); | ||
String input= scanner.nextLine().trim(); | ||
String[] numbers=input.split(", "); | ||
for(String num:numbers) | ||
if(!num.isEmpty()) | ||
winningNumbers.add(Integer.parseInt(num)); | ||
return winningNumbers; | ||
} | ||
|
||
public int inputBonusBall(){ | ||
System.out.println(); | ||
System.out.println(INPUT_BONUS_BALL); | ||
int bonusBall= scanner.nextInt(); | ||
scanner.nextLine(); | ||
return bonusBall; | ||
} | ||
|
||
public int inputManualNum(){ | ||
System.out.println(); | ||
System.out.println(INPUT_MANUAL_NUM); | ||
int manualNum= scanner.nextInt(); | ||
scanner.nextLine(); | ||
return manualNum; | ||
} | ||
|
||
public void InputManualLottoStart(){ | ||
System.out.println(); | ||
System.out.println(INPUT_MANUAL_LOTTO_NUM); | ||
} | ||
public List<Integer> inputManualLottoNum(){ | ||
List<Integer> lottoNumbers=new ArrayList<>(); | ||
String input= scanner.nextLine().trim(); | ||
String[] numbers=input.split(", "); | ||
for(String num:numbers) | ||
if(!num.isEmpty()) | ||
lottoNumbers.add(Integer.parseInt(num)); | ||
return lottoNumbers; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package view; | ||
|
||
import model.LottoService; | ||
import model.Match; | ||
import model.Prize; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OutputView { | ||
|
||
private final static String PRINT_HEAD="당첨 통계"; | ||
private final static String PRINT_DIVIDER="---------"; | ||
private final static String BUY_MESSAGE = "수동으로 %d장, 자동으로 %d개를 구매했습니다.\n"; | ||
|
||
public void countPrint (int manualNum,int count){ | ||
System.out.printf("\n"+BUY_MESSAGE, manualNum, count); | ||
} | ||
|
||
public void printWinningResult(Map<Integer,Integer> stats, double revenue, Prize prize){ | ||
System.out.println(); | ||
System.out.println(PRINT_HEAD); | ||
System.out.println(PRINT_DIVIDER); | ||
printStatsResult(stats,prize); | ||
System.out.printf("총 수익률은 %.2f입니다. (기준이 1이기 때문에 결과적으로 %s라는 의미임)\n", revenue, revenue >= 1 ? "이득이" : "손해"); | ||
} | ||
|
||
public void printStatsResult(Map<Integer,Integer> stats,Prize prize){ | ||
|
||
for(int i=3;i<=6;i++){ | ||
int count=stats.getOrDefault(i,0); | ||
int realPrize=prize.getPrize(i); | ||
System.out.printf("%d개 일치 (%d원)- %d개\n", i, realPrize, count); | ||
if(i==5) | ||
print2ndResult(stats,prize); | ||
} | ||
} | ||
|
||
public void print2ndResult(Map<Integer,Integer> stats,Prize prize){ | ||
int count= stats.getOrDefault(7,0); | ||
int realPrize=prize.getPrize(7); | ||
System.out.printf("5개 일치, 보너스 볼 일치 (%d원)- %d개\n",realPrize,count); | ||
} | ||
|
||
} |
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.
전체적으로 확인해 봤을때 중점적으로 보라고 하셨던 4가지들이 대부분 잘 지켜진 것 같아요😀
그런데 입력에 대한 예외 처리가 되어 있지 않아 예외 처리가 필요해 보이는 것 같아요.
예외 처리 해야되는 것들의 예시
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.
헉 예외 처리를 안 하고 넘어갔네요. 죄송합니다. 반영해서 수정하겠습니다!