Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
# java-planetlotto-precourse


---

## 입출력

### 입력

입력 함수는 입력만 담당하며, 객체 생성과 검증은 다른 계층에서 수행한다.

### 1. 로또 구입 금액 입력

- 입력값은 500으로 나누어 떨어져야 한다.
- 숫자만 입력해야 한다.

### 2. 당첨 번호 입력

- 5개의 숫자만 입력해야 하며 허용된 범위 안의 숫자여야 한다.
- 중복된 숫자는 입력할 수 없다.
- 쉼표로 구분하며 공백은 허용되지 않는다.

### 3. 보너스 번호 입력

- 당첨 번호와 중복되서는 안된다.
- 허용된 범위 안의 숫자여야 한다.

---

### 출력

출력 함수는 계산된 값을 전달받아 출력만 담당한다.

1. 구매한 로또 출력
2. 당첨 통계 출력

---

## 핵심 기능

### 로또 구입 기능
- 금액을 입력하면 입력한 금액만큼의 로또를 랜덤으로 생성한다.
### 당첨 번호 및 보너스 번호 입력 기능
- 당첨 번호를 입력할 수 있다.

### 당첨 통계 기능
- 사용자가 구입한 로또와 당첨 로또를 비교하여 등수를 측정한다.
- 총 등수와 금액을 출력한다.

---

6 changes: 5 additions & 1 deletion src/main/java/planetlotto/Application.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package planetlotto;

import planetlotto.config.AppConfig;


public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
AppConfig.getInstance()
.mainController();
}
}
86 changes: 86 additions & 0 deletions src/main/java/planetlotto/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package planetlotto.config;

import planetlotto.controller.LottoController;
import planetlotto.controller.MainController;
import planetlotto.controller.UserController;
import planetlotto.generator.NumberGenerator;
import planetlotto.generator.RandomNumberGenerator;
import planetlotto.repository.UserLotto;
import planetlotto.service.LottoService;
import planetlotto.service.UserService;

public class AppConfig {

private static final AppConfig INSTANCE = new AppConfig();

// ===== Infra / Storage =====
private UserLotto userLottoRepository;

// ===== Services =====
private UserService userService;
private LottoService lottoService;

// ===== Controllers =====
private MainController mainController;
private UserController userController;
private LottoController lottoController;

private NumberGenerator numberGenerator = new RandomNumberGenerator();

private AppConfig() {
}

public static AppConfig getInstance() {
return INSTANCE;
}

public MainController mainController() {
if (mainController == null) {
mainController = new MainController(
userController(),
lottoController()
);
}
return mainController;
}

protected UserController userController() {
if (userController == null) {
userController = new UserController(
userService()
);
}
return userController;
}

protected LottoController lottoController() {
if (lottoController == null) {
lottoController = new LottoController(
lottoService()
);
}
return lottoController;
}

protected UserService userService() {
if (userService == null) {
userService = new UserService(numberGenerator, repository());
}
return userService;
}

protected LottoService lottoService() {
if (lottoService == null) {
lottoService = new LottoService(numberGenerator, repository());
}
return lottoService;
}

protected UserLotto repository() {
if (userLottoRepository == null) {
userLottoRepository = new UserLotto();
}
return userLottoRepository;
}

}
83 changes: 83 additions & 0 deletions src/main/java/planetlotto/controller/LottoController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package planetlotto.controller;


import java.util.List;
import java.util.Map;
import planetlotto.domain.WinningLotto;
import planetlotto.service.LottoService;
import planetlotto.view.InputView;
import planetlotto.view.OutputView;

