diff --git a/docs/README.md b/docs/README.md index e69de29bb2..70cd6fb050 100644 --- a/docs/README.md +++ b/docs/README.md @@ -0,0 +1,21 @@ +# 기능 목록 + +### Member + +- [x] 로또 구입 금액 입력 +- [x] 당첨 번호 입력 +- [x] 보너스 번호 입력 + +### Lotto + +- [x] 랜덤된 로또 번호 생성 후 출력 +- [x] 입력 숫자와 로또 번호 비교 +- [x] 비교된 결과 표 출력 +- [x] 수익률 출력 + + +# 예외 처리 +사용자가 잘못된 값을 입력할 경우 IllegalArgumentException 발생, "[ERROR]"로 시작하는 에러 메시지를 출력 후 종료 +- [x] 로또 구입 금액은 1000 단위로 나누어 떨어지는 숫자 +- [x] 당첨 번호는 중복 안되는 1~45까지의 6개의 숫자 +- [x] 보너스 점수는 1~45까지의 숫자 한개 \ No newline at end of file diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..25a39f9dea 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,15 @@ package lotto; +import lotto.lottoManager.controller.LottoManagerController; + public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + try{ + LottoManagerController lottoManagerController = new LottoManagerController(); + lottoManagerController.startLotto(); + }catch (IllegalArgumentException e){ + System.out.println(e.getMessage()); + } + } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java deleted file mode 100644 index 519793d1f7..0000000000 --- a/src/main/java/lotto/Lotto.java +++ /dev/null @@ -1,20 +0,0 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException(); - } - } - - // TODO: 추가 기능 구현 -} diff --git a/src/main/java/lotto/lottoManager/controller/LottoManagerController.java b/src/main/java/lotto/lottoManager/controller/LottoManagerController.java new file mode 100644 index 0000000000..2633c906a0 --- /dev/null +++ b/src/main/java/lotto/lottoManager/controller/LottoManagerController.java @@ -0,0 +1,34 @@ +package lotto.lottoManager.controller; + +import lotto.lottoManager.service.LottoManagerService; +import lotto.member.controller.MemberController; +import lotto.producer.controller.ProducerController; +import lotto.producer.repository.Lotto; + +import java.util.List; + +public class LottoManagerController { + + private final MemberController memberController; + private final ProducerController producerController; + private final LottoManagerService lottoManagerService; + + public LottoManagerController(){ + this.memberController = new MemberController(); + this.producerController = new ProducerController(); + this.lottoManagerService = new LottoManagerService(); + } + + public void startLotto(){ + int lottoPrice = memberController.inputLottoPrice(); + List lottos = producerController.getLottoNumbers(lottoCount(lottoPrice)); + List winNumber = memberController.inputWinLottoNumber(); + int bonusNumber = memberController.inputBonusNumber(); + lottoManagerService.getStatistics(lottos, winNumber,bonusNumber); + lottoManagerService.getLottoRate(lottoPrice); + } + + private int lottoCount(int lottoPrice){ + return lottoPrice / 1000; + } +} diff --git a/src/main/java/lotto/lottoManager/repository/LottoManagerRepository.java b/src/main/java/lotto/lottoManager/repository/LottoManagerRepository.java new file mode 100644 index 0000000000..dbb7b71952 --- /dev/null +++ b/src/main/java/lotto/lottoManager/repository/LottoManagerRepository.java @@ -0,0 +1,50 @@ +package lotto.lottoManager.repository; + +import lotto.lottoManager.service.LottoStatistics; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; + +public class LottoManagerRepository { + private final Map statisticsCounter = new EnumMap<>(LottoStatistics.class); + + public void upStatisticsMatchCount(List lottoNumber, List winNumber, int bonusNumber) { + int matchCount = getMatchCount(lottoNumber, winNumber); + boolean isMatchBonusNumber = isMatchBonusNumber(winNumber, bonusNumber); + upMatchCount(matchCount, isMatchBonusNumber); + } + + private int getMatchCount(List lottoNumbers, List winNumber) { + return (int) lottoNumbers.stream() + .filter(winNumber::contains) + .count(); + } + + private boolean isMatchBonusNumber(List winNumber, int bonusNumber){ + return winNumber.contains(bonusNumber); + } + + private void upMatchCount(int matchCount, boolean isBonusNumberInWinNumber){ + for (LottoStatistics statistics : LottoStatistics.values()){ + if (matchCount == statistics.getMatchCount() && statistics.isMatchBonus() == isBonusNumberInWinNumber) { + statisticsCounter.put(statistics, statisticsCounter.getOrDefault(statistics, 0)+1); + break; + } + } + } + + public double calculateLottoRate(int lottoPrice) { + double lottoRevenue = 0; + for (LottoStatistics statistics : LottoStatistics.values()){ + lottoRevenue += statistics.getPrice() * statisticsCounter.getOrDefault(statistics, 0); + } + double rate = (lottoRevenue / lottoPrice) * 100; + return Math.round(rate * 100.0) / 100.0; + } + + public Map getStatisticsCounter() { + return statisticsCounter; + } + +} diff --git a/src/main/java/lotto/lottoManager/service/LottoManagerService.java b/src/main/java/lotto/lottoManager/service/LottoManagerService.java new file mode 100644 index 0000000000..3228658680 --- /dev/null +++ b/src/main/java/lotto/lottoManager/service/LottoManagerService.java @@ -0,0 +1,46 @@ +package lotto.lottoManager.service; + +import lotto.lottoManager.repository.LottoManagerRepository; +import lotto.producer.repository.Lotto; + +import java.util.List; +import java.util.Map; + +public class LottoManagerService { + + private final LottoManagerRepository lottoManagerRepository; + + public LottoManagerService(){ + this.lottoManagerRepository = new LottoManagerRepository(); + } + + public void getStatistics(List lottos, List winNumber, int bonusNumber) { + for (Lotto lotto : lottos) { + List lottoNumbers = lotto.getNumbers(); + lottoManagerRepository.upStatisticsMatchCount(lottoNumbers, winNumber, bonusNumber); + } + System.out.println(getMatchResult()); + } + + public String getMatchResult() { + Map statisticsCounter = lottoManagerRepository.getStatisticsCounter(); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("\n") + .append("당첨 통계\n") + .append("---\n"); + for (LottoStatistics statistics : LottoStatistics.values()){ + stringBuilder.append(statistics) + .append(statisticsCounter.getOrDefault(statistics, 0)) + .append("개\n"); + } + return stringBuilder.toString(); + } + + public void getLottoRate(int lottoPrice) { + String printLottoRate = "총 수익률은 " + + lottoManagerRepository.calculateLottoRate(lottoPrice) + + "%입니다."; + System.out.println(printLottoRate); + } + +} diff --git a/src/main/java/lotto/lottoManager/service/LottoStatistics.java b/src/main/java/lotto/lottoManager/service/LottoStatistics.java new file mode 100644 index 0000000000..d6857eaba4 --- /dev/null +++ b/src/main/java/lotto/lottoManager/service/LottoStatistics.java @@ -0,0 +1,48 @@ +package lotto.lottoManager.service; + +import java.text.NumberFormat; + +public enum LottoStatistics { + + THIRD(3, 5_000, false), + FOURTH(4, 50_000, false), + FIFTH(5, 1_500_000, false), + FIFTH_AND_BONUS(5, 30_000_000, true), + SIXTH(6, 2_000_000_000, false); + + private final int matchCount; + private final int price; + private final boolean isMatchBonus; + + LottoStatistics(int matchCount, int price, boolean isMatchBonus){ + this.matchCount = matchCount; + this.price = price; + this.isMatchBonus = isMatchBonus; + } + + @Override + public String toString(){ + StringBuilder sb = new StringBuilder(); + sb.append(matchCount).append("개 일치"); + if(isMatchBonus){ + sb.append(", 보너스 볼 일치"); + } + sb.append(" (") + .append(NumberFormat.getInstance().format(price)) + .append("원)") + .append(" - "); + return sb.toString(); + } + + public int getMatchCount() { + return matchCount; + } + + public int getPrice() { + return price; + } + + public boolean isMatchBonus() { + return isMatchBonus; + } +} diff --git a/src/main/java/lotto/member/controller/InputMessage.java b/src/main/java/lotto/member/controller/InputMessage.java new file mode 100644 index 0000000000..74cf3ea001 --- /dev/null +++ b/src/main/java/lotto/member/controller/InputMessage.java @@ -0,0 +1,19 @@ +package lotto.member.controller; + +public enum InputMessage { + + PURCHASE("구입 금액을 입력해 주세요"), + WIN_NUMBER("당첨 번호를 입력해 주세요"), + BONUS_NUMBER("보너스 번호를 입력해 주세요."), + ENTER("\n"); + + private final String message; + + InputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/lotto/member/controller/MemberController.java b/src/main/java/lotto/member/controller/MemberController.java new file mode 100644 index 0000000000..c57ef78814 --- /dev/null +++ b/src/main/java/lotto/member/controller/MemberController.java @@ -0,0 +1,30 @@ +package lotto.member.controller; +import camp.nextstep.edu.missionutils.Console; +import lotto.member.service.MemberService; + +import java.util.List; + +public class MemberController { + + private final MemberService memberService; + + public MemberController(){ + this.memberService = new MemberService(); + } + + public int inputLottoPrice(){ + System.out.println(InputMessage.PURCHASE.getMessage()); + return memberService.getLottoPrice(Console.readLine()); + } + + public List inputWinLottoNumber(){ + System.out.println(InputMessage.WIN_NUMBER.getMessage()); + return memberService.getWinLottoNumber(Console.readLine()); + } + + public int inputBonusNumber(){ + System.out.print(InputMessage.ENTER.getMessage()); + System.out.println(InputMessage.BONUS_NUMBER.getMessage()); + return memberService.getBonusNumber(Console.readLine()); + } +} diff --git a/src/main/java/lotto/member/exception/InputErrorMessage.java b/src/main/java/lotto/member/exception/InputErrorMessage.java new file mode 100644 index 0000000000..1ed83bc56b --- /dev/null +++ b/src/main/java/lotto/member/exception/InputErrorMessage.java @@ -0,0 +1,20 @@ +package lotto.member.exception; + +public enum InputErrorMessage { + THOUSAND("로또 구입 금액은 1,000 단위 입니다."), + NUMBER("로또 구입 금액은 오로지 숫자입니다."), + NOT_DUPLICATE_AND_SIX("로또 번호는 중복되지 않는 6자리 수 입니다."), + IN_NUMBER_RANGE("보너스 번호는 1~45 사이의 숫자입니다."), + IN_WIN_LOTTO_NUMBER("당첨 번호에 포함되어 있는 숫자입니다."); + + private final String message; + private final String error = "[ERROR] "; + + InputErrorMessage(String message) { + this.message = message; + } + + public String getMessage() { + return error + message; + } +} diff --git a/src/main/java/lotto/member/exception/InputValidator.java b/src/main/java/lotto/member/exception/InputValidator.java new file mode 100644 index 0000000000..f560f46314 --- /dev/null +++ b/src/main/java/lotto/member/exception/InputValidator.java @@ -0,0 +1,53 @@ +package lotto.member.exception; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class InputValidator { + + public void validateInputLottoPrice(String lottoPrice){ + validateInputNumber(lottoPrice); + validateInputThousands(lottoPrice); + } + + private static void validateInputThousands(String lottoPrice) { + if(Integer.parseInt(lottoPrice) % LottoRule.PRICE_UNIT.getValue() != 0){ + throw new IllegalArgumentException(InputErrorMessage.THOUSAND.getMessage()); + } + } + + public void validateInputNumber(String number){ + final String regex = "^[0-9]+$"; + if (!number.matches(regex)){ + throw new IllegalArgumentException(InputErrorMessage.NUMBER.getMessage()); + } + } + + public void validateInputLottoNumber(List winLottoNumbers) { + Set finalLottoNumber = winLottoNumbers.stream() + .peek(this::validateNumberInRange) + .collect(Collectors.toSet()); + if (finalLottoNumber.size() != LottoRule.SIZE.getValue()) { + throw new IllegalArgumentException(InputErrorMessage.NOT_DUPLICATE_AND_SIX.getMessage()); + } + } + + public void validateInputBonusNumber(String bonusNumber, List winLottoNumber) { + validateInputNumber(bonusNumber); + validateNumberInRange(Integer.parseInt(bonusNumber)); + validateNumberContainInList(Integer.parseInt(bonusNumber),winLottoNumber); + } + + private void validateNumberInRange(int number){ + if(number < LottoRule.START_NUMBER.getValue() || number > LottoRule.END_NUMBER.getValue()){ + throw new IllegalArgumentException(InputErrorMessage.IN_NUMBER_RANGE.getMessage()); + } + } + + private void validateNumberContainInList(int bonusNumberInt, List winLottoNumber){ + if(winLottoNumber.contains(bonusNumberInt)){ + throw new IllegalArgumentException(InputErrorMessage.IN_WIN_LOTTO_NUMBER.getMessage()); + } + } +} diff --git a/src/main/java/lotto/member/exception/LottoRule.java b/src/main/java/lotto/member/exception/LottoRule.java new file mode 100644 index 0000000000..3f91583486 --- /dev/null +++ b/src/main/java/lotto/member/exception/LottoRule.java @@ -0,0 +1,19 @@ +package lotto.member.exception; + +public enum LottoRule { + + PRICE_UNIT(1000), + SIZE(6), + START_NUMBER(1), + END_NUMBER(45); + + private final int value; + + LottoRule(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} diff --git a/src/main/java/lotto/member/repository/MemberRepository.java b/src/main/java/lotto/member/repository/MemberRepository.java new file mode 100644 index 0000000000..372b120810 --- /dev/null +++ b/src/main/java/lotto/member/repository/MemberRepository.java @@ -0,0 +1,26 @@ +package lotto.member.repository; + +import java.util.ArrayList; +import java.util.List; + +public class MemberRepository { + + private final List winLottoNumber = new ArrayList<>(); + private int bonusNumber; + + public void saveWinLottoNumber(Integer winNumber){ + winLottoNumber.add(winNumber); + } + + public List getWinLottoNumber() { + return winLottoNumber; + } + + public void saveBonusNumber(int bonusNumber) { + this.bonusNumber = bonusNumber; + } + + public int getBonusNumber(){ + return bonusNumber; + } +} diff --git a/src/main/java/lotto/member/service/MemberService.java b/src/main/java/lotto/member/service/MemberService.java new file mode 100644 index 0000000000..1fb1aa9eea --- /dev/null +++ b/src/main/java/lotto/member/service/MemberService.java @@ -0,0 +1,55 @@ +package lotto.member.service; + +import lotto.member.exception.InputValidator; +import lotto.member.repository.MemberRepository; + +import java.util.List; +import java.util.stream.Collectors; + +public class MemberService { + + private final MemberRepository memberRepository; + private final InputValidator inputValidator; + + public MemberService(){ + this.memberRepository = new MemberRepository(); + this.inputValidator = new InputValidator(); + } + + public int getLottoPrice(String lottoPrice){ + return convertLottoPrice(lottoPrice); + } + + private int convertLottoPrice(String lottoPrice){ + inputValidator.validateInputLottoPrice(lottoPrice); + return Integer.parseInt(lottoPrice); + } + + public List getWinLottoNumber(String winNumbers){ + for (Integer winNumber : convertWinLottoNumber(winNumbers)) { + memberRepository.saveWinLottoNumber(winNumber); + } + return memberRepository.getWinLottoNumber(); + } + + private List convertWinLottoNumber(String winNumbers) { + List winLottoNumbers = winNumbers.chars() + .filter(Character::isDigit) + .mapToObj(c -> Character.getNumericValue((char)c)) + .collect(Collectors.toList()); + inputValidator.validateInputLottoNumber(winLottoNumbers); + return winLottoNumbers; + } + + public int getBonusNumber(String bonusNumber) { + int bonusNumberInt = convertBonusNumber(bonusNumber); + memberRepository.saveBonusNumber(bonusNumberInt); + return memberRepository.getBonusNumber(); + } + + private int convertBonusNumber(String bonusNumber){ + List winLottoNumber = memberRepository.getWinLottoNumber(); + inputValidator.validateInputBonusNumber(bonusNumber, winLottoNumber); + return Integer.parseInt(bonusNumber); + } +} diff --git a/src/main/java/lotto/producer/controller/ProducerController.java b/src/main/java/lotto/producer/controller/ProducerController.java new file mode 100644 index 0000000000..27aa778e11 --- /dev/null +++ b/src/main/java/lotto/producer/controller/ProducerController.java @@ -0,0 +1,21 @@ +package lotto.producer.controller; + +import lotto.producer.repository.Lotto; +import lotto.producer.service.ProducerService; + +import java.util.List; + +public class ProducerController { + + private final ProducerService producerService; + + public ProducerController(){ + this.producerService = new ProducerService(); + } + + public List getLottoNumbers(int lottoAmount){ + return producerService.getLottoNumbers(lottoAmount); + } + + +} diff --git a/src/main/java/lotto/producer/repository/Lotto.java b/src/main/java/lotto/producer/repository/Lotto.java new file mode 100644 index 0000000000..d334df5103 --- /dev/null +++ b/src/main/java/lotto/producer/repository/Lotto.java @@ -0,0 +1,25 @@ +package lotto.producer.repository; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validate(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + Set numberCount = new HashSet<>(numbers); + if (numberCount.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 중복되지 않는 6자리 수 입니다."); + } + } + + public List getNumbers() { + return numbers; + } +} diff --git a/src/main/java/lotto/producer/repository/ProducerRepository.java b/src/main/java/lotto/producer/repository/ProducerRepository.java new file mode 100644 index 0000000000..d5f7c7378d --- /dev/null +++ b/src/main/java/lotto/producer/repository/ProducerRepository.java @@ -0,0 +1,20 @@ +package lotto.producer.repository; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.ArrayList; +import java.util.List; + +public class ProducerRepository { + + private final List lottos = new ArrayList<>(); + + public List createLottoNumber(){ + List lottoNumbers = Randoms.pickUniqueNumbersInRange(1, 45, 6); + lottos.add(new Lotto(lottoNumbers)); + return lottoNumbers; + } + + public List getLottos() { + return lottos; + } +} diff --git a/src/main/java/lotto/producer/service/OutputMessage.java b/src/main/java/lotto/producer/service/OutputMessage.java new file mode 100644 index 0000000000..0121e7892a --- /dev/null +++ b/src/main/java/lotto/producer/service/OutputMessage.java @@ -0,0 +1,21 @@ +package lotto.producer.service; + +public enum OutputMessage { + ENTER("\n"), + PURCHASED("개를 구매했습니다."), + FIRST_BRACKET("["), + LAST_BRACKET("]"), + COMMA(","), + BLANK(" "); + + + private final String message; + + OutputMessage(String message) { + this.message = message; + } + + public String getMessage() { + return message; + } +} diff --git a/src/main/java/lotto/producer/service/ProducerService.java b/src/main/java/lotto/producer/service/ProducerService.java new file mode 100644 index 0000000000..a43fc115a1 --- /dev/null +++ b/src/main/java/lotto/producer/service/ProducerService.java @@ -0,0 +1,40 @@ +package lotto.producer.service; + +import lotto.producer.repository.Lotto; +import lotto.producer.repository.ProducerRepository; + +import java.util.List; + +public class ProducerService { + + private final ProducerRepository producerRepository; + + public ProducerService(){ + this.producerRepository = new ProducerRepository(); + } + + public List getLottoNumbers(int lottoAmount){ + System.out.print(OutputMessage.ENTER.getMessage()); + System.out.println(lottoAmount + OutputMessage.PURCHASED.getMessage()); + for (int i = 0; i < lottoAmount; i++){ + List lottoNumber = producerRepository.createLottoNumber(); + System.out.println(resultLottoNumber(lottoNumber)); + } + System.out.print(OutputMessage.ENTER.getMessage()); + return producerRepository.getLottos(); + } + + public String resultLottoNumber(List lottoNumber){ + StringBuilder lottoResult = new StringBuilder(); + lottoResult.append(OutputMessage.FIRST_BRACKET.getMessage()); + for (int i = 0; i < lottoNumber.size()-1; i++){ + lottoResult.append(lottoNumber.get(i)) + .append(OutputMessage.COMMA.getMessage()) + .append(OutputMessage.BLANK.getMessage()); + } + lottoResult.append(lottoNumber.get(lottoNumber.size()-1)); + lottoResult.append(OutputMessage.LAST_BRACKET.getMessage()); + return lottoResult.toString(); + } + +} diff --git a/src/test/java/lotto/InputValidTest.java b/src/test/java/lotto/InputValidTest.java new file mode 100644 index 0000000000..77176c70e3 --- /dev/null +++ b/src/test/java/lotto/InputValidTest.java @@ -0,0 +1,72 @@ +package lotto; + +import lotto.member.repository.MemberRepository; +import lotto.member.service.MemberService; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class InputValidTest { + + MemberService memberService = new MemberService(); + + @Test + @DisplayName("로또 구입 금액이 1,000원 단위가 아니면 예외가 발생한다.") + void inputLottoAmountIsNotThousand(){ + assertThatThrownBy(() -> memberService.getLottoPrice("14500")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("로또 구입 금액이 숫자가 아니면 예외가 발생한다.") + void inputLottoAmountIsNotNumber(){ + assertThatThrownBy(() -> memberService.getLottoPrice("10000j")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("로또 당첨 번호가 중복된 수를 입력하면 예외가 발생한다.") + void inputWinLottoNumberIsDuplicate(){ + assertThatThrownBy(() -> memberService.getWinLottoNumber("1,2,3,4,5,5")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("로또 당첨 번호가 로또 숫자 범위안에 없으면 예외가 발생한다.") + void inputWinLottoNumberNotInRange(){ + assertThatThrownBy(() -> memberService.getWinLottoNumber("1,2,100,3,4,5")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("보너스 번호가 숫자가 아니면 예외가 발생한다.") + void inputBonusNumberIsNotNumber(){ + assertThatThrownBy(() -> memberService.getBonusNumber("q")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("보너스 번호가 로또 번호 범위에 없으면 예외가 발생한다.") + void inputBonusNumberNotInRange(){ + assertThatThrownBy(() -> memberService.getBonusNumber("100")) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + @DisplayName("보너스 번호가 당첨 번호에 포함되어 있으면 예외가 발생한다.") + void inputBonusNumberInWinLottoNumber(){ + MemberRepository memberRepository = new MemberRepository(); + List winLottoNumber = memberService.getWinLottoNumber("1,2,3,4,5,6"); + + for (Integer num : winLottoNumber){ + memberRepository.saveWinLottoNumber(num); + } + + assertThatThrownBy(() -> memberService.getBonusNumber("3")) + .isInstanceOf(IllegalArgumentException.class); + } + +} diff --git a/src/test/java/lotto/LottoStatisticsTest.java b/src/test/java/lotto/LottoStatisticsTest.java new file mode 100644 index 0000000000..c55a48731a --- /dev/null +++ b/src/test/java/lotto/LottoStatisticsTest.java @@ -0,0 +1,102 @@ +package lotto; + +import lotto.lottoManager.repository.LottoManagerRepository; +import lotto.lottoManager.service.LottoStatistics; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; +import java.util.Map; + +class LottoStatisticsTest { + + LottoManagerRepository lottoManagerRepository = new LottoManagerRepository(); + + @DisplayName("로또 번호가 3개 일치하였을 때 개수가 올라가는지 확인한다.") + @Test + void matchLottoNumberCountThree(){ + //given + lottoManagerRepository.upStatisticsMatchCount(List.of(1,2,3,4,5,6), List.of(9,7,3,2,1,10), 45); + Map resultStatistics = lottoManagerRepository.getStatisticsCounter(); + + //when + int matchThirdCount = resultStatistics.get(LottoStatistics.THIRD); + + //then + assertThat(matchThirdCount).isEqualTo(1); + + } + + @DisplayName("로또 번호가 4개 일치하였을 때 개수가 올라가는지 확인한다.") + @Test + void matchLottoNumberCountFour(){ + //given + lottoManagerRepository.upStatisticsMatchCount(List.of(1,2,3,4,5,6), List.of(6,7,3,2,1,10), 45); + Map resultStatistics = lottoManagerRepository.getStatisticsCounter(); + + //when + int matchFourthCount = resultStatistics.get(LottoStatistics.FOURTH); + + //then + assertThat(matchFourthCount).isEqualTo(1); + + } + + @DisplayName("로또 번호가 5개 일치하였을 때 개수가 올라가는지 확인한다.") + @Test + void matchLottoNumberCountFive(){ + //given + lottoManagerRepository.upStatisticsMatchCount(List.of(1,2,3,4,5,6), List.of(1,2,3,4,5,7), 6); + Map resultStatistics = lottoManagerRepository.getStatisticsCounter(); + + //when + int matchFifthCount = resultStatistics.get(LottoStatistics.FIFTH); + + //then + assertThat(matchFifthCount).isEqualTo(1); + + } + + @DisplayName("로또 번호가 5개와 보너스 번호가 일치하였을 때 개수가 올라가는지 확인한다.") + @Test + void matchLottoNumberCountFiveAndBonusNumber(){ + //given + lottoManagerRepository.upStatisticsMatchCount(List.of(1,2,3,4,5,7), List.of(1,2,3,4,5,6), 6); + Map resultStatistics = lottoManagerRepository.getStatisticsCounter(); + + //when + int matchFifthAndBonusCount = resultStatistics.get(LottoStatistics.FIFTH_AND_BONUS); + + //then + assertThat(matchFifthAndBonusCount).isEqualTo(1); + + } + + @DisplayName("로또 번호가 6개 일치하였을 때 개수가 올라가는지 확인한다.") + @Test + void matchLottoNumberCountSix(){ + //given + lottoManagerRepository.upStatisticsMatchCount(List.of(1,2,3,4,5,6), List.of(1,2,3,4,5,6), 45); + Map resultStatistics = lottoManagerRepository.getStatisticsCounter(); + + //when + int matchSixthCount = resultStatistics.get(LottoStatistics.SIXTH); + + //then + assertThat(matchSixthCount).isEqualTo(1); + + } + + @DisplayName("로또 수익률 계산을 확인한다.") + @Test + void calculateLottoRate(){ + lottoManagerRepository.upStatisticsMatchCount(List.of(1,2,3,4,5,6), List.of(9,7,3,2,1,10), 45); + + double lottoRate =lottoManagerRepository.calculateLottoRate(8000); + + assertThat(lottoRate).isEqualTo(62.5); + + } + +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 0f3af0f6c4..af4cecc66d 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,13 +1,24 @@ package lotto; +import lotto.member.repository.MemberRepository; +import lotto.member.service.MemberService; +import lotto.producer.repository.Lotto; +import lotto.producer.repository.ProducerRepository; +import lotto.producer.service.ProducerService; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; class LottoTest { + + ProducerRepository producerRepository = mock(ProducerRepository.class); + @DisplayName("로또 번호의 개수가 6개가 넘어가면 예외가 발생한다.") @Test void createLottoByOverSize() { @@ -24,4 +35,51 @@ void createLottoByDuplicatedNumber() { } // 아래에 추가 테스트 작성 가능 + @DisplayName("로또 번호가 제대로 생성되어 출력되는지 확인한다.") + @Test + void saveLottoNumberInRepository(){ + //given + given(producerRepository.createLottoNumber()).willReturn(List.of(1,2,3,4,5,6)); + ProducerService producerService = new ProducerService(); + + //when + String resultLottoNumber = producerService.resultLottoNumber(List.of(1,2,3,4,5,6)); + + //then + assertThat(resultLottoNumber).isEqualTo("[1, 2, 3, 4, 5, 6]"); + + } + + @DisplayName("로또 당첨번호가 타입이 변경된 후 저장소에 저장되어 반환하는지 확인한다.") + @Test + void saveWinLottoNumberInRepository(){ + //given + MemberService memberService = new MemberService(); + MemberRepository memberRepository = new MemberRepository(); + List winLottoNumber = memberService.getWinLottoNumber("1,2,3,4,5,6"); + + //when + for (Integer num : winLottoNumber){ + memberRepository.saveWinLottoNumber(num); + } + + //then + assertThat(winLottoNumber).isEqualTo(memberRepository.getWinLottoNumber()); + + } + + @DisplayName("보너스 번호가 타입이 변경된 후 저장소에 저장되어 반환되는지 확인한다.") + @Test + void saveBonusNumberInRepository(){ + //given + MemberService memberService = new MemberService(); + MemberRepository memberRepository = new MemberRepository(); + int bonusNumber = memberService.getBonusNumber("7"); + + //when + memberRepository.saveBonusNumber(7); + + //then + assertThat(bonusNumber).isEqualTo(memberRepository.getBonusNumber()); + } }