diff --git a/README.md b/README.md index 15bb106b5..0b31475e4 100644 --- a/README.md +++ b/README.md @@ -1 +1,27 @@ # javascript-lotto-precourse + +## 기능 요구 사항 +- 1 <= 로또의 숫자 <= 45 +- **중복**되지 않는 6개의 숫자 + 보너스 숫자 +- 당첨 기능 구현 + 1. 6개 번호 일치 / 2,000,000,000원 + 2. 5개 번호 + 보너스 번호 일치 / 30,000,000 + 3. 5개 번호 / 1,500,000 + 4. 4개 번호 / 50,000 + 5. 3개 번호 / 5,000 +- 로또구매금액 == 1000 +- if (구매금액%1000) 예외 처리 + +## 입출력 요구 사항 +- 구입_금액 입력 1000의 배수가 아니라면 예외처리 +- 당첨번호 6개 입력. "," 기준으로 구분 + - 1 <= 번호 <= 45 사이의 수 + - 중복 X + - 숫자 확인 +- 보너스 번호 입력 + - 1 <= 번호 <= 45 사이의 수 + - 중복 X + - 숫자 확인 +- 구입_금액//1000 개의 로또를 랜덤으로 생성 및 출력 + 숫자 순으로 정렬 +- 당첨내역 출력 +- 수익률 출력 \ No newline at end of file diff --git a/src/App.js b/src/App.js index 091aa0a5d..5c8755255 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,31 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +import Lotto from "./Lotto.js"; + class App { - async run() {} + async run() { + const PRICE = await funcLoop("구입금액을 입력해 주세요.\n", Lotto.checkMoney); + const TICKETS = Lotto.buyTickets(PRICE); + + // while (TICKETS[0]) { + // MissionUtils.Console.print(TICKETS.pop().getters()) + // } + const WINNUMS = await funcLoop("당첨 번호를 입력해 주세요.\n", Lotto.setWinNum); + MissionUtils.Console.print(WINNUMS.getters()) + const BONUSNUM = await funcLoop("보너스 번호를 입력해 주세요.\n", Lotto.setBonusNum, WINNUMS.getters()); + } +} + +async function funcLoop(getInput, callback, ...rest) { + while (true) { + try { + let input = await MissionUtils.Console.readLineAsync(getInput); + const RESULT = callback(input, ...rest); + MissionUtils.Console.print(""); + return RESULT; + } catch (error) { + MissionUtils.Console.print(error); + } + } } -export default App; +export default App; \ No newline at end of file diff --git a/src/App1.js b/src/App1.js new file mode 100644 index 000000000..64988176a --- /dev/null +++ b/src/App1.js @@ -0,0 +1,60 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; +import Lotto from "./Lotto.js"; + +class App { + async run() { + try { + const PURCHASE = await funcLoop(Lotto.checkPurchase(MissionUtils.Console.readLineAsync('구입 금액을 입력해 주세요.\n'))); + // const PURCHASE = await Lotto.loopCheckPurchase(); + // MissionUtils.Console.print(PURCHASE); + // const TICKETS = await Lotto.buyLottos(PURCHASE); + + // input = await MissionUtils.Console.readLineAsync('당첨 번호를 입력해 주세요.\n'); + // TICKETS.forEach((value) => { + // console.log(value.getters()) + // }) + // const lotto = new Lotto(); + // const WINNUMS = await MissionUtils.Console.readLineAsync('당첨 번호를 입력해 주세요.\n'); + // MissionUtils.Console.print(""); + // setWINNUMS() + // const BONUSNUM = await MissionUtils.Console.readLineAsync('보너스 번호를 입력해 주세요.\n'); + // setBONUSNUM() + + // showResult() + } catch (error) { + throw new Error(error.message); + } + } +} + +async function funcLoop(callback) { + while (true) { + try { + return await callback() + } catch (error) { + throw new Error(error.message) + } + } +} + +async function buyLottos() { + while (true) { + try { + let input = await MissionUtils.Console.readLineAsync('구입 금액을 입력해 주세요.\n'); + const PURCHASE = Lotto.checkPurchase(input); + return PURCHASE + } catch (error) { + + } + } +} +// function checkPruchase(num) { +// if (Number(num)) { +// if (num%1000) { +// throw new Error("[ERROR] 구입 금액은 1,000의 배수여야 합니다."); +// } + +// } +// } + +export default App; diff --git a/src/Lotto.js b/src/Lotto.js index cb0b1527e..54a502f24 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -1,3 +1,5 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + class Lotto { #numbers; @@ -10,9 +12,92 @@ class Lotto { if (numbers.length !== 6) { throw new Error("[ERROR] 로또 번호는 6개여야 합니다."); } + + // numbers.forEach(element => { + // if ((element < 1) || (45 < element)) { + // throw new Error("[ERROR] 로또 번호는 1과 45사이여야 합니다."); + // } + // }); + const DUP = new Set([]) + numbers.forEach(element => { + DUP.add(element) + if (isNaN(element)) { + throw new Error("[Error] 입력이 숫자가 아닙니다.") + } + if ((element < 1) || (45 < element)) { + throw new Error("[ERROR] 로또 번호는 1과 45사이여야 합니다."); + } + }); + if (DUP.size !== 6) { + throw new Error("[ERROR] 로또 번호는 중복이 없어야 합니다."); + } + + // const DUP = new Set([]) + // numbers.forEach(element => { + // DUP.add(element) + // }) + // if (DUP.length !== 6) { + // throw new Error("[ERROR] 로또 번호는 중복이 없어야 합니다."); + // } } // TODO: 추가 기능 구현 + static checkMoney(input) { + const NUM = Number(input); + // console.log(NUM) + if (isNaN(NUM)) { + throw new Error("[Error] 입력이 숫자가 아닙니다.") + } + if ((NUM < 0)) { + throw new Error("[Error] 입력이 양수가 아닙니다.") + } + if ((NUM % 1000)) { + throw new Error("[Error] 입력이 1000의 배수가 아닙니다.") + } + return NUM + } + + static buyTickets(num) { + const NUM = num / 1000; + MissionUtils.Console.print(NUM + "개를 구매했습니다.") + const TICKETS = []; + + for (let i = 0; i < NUM; i++) { + const LOTTO = new Lotto(MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b)); + MissionUtils.Console.print(LOTTO.#numbers); + TICKETS.push(LOTTO); + } + return TICKETS + } + + static setWinNum(input) { + const NUMS = input.trim().split(",").map(Number).sort((a, b) => a - b); + const WINNUMS = new Lotto(NUMS); + return WINNUMS; + } + // let asdf = "1,2,3,4,5,6" + // let abc = asdf.split(",").trim(); + + static setBonusNum(input, WINNUMS) { + const BONUSNUM = Number(input); + if (isNaN(BONUSNUM)) { + throw new Error("[Error] 입력이 숫자가 아닙니다.") + } + if ((BONUSNUM < 1) || (45 < BONUSNUM)) { + throw new Error("[ERROR] 로또 번호는 1과 45사이여야 합니다."); + } + WINNUMS.forEach(element => { + // MissionUtils.Console.print(element); + if (BONUSNUM == element) { + throw new Error("[ERROR] 보너스 번호가 로또 번호와 중복되었습니다."); + } + }) + return BONUSNUM + } + + getters() { + return this.#numbers; + } } -export default Lotto; +export default Lotto; \ No newline at end of file diff --git a/src/Lotto1.js b/src/Lotto1.js new file mode 100644 index 000000000..102bf476d --- /dev/null +++ b/src/Lotto1.js @@ -0,0 +1,90 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + +class Lotto { + #numbers; + + constructor(numbers) { + this.#validate(numbers); + this.#numbers = numbers; + + } + + #validate(numbers) { + if (numbers.length !== 6) { + throw new Error("[ERROR] 로또 번호는 6개여야 합니다."); + } + // if (numbers) + } + + // static funcLoop(callback) { + // while (true) { + // try { + // return callback() + // } catch (error) { + // throw new Error(error.message) + // } + // } + // } + + static checkPurchase(callback) { + const NUM = Number(callback); + // const NUM = Number(num); + if (NUM) { + if (NUM % 1000) { + throw new Error("[ERROR] 구입 금액은 1,000의 배수여야 합니다."); + } + return NUM + } + throw new Error("[ERROR] 구입 금액은 숫자여야 합니다."); + } + + static async loopCheckPurchase(num) { + return funcLoop(() => this.checkPurchase(num)) + } + + static async buyLottos(num) { + const TICKET = parseInt(num / 1000); + const TICKETS = [] + MissionUtils.Console.print(TICKET + "개를 구매했습니다."); + // MissionUtils.Console.print(); + for (let i = 0; i < TICKET; i++) { + // const RANNUM = MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b); + // MissionUtils.Console.print(RANNUM); + const LOTTO = new Lotto(MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b)); + MissionUtils.Console.print(LOTTO.#numbers); + // MissionUtils.Console.print(LOTTO.getters()); + TICKETS.push(LOTTO); + // MissionUtils.Console.print(Lotto.genLotto()); + } + return TICKETS; + } + + static async loopBuyLottos(num) { + return funcLoop(() => this.buyLottos(num)) + } + + static getWinNum() { + // const + // return + } + + getters() { + return this.#numbers; + } + // genLotto() { + // return MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b); + // } + // TODO: 추가 기능 구현 +} + +async function funcLoop(callback) { + while (true) { + try { + return await callback() + } catch (error) { + throw new Error(error.message) + } + } +} + +export default Lotto;