diff --git a/README.md b/README.md new file mode 100644 index 00000000..e5b08ef0 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +## ๐Ÿ”จ ๊ตฌํ˜„ ๊ธฐ๋Šฅ ๋ชฉ๋ก + +### ์ž…๋ ฅ + +- ๊ตฌ์ž… ๊ธˆ์•ก ์ž…๋ ฅ + - ํ•œ ์žฅ๋‹น 1000์› + - 1000์›์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€์ง€ ์•Š์œผ๋ฉด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- ์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋กœ๋˜ ์ˆ˜ ์ž…๋ ฅ + - ์ˆซ์ž ์™ธ์˜ ์ž…๋ ฅ์€ ๋ชจ๋‘ ์˜ˆ์™ธ์ฒ˜๋ฆฌ +- ์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž…๋ ฅ + - ๋ฒˆํ˜ธ๋Š” ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ + - ์ˆซ์ž์™€ ์‰ผํ‘œ(,) ์™ธ์˜ ์ž…๋ ฅ์€ ๋ชจ๋‘ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + - 6๊ฐœ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- ๋‹น์ฒจ ๋ฒˆํ˜ธ ์ž…๋ ฅ + - ๋ฒˆํ˜ธ๋Š” ์‰ผํ‘œ(,)๋กœ ๊ตฌ๋ถ„ + - ์ˆซ์ž์™€ ์‰ผํ‘œ(,) ์™ธ์˜ ์ž…๋ ฅ์€ ๋ชจ๋‘ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ + - 6๊ฐœ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์˜ˆ์™ธ ์ฒ˜๋ฆฌ +- ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ ์ž…๋ ฅ + - ์ˆซ์ž ์™ธ์˜ ์ž…๋ ฅ์€ ๋ชจ๋‘ ์˜ˆ์™ธ์ฒ˜๋ฆฌ + + +### ์ถœ๋ ฅ + + +- ์ˆ˜๋™ ๋กœ๋˜์™€ ์ž๋™ ๋กœ๋˜ ๋ฐœํ–‰ + - ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด์„œ ๋ณด์—ฌ์คŒ + - ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” 6๊ฐœ ์ˆซ์ž +- ๋‹น์ฒจ ๋‚ด์—ญ ์ถœ๋ ฅ + - `3๊ฐœ ์ผ์น˜ (5000์›) - n๊ฐœ` + - `4๊ฐœ ์ผ์น˜ (50000์›) - n๊ฐœ` + - `5๊ฐœ ์ผ์น˜ (1500000์›) - n๊ฐœ` + - `5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜ (30000000์›) - n๊ฐœ` + - `6๊ฐœ ์ผ์น˜ (2000000000์›) - n๊ฐœ` +- ์ˆ˜์ต๋ฅ  ์ถœ๋ ฅ + - `์ด ์ˆ˜์ต๋ฅ ์€ n์ž…๋‹ˆ๋‹ค.` + - ์†Œ์ˆซ์  ๋‘˜์งธ์ž๋ฆฌ ๋ฐ‘์œผ๋ก  ๋ฒ„๋ฆผ +- ์˜ˆ์™ธ ์ƒํ™ฉ์‹œ ์—๋Ÿฌ ๋ฌธ๊ตฌ ์ถœ๋ ฅ + - `[ERROR] error message` + +### ๊ฒŒ์ž„ ์ง„ํ–‰ + +- ๊ตฌ์ž… ๊ธˆ์•ก์„ ์ž…๋ ฅ +- ์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋กœ๋˜ ์ˆ˜ ์ž…๋ ฅ +- ์ˆ˜๋™ ๋กœ๋˜ ๋ฒˆํ˜ธ ์ž…๋ ฅ +- ๋กœ๋˜ ๋ฒˆํ˜ธ 1~45 ์‚ฌ์ด์˜ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์ •์ˆ˜๋กœ 6๊ฐœ ๋ฌด์ž‘์œ„ ์ถ”์ฒจํ•ด์„œ ๊ตฌ์ž…ํ•œ ๊ฐœ์ˆ˜(์ˆ˜๋™ ๋กœ๋˜ ๊ฐœ์ˆ˜ ์ œ์™ธ) ๋งŒํผ ์ž๋™ ๋กœ๋˜ ๋ฐœํ–‰ +- ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์Œ +- ์ผ์น˜ํ•˜๋Š” ๋‚ด์—ญ๊ณผ ์ด ์ˆ˜์ต๋ฅ ์„ ์ถœ๋ ฅ +- ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฌธ๊ตฌ ์ถœ๋ ฅ \ No newline at end of file diff --git a/src/main/java/Application.java b/src/main/java/Application.java new file mode 100644 index 00000000..bf82be8e --- /dev/null +++ b/src/main/java/Application.java @@ -0,0 +1,11 @@ +import controller.LottoController; + +import java.io.IOException; + +public class Application { + + public static void main(String [] args) throws IOException { + LottoController lottoController = new LottoController(); + lottoController.startLottoGame(); + } +} diff --git a/src/main/java/controller/LottoController.java b/src/main/java/controller/LottoController.java new file mode 100644 index 00000000..b7b9ee73 --- /dev/null +++ b/src/main/java/controller/LottoController.java @@ -0,0 +1,145 @@ +package controller; + +import model.*; +import view.InputView; +import view.OutputView; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.Predicate; + +public class LottoController { + private InputView inputView = new InputView(); + private OutputView outputView = new OutputView(); + private LottoList lottoList; + + public LottoController() { + } + public void startLottoGame() { + UserCount userMoney = receiveMoneyInput(); + ManualCount manualCount = manualLottoCount(); + if(manualCount.getManualCount() == 0){ + displayLottoTickets(userMoney, null); + } + else{ + displayLottoTickets(userMoney, manualLottoNumbers(manualCount.getManualCount())); + } + Lotto winningNumbers = getWinningNumbers(); + BonusNumber bonusNum = receiveBonusNumberInput(); + validateDuplicate(winningNumbers, bonusNum); + + List matchCounts = calculateMatches(lottoList, winningNumbers, bonusNum); + double winRate = calculateWinningRate(matchCounts, userMoney); + outputView.printResults(matchCounts, Math.floor(winRate*100)/100 ); + } + private ManualCount manualLottoCount(){ + return new ManualCount(inputView.inputManualLottoCount()); + } + private List manualLottoNumbers(int count){ + List manualLottoNums = inputView.manualLottos(count); + List manualLottos = new ArrayList<>(); + + for (String manualLotto : manualLottoNums) { + Lotto lotto = new Lotto(); + manualLottos.add(lotto.convertToList(manualLotto)); + } + return manualLottos; + } + + private BonusNumber receiveBonusNumberInput() { + return new BonusNumber(inputView.inputBonusNumber()); + } + + private void validateDuplicate(Lotto winningLotto, BonusNumber bonusNumber){ + winningLotto.validateDuplicateWithBonus(bonusNumber.getBonusNumber()); + } + + private UserCount receiveMoneyInput() { + return new UserCount(inputView.inputPrice()); + } + + private void displayLottoTickets(UserCount count, List lottos){ + int autoCount = 0; + int manualCount = 0; + if(lottos == null){ + autoCount = count.getCount(); + manualCount = 0; + } + else{ + autoCount = count.getCount() - lottos.size(); + manualCount = lottos.size(); + } + outputView.printTicketCount(autoCount, manualCount); + lottoList = generateLottoList(autoCount, lottos); + outputView.printLottoNumbers(lottoList); + } + LottoList generateLottoList(int count, List lottos){ + lottoList = new LottoList(); + + if(lottos != null){ + for (Lotto lotto : lottos) { + lottoList.setLottoList(lotto); + } + } + for(int i=0; i numbers = new ArrayList<>(); + AutoLotto autoLotto = new AutoLotto(); + numbers = autoLotto.getAutoLotto(); + return new Lotto(numbers); + } + + private Lotto getWinningNumbers(){ + String winnerNumbersStr = inputView.inputWinnerNumber(); + Lotto winningLotto = new Lotto(); + return winningLotto.convertToList(winnerNumbersStr); + } + + + private List calculateMatches(LottoList ticketList, Lotto winningNumbers, BonusNumber bonusNum) { + List matchCounts = new ArrayList<>(Arrays.asList(0, 0, 0, 0, 0)); + + for (Lotto ticket : ticketList.getLottoList()) { + int countMatches = ticket.calculateMatches(winningNumbers); + updateMatchCounts(matchCounts, countMatches, bonusNum); + } + + return matchCounts; + } + + + private void updateMatchCounts(List matchCounts, int countMatches, BonusNumber bonusNum) { + Predicate isBonusMatch = num -> num == 5 && bonusNum.getBonusNumber() > 0; + + if (countMatches == 3) { + matchCounts.set(0, matchCounts.get(0) + 1); + } else if (countMatches == 4) { + matchCounts.set(1, matchCounts.get(1) + 1); + } else if (countMatches == 5) { + if (isBonusMatch.test(countMatches)) { + matchCounts.set(3, matchCounts.get(3) + 1); + } else { + matchCounts.set(2, matchCounts.get(2) + 1); + } + } else if (countMatches == 6) { + matchCounts.set(4, matchCounts.get(4) + 1); + } + + } + double calculateWinningRate(List matchCounts, UserCount count) { + int totalWinnings = matchCounts.get(0) * 5000 + + matchCounts.get(1) * 50000 + + matchCounts.get(2) * 1500000 + + matchCounts.get(3) * 30000000 + + matchCounts.get(4) * 2000000000; + + double winningRate = (double) totalWinnings / (double) count.getPrice(); + return Math.floor(winningRate * 100) / 100; + } + +} diff --git a/src/main/java/model/AutoLotto.java b/src/main/java/model/AutoLotto.java new file mode 100644 index 00000000..d7f65064 --- /dev/null +++ b/src/main/java/model/AutoLotto.java @@ -0,0 +1,36 @@ +package model; + +import java.util.*; + +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 lottoNums = new ArrayList<>(); + public AutoLotto(){ + createAutoLotto(); + } + public void createAutoLotto(){ + + List 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); + } + public List getAutoLotto(){ + return lottoNums; + } + +} diff --git a/src/main/java/model/BonusNumber.java b/src/main/java/model/BonusNumber.java new file mode 100644 index 00000000..8bbea538 --- /dev/null +++ b/src/main/java/model/BonusNumber.java @@ -0,0 +1,23 @@ +package model; + +public class BonusNumber { + + private int bonusNumber; + + public BonusNumber(String bonusNumber){ + this.bonusNumber = convertStringToInt(bonusNumber) + ; + } + + public int convertStringToInt(String bonusNumber){ + try { + return Integer.parseInt(bonusNumber); + } catch (NumberFormatException e) { + System.err.println("[ERROR] ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + throw e; + } + } + public int getBonusNumber(){ + return bonusNumber; + } +} diff --git a/src/main/java/model/Lotto.java b/src/main/java/model/Lotto.java new file mode 100644 index 00000000..cc4ac863 --- /dev/null +++ b/src/main/java/model/Lotto.java @@ -0,0 +1,78 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public class Lotto { + private static final int BONUS_NUMBER_INDEX = 6; + private List numbers; + + public Lotto(List numbers) { + this.numbers = numbers; + } + public Lotto(){ + } + public Lotto convertToList(String lottoNumbersStr) { + String[] str = lottoNumbersStr.split(",\\s*"); + List lottoList = new ArrayList<>(); + for (String s : str) { + lottoList.add(convertStringToInt(s)); + } + + Lotto lotto = new Lotto(lottoList); + validate(lotto.getNumbers()); + return lotto; + } + private int convertStringToInt(String price){ + try { + return Integer.parseInt(price); + } catch (NumberFormatException e) { + System.err.println("[ERROR] ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + throw e; + } + } + private void validate(List numbers){ + validateSize(numbers); + validateRange(numbers); + validateDuplicate(numbers); + } + void validateSize(List numbers){ + if(numbers.size() != 6){ + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 6๊ฐœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + void validateRange(List numbers){ + for(int num : numbers){ + if(num<=0||num>45){ + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ 45 ์‚ฌ์ด์˜ ์ˆซ์ž๋กœ ์ž…๋ ฅํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } + } + void validateDuplicate(List numbers){ + List tmp = numbers; + if(tmp.stream().distinct().count()!=numbers.size()){ + throw new IllegalArgumentException("[ERROR] ๋กœ๋˜ ๋ฒˆํ˜ธ์— ์ค‘๋ณต๋œ ์ˆซ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค."); + } + + } + public ListgetNumbers(){ + return numbers; + } + + public int calculateMatches(Lotto winningLotto) { + int cnt = 0; + List winningLottoList = winningLotto.getNumbers(); + for (Integer integer : winningLottoList) { + if(numbers.contains(integer)){ + cnt ++; + } + } + return cnt; + } + + public void validateDuplicateWithBonus(int bonusNumber) { + if (numbers.contains(bonusNumber)) { + throw new IllegalArgumentException("[ERROR] ๋ณด๋„ˆ์Šค ๋ฒˆํ˜ธ๋Š” ๋‹น์ฒจ ๋ฒˆํ˜ธ์™€ ์ค‘๋ณต๋˜์–ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค."); + } + } +} diff --git a/src/main/java/model/LottoList.java b/src/main/java/model/LottoList.java new file mode 100644 index 00000000..ce3ef403 --- /dev/null +++ b/src/main/java/model/LottoList.java @@ -0,0 +1,18 @@ +package model; + +import java.util.ArrayList; +import java.util.List; + +public class LottoList { + List lottoList = new ArrayList<>(); + private int totalPrice; + public LottoList(){ + + } + public void setLottoList(Lotto lotto){ + lottoList.add(lotto); + } + public List getLottoList(){ + return lottoList; + } +} diff --git a/src/main/java/model/ManualCount.java b/src/main/java/model/ManualCount.java new file mode 100644 index 00000000..c97da843 --- /dev/null +++ b/src/main/java/model/ManualCount.java @@ -0,0 +1,25 @@ +package model; + +public class ManualCount { + private int manualCount; + + public ManualCount(){ + + } + public ManualCount(String manualCount){ + this.manualCount = convertStringToInt(manualCount) + ; + } + + public int convertStringToInt(String manualCount){ + try { + return Integer.parseInt(manualCount); + } catch (NumberFormatException e) { + System.err.println("[ERROR] ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + throw e; + } + } + public int getManualCount(){ + return manualCount; + } +} diff --git a/src/main/java/model/UserCount.java b/src/main/java/model/UserCount.java new file mode 100644 index 00000000..1f366a13 --- /dev/null +++ b/src/main/java/model/UserCount.java @@ -0,0 +1,50 @@ +package model; + +public class UserCount { + + private static final int LOTTO_UNIT = 1000; + + private int count; + private int price; + + public UserCount(){ + } + public UserCount(String price){ + int priceNum = convertStringToInt(price); + validate(priceNum); + this.price = priceNum; + this.count = calculateCount(priceNum); + } + public int calculateCount(int price){ + return price/1000; + } + public int getCount(){ + return count; + } + public int getPrice(){ + return price; + } + public int convertStringToInt(String price){ + try { + return Integer.parseInt(price); + } catch (NumberFormatException e) { + System.err.println("[ERROR] ์ˆซ์ž๋กœ๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”."); + throw e; + } + } + + public void validate(int priceNum){ + validateRange(priceNum); + validateUnit(priceNum); + } + public void validateRange(int priceNum){ + if(priceNum<=0){ + throw new IllegalArgumentException("[ERROR] 0์› ์ดํ•˜๋Š” ๊ตฌ๋งค๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค"); + } + } + public void validateUnit(int priceNum){ + if(priceNum % LOTTO_UNIT != 0){ + throw new IllegalArgumentException("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."); + } + } +} diff --git a/src/main/java/model/WinningType.java b/src/main/java/model/WinningType.java new file mode 100644 index 00000000..2212a341 --- /dev/null +++ b/src/main/java/model/WinningType.java @@ -0,0 +1,23 @@ +package model; + +public enum WinningType { + FIFTH(3, 5000, "3๊ฐœ ์ผ์น˜ (5000์›)- "), + FOURTH(4, 50000, "4๊ฐœ ์ผ์น˜ (50000์›)- "), + THIRD(5, 150000, "5๊ฐœ ์ผ์น˜ (1500000์›)- "), + SECOND(5, 30000000, "5๊ฐœ ์ผ์น˜, ๋ณด๋„ˆ์Šค ๋ณผ ์ผ์น˜(30000000์›)- "), + FIRTH(6, 2000000000, "6๊ฐœ ์ผ์น˜ (2000000000์›)- "); + + private static final int WINNING_MIN_COUNT = 3; + private static final String ERROR_MESSAGE = "[ERROR]"; + + private String message; + + WinningType(int countOfCorrect, int reward, String message) { + this.message = message; + } + + public String getMessage(){ + return message; + } + +} diff --git a/src/main/java/view/InputView.java b/src/main/java/view/InputView.java new file mode 100644 index 00000000..fdae7afc --- /dev/null +++ b/src/main/java/view/InputView.java @@ -0,0 +1,52 @@ +package view; + +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class InputView { + + private final static String INPUT_PRICE = "๊ตฌ์ž…๊ธˆ์•ก์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final static String MANUAL_BUY_LOTTO = "์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋กœ๋˜ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final static String INPUT_MANUAL_LOTTO_NUMBERS = "์ˆ˜๋™์œผ๋กœ ๊ตฌ๋งคํ•  ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final static String INPUT_WINNER_NUMBER = "์ง€๋‚œ ์ฃผ ๋‹น์ฒจ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + private final static String INPUT_BONUS_NUMBER = "๋ณด๋„ˆ์Šค ๋ณผ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”."; + + + private static final Scanner scanner = new Scanner(System.in); + + + public String inputPrice(){ + System.out.println(INPUT_PRICE); + return scanner.nextLine(); + } + + public String inputManualLottoCount(){ + System.out.println(); + System.out.println(MANUAL_BUY_LOTTO); + return scanner.nextLine(); + } + + public List manualLottos(int cnt){ + System.out.println(); + System.out.println(INPUT_MANUAL_LOTTO_NUMBERS); + List manualLottoNums = new ArrayList<>(); + for (int i = 0; i < cnt; i++) { + manualLottoNums.add(scanner.nextLine()); + } + return manualLottoNums; + } + + public String inputWinnerNumber(){ + System.out.println(); + System.out.println(INPUT_WINNER_NUMBER); + return scanner.nextLine(); + } + + public String inputBonusNumber(){ + + System.out.println(); + System.out.println(INPUT_BONUS_NUMBER); + return scanner.nextLine(); + } +} diff --git a/src/main/java/view/OutputView.java b/src/main/java/view/OutputView.java new file mode 100644 index 00000000..3bd253d7 --- /dev/null +++ b/src/main/java/view/OutputView.java @@ -0,0 +1,58 @@ +package view; + +import model.Lotto; +import model.LottoList; +import model.WinningType; + +import java.util.List; + +public class OutputView { + + private final static String BUY_MESSAGE = "์ˆ˜๋™์œผ๋กœ %d์žฅ, ์ž๋™์œผ๋กœ %d๊ฐœ๋ฅผ ๊ตฌ๋งคํ–ˆ์Šต๋‹ˆ๋‹ค."; + private final static String COUNT_SUFFIX = "๊ฐœ"; + + + public void printTicketCount (int autoCount, int manualCount){ + System.out.println(); + String message = String.format(BUY_MESSAGE, manualCount, autoCount); + System.out.println(message); } + public void printLottoNumbers(LottoList lottoList) { + List lottos = lottoList.getLottoList(); + for (Lotto lotto : lottos) { + System.out.println(lotto.getNumbers()); + } + } + + public void printResults(List matchCounts, double rate) { + printResultHeader(); + printMatchCounts(matchCounts); + printWinningRate(rate); + } + + private void printResultHeader() { + System.out.println(); + System.out.println("๋‹น์ฒจ ํ†ต๊ณ„"); + System.out.println("---------"); + } + + private void printMatchCounts(List matchCounts) { + WinningType[] winningTypes = WinningType.values(); + + for (int i = 0; i < matchCounts.size(); i++) { + WinningType type = winningTypes[i]; + int count = matchCounts.get(i); + + String message = getMessageForWinningType(type, count); + System.out.println(message); + } + } + private String getMessageForWinningType(WinningType type, int count) { + String message = type.getMessage(); + return message + count + COUNT_SUFFIX + " - " + count + "๊ฐœ"; + + } + + private void printWinningRate(double rate) { + System.out.printf("์ด ์ˆ˜์ต๋ฅ ์€ %.2f ์ž…๋‹ˆ๋‹ค. (๊ธฐ์ค€์ด 1์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ์ ์œผ๋กœ ์†ํ•ด๋ผ๋Š” ์˜๋ฏธ์ž„)%n", rate); + } +} diff --git a/src/test/java/controller/LottoControllerTest.java b/src/test/java/controller/LottoControllerTest.java new file mode 100644 index 00000000..d146526b --- /dev/null +++ b/src/test/java/controller/LottoControllerTest.java @@ -0,0 +1,47 @@ +package controller; + +import model.Lotto; +import model.LottoList; +import model.UserCount; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class LottoControllerTest { + @Test + void testCalculateWinningRate() { + // Given + LottoController controller = new LottoController(); + List matchCounts = Arrays.asList(1, 0, 0, 0, 0, 0); + String price = "14000"; + double priceNum = 14000.0; + UserCount userCount = new UserCount(price); + + // When + double winningRate = controller.calculateWinningRate(matchCounts, userCount); + + // Then + double expectedRate = (double) (1 * 5000 + 0 * 50000 + 0 * 150000 + 0 * 30000000 + 0 * 2000000000) / priceNum; + expectedRate = Math.floor(expectedRate * 100) / 100; + System.out.println("dd" + winningRate); + assertEquals(expectedRate, winningRate); + } + + @Test + void testGenerateLottoList() { + // Given + LottoController controller = new LottoController(); + int count = 5; + + // When + LottoList lottoList = controller.generateLottoList(count, null); + + // Then + List lottos = lottoList.getLottoList(); + assertEquals(count, lottos.size()); + + } +} \ No newline at end of file diff --git a/src/test/java/model/BonusNumberTest.java b/src/test/java/model/BonusNumberTest.java new file mode 100644 index 00000000..a005c39a --- /dev/null +++ b/src/test/java/model/BonusNumberTest.java @@ -0,0 +1,21 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class BonusNumberTest { + @Test + void bonus_ValidInput() { + String bonusNumberStr = "10"; + BonusNumber bonusNumber = new BonusNumber(bonusNumberStr); + + assertEquals(10, bonusNumber.getBonusNumber()); + } + + @Test + void bonus_InvalidInput() { + String bonusNumberStr = "ABC"; + assertThrows(NumberFormatException.class, () -> new BonusNumber(bonusNumberStr)); + } +} \ No newline at end of file diff --git a/src/test/java/model/LottoTest.java b/src/test/java/model/LottoTest.java new file mode 100644 index 00000000..923b559c --- /dev/null +++ b/src/test/java/model/LottoTest.java @@ -0,0 +1,92 @@ +package model; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +class LottoTest { + @Test + void convertToList_ValidInput() { + String lottoNumbersStr = "1, 2, 3, 4, 5, 6"; + Lotto lotto = new Lotto().convertToList(lottoNumbersStr); + + List expectedNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); + assertEquals(expectedNumbers, lotto.getNumbers()); + } + + @Test + void convertToList_InvalidInput() { + String lottoNumbersStr = "1, 2, 3, 4, A, 6"; + assertThrows(NumberFormatException.class, () -> new Lotto().convertToList(lottoNumbersStr)); + } + + @Test + void validateSize_ValidSize() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateSize(numbers)); + } + + @Test + void validateSize_InvalidSize() { + List numbers = Arrays.asList(1, 2, 3, 4, 5); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateSize(numbers)); + } + + @Test + void validateRange_ValidRange() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateRange(numbers)); + } + + @Test + void validateRange_InvalidRange() { + List numbers = Arrays.asList(1, 2, 3, 4, 50, 6); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateRange(numbers)); + } + + @Test + void validateDuplicate_NoDuplicates() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateDuplicate(numbers)); + } + + @Test + void validateDuplicate_DuplicateNumbers() { + List numbers = Arrays.asList(1, 2, 3, 4, 4, 6); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateDuplicate(numbers)); + } + + @Test + void calculateMatches_WithWinningLotto() { + List myNumbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto myLotto = new Lotto(myNumbers); + + List winningNumbers = Arrays.asList(1, 3, 5, 7, 9, 11); + Lotto winningLotto = new Lotto(winningNumbers); + + assertEquals(3, myLotto.calculateMatches(winningLotto)); + } + + @Test + void validateDuplicateWithBonus_ValidBonusNumber() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertDoesNotThrow(() -> lotto.validateDuplicateWithBonus(7)); + } + + @Test + void validateDuplicateWithBonus_InvalidBonusNumber() { + List numbers = Arrays.asList(1, 2, 3, 4, 5, 6); + Lotto lotto = new Lotto(numbers); + assertThrows(IllegalArgumentException.class, () -> lotto.validateDuplicateWithBonus(6)); + } +} \ No newline at end of file diff --git a/src/test/java/model/ManualCountTest.java b/src/test/java/model/ManualCountTest.java new file mode 100644 index 00000000..7a82b1b8 --- /dev/null +++ b/src/test/java/model/ManualCountTest.java @@ -0,0 +1,43 @@ +package model; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class ManualCountTest { + + @Test + void validManualCount() { + String manualCountStr = "5"; + ManualCount manualCount = new ManualCount(manualCountStr); + + assertEquals(5, manualCount.getManualCount()); + } + + @Test + void invalidInput() { + String manualCountStr = "abc"; + assertThrows(NumberFormatException.class, () -> new ManualCount(manualCountStr)); + } + + @Test + void getManualCount() { + String manualCountStr = "10"; + ManualCount manualCount = new ManualCount(manualCountStr); + + assertEquals(10, manualCount.getManualCount()); + } + + @Test + void convertStringToInt_ValidInput() { + ManualCount manualCount = new ManualCount(); + assertEquals(15, manualCount.convertStringToInt("15")); + } + + @Test + void convertStringToInt_InvalidInput() { + ManualCount manualCount = new ManualCount(); + assertThrows(NumberFormatException.class, () -> manualCount.convertStringToInt("abc")); + } + +} \ No newline at end of file diff --git a/src/test/java/model/UserCountTest.java b/src/test/java/model/UserCountTest.java new file mode 100644 index 00000000..4a50deec --- /dev/null +++ b/src/test/java/model/UserCountTest.java @@ -0,0 +1,81 @@ +package model; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +class UserCountTest { + + @Test + void validPrice() { + String priceStr = "3000"; + UserCount userCount = new UserCount(priceStr); + + assertEquals(3000, userCount.getPrice()); + assertEquals(3, userCount.getCount()); + } + + @Test + void invalidZeroPrice() { + String priceStr = "0"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new UserCount(priceStr)); + assertEquals("[ERROR] 0์› ์ดํ•˜๋Š” ๊ตฌ๋งค๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค", exception.getMessage()); + } + + @Test + void invalidNegativePrice() { + String priceStr = "-5000"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new UserCount(priceStr)); + assertEquals("[ERROR] 0์› ์ดํ•˜๋Š” ๊ตฌ๋งค๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค", exception.getMessage()); + } + + @Test + void invalidNonMultipleOf1000() { + String priceStr = "2500"; + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new UserCount(priceStr)); + assertEquals("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.", exception.getMessage()); + } + + @Test + void calculateCount_ValidPrice() { + UserCount userCount = new UserCount("4000"); + assertEquals(4, userCount.getCount()); + } + + @Test + void convertStringToInt_ValidInput() { + UserCount userCount = new UserCount(); + assertEquals(5000, userCount.convertStringToInt("5000")); + } + + @Test + void convertStringToInt_InvalidInput() { + UserCount userCount = new UserCount(); + assertThrows(NumberFormatException.class, () -> userCount.convertStringToInt("abc")); + } + + @Test + void validateRange_ValidPrice() { + UserCount userCount = new UserCount(); + assertDoesNotThrow(() -> userCount.validateRange(2000)); + } + + @Test + void validateRange_InvalidZeroPrice() { + UserCount userCount = new UserCount(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> userCount.validateRange(0)); + assertEquals("[ERROR] 0์› ์ดํ•˜๋Š” ๊ตฌ๋งค๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค", exception.getMessage()); + } + + @Test + void validateUnit_ValidPrice() { + UserCount userCount = new UserCount(); + assertDoesNotThrow(() -> userCount.validateUnit(3000)); + } + + @Test + void validateUnit_InvalidNonMultipleOf1000() { + UserCount userCount = new UserCount(); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> userCount.validateUnit(2500)); + assertEquals("[ERROR] ๊ตฌ์ž… ๊ธˆ์•ก์€ 1000์› ๋‹จ์œ„๋กœ ์ž…๋ ฅ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.", exception.getMessage()); + } +}