자바/초록스터디

(8)초록스터디 로또 3,4 단계 구현

문상휘파람 2024. 7. 15. 17:33

진도가 조금 늦어서 코드 리뷰 받음과 동시에 3,4 단계 구현까지 해보았다.

 

 

*Lotto

package domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Lotto {
    private final List<Integer> lottoNumber;

    public Lotto(CreateLottoNumber createLottoNumber) {
        this.lottoNumber = makeLotto(createLottoNumber);
    }

    public List<Integer> getLottoNumber() {
        return lottoNumber;
    }

    private List<Integer> makeLotto(CreateLottoNumber createLottoNumber) {
        List<Integer> singleLotto = new ArrayList<>(createLottoNumber.getRandomLottoNumber());
        sortLottoNumber(singleLotto);
        return singleLotto;
    }

    private void sortLottoNumber(List<Integer> lottoNumber) {
        Collections.sort(lottoNumber);
    }
}

 

*LottoNumberGenerator

package domain;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LottoNumberGenerator implements CreateLottoNumber {

    private static final int INITIAL_NUMBER = 0;
    private static final int FIRST_LOTTO_NUMBER = 1;
    private static final int LAST_LOTTO_NUMBER = 45;
    private static final int LOTTO_NUMBER_LENGTH_BOUNDARY = 6;
    private static final Random randomNumberGenerator = new Random();

    @Override
    public List<Integer> getRandomLottoNumber() {
        List<Integer> getLotto = new ArrayList<>();
        for (int i = INITIAL_NUMBER; i < LOTTO_NUMBER_LENGTH_BOUNDARY; i++) {
            int randomLottoNumber = checkDuplicateNumber(getLotto, randomNumberGenerator.nextInt(FIRST_LOTTO_NUMBER, LAST_LOTTO_NUMBER));
            getLotto.add(randomLottoNumber);
        }
        return getLotto;
    }

    private int generateRandomNumber() {
        return randomNumberGenerator.nextInt(FIRST_LOTTO_NUMBER, LAST_LOTTO_NUMBER);
    }

    private int checkDuplicateNumber(List<Integer> buyLotto, int randomLottoNumber) {
        if (buyLotto.contains(randomLottoNumber)) {
            return checkDuplicateNumber(buyLotto, generateRandomNumber());
        }
        return randomLottoNumber;
    }
}

 

*LottoPrice (enum 클래스)

package domain;

import java.util.ArrayList;
import java.util.List;

public enum LottoPrice {

    THREE("3", 5000, "3개 일치 (5000원)- "),
    FOUR("4", 50000, "4개 일치 (50000원)- "),
    FIVE("5", 1500000, "5개 일치 (1500000원)- "),
    BONUS("5BONUS", 30000000, "5개 일치, 보너스 볼 일치(30000000)- "),
    SIX("6", 2000000000, "6개 일치 (2000000000원)- ");


    private static final String BONUS_SAME_LOTTO_NUMBER = "5BONUS";
    private final String sameLottoNumber;
    private final int lottoPrice;
    private final String lottoMessage;

    LottoPrice(String sameLottoNumber, int lottoPrice, String lottoMessage) {
        this.sameLottoNumber = sameLottoNumber;
        this.lottoPrice = lottoPrice;
        this.lottoMessage = lottoMessage;
    }

    public static List<String> getLottoMessageBundle() {
        List<String> lottoMessages = new ArrayList<>();
        for (LottoPrice lottoPrice : LottoPrice.values()) {
            lottoMessages.add(lottoPrice.getLottoMessege());
        }
        return lottoMessages;
    }

    public static List<Integer> getLottoPriceBundle() {
        List<Integer> lottoPrices = new ArrayList<>();
        for (LottoPrice lottoPrice : LottoPrice.values()) {
            lottoPrices.add(lottoPrice.getLottoPrice());
        }
        return lottoPrices;
    }

    public static List<String> getSameLottoNumberBundle() {
        List<String> sameLottoNumbers = new ArrayList<>();
        for (LottoPrice lottoPrice : LottoPrice.values()) {
            sameLottoNumbers.add(lottoPrice.getSameLottoNumber());
        }
        return sameLottoNumbers;
    }

    public static String getBonusSameLottoNumber(){
        for(LottoPrice lottoPrice : LottoPrice.values()){
            if(BONUS_SAME_LOTTO_NUMBER.equals(lottoPrice.getSameLottoNumber())){
                return lottoPrice.getSameLottoNumber();
            }
        }
        return null;
    }

