# [์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค 6๊ธฐ] ํ”„๋ฆฌ์ฝ”์Šค 3์ฃผ์ฐจ ๋กœ๋˜ ๋ฏธ์…˜ ํ›„๊ธฐ
Study Repository

[์šฐ์•„ํ•œํ…Œํฌ์ฝ”์Šค 6๊ธฐ] ํ”„๋ฆฌ์ฝ”์Šค 3์ฃผ์ฐจ ๋กœ๋˜ ๋ฏธ์…˜ ํ›„๊ธฐ

by rlaehddnd0422

3์ฃผ์ฐจ๊ฐ€ ๋งˆ๋ฌด๋ฆฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 2์ฃผ์ฐจ์™€ ๋น„๊ตํ•ด๋ณด์ž๋ฉด ์ฒ˜์Œ ๋ฏธ์…˜์„ ๋ฐ›๊ณ  ์š”๊ตฌ์‚ฌํ•ญ์„ ์ฝ์–ด๋ณด์•˜์„ ๋•Œ ์ˆœ๊ฐ„ ๋งŒํผ์€ ์ฒด๊ฐ ๋‚œ์ด๋„๊ฐ€ ๊ฝค ๋†’๋‹ค๊ณ  ๋Š๊ปด์กŒ๋˜ ๋ฏธ์…˜์ด์—ˆ์Šต๋‹ˆ๋‹ค.

 

3์ฃผ์ฐจ ๋ชฉํ‘œ 

1. ๊ฐ์ฒด ์ง€ํ–ฅ์ ์œผ๋กœ ์„ค๊ณ„ํ•˜๊ธฐ

์ด๋ฒˆ ์ฃผ์ฐจ์—๋Š” ์ง€๋‚œ ์ฃผ์ฐจ์—์„œ ๊ณ ๋ฏผํ–ˆ๋˜ ๊ฒƒ๋“ค์— ๋Œ€ํ•œ ๋‹ต์„ ์ •ํ•˜๊ณ  ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•œ๋งŒํผ ๊ฐ์ฒด์ง€ํ–ฅ์  ๊ฐœ๋…๋“ค์„ ๋งŽ์ด ์‹ ๊ฒฝ์จ์„œ ์ฝ”๋“œ์— ์ ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

 

2. ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์„ ๋ฐ˜๋“œ์‹œ ์ง€ํ‚ค๊ธฐ

2์ฃผ์ฐจ ๊ณตํ†ต ํ”ผ๋“œ๋ฐฑ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. README ์ƒ์„ธํžˆ ์ž‘์„ฑํ•˜๊ธฐ
  2. ๊ธฐ๋Šฅ ๋ชฉ๋ก ์ˆ˜์‹œ๋กœ ์žฌ๊ฒ€ํ†  ๋ฐ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ
  3. ๋งค์ง๋„˜๋ฒ„ ์ƒ์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ
  4. ์„ ์–ธ ์ˆœ์„œ ์ง€ํ‚ค๊ธฐ ( ์ƒ์ˆ˜ - ๋ฉค๋ฒ„๋ณ€์ˆ˜ - ์ƒ์„ฑ์ž - ๋ฉ”์†Œ๋“œ )
  5. ๋ณ€์ˆ˜ ์ด๋ฆ„์— ์ž๋ฃŒํ˜• ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ธฐ
  6. ํ•œ ํ•จ์ˆ˜๋Š” "ํ•œ ๊ฐ€์ง€" ๊ธฐ๋Šฅ๋งŒ ๋‹ด๋‹นํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๊ธฐ
  7. ํ…Œ์ŠคํŠธ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ด์œ ์— ๋Œ€ํ•ด ์ƒ๊ฐํ•ด๋ณด๊ธฐ
  8. ์ž‘์€ ๋‹จ์œ„ -> ํฐ๋‹จ์œ„ ์ˆœ์œผ๋กœ ํ…Œ์ŠคํŠธ ์ž‘์„ฑํ•˜๊ธฐ 

 

์ด๋ฒˆ ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•˜๋ฉด์„œ ํ•ญ์ƒ ์ฝ”๋“œ๋ฅผ ์˜์‹ฌํ•˜๊ณ  ์ ๊ฒ€ํ•˜๋Š” ๊ณผ์ •์„ ํ†ตํ•ด ํ”ผ๋“œ๋ฐฑ์„ ์ง€ํ‚ค๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

