Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/main/java/lotto/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package lotto;

import java.util.*;
import java.util.stream.Collectors;

public class Game {
public static final int LOTTO_PRICE = 1000;

private final List<Lotto> lottos;
private final Map<Integer, Integer> map;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

굳이 인스턴스 변수로 구현할 필요가 있나?


public Game(int money) {
this.lottos = LottoFactory.create(money / LOTTO_PRICE);
this.map = initMap();
}

private Map<Integer, Integer> initMap() {
Map<Integer, Integer> map = new HashMap<>();
map.put(3, 0);
map.put(4, 0);
map.put(5, 0);
map.put(6, 0);
return map;
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

클래스의 구현 순서는 다음과 같다. 다음 원칙에 따라 구현한다.

class A {
    상수(static final) 또는 클래스 변수
    인스턴스 변수
    생성자
    메소드
}


public Game(Lotto lotto) {
this.lottos = Arrays.asList(lotto);
this.map = initMap();
}

public Map<Integer, Integer> match(String s) {
return winningList(getIntegers(s));
}

private Map<Integer, Integer> winningList(List<Integer> list) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

List<Integer> list도 로또로 볼 수 있지 않을까?

for (Lotto lotto : lottos) {
Integer num = lotto.existCount(list);
if(num > 2)
this.addWins(num);
}
return this.map;
}

static List<Integer> getIntegers(String numString) {
return Arrays.stream(numString.split(","))
.map(t -> Integer.parseInt(t.trim()))
.collect(Collectors.toList());
}

private void addWins(Integer num) {
addWins(num, this.map);
}

static void addWins(Integer i, Map<Integer, Integer> map) {
synchronized (map) {
Integer value = map.getOrDefault(i, 0);
map.put(i, ++value);
}
}

public List<Lotto> getLottos() {
return this.lottos;
}
}
18 changes: 18 additions & 0 deletions src/main/java/lotto/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lotto;

import java.util.Scanner;

public class InputView {
public static Integer getMoney() {
Scanner s = new Scanner(System.in);
System.out.println("구매금액을 입력해주세요.");
return s.nextInt();
}


public static String getWinsNumber() {
Scanner s = new Scanner(System.in);
System.out.println("당첨 번호를 입력하세요.");
return s.nextLine();
}
}
44 changes: 44 additions & 0 deletions src/main/java/lotto/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package lotto;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Lotto {
private List<Integer> numbers;

public Lotto() {
this(selectNumber(createNumber()));
}

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

static List<Integer> sorted(List<Integer> numbers) {
Collections.sort(numbers);
return numbers;
}

public static List<Integer> createNumber() {
List<Integer> lotto = IntStream.rangeClosed(1, 45).boxed().collect(Collectors.toList());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lotto를 생성할 때마다 매번 45개의 값을 생성할 필요가 있을까?
45개의 값 하나를 생성해 놓고 재사용할 수 없을까?

Collections.shuffle(lotto);
return lotto;
}

public static List<Integer> selectNumber(List<Integer> numbers) {
return numbers.subList(0, 6);
}

public List<Integer> getNumbers() {
return this.numbers;
}

public Integer existCount(List<Integer> winnerNumbers) {
return winnerNumbers.stream()
.filter(t -> numbers.contains(t))
.collect(Collectors.toList())
.size();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.collect(Collectors.toList()) 사용하지 말고 filter().count() 가능하지 않나?

}
}
14 changes: 14 additions & 0 deletions src/main/java/lotto/LottoFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lotto;

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

public class LottoFactory {
public static List<Lotto> create(int size) {
List<Lotto> lottos = new ArrayList<>();
for (int i = 0; i < size; i++) {
lottos.add(new Lotto());
}
return lottos;
}
}
22 changes: 22 additions & 0 deletions src/main/java/lotto/Play.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lotto;

import java.util.Map;

public class Play {
public static void main(String[] args) {

Integer money = InputView.getMoney();
Game g = new Game(money);
ResultView.printLottos(g.getLottos());

Map<Integer, Integer> map = g.match(InputView.getWinsNumber());
ResultView.printStatistics(map);

int revenue = map.entrySet().stream()
.mapToInt(e -> e.getValue() * WinPrize.getPrize(e.getKey()))
.sum();

ResultView.printProfit(revenue, money);

}
}
23 changes: 23 additions & 0 deletions src/main/java/lotto/ResultView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package lotto;

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

