Skip to content
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

[로또 미션] 오지현 미션 제출합니다. #20

Open
wants to merge 7 commits into
base: zhy2on
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/main/java/LottoApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import controller.LottoController;

public class LottoApplication {

public static void main(String[] args) {
LottoController controller = new LottoController();
controller.run();
}
}
31 changes: 31 additions & 0 deletions src/main/java/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package controller;

import domain.LottoTicket;
import view.InputView;
import view.OutputView;

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

public class LottoController {
public void run() {
int amountToBuy = InputView.getAmountToBuy();
List<LottoTicket> tickets = buyLottoTickets(amountToBuy);
OutputView.displayTicketCount(tickets.size());
OutputView.displayLottoTickets(tickets);
}
Copy link

Choose a reason for hiding this comment

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

1단계에서 구현을 하신 것처럼 Controller 안에서 말고 Application 안에서 컨트롤러 객체 생성 후 controller.run() 사용하시면 좋을 것 같습니다.


private List<LottoTicket> buyLottoTickets(final int amountToBuy) {
int ticketCount = amountToBuy / LottoTicket.getLottoPrice();
return generateLottoTickets(ticketCount);
}

private List<LottoTicket> generateLottoTickets(final int ticketCount) {
return IntStream.range(0, ticketCount)
.mapToObj(i -> new LottoTicket())
.collect(Collectors.toList());
}

}
36 changes: 36 additions & 0 deletions src/main/java/domain/LottoTicket.java
Copy link

Choose a reason for hiding this comment

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

원시 값 포장을 잘 하신 것 같습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package domain;

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

public class LottoTicket {
private static final int LOTTO_PRICE = 1000;
private static final int LOTTO_NUMBER_COUNT = 6;
private static final int LOTTO_NUMBER_MAX = 45;

private List<Integer> numbers;

public LottoTicket() {
this.numbers = generateLottoNumbers();
}

private List<Integer> generateLottoNumbers() {
List<Integer> numbers = new ArrayList<>();
for (int i = 1; i <= LOTTO_NUMBER_MAX; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
List<Integer> selectedNumbers = numbers.subList(0, LOTTO_NUMBER_COUNT);
Collections.sort(selectedNumbers); // 오름차순 정렬
return selectedNumbers;
}

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

public static int getLottoPrice() {
return LOTTO_PRICE;
}
}
12 changes: 12 additions & 0 deletions src/main/java/view/InputView.java
Copy link

Choose a reason for hiding this comment

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

문자열도 포장을 하시면 좋을 것 같아요. 위에서 원시 값을 포장하신 것처럼요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package view;

import java.util.Scanner;

public class InputView {
private static final Scanner scanner = new Scanner(System.in);

public static int getAmountToBuy() {
System.out.println("구입금액을 입력해 주세요.");
return scanner.nextInt();
}
}
17 changes: 17 additions & 0 deletions src/main/java/view/OutputView.java
Copy link

Choose a reason for hiding this comment

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

문자열을 포장하시고 반복문으로 출력하시면 코드가 훨씬 더 간결해질 것 같습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package view;

import domain.LottoTicket;

import java.util.List;

public class OutputView {
public static void displayTicketCount(final int ticketCount) {
System.out.println(ticketCount + "개를 구매했습니다.");
}

public static void displayLottoTickets(final List<LottoTicket> tickets) {
for (LottoTicket ticket : tickets) {
System.out.println(ticket.getNumbers());
}
}
}