Skip to content

Commit

Permalink
step1
Browse files Browse the repository at this point in the history
  • Loading branch information
seongjae6751 committed Jul 8, 2024
1 parent b3ba83a commit 562d80f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.Collections;
import java.util.List;

public class Lotto {
private List<Integer> numbers;

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

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

@Override
public String toString() {
return numbers.toString();
}
}
12 changes: 12 additions & 0 deletions src/main/java/LottoApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.List;

public class LottoApp {
public static void main(String[] args) {
LottoMachine lottoMachine = new LottoMachine();
int purchaseAmount = lottoMachine.getPurchaseAmount();
int numberOfLottos = lottoMachine.calculateNumberOfLottos(purchaseAmount);
lottoMachine.printPurchaseInfo(numberOfLottos);
List<Lotto> lottos = lottoMachine.generateLottos(numberOfLottos);
lottoMachine.printLottos(lottos);
}
}
47 changes: 47 additions & 0 deletions src/main/java/LottoMachine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class LottoMachine {
private static final int LOTTO_PRICE = 1000;
private static final int LOTTO_NUMBER_COUNT = 6;
private static final int LOTTO_MAX_NUMBER = 45;

public int getPurchaseAmount() {
Scanner scanner = new Scanner(System.in);
System.out.println("구입금액을 입력해 주세요.");
return scanner.nextInt();
}

public int calculateNumberOfLottos(int purchaseAmount) {
return purchaseAmount / LOTTO_PRICE;
}

public void printPurchaseInfo(int numberOfLottos) {
System.out.println(numberOfLottos + "개를 구매했습니다.");
}

public List<Lotto> generateLottos(int numberOfLottos) {
List<Lotto> lottos = new ArrayList<>();
for (int i = 0; i < numberOfLottos; i++) {
lottos.add(generateLotto());
}
return lottos;
}

private Lotto generateLotto() {
List<Integer> numbers = new ArrayList<>();
for (int i = 1; i <= LOTTO_MAX_NUMBER; i++) {
numbers.add(i);
}
Collections.shuffle(numbers);
return new Lotto(numbers.subList(0, LOTTO_NUMBER_COUNT));
}

public void printLottos(List<Lotto> lottos) {
for (Lotto lotto : lottos) {
System.out.println(lotto);
}
}
}

0 comments on commit 562d80f

Please sign in to comment.