3์ฃผ์ฐจ์—์„œ ๋ฐฐ์šด ์ ๋“ค 

1. Enum ํ™œ์šฉํ•˜๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์š”๊ตฌ์‚ฌํ•ญ์— ์ถ”๊ฐ€์ ์œผ๋กœ Enum ์„ ํ™œ์šฉํ•ด๋ณด๋ผ๋Š” ์š”๊ตฌ์‚ฌํ•ญ์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. Enum์„ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•  ์ง€ ๊ณ ๋ฏผ์ด ๋งŽ์•˜์Šต๋‹ˆ๋‹ค.

 

  • ์œ„์™€ ๊ฐ™์ด ์ „์—ญ์ ์œผ๋กœ ์žฌ์‚ฌ์šฉ์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋Š” ์ƒ์ˆ˜๋“ค์€ Enum์œผ๋กœ ์ƒ์ˆ˜๋ฅผ ๊ด€๋ฆฌํ•˜๋„๋ก ํ•˜๊ธด ํ–ˆ์œผ๋‚˜, ์ด ํ•˜๋‚˜๋กœ Enum์„ ํ™œ์šฉํ–ˆ๋‹ค๊ธฐ์—๋Š” ๋ถ€์กฑํ–ˆ์Šต๋‹ˆ๋‹ค.

  • ์–ด๋–ป๊ฒŒ Enum์„ ํ™œ์šฉํ•ด์•ผ ํ•  ์ง€ ๊ณ ๋ฏผํ•˜๋˜ ์ค‘, ๋กœ๋˜์˜ ํ†ต๊ณ„๋ฅผ ์‚ฐ์ถœํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ๋•Œ Enum์„ ์ ๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

  • ์—ฐ๊ด€์„ฑ์ด ์žˆ๋Š” ์ƒ์ˆ˜๋“ค์„ enum์œผ๋กœ ๊ด€๋ฆฌํ•จ๊ณผ ๋™์‹œ์— ์œ„์™€ ๊ฐ™์ด ๋กœ๋˜ ๊ฒฐ๊ณผ๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ EnumMap์œผ๋กœ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.
  • 1๋“ฑ๋ถ€ํ„ฐ 5๋“ฑ๊นŒ์ง€์˜ ๋‹น์ฒจ ์ •๋ณด๋ฅผ Enum์œผ๋กœ ๋งŒ๋“ค๊ณ , Key์— Enum์„, Value์—๋Š” Integer๋ฅผ ๋„ฃ์€ EnumMap์„ ๋งŒ๋“ค์–ด ํ†ต๊ณ„๋ฅผ ์‚ฐ์ถœํ•  ๋•Œ Key๊ฐ’์„ ์ŠคํŠธ๋ฆผํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” Key๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด Value๋ฅผ ํ•˜๋‚˜ ์˜ฌ๋ฆฌ๋Š” ๋ฐฉ์‹์œผ๋กœ ํ†ต๊ณ„๋ฅผ ์‚ฐ์ถœํ•˜์—ฌ Enum์„ ์ €๊ทน์ ์œผ๋กœ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
  • Enum์€ ๋‹จ์ˆœํžˆ ์—ด๊ฑฐํ˜• ์ƒ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์šฉ๋„๋กœ๋งŒ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์— Enum์„ ํ™œ์šฉํ•˜๋‹ˆ Enum ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๋•Œ์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ฏธ์…˜์„ ์ˆ˜์›”ํ•˜๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

2. ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•˜๊ธฐ

