-
Notifications
You must be signed in to change notification settings - Fork 20
[Team2 Pair1] 로또 Step1 완료 #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: team2_pair1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
|
|
||
| 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; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 클래스의 구현 순서는 다음과 같다. 다음 원칙에 따라 구현한다. |
||
|
|
||
| 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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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; | ||
| } | ||
| } | ||
| 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(); | ||
| } | ||
| } |
| 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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lotto를 생성할 때마다 매번 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .collect(Collectors.toList()) 사용하지 말고 filter().count() 가능하지 않나? |
||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } |
| 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); | ||
|
|
||
| } | ||
| } |
| 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()); | ||
| } | ||
| } | ||
| } |
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enum은 "_"를 사용하기 보다 대문자로 사용하는 것이 일반적임 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 처음에 저희도 일반적으로 values()로 Loop을 도는것을 생각을 했는데요. |
||
|
|
||
| private final int value; | ||
|
|
||
| WinPrize(int prize) { | ||
| this.value = prize; | ||
| } | ||
|
|
||
| public static Integer getPrize(Integer i) { | ||
| return valueOf("_" + i).value; | ||
| } | ||
| } | ||
| 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)); | ||
| } | ||
|
|
||
| } |
| 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()); | ||
| } | ||
| } |
| 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)); | ||
|
|
||
|
|
||
| } | ||
| } |
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳이 인스턴스 변수로 구현할 필요가 있나?