    private String getSameLottoNumber() {
        return sameLottoNumber;
    }

    private String getLottoMessege() {
        return lottoMessage;
    }

    private int getLottoPrice() {
        return lottoPrice;
    }
}

 

*LottoRank

package domain;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LottoRank {

    private static final int RESET_NUMBER = 0;
    private static final int INITIAL_NUMBER = 1;
    private static final int BONUS_COUNT = 5;

    private final Map<String, Integer> lottoRank;

    public LottoRank(Lottos lottos, List<Integer> lastWeekLottoNumber) {
        this.lottoRank = makeLottoRank(lottos, lastWeekLottoNumber);
    }

    public Map<String, Integer> getLottoRank() {
        return lottoRank;
    }

    private Map<String, Integer> makeLottoRank(Lottos lottos, List<Integer> lastWeekLottoNumber) {
        Map<String, Integer> rankLotto = checkCorrespondingLottosNumber(lottos, lastWeekLottoNumber);
        List<String> sameLottoNumbers = LottoPrice.getSameLottoNumberBundle();
        for (String sameLottoNumber : sameLottoNumbers) {
            rankLotto.put(sameLottoNumber, rankLotto.getOrDefault(sameLottoNumber, RESET_NUMBER) + RESET_NUMBER);
        }
        return rankLotto;
    }

    private Map<String, Integer> checkCorrespondingLottosNumber(Lottos lottos, List<Integer> lastWeekLottoNumber) {
        Map<String, Integer> rankLotto = new HashMap<>();
        for (Lotto lotto : lottos.getLottos()) {
            String sameLottoNumber = checkCorrespondingLottoNumber(lotto.getLottoNumber(), lastWeekLottoNumber);
            rankLotto.put(sameLottoNumber, rankLotto.getOrDefault(sameLottoNumber, RESET_NUMBER) + INITIAL_NUMBER);
        }
        return rankLotto;
    }

    private String checkCorrespondingLottoNumber(List<Integer> lottoNumber, List<Integer> lastWeekLottoNumber) {
        int sameLottoNumber = RESET_NUMBER;
        for (int elementOfLastWeekLottoNumber : lastWeekLottoNumber) {
            if (lottoNumber.contains(elementOfLastWeekLottoNumber)) {
                sameLottoNumber++;
            }
        }
        if (checkBonusNumber(lottoNumber, lastWeekLottoNumber, sameLottoNumber)) {
            return LottoPrice.getBonusSameLottoNumber();
        }
        return Integer.toString(sameLottoNumber);
    }

    private boolean checkBonusNumber(List<Integer> lottoNumber, List<Integer> lastWeekLottoNumber, int sameLottoNumber) {
        return lottoNumber.contains(lastWeekLottoNumber.get(lastWeekLottoNumber.size() - INITIAL_NUMBER)) && sameLottoNumber == BONUS_COUNT;
    }
}

 

*LottoReturnRate

package domain;

import java.util.List;
import java.util.Map;

public class LottoReturnRate {

    private static final int RESET_NUMBER = 0;
    private static final double MAKE_RETURN_RATE_DEVIDE_NUMBER = 100.0;
    private static final int MAKE_RETURN_RATE_MULTIPLE_NUMBER = 100;

    private final double lottoReturnRate;

    public LottoReturnRate(Map<String, Integer> lottoRank, int getLottoMoney) {
        this.lottoReturnRate = calculateLottoReturnRate(lottoRank, getLottoMoney);
    }

    public double getLottoReturnRate() {
        return lottoReturnRate;
    }

    private double calculateLottoReturnRate(Map<String, Integer> lottoRank, int getLottoMoney) {
        List<Integer> lottoRankPrice = LottoPrice.getLottoPriceBundle();
        List<String> sameLottoNumbers = LottoPrice.getSameLottoNumberBundle();
        int sumOfLottoMoney = RESET_NUMBER;
        int count = RESET_NUMBER;
        for (String sameLottoNumber : sameLottoNumbers) {
            sumOfLottoMoney += lottoRank.get(sameLottoNumber) * lottoRankPrice.get(count);
            count++;
        }
        double lottoReturnRate = (double) sumOfLottoMoney / getLottoMoney;
        return Math.floor(lottoReturnRate * MAKE_RETURN_RATE_MULTIPLE_NUMBER) / MAKE_RETURN_RATE_DEVIDE_NUMBER;
    }
}

 

*Lottos

package domain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Lottos {

    private static final int INITIAL_NUMBER = 0;
    private static final int LOTTO_COUNT_NUMBER = 1000;

    private final List<Lotto> lottos;

    public Lottos(CreateLottoNumber createLottoNumber, int lottoMoney) {
        this.lottos = makeLottos(createLottoNumber, lottoMoney);
    }

    public List<Lotto> getLottos() {
        return lottos;
    }

    private List<Lotto> makeLottos(CreateLottoNumber createLottoNumber, int lottoMoney) {
        List<Lotto> lottosBundle = new ArrayList<>();
        for (int i = INITIAL_NUMBER; i < lottoMoney / LOTTO_COUNT_NUMBER; i++) {
            Lotto lotto = new Lotto(createLottoNumber);
            lottosBundle.add(lotto);
        }
        return lottosBundle;
    }
}

 

*OutputView

package view;

import java.util.List;
import java.util.Map;

public class OutputView {

    private static final int RESET_NUMBER = 0;
    private static final int DEVIDE_LOTTO_COUNT_NUMBER = 1000;

    public void printGetLottoMoney() {
        System.out.println("구입금액을 입력해 주세요.");
    }

    public void printLottoCount(int lottoCount) {
        System.out.println("\n" + lottoCount / DEVIDE_LOTTO_COUNT_NUMBER + "개를 구매했습니다.");
    }

    public void printLotto(List<Integer> lottoNumber) {
        System.out.println(lottoNumber);
    }

    public void LastWeekLottoNumber() {
        System.out.println("\n" + "지난 주 당첨 번호를 입력해 주세요.");
    }

    public void inputBonusBall() {
        System.out.println("\n보너스 볼을 입력해주세요.");
    }

    public void printLottoStatistics() {
        System.out.println("\n당첨 통계");
        System.out.println("---------");
    }

    public void printLottoRank(List<String> lottoRankMessage, Map<String, Integer> lottoRank, List<String> sameLottoNumbers) {
        int count = RESET_NUMBER;
        for (String sameLottoNumber : sameLottoNumbers) {
            System.out.println(lottoRankMessage.get(count) + lottoRank.get(sameLottoNumber) + "개");
            count++;
        }
    }

    public void printRateOfReturn(double rateOfReturn) {
        System.out.println("총 수익률은 " + rateOfReturn + "입니다.");
    }
}

 

*InputView

package view;

import java.util.Scanner;

public class InputView {

    private final Scanner input = new Scanner(System.in);

    public int getLottoMoney() {
        return input.nextInt();
    }

    public String inputLastWeekLottoNumber() {
        return input.next();
    }

    public int inputBonusBall() {
        return input.nextInt();
    }
}

 

*LastWeekLottoNumber

package domain;

import java.util.ArrayList;
import java.util.List;

public class LastWeekLottoNumber {

    private static final String SPLIT_STRING_DELIMITER = ",";

    private final List<Integer> lastWeekLottoNumber;

    public LastWeekLottoNumber(String LastWeekLottoNumber, int bonusBall) {
        this.lastWeekLottoNumber = makeLastWeekLottoNumberList(LastWeekLottoNumber, bonusBall);
    }

    public List<Integer> getLastWeekLottoNumber() {
        return lastWeekLottoNumber;
    }

    private List<Integer> makeLastWeekLottoNumberList(String inputLastWeekLottoNumber, int bonusBall) {
        List<String> inputLottoNumber = splitStringLottoNumber(inputLastWeekLottoNumber);
        List<Integer> lastWeekLottoNumber = new ArrayList<>();
        for (String lottoNumber : inputLottoNumber) {
            lastWeekLottoNumber.add(Integer.parseInt(lottoNumber));
        }
        lastWeekLottoNumber.add(bonusBall);
        return lastWeekLottoNumber;
    }

    private List<String> splitStringLottoNumber(String inputLastWeekLottoNumber) {
        String[] lottoNumber = inputLastWeekLottoNumber.split(SPLIT_STRING_DELIMITER);
        return List.of(lottoNumber);
    }
}

 

일단 구현은 했는데, 당연히 코드 리뷰 받으면 고칠 부분이 많을 것 같다.... 풀리퀘 땡기고 코드리뷰 받은 모습으로 돌아와볼게요!!