Skip to content

Commit 8346441

Browse files
committed
[feat/next-step#1] step1
1 parent b3ba83a commit 8346441

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

src/main/java/Application.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import controller.LottoController;
2+
3+
public class Application {
4+
5+
public static void main(String[] args) {
6+
LottoController lottoController = new LottoController();
7+
lottoController.run();
8+
}
9+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package controller;
2+
3+
import model.Lotto;
4+
import view.InputView;
5+
import view.ResultView;
6+
7+
import java.util.List;
8+
import java.util.stream.Collectors;
9+
10+
public class LottoController {
11+
12+
public void run() {
13+
int purchaseAmount = InputView.getPurchaseAmount();
14+
15+
int ticketCount = Lotto.getTicketCount(purchaseAmount);
16+
17+
List<Lotto> tickets = Lotto.generateLottoTickets(ticketCount);
18+
19+
ResultView.printOrderTickets(ticketCount);
20+
ResultView.printTickets(ticketCount, formatTickets(tickets));
21+
}
22+
23+
public List<String> formatTickets(List<Lotto> tickets) {
24+
List<String> formattedTickets = tickets.stream()
25+
.map(lotto -> lotto.getNumbers()
26+
.stream()
27+
.sorted()
28+
.map(String::valueOf)
29+
.collect(Collectors.joining(",","[","]")))
30+
.toList();
31+
32+
return formattedTickets;
33+
}
34+
}

src/main/java/model/Lotto.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package model;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
import java.util.stream.IntStream;
6+
7+
public class Lotto {
8+
9+
// 로또 번호 관련 상수 선언
10+
public static final int LOTTO_MIN_NUMBER = 1;
11+
public static final int LOTTO_MAX_NUMBER = 45;
12+
public static final int LOTTO_CREATE_SIZE = 6;
13+
public static final int LOTTO_PRICE = 1000;
14+
public static final List<Integer> LOTTO_NUMBER_POOL =
15+
IntStream
16+
.rangeClosed(LOTTO_MIN_NUMBER,LOTTO_MAX_NUMBER)
17+
.boxed()
18+
.collect(Collectors.toList());
19+
20+
private List<Integer> numbers = new ArrayList<>();
21+
22+
public static int getTicketCount(int purchaseAmount){
23+
return purchaseAmount / LOTTO_PRICE;
24+
}
25+
26+
public Lotto(){
27+
this.numbers = createLottoNumbers();
28+
}
29+
30+
private List<Integer> createLottoNumbers(){
31+
List<Integer> shuffledNumbers = new ArrayList<>(LOTTO_NUMBER_POOL);
32+
Collections.shuffle(shuffledNumbers);
33+
numbers = shuffledNumbers.subList(0, LOTTO_CREATE_SIZE);
34+
35+
return numbers;
36+
}
37+
38+
public static List<Lotto> generateLottoTickets(int ticketCount){
39+
List<Lotto> tickets = new ArrayList<>();
40+
41+
for (int i = 0; i < ticketCount; i++){
42+
tickets.add(new Lotto());
43+
}
44+
45+
return tickets;
46+
}
47+
48+
public List<Integer> getNumbers() {
49+
List<Integer> sortedNumbers = new ArrayList<>(numbers);
50+
Collections.sort(sortedNumbers);
51+
52+
return sortedNumbers;
53+
}
54+
}

src/main/java/view/InputView.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package view;
2+
3+
import java.util.Scanner;
4+
5+
public class InputView {
6+
7+
private static final Scanner scanner = new Scanner(System.in);
8+
9+
public static int getPurchaseAmount(){
10+
System.out.println("구입금액을 입력해 주세요.");
11+
int purchaseAmount = scanner.nextInt();
12+
scanner.close();
13+
14+
return purchaseAmount;
15+
}
16+
}

src/main/java/view/ResultView.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package view;
2+
import java.util.List;
3+
4+
public class ResultView {
5+
6+
public static void printOrderTickets(int ticketCount){
7+
System.out.println();
8+
System.out.println(ticketCount + "개를 구매했습니다.");
9+
}
10+
11+
public static void printTickets(int ticketCount ,List<String> formattedTickets){
12+
for (String ticket : formattedTickets) {
13+
System.out.println(ticket);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)