-
Notifications
You must be signed in to change notification settings - Fork 87
[로또 미션] 한상우 미션 제출합니다. #17
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
base: sangu1026
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,8 @@ | ||
import controller.LottoController; | ||
|
||
public class Application { | ||
public static void main(String[] args) { | ||
LottoController lottoController = new LottoController(); | ||
lottoController.start(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package controller; | ||
|
||
import domain.LottoNumber; | ||
import domain.Money; | ||
import domain.Lotto; | ||
import domain.Lottos; | ||
import service.CalculateProfit; | ||
import service.FindBingo; | ||
import service.RandomLottoGenerator; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class LottoController { | ||
|
||
private static Lottos userLottos = new Lottos(); | ||
private static int manualLottoNum; | ||
|
||
public void start() { | ||
Money userMoney = getUserMoney(); | ||
int ticket = userMoney.ticket(); | ||
|
||
addAllLottos(ticket); | ||
showAllLotto(ticket); | ||
|
||
Lotto lastWeekLotto = new Lotto(getLastWeekLotto()); | ||
int bonusNum = getBonusLottoNum(); | ||
|
||
makeBingo(userLottos, lastWeekLotto, bonusNum); | ||
|
||
showLottoResult(); | ||
showProfit(userMoney.getMoney()); | ||
} | ||
|
||
public static Money getUserMoney() { | ||
String userMoney = InputView.readMoney(); | ||
return new Money(userMoney); | ||
} | ||
|
||
public static void addAllLottos(int ticket) { | ||
manualLottoNum = getManualLottoNum(); | ||
for (int i = 0; i < manualLottoNum; i++) { | ||
addManualLottos(); | ||
} | ||
RandomLottoGenerator.makeAutomaticLottos(ticket - manualLottoNum, userLottos); | ||
} | ||
|
||
public static int getManualLottoNum() { | ||
Integer manualLottoNum = InputView.readManualLottoNum(); | ||
OutputView.printManualLottoMessage(); | ||
return manualLottoNum; | ||
} | ||
|
||
public static void addManualLottos() { | ||
|
||
String manualLotto = InputView.readManualLotto(); | ||
userLottos.addLotto(new Lotto(makeLottoList(manualLotto))); | ||
} | ||
|
||
public static void showAllLotto(int ticket) { | ||
OutputView.printLottos(manualLottoNum, ticket - manualLottoNum, userLottos); | ||
} | ||
|
||
public static List<LottoNumber> getLastWeekLotto() { | ||
String lastWeekLotto = InputView.readlastWeekLotto(); | ||
return makeLottoList(lastWeekLotto); | ||
} | ||
|
||
public static List<LottoNumber> makeLottoList(String lotto) { | ||
List<LottoNumber> LottoList = new ArrayList<>(); | ||
for (String lottoNumber : lotto.split(",\\s*")) { | ||
LottoList.add(new LottoNumber(lottoNumber)); | ||
} | ||
return LottoList; | ||
} | ||
|
||
public static int getBonusLottoNum() { | ||
return InputView.readBonusNumber(); | ||
} | ||
|
||
public void makeBingo(Lottos lottos, Lotto userLotto, int bonusNum) { | ||
FindBingo findBingo = new FindBingo(); | ||
findBingo.findBingos(lottos, userLotto, bonusNum); | ||
} | ||
|
||
public static void showLottoResult() { | ||
OutputView.printResult(); | ||
} | ||
|
||
public static double calculate(int money) { | ||
CalculateProfit.calcProfit(); | ||
return CalculateProfit.calcProfitPercent(money); | ||
} | ||
|
||
public static void showProfit(int money) { | ||
OutputView.printPercent(calculate(money)); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package domain; | ||
|
||
public class Bingo { | ||
int bingoNum; | ||
boolean checkBonus; | ||
private int correctLottoNum = 0; | ||
|
||
private Bingo(int bingoNum, boolean checkBonus) { | ||
this.bingoNum = bingoNum; | ||
this.checkBonus = checkBonus; | ||
} | ||
|
||
private static Bingo bingo3 = new Bingo(3, false); | ||
private static Bingo bingo4 = new Bingo(4, false); | ||
private static Bingo bingo5 = new Bingo(5, false); | ||
private static Bingo bingo5wB = new Bingo(5, true); | ||
private static Bingo bingo6 = new Bingo(6, false); | ||
|
||
public void addCorrectLottoNum() { | ||
correctLottoNum++; | ||
} | ||
|
||
public int getCorrectLottoNum() { | ||
return correctLottoNum; | ||
} | ||
|
||
public static Bingo getBingo3() { | ||
return bingo3; | ||
} | ||
|
||
public static Bingo getBingo4() { | ||
return bingo4; | ||
} | ||
|
||
public static Bingo getBingo5() { | ||
return bingo5; | ||
} | ||
|
||
public static Bingo getBingo5wB() { | ||
return bingo5wB; | ||
} | ||
|
||
public static Bingo getBingo6() { | ||
return bingo6; | ||
} | ||
|
||
} | ||
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,28 @@ | ||
package domain; | ||
|
||
public class BingoResult { | ||
public enum Result { | ||
BINGO3(Bingo.getBingo3().getCorrectLottoNum(), "3개 일치 (5000원) - "), | ||
BINGO4(Bingo.getBingo4().getCorrectLottoNum(), "4개 일치 (50000원) - "), | ||
BINGO5(Bingo.getBingo5().getCorrectLottoNum(), "5개 일치 (1500000원) - "), | ||
BINGO5WB(Bingo.getBingo5wB().getCorrectLottoNum(), "5개 일치, 보너스 볼 일치 (30000000원) - "), | ||
BINGO6(Bingo.getBingo6().getCorrectLottoNum(), "6개 일치 (2000000000원) - "); | ||
|
||
final private int matchNum; | ||
|
||
final private String message; | ||
|
||
Result(int matchNum, final String message) { | ||
this.matchNum = matchNum; | ||
this.message = message; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
|
||
public int getMatchNum() { | ||
return matchNum; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
public class Lotto { | ||
private List<LottoNumber> lotto = new ArrayList<>(); | ||
|
||
public static final int LOTTO_SIZE = 6; | ||
|
||
public Lotto(List<LottoNumber> lotto) { | ||
validate(lotto); | ||
this.lotto = lotto; | ||
} | ||
|
||
public List<LottoNumber> getLotto() { | ||
return lotto; | ||
} | ||
|
||
private void validate(List<LottoNumber> numbers) { | ||
validateSize(numbers); | ||
validateDuplication(numbers); | ||
} | ||
|
||
private void validateSize(List<LottoNumber> numbers) { | ||
if (numbers.size() != LOTTO_SIZE) { | ||
throw new IllegalArgumentException("로또는 6개의 숫자로 이루어져야 합니다."); | ||
} | ||
} | ||
|
||
private void validateDuplication(List<LottoNumber> numbers) { | ||
long duplicatedSize = checkDuplicatedSize(numbers); | ||
if (numbers.size() != duplicatedSize) { | ||
throw new IllegalArgumentException("로또는 중복되지 않는 6개의 숫자로 이루어져야 합니다."); | ||
} | ||
} | ||
|
||
private int checkDuplicatedSize(List<LottoNumber> numbers) { | ||
List<Integer> numbersList = new ArrayList<>(); | ||
for (LottoNumber lottoNumber : numbers) { | ||
numbersList.add(lottoNumber.getLottoNumber()); | ||
} | ||
Set<Integer> nonDuplicateNumbers = new HashSet<>(numbersList); | ||
return nonDuplicateNumbers.size(); | ||
} | ||
|
||
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.
스트림 문법을 쓰시면 더 깔끔하게 하실 수 있어요! 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 boolean iscontained(int number) { | ||
for (LottoNumber lottoNumber : lotto) { | ||
if (lottoNumber.getLottoNumber() == number) return true; | ||
} | ||
return false; | ||
} | ||
} | ||
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. 스트림 문법의 contains를 사용하면 더 깔끔해 질 것 같아용. 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,37 @@ | ||
package domain; | ||
|
||
public class LottoNumber { | ||
int lottoNumber; | ||
|
||
public int getLottoNumber() { | ||
return lottoNumber; | ||
} | ||
|
||
public LottoNumber(int lottoNumber){ | ||
this.lottoNumber=lottoNumber; | ||
} | ||
|
||
public LottoNumber(String lottoNumber) { | ||
this.lottoNumber = validate(lottoNumber); | ||
} | ||
private int validate(String lottoNumber) { | ||
int IntLottoNumber = validateInteger(lottoNumber); | ||
validateRange(IntLottoNumber); | ||
return IntLottoNumber; | ||
} | ||
|
||
private void validateRange(int lottoNumber){ | ||
if(lottoNumber<1||lottoNumber>45){ | ||
throw new IllegalArgumentException("로또 숫자는 1에서 45사이의 숫자이어야 합니다."); | ||
} | ||
} | ||
|
||
|
||
public static int validateInteger(String money) { | ||
try { | ||
return Integer.parseInt(money); | ||
} catch (NumberFormatException numberFormatException) { | ||
throw new IllegalArgumentException("로또 숫자는 숫자형식이어야 합니다."); | ||
} | ||
} | ||
} | ||
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,20 @@ | ||
package domain; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Lottos { | ||
private List<Lotto> lottos=new ArrayList<>(); | ||
|
||
public Lottos() { | ||
} | ||
|
||
public List<Lotto> getLottos() { | ||
return lottos; | ||
} | ||
|
||
public void addLotto(Lotto lotto){ | ||
lottos.add(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. 저랑 비슷한 구조로 하셔서 잘 읽히네요 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package domain; | ||
|
||
public class Money { | ||
private final int money; | ||
public static final int PRICE_UNIT = 1_000; | ||
|
||
public Money(String money) { | ||
this.money = validate(money); | ||
} | ||
|
||
private int validate(String money) { | ||
int IntMoney = validateInteger(money); | ||
validateRange(IntMoney); | ||
validateUnit(IntMoney); | ||
return IntMoney; | ||
} | ||
|
||
public static int validateInteger(String money) { | ||
try { | ||
return Integer.parseInt(money); | ||
} catch (NumberFormatException numberFormatException) { | ||
throw new IllegalArgumentException("금액은 숫자형식이여야 합니다."); | ||
} | ||
} | ||
|
||
private void validateRange(int price) { | ||
if (price < 0) { | ||
throw new IllegalArgumentException("금액은 0보다 큰값이여야 합니다."); | ||
} | ||
} | ||
|
||
private void validateUnit(int price) { | ||
if (price % 1000 != 0) { | ||
throw new IllegalArgumentException("금액은 1000원 단위여야 합니다."); | ||
} | ||
} | ||
|
||
public int getMoney() { | ||
return money; | ||
} | ||
|
||
public int ticket() { | ||
return money / 1000; | ||
} | ||
} | ||
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,20 @@ | ||
package service; | ||
|
||
import domain.Bingo; | ||
|
||
public class CalculateProfit { | ||
private static double profit = 0; | ||
|
||
public static void calcProfit() { | ||
profit += 5000 * Bingo.getBingo3().getCorrectLottoNum(); | ||
profit += 50000 * Bingo.getBingo4().getCorrectLottoNum(); | ||
profit += 1500000 * Bingo.getBingo5().getCorrectLottoNum(); | ||
profit += 30000000 * Bingo.getBingo5wB().getCorrectLottoNum(); | ||
profit += 2000000000 * Bingo.getBingo6().getCorrectLottoNum(); | ||
} | ||
|
||
public static double calcProfitPercent(int money) { | ||
double percent = (profit / (double) money); | ||
return Math.round((int) (percent * 100)) / 100.0; | ||
} | ||
} |
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.
다 좋은데 controller가 너무 비대한 것 같아요.
몇몇을 분리해 줄 필요가 있을 것 같아요