본문 바로가기
이론/JAVA

로또 생성기 - 자바 스터디 2회차 과제

by 유세지 2017. 10. 13.

 1회차 과제보다 어려워서 삽질을 좀 많이 한 것 같다. 문제에서는 contains를 이용해 비교하는 것을 의도한것 같지만 안되길래 어레이가 가지고 있는 값들을 일일히 비교시켰다.


 어차피 시간 복잡도는 같을테니까 상관 없겠지...?



 만들때 생각해야 하는건 자동으로 뽑은 로또 번호들이 sort 되어서 나온다는 점, ArrayList 안에 ArrayList 를 넣어서 ArrayList<ArrayList> 같은 이상한 걸 만들어서 쓴다는 점 정도 있는듯. 


 코드 리뷰 후 수정 : 수입률 자료형 수정(int → long), ArrayList형을 ArrayList<Integer>형으로 선언, 중구난방했던 static 변수 수정, 캡슐화, Scanner 하나로 재사용, 필요 없던 메서드 삭제




 프로젝트 폴더 구성




 LottoMain.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.ArrayList;
import java.util.Scanner;
 
public class LottoMain {
 
    public static String lottonumber;
    public static ArrayList<ArrayList<Integer>> myLottos;
 
    public static void main(String[] args) {
 
        System.out.println("구입금액을 입력해 주세요.");
 
        Scanner sc = new Scanner(System.in);
 
        BuyLotto.getBuyingMoney(sc.nextInt());
 
        BuyLotto.buyingLotto();
        
        System.out.println("\n지난 주 당첨 번호를 입력해주세요.");
        
        sc.nextLine();
        lottonumber = sc.nextLine();
 
        BuyLotto.wonLotto();
 
        sc.close();
 
    }
}
 
cs





 BuyLotto.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.util.ArrayList;
import java.util.Collections;
 
public class BuyLotto {
 
    public static int money = 0;
    public static int pbright = 0;
 
    // 얼마가 들어왔지?
    public static void getBuyingMoney(int i) {
        money = i;
    }
 
    public static int getBuyingMoney() {
        return money;
    }
 
    // 로또를 얼마나 살수있지?
    public static int countOfLotto() {
        return getBuyingMoney() / 1000;
    }
 
    // 로또를 사보자
    public static void buyingLotto() {
        
        System.out.printf("%d개를 구매했습니다.\n", countOfLotto());
        ArrayList<ArrayList<Integer>> buyedLotto = new ArrayList<ArrayList<Integer>>();
        buyedLotto = buyAutoLottos(countOfLotto());
 
        for (int i = 0; i < countOfLotto(); i++) {
            System.out.println(buyedLotto.get(i));
        }
 
        LottoMain.myLottos = buyedLotto;        
    }
 
    // 당첨을 확인해보자
    public static void wonLotto() {
 
        Result results = resulting();
        long percentage = revenue(results) * 100;
 
        System.out.println("\n당첨 통계\n----------");
 
        System.out.println("3개 일치 (5000원)- " + (int) results.getCountOfMatch3() + "개");
        System.out.println("4개 일치 (50000원)- " + (int) results.getCountOfMatch4() + "개");
        System.out.println("5개 일치 (1500000원)- " + (int) results.getCountOfMatch5() + "개");
        System.out.println("6개 일치 (2000000000원)- " + (int) results.getCountOfMatch6() + "개");
 
        System.out.println("총 수입률은 " + percentage + "%입니다.");
 
    }
 
    // 수익률을 계산 해보자.
    public static long revenue(Result results) {
 
        long revenue = (results.getCountOfMatch3() * 5000 + results.getCountOfMatch4() * 50000
                + results.getCountOfMatch5() * 1500000 + results.getCountOfMatch6() * 2000000000/ money;
 
        return revenue;
    }
 
    // 지난 주 번호는 뭐였지?
    public static String[] getWinningNumber() {
 
        String winningString = LottoMain.lottonumber;
 
        String[] splited = winningString.split(", ");
 
        return splited;
    }
 
    // 로또 번호와 대조
    public static void isRight(ArrayList<Integer> arrayList, String[] splited) {
 
        int right = 0;
 
        for (int i = 0; i < 6; i++) {
 
            for (int j = 0; j < 6; j++) {
 
                if (arrayList.get(i).toString().equals(splited[j]) == true) {
                    right += 1;
                }
            }
        }
 
        pbright = right;
    }
 
    // 로또 번호 여러개 대조
    public static Result resulting() {
 
        Result result = new Result();
 
        ArrayList<Integer> NowLottos = new ArrayList<Integer>();
 
        for (int i = 0; i < countOfLotto(); i++) {
 
            NowLottos = (ArrayList<Integer>)LottoMain.myLottos.get(i);
 
            isRight((ArrayList<Integer>) NowLottos, getWinningNumber());
 
            if (pbright == 6) {
                result.setCountOfMatch6(result.getCountOfMatch6() + 1);
            } else if (pbright == 5) {
                result.setCountOfMatch5(result.getCountOfMatch5() + 1);
            } else if (pbright == 4) {
                result.setCountOfMatch4(result.getCountOfMatch4() + 1);
            } else if (pbright == 3) {
                result.setCountOfMatch3(result.getCountOfMatch3() + 1);
            } else {
 
            }
 
        }
 
        return result;
    }
 
    // 로또 하나 구입하는거
    public static ArrayList<Integer> buyAutoLotto() {
        ArrayList<Integer> original = new ArrayList<Integer>();
        ArrayList<Integer> list = new ArrayList<Integer>();
 
        Integer number[] = { 12345678910111213141516171819202122232425,
                2627282930313233343536373839404142434445 };
 
        for (int i = 0; i < number.length; i++) {
            original.add(number[i]);
        }
 
        Collections.shuffle(original);
 
        for (int i = 0; i < 6; i++) {
            list.add(original.get(i));
        }
 
        Collections.sort(list);
 
        return list;
    }
 
    // 여러개 구입하는거
    public static ArrayList<ArrayList<Integer>> buyAutoLottos(int countOfLotto) {
 
        ArrayList<ArrayList<Integer>> LottoArray = new ArrayList<ArrayList<Integer>>();
 
        for (int j = 0; j < BuyLotto.countOfLotto(); j++) {
            LottoArray.add(buyAutoLotto());
        }
 
        return LottoArray;
    }
 
}
cs


● Result.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Result {
 
    public int getCountOfMatch3() {
        return countOfMatch3;
    }
 
    public void setCountOfMatch3(int countOfMatch3) {
        this.countOfMatch3 = countOfMatch3;
    }
 
    public int getCountOfMatch4() {
        return countOfMatch4;
    }
 
    public void setCountOfMatch4(int countOfMatch4) {
        this.countOfMatch4 = countOfMatch4;
    }
 
    public int getCountOfMatch5() {
        return countOfMatch5;
    }
 
    public void setCountOfMatch5(int countOfMatch5) {
        this.countOfMatch5 = countOfMatch5;
    }
 
    public int getCountOfMatch6() {
        return countOfMatch6;
    }
 
    public void setCountOfMatch6(int countOfMatch6) {
        this.countOfMatch6 = countOfMatch6;
    }
 
    private int countOfMatch3 = 0;
 
    private int countOfMatch4 = 0;
 
    private int countOfMatch5 = 0;
 
    private int countOfMatch6 = 0;
}
cs


 실행결과


반응형

댓글