public class ResultView {
public static void printStatistics(Map<Integer, Integer> map) {
for (Map.Entry<Integer, Integer> e : map.entrySet()) {
System.out.println(e.getKey() + "개 일치 (" + WinPrize.getPrize(e.getKey()) + ") - " + e.getValue() + "개");
}
}

public static void printProfit(int sum, int money) {
System.out.println(String.format("총 수익률은 %.2f%%입니다.", (float) sum/money*100));
}

public static void printLottos(List<Lotto> lottos) {
System.out.println(lottos.size()+ "개를 구매했습니다.");
for (Lotto lotto : lottos) {
System.out.println(lotto.getNumbers());
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/lotto/WinPrize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lotto;

public enum WinPrize {
_3(5000), _4(50000), _5(1500000), _6(2000000000);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enum은 "_"를 사용하기 보다 대문자로 사용하는 것이 일반적임
FIRST와 같이 등수를 사용하면 어떨까?

Copy link
Copy Markdown

@ryuneeee ryuneeee Jul 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

처음에 저희도 일반적으로 values()로 Loop을 도는것을 생각을 했는데요.
별도의 Loop을 돌지 않는 간결한 코드, O(N) -> O(1)로 Time Complexity를 줄인다는 목적(사실 속도면에서 의미 있을까 싶지만)으로 이렇게 설계를 진행 했습니다만, 다시보니 valueOf에서 String을 새롭게 만드는 작업이 더 오래걸릴꺼 같군요 ㅠ.ㅠ


private final int value;

WinPrize(int prize) {
this.value = prize;
}

public static Integer getPrize(Integer i) {
return valueOf("_" + i).value;
}
}
41 changes: 41 additions & 0 deletions src/test/java/lotto/GameTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lotto;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class GameTest {

@Test
public void getWinnings() {
Game g = new Game(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6)));
Map<Integer, Integer> map = g.match("4, 5, 6, 7, 8, 9");
assertEquals(new Integer(1), map.get(3));
}

@Test
public void getEmptyWinnings() {
Game g = new Game(new Lotto(Arrays.asList(1, 2, 3, 4, 5, 6)));
Map<Integer, Integer> map = g.match("10, 11, 23, 7, 8, 9");
assertEquals(4, map.size());
}

@Test
public void getIntegers() {
assertThat(Game.getIntegers("1, 2, 3, 4, 5, 6"))
.containsExactly(1, 2, 3, 4, 5, 6);
}

@Test
public void countingWins() {
Map<Integer, Integer> map = new HashMap<>();
Game.addWins(3, map);
assertEquals(new Integer(1), map.get(3));
}

}
16 changes: 16 additions & 0 deletions src/test/java/lotto/LottoFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package lotto;

import org.junit.Test;

import java.util.List;

import static junit.framework.TestCase.assertEquals;

public class LottoFactoryTest {

@Test
public void createLotto() {
List<Lotto> lottos = LottoFactory.create(14);
assertEquals(14, lottos.size());
}
}
57 changes: 57 additions & 0 deletions src/test/java/lotto/LottoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lotto;

import org.junit.Test;

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

import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

public class LottoTest {

@Test
public void lottoSize() {
Lotto lotto = new Lotto();
assertEquals(6, lotto.getNumbers().size());
}

@Test
public void createNumber() {
List<Integer> nums = Lotto.createNumber();
for (Integer num : nums) {
assertTrue(num > 0 && num < 46);
}
}

@Test
public void selectNumber() {
List<Integer> numbers = Lotto.createNumber();
List<Integer> subNumbers = Lotto.selectNumber(numbers);
assertEquals(6, subNumbers.size());
}

@Test
public void sorting() {
List<Integer> numbers = Arrays.asList(1, 2, 30, 10, 20, 3);
List<Integer> cloned = new ArrayList<>(numbers);
assertThat(Lotto.sorted(numbers)).doesNotContainSubsequence(cloned);
}

@Test
public void checkExistCount() {
Lotto lotto = new Lotto(Arrays.asList(1, 2, 3, 10, 20, 30));
List<Integer> winnerNumbers = Arrays.asList(1, 2, 3, 20, 30, 6);
assertEquals(new Integer(5), lotto.existCount(winnerNumbers));

winnerNumbers = Arrays.asList(1, 2, 3, 20, 7, 6);
assertEquals(new Integer(4), lotto.existCount(winnerNumbers));

winnerNumbers = Arrays.asList(1, 4, 5, 7, 9, 11);
assertEquals(new Integer(1), lotto.existCount(winnerNumbers));


}
}
19 changes: 19 additions & 0 deletions src/test/java/lotto/ShuffleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lotto;

import org.junit.Test;

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

import static org.assertj.core.api.Assertions.assertThat;

public class ShuffleTest {
@Test
public void shuffleTest() {
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6);
Collections.shuffle(integers);
assertThat(integers).doesNotContainSubsequence(1, 2, 3, 4, 5, 6);
}
}