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

[로또 게임 미션] 박세호 제출합니다. #647

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
feat: 당첨자 처리 구현
sayyyho committed Nov 8, 2023
commit f2b5402539b1f38244113dba5fadd7b0682864f4
42 changes: 39 additions & 3 deletions src/Lotto.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,54 @@
import InputView from './views/InputView';
import ResultAnalyzer from './models/ResultAnalyzer';

class Lotto {
#numbers;

constructor(numbers) {
#bonusNumber;

#resultAnalizer;

#ticketList;

constructor(numbers, ticketList) {
this.#validate(numbers);
this.#numbers = numbers;
this.#ticketList = ticketList;
}

#validate(numbers) {
if (numbers.length !== 6) {
throw new Error("[ERROR] 로또 번호는 6개여야 합니다.");
throw new Error('[ERROR] 로또 번호는 6개여야 합니다.');
}
this.makeBonusNumber();
}

// TODO: 추가 기능 구현
makeBonusNumber() {
const bonusNumber = InputView.InputBonusNumber();
this.confirmNumber(bonusNumber);
}

confirmNumber(bonusNumber) {
if (bonusNumber < 1 || bonusNumber > 45) {
throw new Error('[ERROR] 로또 번호는 1에서 45 사이 숫자만 가능합니다.');
}
this.#bonusNumber = bonusNumber;
this.noticeNumber();
}

noticeNumber() {
this.#resultAnalizer = new ResultAnalyzer();
this.#resultAnalizer(this.#numbers, this.#bonusNumber, this.#ticketList);
this.getResult();
}

getResult() {
this.#resultAnalizer.getResult();
}
}

export default Lotto;

// 여기서 셀러한테 보냈다가
// 셀러가 해당 정보들로 결과 판별 머신에 넣어서
// 결과 보여주는 느낌으로 가야할듯
51 changes: 51 additions & 0 deletions src/models/ResultAnalyzer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class ResultAnalyzer {
#winningNumber;

#bonusNumber;

#ticketList;

#result;

#prize;

constructor(winningNumber, bonusNumber, ticketList) {
this.#winningNumber = winningNumber;
this.#bonusNumber = bonusNumber;
this.#ticketList = ticketList;
this.#prize = {
3: 0,
4: 0,
5: 0,
6: 0,
bonus: 0,
};
}

getResult() {
this.findResult();
}

findResult() {
this.#ticketList.forEach((ticket) => {
this.countCorrect(ticket);
});
}

countCorrect(ticket) {
let count = 0;
ticket.forEach((number) => {
if (this.winningNumber.includes(number)) {
count += 1;
}
});
if (count === 5 && ticket.includes(this.#bonusNumber)) {
this.#prize.bonus += 1;
}
if (count > 2) {
this.#prize.count += 1;
}
}
}

export default ResultAnalyzer;
18 changes: 18 additions & 0 deletions src/models/Seller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import InputView from '../views/InputView';
import OutputView from '../views/OutputView';
import LottoMachine from './LottoMachine';
import Lotto from '../Lotto';

class Seller {
#lottoMachine;

#lotto;

constructor() {
this.ticketList = [];
this.result = [];
}

setAmount(money) {
@@ -21,6 +26,19 @@ class Seller {
this.ticketList.push(ticket);
}
OutputView.printTickets(this.ticketList);
this.makeWinnigNumber();
}

makeWinnigNumber() {
const winningNumber = InputView.InputWinningNumber();
this.#lotto = new Lotto(winningNumber, this.ticketList);
this.#lotto.makeBonusNumber();
this.printResult();
}

printResult() {
const result = this.#lotto.getResult();
OutputView.printResult(result);
}
}

14 changes: 14 additions & 0 deletions src/views/InputView.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,20 @@ const InputView = {
validate.isValidate(money);
return money;
},
async InputWinningNumber() {
const winningNumber = parseInt(
await Console.readLineAsync('당첨 번호를 입력해 주세요.'),
10,
);
return winningNumber;
},
async InputBonusNumber() {
const bonusNumber = parseInt(
await Console.readLineAsync('보너스 번호를 입력해 주세요.'),
10,
);
return bonusNumber;
},
};

export default InputView;
1 change: 1 addition & 0 deletions src/views/OutputView.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const OutputView = {
Console.print(ticket);
});
},
printResult() {},
};

export default OutputView;