์บก์Šํ™”์™€, ๋‹จ์ผ ์ฑ…์ž„์›์น™์—์„œ ์ž…๊ฐํ•œ ๊ฐ์ฒด์„ค๊ณ„์— ์ง‘์ค‘ํ–ˆ์Šต๋‹ˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด ๊ฐ์ฒด๊ฐ€ ์Šค์Šค๋กœ ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์€ ์™ธ๋ถ€์—์„œ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ง๊ณ , ๊ฐ์ฒด ์Šค์Šค๋กœ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ง๋กœ ์„ค๋ช…ํ•˜๋Š”๋ฐ๋Š” ํ•œ๊ณ„๊ฐ€ ์žˆ์–ด ์˜ˆ์‹œ๋ฅผ ํ•˜๋‚˜ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public class Lotto {

    private final List<Integer> numbers;

 

์œ„ ํด๋ž˜์Šค๋Š” ๋กœ๋˜ 1์žฅ์— ๋Œ€ํ•œ ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค. ํ•„๋“œ๋กœ๋Š” ๋กœ๋˜ ๋ฒˆํ˜ธ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ Integer List numbers๋กœ ๋‹ด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ํด๋ž˜์Šค์— ์˜ํ•ด ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋ฅผ ๊ฐ์ฒด์Šค๋Ÿฝ๊ฒŒ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ์š”?

 

๋จผ์ € ์บก์Šํ™” ์ธก๋ฉด์—์„œ ๋ฐ”๋ผ๋ด…์‹œ๋‹ค.

 

numbers ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋Š” ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ด ๋ฒˆํ˜ธ๋ฅผ ๋ณด์—ฌ์ฃผ์–ด์•ผ ํ•œ๋‹ค๊ฑฐ๋‚˜, ๋‹ค๋ฅธ ๋ชจ๋ธ์„ ์ƒ์„ฑํ•  ๋•Œ ์ด ๋ฒˆํ˜ธ๋ฅผ ํ•„์š”๋กœ ํ•œ๋‹ค๊ฑฐ๋‚˜ ๋“ฑ๋“ฑ์˜ ๊ฒฝ์šฐ๋กœ ์™ธ๋ถ€์— ๋ฌผ๋ก  ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ฒˆํ˜ธ ํ•„๋“œ๋ฅผ ์™ธ๋ถ€์—์„œ ์กฐ์ž‘ํ•˜๊ฑฐ๋‚˜ ์™ธ๋ถ€์—์„œ ์ด ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๊ฒฝ์šฐ ์ด ๊ฐ์ฒด์˜ ์บก์Šํ™”๊ฐ€ ๊นจ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

์บก์Šํ™” : ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ์†์„ฑ์ด๋‚˜ ํ–‰์œ„๋ฅผ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด ์™ธ๋ถ€์—์„œ ์ง์ ‘ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๊ฒŒ ํ•˜๋Š” ํ–‰์œ„.

 

์บก์Šํ™”๋ฅผ ์ง€ํ‚ค๊ธฐ ์œ„ํ•ด์„œ๋Š” Tell, Do not Ask ์›์น™์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊บผ๋‚ด์™€์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ๊ฐ์ฒด์—๊ฒŒ ์ฒ˜๋ฆฌํ•  ํ–‰์œ„๋ฅผ ์š”์ฒญํ•˜๋ผ๋Š” ํ–‰์œ„์ž…๋‹ˆ๋‹ค. ์ ์šฉํ•˜์ง€ ์•Š์€ ์˜ˆ์‹œ๋ฅผ ๋จผ์ € ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

<์ ์šฉํ•˜์ง€ ์•Š์€ ์˜ˆ์‹œ>

public class Lotto {

    private final List<Integer> numbers;
    ...
}

public class TestClass {

    public static void main(String[] args) {
        List<Integer> userNumbers = SixRandomLottoNumber.generate(); 
        List<Integer> winningNumbers = input.getNumbers();

        Lotto userLotto = new Lotto(userNumbers);
        Lotto winningLotto = new Lotto(winningNumbers);      

        Integer count = calculateMatchedCount(winningLotto.getNumbers(), userLotto.getNumbers());
    }
    
    private Integer calculateMatchedCount(List<Integer> winningNumbers, List<Integer> userNumbers) {
        Integer count = 0;

        // ๋น„๊ตํ•˜๋Š” ๊ธฐ๋Šฅ ๊ตฌํ˜„ ...


        return count;
    }
}

 

  • ์ฝ”๋“œ๋ฅผ ๋ณด์‹œ๋ฉด, Lotto ๊ฐ์ฒด์˜ ํ•„๋“œ๊ฐ’์„ ๊บผ๋‚ด์™€์„œ ์™ธ๋ถ€์—์„œ ์ผ์น˜ํ•˜๋Š” ๋ฒˆํ˜ธ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด๋ ‡๊ฒŒ ์บก์Šํ™”๊ฐ€ ๊นจ์ง€๊ฒŒ ๋˜๋ฉด ์™ธ๋ถ€์—์„œ ๊ฐ์ฒด๋ฅผ ์˜์กดํ•˜๊ฒŒ ๋˜๋ฏ€๋กœ ์‘์ง‘๋„๊ฐ€ ์˜ฌ๋ผ๊ฐ€๊ณ , ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์ฑ…์ž„์ ธ์•ผ ํ•  ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋˜์–ด ์žˆ์–ด ๊ฐ์ฒด์˜ ์—ญํ• ๊ณผ ์ฑ…์ž„์ด ๊นจ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด๋ฒˆ์—๋Š” ์ด ๋ฌธ์ œ๋ฅผ ๊ฐ์ฒด๊ฐ€ ์Šค์Šค๋กœ ํ•ด๊ฒฐํ•˜๋„๋ก ๋‹ค์‹œ ์ ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

public class Lotto {

    private final List<Integer> numbers;

	... 
    
    public int countMatchedNumbers(final Lotto lotto) {
        return (int) numbers.stream()
            .filter(number -> lotto.contain(number))
            .count();
    }

    public boolean checkBonusNumberContain(final BonusNumber bonusNumber) {
        return this.contain(bonusNumber.number());
    }

    private boolean contain(final Integer number) {
        return numbers.contains(number);
    }
}

 

  • ๊ฐ์ฒด๊ฐ€ ์Šค์Šค๋กœ ์ž์‹ ๊ณผ ๋™์ผํ•œ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ์ˆซ์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด์™€ ๊ฐ™์ด ์บก์Šํ™”๋ฅผ ์ง€ํ‚ค๊ฒŒ ๋˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋™์ž‘ ๋ฐฉ์‹์„ ์™ธ๋ถ€์—์„œ ์•Œ ํ•„์š”๊ฐ€ ์—†๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์™ธ๋ถ€์—์„œ ๋™์ž‘๋ฐฉ์‹์„ ์•Œ ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€์—์„œ๋Š” ์ด ๊ฐ์ฒด์— ๋Œ€ํ•œ ์˜์กด์ด ์‚ฌ๋ผ์ ธ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒฝ์šฐ์—๋„ ์œ ์ง€๋ณด์ˆ˜ ์ธก๋ฉด์—์„œ ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

 getter๋ฅผ ์ง€์–‘ํ•˜๋ผ๋Š” ๋ฉ”์‹œ์ง€์— ์˜์•„ํ•จ์ด ์žˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—์•ผ ๊ทธ ์˜๋ฏธ๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. 

 

3. ์ „๋žต ํŒจํ„ด ์ ์šฉ

์ด๋ฒˆ ๋ฏธ์…˜์—์„œ๋Š” ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž‘์„ฑ์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด ๋‚œ์ˆ˜ ์ƒ์„ฑ ํด๋ž˜์Šค์— ์ „๋žตํŒจํ„ด์„ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. 

 

๋จผ์ € ์ง€๋‚œ ๋ฏธ์…˜์—์„œ์˜ ๋‚œ์ˆ˜ ์ƒ์„ฑ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•œ ํด๋ž˜์Šค๋ฅผ ํ…Œ์ŠคํŠธํ•œ ๋ฐฉ์‹์„ ๋จผ์ € ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

class CarTest {

    private static final Integer START_POINT = 0;
    private static final Integer THRESHOLD = 4;

    private static MockedStatic<RandomNumberGenerator> randomNumberGeneratorMockedStatic;
    private Car car;

    @BeforeAll
    public static void beforeAll() {
        randomNumberGeneratorMockedStatic = mockStatic(RandomNumberGenerator.class);
    }
    
    @DisplayName("๋žœ๋ค ์ˆซ์ž๊ฐ€ ์ž„๊ณ„์น˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ ์ž๋™์ฐจ๋Š” ์ „์ง„ํ•œ๋‹ค.")
    @Test
    void updatePosition_moving_required_test() throws Exception {
        Car car = new Car(new CarName("Car1"));
        given(RandomNumberGenerator.pickNumber()).willReturn(THRESHOLD);

        Integer originalPosition = car.getCarPosition().position();

        for (int i = 1; i <= 1000; i++) {
            car.updatePosition();
            Integer newPosition = car.getCarPosition().position();
            assertTrue(newPosition == (originalPosition + i));
        }
    }
}

 

์ง€๋‚œ ๋ฏธ์…˜์—์„œ๋Š” ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ •์ ํŒฉํ† ๋ฆฌ๋ฉ”์†Œ๋“œ๋กœ ๊ด€๋ฆฌํ•˜๊ณ , ์ด๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์œ„ํ•ด MockedStatic ํ…Œ์ŠคํŠธ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ์š”. ๋ฌผ๋ก  ์ด ๋ฐฉ๋ฒ•๋„ ๋‚˜์œ ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•  ์ˆ˜๋Š” ์—†์ง€๋งŒ,  ๋ชจ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ๋™์ž‘์„ ํ†ต์ œํ•˜๋Š” ๋ฐฉ์‹์€ ํ…Œ์ŠคํŠธํ•˜๊ธฐ์— ์œ ์—ฐํ•˜์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค. 

 

๋”ฐ๋ผ์„œ ์ด๋ฒˆ ๋ฏธ์…˜์—์„œ๋Š” ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๋ฅผ ํ™•์žฅํ•œ ์ „๋žตํŒจํ„ด์„ ์‚ฌ์šฉํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ด…์‹œ๋‹ค.

 

public interface LottoNumberGenerator {

    List<Integer> pickNumbers();
}
  • ์ „๋žต ํŒจํ„ด์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋‚œ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ํด๋ž˜์Šค๋ฅผ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ๋‘๊ณ ,
public class RandomLottoNumberGenerator implements LottoNumberGenerator {

    @Override
    public List<Integer> pickNumbers() {
        List<Integer> numbers = Randoms.pickUniqueNumbersInRange(
                LOTTO_MIN_NUMBER.getValue(),
                LOTTO_MAX_NUMBER.getValue(),
                LOTTO_SIZE.getValue());

        return numbers;
    }
}
private Lotteries initLotteries(final TicketCount ticketCount) {
    return Lotteries.createLotteries(ticketCount, new RandomLottoNumberGenerator());
}
  • ์œ„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ๋‹คํ˜•์„ฑ์„ ์ด์šฉํ•˜์—ฌ RandomNumberGenerator์˜ pickNumbers๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.  
  • ์ด๋ ‡๊ฒŒ ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๋ฅผ ํ™•์žฅํ•˜๊ฒŒ ๋˜๋ฉด ํ…Œ์ŠคํŠธ ์šฉ์ด์„ฑ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋ฐ”๋€Œ์—ˆ์„ ๋•Œ ์œ ์—ฐํ•˜๊ฒŒ ๊ฐˆ์•„๋ผ์šธ ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

class LotteriesTest {

    LottoNumberGenerator numberGenerator;
    Lotteries lotteries;

    @BeforeEach
    void init() {
        numberGenerator = new LottoNumberGenerator() {
            @Override
            public List<Integer> pickNumbers() {
                return List.of(1, 2, 3, 4, 5, 6);
            }
        };
        TicketCount ticketCount = new TicketCount(3);
        lotteries = Lotteries.createLotteries(ticketCount, numberGenerator);
    }

    @DisplayName("ํ”Œ๋ ˆ์ด์–ด์˜ ๋กœ๋˜๋ฒˆํ˜ธ ์ƒ์„ฑ์ž ํ…Œ์ŠคํŠธ")
    @Test
    void createLotteries_test() {
        // given
        TicketCount ticketCount = new TicketCount(3);

        // when
        Lotteries lotteries = Lotteries.createLotteries(ticketCount, numberGenerator);

        // then
        int expectedSize = ticketCount.count();
        int actualSize = lotteries.getLotteries().size();
        assertEquals(expectedSize, actualSize);

        lotteries.getLotteries().forEach(lotto -> {
            List<Integer> numbers = lotto.getNumbers();
            assertTrue(numbers.containsAll(numberGenerator.pickNumbers()));
        });
    }

 

  • LottoNumberGenerator๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•ด์ฃผ๊ณ  pickNumbers๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•จ์œผ๋กœ์จ, ๋™์ž‘์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
  • ์ด์ฒ˜๋Ÿผ ์ „๋žต ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ํ”„๋กœ๋•์…˜ ์ฝ”๋“œ๋ฅผ ‘์ˆ˜์ •’ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ‘ํ™•์žฅ’ํ•˜๋Š” ๊ฒƒ์ด์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธ๋ฅผ ์šฉ์ดํ•˜๊ฒŒ ํ•  ๋•Œ ์ข‹์€ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค.
  • ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, ๋ชจ์˜ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์ œ์–ดํ•œ ๋™์ž‘์„ ๊ตฌํ˜„ํ•œ ๋ฉ”์†Œ๋“œ์˜ ๋‚ด์šฉ์„ ํ†ตํ•ด ๋ช…์‹œ์ ์œผ๋กœ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ…Œ์ŠคํŠธ๊ฐ€ ๋”์šฑ ๋ช…ํ™•ํ•ด์กŒ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

4. ์ƒ์„ฑ์ž ๋Œ€์‹  ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ์ ์šฉ

public class Lotto {

    private final List<Integer> numbers;

    public Lotto(final List<Integer> numbers) {
        validateLottoNumbers(numbers);
        this.numbers = numbers;
    }

    public static Lotto createWinningLotto(final List<Integer> winningNumbers) {
        return new Lotto(winningNumbers);
    }

    public static Lotto createLotto(final LottoNumberGenerator lottoNumberGenerator) {
        return new Lotto(createNumbersWithGenerator(lottoNumberGenerator));
    }
  • ์š”๊ตฌ์‚ฌํ•ญ์— ๋”ฐ๋ผ Lotto ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ public์œผ๋กœ ์—ด์–ด์ฃผ๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๊ณ ๋Š” ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์ž๋ฅผ ๋Œ€์‹ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
public class Lotteries {

    private final List<Lotto> lotteries;

    private Lotteries(final List<Lotto> lotteries) {
        this.lotteries = lotteries;
    }

    public static Lotteries createLotteries(final TicketCount ticketCount,
                                            final LottoNumberGenerator lottoNumberGenerator) {
        List<Lotto> lotteries = IntStream.range(0, ticketCount.count())
                .mapToObj(eachTicket -> Lotto.createLotto(lottoNumberGenerator))
                .toList();

        return new Lotteries(lotteries);
    }

 

public class Statistic {

    private static final Integer DEFAULT_COUNT = 0;
    private static final Integer FIVE_COUNT = 5;
    private static final Integer COUNT = 1;

    private final Map<LottoRank, Integer> result;

    private Statistic(final Map<LottoRank, Integer> result) {
        this.result = result;
    }

    public static Statistic createStatistic(final List<Integer> matchedNumberCount,
                                            final List<Boolean> containBonusNumber) {
        return new Statistic(compileStatistic(matchedNumberCount, containBonusNumber));
    }

 

public class ProfitRate {

    private static final Integer PERCENTAGE = 100;

    private final Double rate;

    private ProfitRate(final Double rate) {
        this.rate = rate;
    }

    public static ProfitRate createProfitRate(final Map<LottoRank, Integer> result, final BuyAmount buyAmount) {
        return new ProfitRate(calculateProfitRate(result, buyAmount));
    }
  • ๋ฌด์—‡๋ณด๋‹ค ๋กœ๋˜ ํด๋ž˜์Šค์˜ ๊ฒฝ์šฐ ๋ฉ”์†Œ๋“œ์— ๋ช…ํ™•ํ•œ ๋„ค์ด๋ฐ์„ ์„ค์ •ํ•˜์—ฌ ์ด๋ฆ„์„ ๊ฐ€์ง„ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ฆ์œผ๋กœ์จ ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ์„ ๋”์šฑ ์ฆ๊ฐ€์‹œํ‚ฌ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
  • ๋‹น์ฒจ๋ฒˆํ˜ธ์˜ ๋กœ๋˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒ์„ฑ์ž์™€ ํ”Œ๋ ˆ์ด์–ด์˜ ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ธ์ž๋ฅผ ๋‹ค๋ฅด๊ฒŒ ํ•˜์—ฌ ๋‹น์ฒจ๋ฒˆํ˜ธ์— ๋Œ€ํ•œ ๋กœ๋˜ ํด๋ž˜์Šค์™€, ํ”Œ๋ ˆ์ด์–ด์˜ ๋กœ๋˜์— ๋Œ€ํ•œ ํด๋ž˜์Šค๋ฅผ ๋ถ„๋ฆฌํ•˜์ง€ ์•Š๊ณ  ๋กœ๋˜ ๋ฒˆํ˜ธ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹๋งŒ ๋ณ€๊ฒฝํ•˜์—ฌ ํด๋ž˜์Šค๋ฅผ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
  • ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์†Œ๋“œ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค์˜ ์žฌ์‚ฌ์šฉ์„ ํ†ตํ•ด ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ์˜ ์ค‘๋ณต์„ ๋ง‰๊ณ , ๋‚œ์ˆ˜ ์ƒ์„ฑ ํด๋ž˜์Šค์— ์˜ํ•ด ์ƒ์„ฑ๋œ ๋‹น์ฒจ๋ฒˆํ˜ธ ๋˜ํ•œ ๊ฒ€์ฆ์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ฝ”๋“œ์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฒ„๊ทธ์™€ ์˜ค๋ฅ˜์˜ ๊ฐ€๋Šฅ์„ฑ์„ ์ค„์ผ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

์ด ์™ธ์—๋„ ์ด๋ฒˆ ๋ฏธ์…˜์—์„œ๋Š” ๊ฒ€์ฆ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ๋“ค์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ , final ํ‚ค์›Œ๋“œ์™€ UnmodifiableXXX ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๊ฐ€ ํ•˜๋‚˜์˜ ์—ญํ• ์„ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๋“ฑ ๊น”๋”ํ•˜๊ณ  ๊ตฌ์ฒด์ ์ด๋ฉด์„œ, ์ •๋ˆ๋œ ๋Š๋‚Œ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋ ค๊ณ  ๋งŽ์ด ๋…ธ๋ ฅํ–ˆ๋˜ ๋ฏธ์…˜์ด์—ˆ์Šต๋‹ˆ๋‹ค.


3์ฃผ์ฐจ ์ „์ฒด ์†Œ๊ฐ

์ข‹์€ ์ฝ”๋“œ, ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด ๋งŽ์ด ๊ณ ๋ฏผํ–ˆ๋˜ ํ•œ ์ฃผ์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋งŒํผ ์ด๋ฒˆ ๋ฏธ์…˜์€ ๋ฐฐ์›Œ๊ฐ€๋Š” ์ ๋“ค์ด ๋”์šฑ ๋งŽ์•˜๋˜ ๋ฏธ์…˜์ด์—ˆ์Šต๋‹ˆ๋‹ค. ๋ฏธ์…˜์„ ์ง„ํ–‰ํ•  ๋•Œ๋Š” ๋‹ค์†Œ ์–ด๋ ค์› ๋‹ค๊ณ  ์ฒด๊ฐํ–ˆ์—ˆ๋Š”๋ฐ, ๋ฆฌํŽ™ํ† ๋ง ๊ณผ์ •๊นŒ์ง€ ๋๋‚ด๊ณ  ์ œ์ถœ๋ฌธ์„ ์“ฐ๋Š” ์‹œ์ ์—์„œ ์ด๋ฒˆ ๋ฏธ์…˜์„ ๋Œ์•„๋ณด์•˜์„ ๋•Œ๋Š” 1,2์ฃผ์ฐจ ๋ฏธ์…˜๊ณผ ๋น„์Šทํ•œ ๋‚œ์ด๋„๋ผ๊ณ  ๋Š๊ปด์กŒ์Šต๋‹ˆ๋‹ค. ๊ทธ๋งŒํผ ์ด๋ฒˆ ๋ฏธ์…˜์„ ์™„์„ฑํ•ด ๋‚˜์•„๊ฐ€๋Š” ๊ณผ์ •์—์„œ 1, 2์ฃผ์ฐจ์™€ ๋น„๊ตํ•˜์˜€์„ ๋•Œ ์ฝ”๋“œ๋ฅผ ๋ณด๋Š” ์‹œ์•ผ๊ฐ€ ๋งŽ์ด ๋„“์–ด์กŒ๊ณ , ๊ฐ์ฒด์ง€ํ–ฅ์ ์ธ ๊ฐœ๋…๋“ค์ด ์กฐ๊ธˆ์”ฉ์ด๋‚˜๋งˆ ์ •๋ฆฝ๋˜์—ˆ๋˜ ํ•œ ์ฃผ์˜€์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ ์ฃผ๋ฉด ๋ฒŒ์จ ๋งˆ์ง€๋ง‰ ๋ฏธ์…˜์ธ๋ฐ ๋งˆ์ง€๋ง‰๊นŒ์ง€ ์—ด์‹ฌํžˆ ํ•ด์„œ ๊ฒฐ์‹ค์„ ์ž˜ ๋งบ์—ˆ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค.


๋ฏธ์…˜ ๋งํฌ

๋ฏธ์…˜์— ๋Œ€ํ•œ ์ œ ์ฝ”๋“œ๊ฐ€ ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

 

GitHub - Dongwoongkim/java-lotto-6

Contribute to Dongwoongkim/java-lotto-6 development by creating an account on GitHub.

github.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