Skip to content

Conversation

@junepil
Copy link

@junepil junepil commented Nov 3, 2025

로또 추첨기 구현

사용자가 구매 금액을 입력하면 로또를 발행하고, 당첨 번호와 보너스 번호를 입력받아 당첨 통계와 수익률을 계산하는 애플리케이션입니다.

주요 기능

  • 로또 구매: 1,000원 단위로 로또 구매 (1~45 범위의 중복되지 않는 6개 번호)
  • 당첨 확인: 당첨 번호 6개와 보너스 번호 1개를 입력받아 등수별 당첨 개수 계산
  • 수익률 계산: 총 당첨금 대비 구매 금액의 수익률을 소수점 둘째 자리에서 반올림하여 표시
  • 에러 처리: 잘못된 입력에 대해 [ERROR] 메시지 출력 후 재입력 요구

구현 특징

아키텍처 설계

  • 관심사 분리: MVC 패턴 대신 도메인 모델과 뷰의 명확한 분리
  • 단일 책임 원칙: 각 클래스가 하나의 명확한 책임을 가지도록 설계
  • 의존성 최소화: 도메인 객체가 UI에 의존하지 않도록 구현

핵심 클래스

  • Lotto: 로또 번호 6개를 관리하는 도메인 객체
  • LottoVender: 로또 발행을 담당하는 정적 클래스
  • LottoStatistician: 당첨 통계 계산을 담당하는 클래스
  • Validator: 입력값 검증을 위한 체이닝 방식의 유틸리티 클래스
  • CliLottoView: 콘솔 입출력을 담당하는 뷰 클래스

검증 시스템

체이닝 방식의 Validator 클래스로 다양한 검증 로직을 재사용 가능하게 구현:

new Validator(numbers)
  .len(6)
  .onlyInt()
  .inRange(1, 45)
  .unique()

에러 처리

  • 커스텀 에러 클래스 계층 구조 (CustomError → 각 도메인별 에러)
  • 모든 에러 메시지에 [ERROR] 접두사 자동 추가
  • 입력 단계별로 적절한 에러 메시지 제공

테스트 전략

  • 단위 테스트: 각 클래스별로 독립적인 테스트 작성
  • 통합 테스트: ApplicationTest.js에서 전체 플로우 검증
  • 모킹: 외부 의존성(Random, Console) 모킹으로 테스트 안정성 확보
  • 예외 케이스: 다양한 잘못된 입력에 대한 에러 처리 검증

코드 품질

  • ESLint + Prettier: 일관된 코드 스타일 유지
  • Husky: 커밋 전 자동 린팅으로 코드 품질 보장
  • Jest: 포괄적인 테스트 커버리지
  • ES6+ 문법: 클래스, 화살표 함수, 구조 분해 할당 등 모던 JavaScript 활용

파일 구조

src/
├── App.js                    # 메인 애플리케이션 클래스
├── CliLottoView.js          # 콘솔 입출력 담당
├── const/                   # 상수 정의
├── error/                   # 커스텀 에러 클래스들
├── model/                   # 도메인 모델 클래스들
│   ├── Lotto.js
│   ├── LottoVender.js
│   └── LottoStatistician.js
└── util/                    # 유틸리티 클래스
    └── Validator.js

- When generating `Lotto` instance, it validates the input by it's own.
- `Lotto` class saves the numbers in ascending order .
- Using private field for numbers and add getter for data fetch.
This class is for centralized validation.

It is desinged to be used for various kinds of data.
Currently it supports the following validation for the data.
- if data is only constitued with integer
- if every data has a unique value
- if the data satisfies certain length
- if every data is in certain range
Update the validation code inside the `Lotto.js` to use the centralized validator class.
Add `CustomError.js` to generate consistant errors.
Now it can validate if the data only consist with specific class of subclasses of that class.
This class is for generating lotto instances.
It gives lottos according to the amount of input.
LottoStatistician can generate statistic with a given numbers and lottos.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant