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

[2주차] 방현우 객체지향 코드 연습(baaamk) #33

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
202edc7
refactor: 로또 저장 클래스 생성
baaamk Sep 30, 2024
f25afa1
refactor: 로또 저장 클래스 삭제, 배열 사용해 로또 번호 저장
baaamk Sep 30, 2024
aec94ea
refactor: 구현만 완료
baaamk Oct 1, 2024
620166d
refactor: LottoRank Enum으로 구현. LottoService 클래스 생성
baaamk Oct 1, 2024
9779e89
refactor: MVC 패턴 구사 위해 패키지화.
baaamk Oct 2, 2024
5194cd6
refactor: MVC 패턴 구사 위해 패키지화.
baaamk Oct 2, 2024
e6981f8
refactor: Application test파일 이동
baaamk Oct 2, 2024
906539e
refactor: 코드 컨벤션 수정, 매직넘버 수정
baaamk Oct 2, 2024
4627efd
refactor: 제공 라이브러리 사용
baaamk Oct 3, 2024
ba59f4f
refactor: 추상 클래스 생성으로 상수 관리. 수익률 반올림 가능하세 변경.
baaamk Oct 3, 2024
338173c
refactor: Enum으로 예외 변경, OutputMessage 클래스 view 패키지로 이동
baaamk Oct 3, 2024
8250e7d
refactor: LottoRepository 생성, LottoService 생성, 피드백 반영
baaamk Oct 7, 2024
6ca5717
refactor: 패키지명 수정
baaamk Oct 7, 2024
dbe2e6e
refactor: setQuantity 삭제
baaamk Oct 9, 2024
8e5364f
refactor: 로또 WinningImpl 클래스 싱글톤 변경,패키시 생성
baaamk Oct 9, 2024
1406a83
클래스 조직도
baaamk Oct 10, 2024
a503c0d
lotto 클래스 조직도.drawio 추가
baaamk Oct 10, 2024
3c53653
lotto 클래스 조직도.drawio 업데이트
baaamk Oct 10, 2024
7e7456e
Merge branch 'COW-edu:main' into main
baaamk Oct 10, 2024
82f770f
refactor: numbers 전역번수로 변경,
baaamk Oct 10, 2024
7aeb106
refactor: 제공 라이브러리 사용(scanner 변경)
baaamk Oct 10, 2024
1baf846
refactor: 패키지 명 변경
baaamk Oct 10, 2024
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 객체지향 코드 연습 - 로또([우아한테크코스 프리코스 - lotto](https://github.com/woowacourse-precourse/java-lotto))
# 객체지향 코드 연습 - 로또([우아한테크코스 프리코스 - lotto_view](https://github.com/woowacourse-precourse/java-lotto))

## 🔍 진행 방식

Expand Down Expand Up @@ -179,7 +179,7 @@ BUILD SUCCESSFUL in 0s
- Java Enum을 적용한다.
- 도메인 로직에 단위 테스트를 구현해야 한다. 단, UI(System.out, System.in, Scanner) 로직은 제외한다.
- 핵심 로직을 구현하는 코드와 UI를 담당하는 로직을 분리해 구현한다.
- 단위 테스트 작성이 익숙하지 않다면 `test/java/lotto/LottoTest`를 참고하여 학습한 후 테스트를 구현한다.
- 단위 테스트 작성이 익숙하지 않다면 `test/java/lotto_view/LottoTest`를 참고하여 학습한 후 테스트를 구현한다.

### 라이브러리

Expand Down Expand Up @@ -224,7 +224,7 @@ public class Lotto {

## ✏️ 과제 진행 요구 사항

- 미션은 [practice-oop-lotto](https://github.com/COW-edu/practice-oop-lotto) 저장소를 Fork & Clone해 시작한다.
- 미션은 [practice-oop-lotto_view](https://github.com/COW-edu/practice-oop-lotto) 저장소를 Fork & Clone해 시작한다.
- **기능을 구현하기 전 `docs/README.md`에 구현할 기능 목록을 정리**해 추가한다.
- **Git의 커밋 단위는 앞 단계에서 `docs/README.md`에 정리한 기능 목록 단위**로 추가한다.
- [커밋 메시지 컨벤션](https://gist.github.com/stephenparish/9941e89d80e2bc58a153) 가이드를 참고해 커밋 메시지를 작성한다.
7 changes: 7 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Application : 메인 실행 클래스

LottoService : 로또 생성 및 계산

LottoRankENUM 사용) : 당첨 결과 관리

Lotto: 사용자 번호 저장
38 changes: 38 additions & 0 deletions src/main/java/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import lotto_controller.LottoController;
import lotto_model.LottoComparator;
import lotto_model.LottoGenerator;
import lotto_model.LottoRepository;
import lotto_model.LottoService;
import lotto_view.LottoInput;
import lotto_view.LottoOutput;

public class AppConfig {
public LottoInput lottoInput() {
return new LottoInput();
}

public LottoOutput lottoOutput() {
return new LottoOutput();
}

public LottoComparator lottoComparator() {
return new LottoComparator(lottoRepository());
}

public LottoGenerator lottoGenerator() {
return new LottoGenerator(lottoRepository());
}

public LottoRepository lottoRepository() {
return LottoRepository.getInstance();
}

public LottoService lottoService() {
return new LottoService(lottoComparator(), lottoGenerator());
}

public LottoController lottoController() {
return new LottoController(lottoInput(), lottoOutput(), lottoService());
}

}
10 changes: 10 additions & 0 deletions src/main/java/Application.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import lotto_controller.LottoController;

public class Application {
public static void main(String[] args) {
AppConfig appConfig = new AppConfig();
LottoController lottoController = appConfig.lottoController();

lottoController.run();
}
}
7 changes: 0 additions & 7 deletions src/main/java/lotto/Application.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/lotto/Lotto.java

This file was deleted.

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

import lotto_model.Lotto;
import lotto_model.LottoService;
import lotto_view.LottoInput;
import lotto_view.LottoOutput;

public class LottoController {
private final LottoInput lottoInput;
private final LottoOutput lottoOutput;
private final LottoService lottoService;

public LottoController(LottoInput lottoInput, LottoOutput lottoOutput, LottoService lottoService) {
this.lottoInput = lottoInput;
this.lottoOutput = lottoOutput;
this.lottoService = lottoService;
}

public void run() {
int inputMoney = lottoInput.getInputMoney();
lottoService.printLotto(inputMoney);
Lotto targetLotto = lottoInput.inputNumber();
int bonusNumber = lottoInput.getBonusNum();
lottoService.lottoCompare(targetLotto, bonusNumber);
lottoOutput.resultPrint(inputMoney);
}
}
18 changes: 18 additions & 0 deletions src/main/java/lotto_exception/ErrorMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package lotto_exception;

public enum ErrorMessage {
OUT_OF_RANGE("[ERROR] 1부터 45사이의 숫자를 입력해 주세요."),
DUPLICATE_NUMBER("ERROR] 중복된 숫자가 입력되었습니다."),
OUT_OF_UNIT("[ERROR] 1000원 단위로 입력해 주세요."),
INVALID_NUMBER_COUNT("[ERROR] 6개의 숫자만 입력해주세요.");

private final String errorMessage;

ErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}

public String getErrorMessage() {
return errorMessage;
}
}
33 changes: 33 additions & 0 deletions src/main/java/lotto_model/Lotto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lotto_model;

import java.util.*;

import static lotto_exception.ErrorMessage.*;

public class Lotto implements Iterable<Integer> {
private final List<Integer> numbers;

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

private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException(INVALID_NUMBER_COUNT.getErrorMessage());
}
}

@Override
public Iterator<Integer> iterator() {
return numbers.iterator();
}

public boolean contains(Integer element) {
return numbers.contains(element);
}

public List<Integer> getNumbers() {
return numbers;
}
}
51 changes: 51 additions & 0 deletions src/main/java/lotto_model/LottoComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package lotto_model;

import java.util.Iterator;

import static lotto_model.LottoRank.*;
import static lotto_model.LottoRank.FIFTH;

public class LottoComparator {

private final LottoRepository lottoRepository;

public LottoComparator(LottoRepository lottoRepository) {
this.lottoRepository = lottoRepository;
}

public void compare(Lotto userLotto, int bonusNumber) {
Iterator<Lotto> lottoNumbers = lottoRepository.getLottoSaved().iterator();
while (lottoNumbers.hasNext()) {
Lotto current = lottoNumbers.next();
int sameNumbers = 0;

for (Integer element : userLotto) {
if (current.contains(element)) {
sameNumbers++;
}
}
if (sameNumbers == 6) {
FIRST.setQuantity(FIRST.getQuantity() + 1);
continue;
}
if (sameNumbers == 5 && current.contains(bonusNumber)) {
SECOND.setQuantity(SECOND.getQuantity() + 1);
continue;
}

if (sameNumbers == 5) {
THIRD.setQuantity(THIRD.getQuantity() + 1);
continue;
}

if (sameNumbers == 4) {
FOURTH.setQuantity(FOURTH.getQuantity() + 1);
continue;
}

if (sameNumbers == 3) {
FIFTH.setQuantity(FIFTH.getQuantity() + 1);
}
}
}
}
46 changes: 46 additions & 0 deletions src/main/java/lotto_model/LottoGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package lotto_model;

import camp.nextstep.edu.missionutils.Randoms;

import java.util.Collections;

import static lotto_view.OutputMessage.AMOUNT_OUTPUT;

public class LottoGenerator {
private final LottoRepository lottoRepository;

public LottoGenerator(LottoRepository lottoRepository) {
this.lottoRepository = lottoRepository;
}

int lottoAmount = 0;

public void printLottoSaved(int inputMoney) {
lottoAmount = getLottoAmount(inputMoney);
lottoAlign(inputMoney);
System.out.printf((AMOUNT_OUTPUT.getMessage()) + "%n", lottoAmount);
lottoRepository.getLottoSaved().stream().forEach(item -> System.out.println(item.getNumbers()));
}

private void lottoAlign(int inputMoney) {
lottoAmount = getLottoAmount(inputMoney);
for (int i = 0; i < lottoAmount; i++) {
Lotto generatedNumber = numberGenerate();
lottoSave(generatedNumber);
}
}

private Lotto numberGenerate() {
Lotto lotto = new Lotto(Randoms.pickUniqueNumbersInRange(Unit.MIN_NUMBER.getValue(), Unit.MAX_NUMBER.getValue(), Unit.UNIT_LOTTO_NUMBER.getValue()));
Collections.sort(lotto.getNumbers());
return lotto;
}

private static int getLottoAmount(int inputMoney) {
return inputMoney / Unit.UNIT_MONEY.getValue();
}

private void lottoSave(Lotto generatedNumber) {
lottoRepository.save(generatedNumber);
}
}
34 changes: 34 additions & 0 deletions src/main/java/lotto_model/LottoRank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lotto_model;

public enum LottoRank {
FIRST(1000000000, 0),
SECOND(30000000, 0),
THIRD(1500000, 0),
FOURTH(50000, 0),
FIFTH(5000, 0);

private final int lottoPrize;

private int quantity;

LottoRank(int lottoPrize, int quantity) {
this.lottoPrize = lottoPrize;
this.quantity = quantity;
}

public int getLottoPrize() {
return lottoPrize;
}

public int getQuantity() {
return quantity;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}

public double getLottoTotalPrize() {
return getLottoPrize() * getQuantity();
}
}
26 changes: 26 additions & 0 deletions src/main/java/lotto_model/LottoRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lotto_model;

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

public class LottoRepository {

private static final LottoRepository instance = new LottoRepository(); // 싱글턴 인스턴스
private final List<Lotto> lottoSaved; // Lotto 저장 리스트

private LottoRepository() {
lottoSaved = new ArrayList<>();
}

public static LottoRepository getInstance() {
return instance;
}

public List<Lotto> getLottoSaved() {
return lottoSaved;
}

public void save(Lotto lotto) {
lottoSaved.add(lotto);
}
}
19 changes: 19 additions & 0 deletions src/main/java/lotto_model/LottoService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lotto_model;

public class LottoService {
private final LottoComparator lottoComparator;
private final LottoGenerator lottoGenerator;

public LottoService(LottoComparator lottoComparator, LottoGenerator lottoGenerator) {
this.lottoComparator = lottoComparator;
this.lottoGenerator = lottoGenerator;
}

public void lottoCompare(Lotto targetLotto, int bonusNumber) {
lottoComparator.compare(targetLotto, bonusNumber);
}

public void printLotto(int inputMoney) {
lottoGenerator.printLottoSaved(inputMoney);
}
}
19 changes: 19 additions & 0 deletions src/main/java/lotto_model/Unit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package lotto_model;

public enum Unit {
UNIT_MONEY(1000),
PERCENT_UNIT(100),
MAX_NUMBER(45),
MIN_NUMBER(1),
UNIT_LOTTO_NUMBER(6);

private final int value;

Unit(int value) {
this.value = value;
}

public int getValue() {
return value;
}
}
Loading