public class LottoController {

private final LottoService lottoService;

public LottoController(LottoService lottoService) {
this.lottoService = lottoService;
}

public void run() {

WinningLotto winningLotto = runSetWinningLottoStep();
runSetBonusNumberStep(winningLotto);

runPrintResultStep(winningLotto);
}

private WinningLotto runSetWinningLottoStep(){
while (true) {
try {
WinningLotto winningLotto = setWinningLotto();
return winningLotto;
} catch (IllegalArgumentException e) {
handleError(e);
}
}
}


private WinningLotto setWinningLotto() {
List<Integer> integers = InputView.askWinningLotto();

return lottoService.setWinningLotto(integers);
}

private void runSetBonusNumberStep(WinningLotto winningLotto){
while (true) {
try {
setBonusNumber(winningLotto);
return;
} catch (IllegalArgumentException e) {
handleError(e);
}
}
}

private void setBonusNumber(WinningLotto winningLotto) {

int bonusNumber = InputView.askBonusNumber();
lottoService.setBonusNumber(winningLotto, bonusNumber);

}

private void runPrintResultStep(WinningLotto winningLotto) {
while (true) {
try {
getResult(winningLotto);
return;
} catch (IllegalArgumentException e) {
handleError(e);
}
}
}

private void getResult(WinningLotto winningLotto) {

Map<Integer, Integer> userResult = lottoService.getUserResult(winningLotto);
OutputView.printResult(userResult);
}

private void handleError(IllegalArgumentException e) {
OutputView.printErrorMessage(e.getMessage());
}
}
25 changes: 25 additions & 0 deletions src/main/java/planetlotto/controller/MainController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package planetlotto.controller;

public class MainController {

private boolean running = true;

public MainController(
UserController userController,
LottoController lottoController
) {
userController.run();
lottoController.run();
}

public void run() {
while (running) {
try {

} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}

}
43 changes: 43 additions & 0 deletions src/main/java/planetlotto/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package planetlotto.controller;

import java.util.List;
import planetlotto.service.UserService;
import planetlotto.view.InputView;
import planetlotto.view.OutputView;

public class UserController {

private final UserService userService;

public UserController(UserService userService) {
this.userService = userService;
}

public void run() {
runPurchaseLottoStep();

}

private void runPurchaseLottoStep() {
while (true) {
try {
purchaseLotto();
return;
} catch (IllegalArgumentException e) {
handleError(e);
}
}
}

private void purchaseLotto() {
int amount = InputView.askAmount();
userService.purchaseLotto(amount);

List<List<Integer>> results = userService.getUserLottos();
OutputView.printPurchasedLottos(results);
}

private void handleError(IllegalArgumentException e) {
OutputView.printErrorMessage(e.getMessage());
}
}
42 changes: 42 additions & 0 deletions src/main/java/planetlotto/domain/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package planetlotto.domain;

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

public class Lotto {

private List<Integer> numbers = new ArrayList<>();

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

public Lotto() {
}

public void addNumber(int number){
numbers.add(number);
}

public void validateLotto(List<Integer> numbers) {

//숫자 범위 검증
for (Integer number : numbers) {
if (number < 1 || number > 30) {
throw new IllegalArgumentException("로또 번호는 1부터 30 사이의 숫자여야 합니다.\n");
}
}

//숫자 개수 검증
if (numbers.size() != 5) {
throw new IllegalArgumentException("로또 번호는 5개여야 합니다.");
}

}

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

}
37 changes: 37 additions & 0 deletions src/main/java/planetlotto/domain/Rank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package planetlotto.domain;

import java.util.Arrays;

public enum Rank {
FIRST(5, false, 1),
SECOND(4, true, 2),
THIRD(4, false, 3),
FOURTH(3, true, 4),
FIFTH(2, true, 5),
MISS(0, true, 0);

private final int matchCount;
private final boolean matchedBonusNumber;
private final int prize;

Rank(int matchCount, boolean matchedBonusNumber, int prize) {
this.matchCount = matchCount;
this.prize = prize;
this.matchedBonusNumber = matchedBonusNumber;
}

public static Rank of(int matchCount, boolean matchedBonusNumber) {
return Arrays.stream(values())
.filter(rank -> rank.matchCount == matchCount)
.filter(rank -> rank.matchedBonusNumber == matchedBonusNumber)
.findFirst()
.orElse(MISS);
}

public int getPrize(){
return prize;
}



}
Loading