From 08823c18e445749356d39cad5478fc66d73b60ee Mon Sep 17 00:00:00 2001 From: Juyudang Date: Sat, 2 Nov 2024 14:15:43 +0900 Subject: [PATCH 1/7] Docs: Edit README.md Write down what to make & how to make. --- README.md | 26 ++++++++++++++++++++++++++ src/App.js | 7 ++++++- 2 files changed, 32 insertions(+), 1 deletion(-) 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..195cb8d96 100644 --- a/src/App.js +++ b/src/App.js @@ -1,5 +1,10 @@ +import lotto from "./Lotto.js"; class App { - async run() {} + async run() { + let arr3 = [1100, 2, 30] + console.log(arr3.sort((a, b) => a - b)) + + } } export default App; From c9cd80e1197181365b78c1a88444b54c62e8a44b Mon Sep 17 00:00:00 2001 From: Juyudang Date: Sat, 2 Nov 2024 19:44:13 +0900 Subject: [PATCH 2/7] Feat: Add Purchase input check Make "Lotto.checkPurchase(input)" for checking the input is fit for Purchase requirements --- src/App.js | 30 +++++++++++++++++++++++++++--- src/Lotto.js | 13 +++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/App.js b/src/App.js index 195cb8d96..ef9bf9359 100644 --- a/src/App.js +++ b/src/App.js @@ -1,10 +1,34 @@ -import lotto from "./Lotto.js"; +import { MissionUtils } from "@woowacourse/mission-utils"; +import Lotto from "./Lotto.js"; + class App { async run() { - let arr3 = [1100, 2, 30] - console.log(arr3.sort((a, b) => a - b)) + try { + let input = await MissionUtils.Console.readLineAsync('구입 금액을 입력해 주세요.\n'); + const PURCHASE = Lotto.checkPurchase(input); + MissionUtils.Console.print(PURCHASE); + // buyLottos() + // 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); + } } } +// 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..8ca891819 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -1,3 +1,5 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + class Lotto { #numbers; @@ -12,6 +14,17 @@ class Lotto { } } + static checkPurchase(num) { + const NUM = Number(num); + if (NUM) { + if (NUM % 1000) { + throw new Error("[ERROR] 구입 금액은 1,000의 배수여야 합니다."); + } + return NUM + } + throw new Error("[ERROR] 구입 금액은 숫자여야 합니다."); + } + // TODO: 추가 기능 구현 } From 978eed34453eaf6e5568c83bb468f35ec490305a Mon Sep 17 00:00:00 2001 From: Juyudang Date: Sat, 2 Nov 2024 21:04:28 +0900 Subject: [PATCH 3/7] Feat: Make Lotto.buyLottos Make Lotto.buyLottos() to make a list of Lottos containing users Lottos --- src/App.js | 8 ++++++-- src/Lotto.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/App.js b/src/App.js index ef9bf9359..cdf91ff9a 100644 --- a/src/App.js +++ b/src/App.js @@ -6,8 +6,12 @@ class App { try { let input = await MissionUtils.Console.readLineAsync('구입 금액을 입력해 주세요.\n'); const PURCHASE = Lotto.checkPurchase(input); - MissionUtils.Console.print(PURCHASE); - // buyLottos() + // MissionUtils.Console.print(PURCHASE); + const TICKETS = Lotto.buyLottos(PURCHASE); + + // TICKETS.forEach((value) => { + // console.log(value.getters()) + // }) // const lotto = new Lotto(); // const WINNUMS = await MissionUtils.Console.readLineAsync('당첨 번호를 입력해 주세요.\n'); // MissionUtils.Console.print(""); diff --git a/src/Lotto.js b/src/Lotto.js index 8ca891819..25f860eab 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -25,6 +25,29 @@ class Lotto { throw new Error("[ERROR] 구입 금액은 숫자여야 합니다."); } + static 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; + } + + getters() { + return this.#numbers; + } + // genLotto() { + // return MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b); + // } // TODO: 추가 기능 구현 } From aeae23760a9b953c37e2ba6936b6877753e41a19 Mon Sep 17 00:00:00 2001 From: Juyudang Date: Mon, 4 Nov 2024 22:31:11 +0900 Subject: [PATCH 4/7] Feat: Make get input with loop By making funcLoop(getInput, callback) get input everytime after get error. --- src/App.js | 38 +++++++--------------- src/App1.js | 60 ++++++++++++++++++++++++++++++++++ src/Lotto.js | 48 ++++++++------------------- src/Lotto1.js | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 174 insertions(+), 62 deletions(-) create mode 100644 src/App1.js create mode 100644 src/Lotto1.js diff --git a/src/App.js b/src/App.js index cdf91ff9a..6203600de 100644 --- a/src/App.js +++ b/src/App.js @@ -3,36 +3,20 @@ import Lotto from "./Lotto.js"; class App { async run() { - try { - let input = await MissionUtils.Console.readLineAsync('구입 금액을 입력해 주세요.\n'); - const PURCHASE = Lotto.checkPurchase(input); - // MissionUtils.Console.print(PURCHASE); - const TICKETS = Lotto.buyLottos(PURCHASE); - - // 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() + const PRICE = await funcLoop("구입금액을 입력해 주세요.\n", Lotto.checkMoney); + } +} - // showResult() +async function funcLoop(getInput, callback) { + while (true) { + try { + let input = await MissionUtils.Console.readLineAsync(getInput); + const RESULT = callback(input); + return RESULT; } catch (error) { - throw new Error(error.message); + MissionUtils.Console.print(error); } } } -// function checkPruchase(num) { -// if (Number(num)) { -// if (num%1000) { -// throw new Error("[ERROR] 구입 금액은 1,000의 배수여야 합니다."); -// } - -// } -// } - -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 25f860eab..66b3840d4 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -1,5 +1,3 @@ -import { MissionUtils } from "@woowacourse/mission-utils"; - class Lotto { #numbers; @@ -14,41 +12,21 @@ class Lotto { } } - static checkPurchase(num) { - const NUM = Number(num); - if (NUM) { - if (NUM % 1000) { - throw new Error("[ERROR] 구입 금액은 1,000의 배수여야 합니다."); - } - return NUM + // TODO: 추가 기능 구현 + static checkMoney(input) { + const NUM = Number(input); + // console.log(NUM) + if (isNaN(NUM)) { + throw new Error("[Error] 입력이 숫자가 아닙니다.") } - throw new Error("[ERROR] 구입 금액은 숫자여야 합니다."); - } - - static 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()); + if ((NUM < 0)) { + throw new Error("[Error] 입력이 양수가 아닙니다.") } - return TICKETS; - } - - getters() { - return this.#numbers; + if ((NUM % 1000)) { + throw new Error("[Error] 입력이 1000의 배수가 아닙니다.") + } + return NUM } - // genLotto() { - // return MissionUtils.Random.pickUniqueNumbersInRange(1, 45, 6).sort((a, b) => a - b); - // } - // TODO: 추가 기능 구현 } -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; From 72e6fc8b36d521aa9c6ae7b7ae071f64dd57cf35 Mon Sep 17 00:00:00 2001 From: Juyudang Date: Mon, 4 Nov 2024 22:42:25 +0900 Subject: [PATCH 5/7] Feat: Make Lotto.buyTickets(num) After get PRICE, make PRICE/1000 TICKETS. In Lotto class, can get \#numbers by Lotto.#numbers, but out of Lotto class, get \#numbers by getters() --- src/App.js | 6 ++++++ src/Lotto.js | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/App.js b/src/App.js index 6203600de..bde9b1180 100644 --- a/src/App.js +++ b/src/App.js @@ -4,6 +4,12 @@ import Lotto from "./Lotto.js"; class App { 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); } } diff --git a/src/Lotto.js b/src/Lotto.js index 66b3840d4..c74342166 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -1,3 +1,5 @@ +import { MissionUtils } from "@woowacourse/mission-utils"; + class Lotto { #numbers; @@ -27,6 +29,24 @@ class Lotto { } 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 + } + + + getters() { + return this.#numbers; + } } export default Lotto; \ No newline at end of file From 22b8f7e7bb9bd6e11d3f96b1bb46f7dfa60f4cb4 Mon Sep 17 00:00:00 2001 From: Juyudang Date: Mon, 4 Nov 2024 23:16:17 +0900 Subject: [PATCH 6/7] Feat: Make setWinNum(string) & Add more validations of Lotto Make setWinNum(string) to get user input and setting WINNUMS Add more validations of Lotto constructor to check - check type - check range of number - check duplication --- src/App.js | 9 +++++---- src/Lotto.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index bde9b1180..8de1e24f2 100644 --- a/src/App.js +++ b/src/App.js @@ -6,10 +6,10 @@ class App { 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); + // while (TICKETS[0]) { + // MissionUtils.Console.print(TICKETS.pop().getters()) + // } + const WINNUMS = await funcLoop("당첨 번호를 입력해 주세요.\n", Lotto.setWinNum); } } @@ -18,6 +18,7 @@ async function funcLoop(getInput, callback) { try { let input = await MissionUtils.Console.readLineAsync(getInput); const RESULT = callback(input); + MissionUtils.Console.print(""); return RESULT; } catch (error) { MissionUtils.Console.print(error); diff --git a/src/Lotto.js b/src/Lotto.js index c74342166..f10dd9a79 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -12,6 +12,33 @@ 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: 추가 기능 구현 @@ -43,6 +70,14 @@ class Lotto { return TICKETS } + static setWinNum(string) { + const NUMS = string.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(); + getters() { return this.#numbers; From dce7e8b0551e87abc2a0f7891414d5927cd924b3 Mon Sep 17 00:00:00 2001 From: Juyudang Date: Mon, 4 Nov 2024 23:51:50 +0900 Subject: [PATCH 7/7] Feat: Make setBonusNum() & change funcLoop() Making funcLoop() to get multiple parameters by ...rest --- src/App.js | 6 ++++-- src/Lotto.js | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/App.js b/src/App.js index 8de1e24f2..5c8755255 100644 --- a/src/App.js +++ b/src/App.js @@ -10,14 +10,16 @@ class App { // 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) { +async function funcLoop(getInput, callback, ...rest) { while (true) { try { let input = await MissionUtils.Console.readLineAsync(getInput); - const RESULT = callback(input); + const RESULT = callback(input, ...rest); MissionUtils.Console.print(""); return RESULT; } catch (error) { diff --git a/src/Lotto.js b/src/Lotto.js index f10dd9a79..54a502f24 100644 --- a/src/Lotto.js +++ b/src/Lotto.js @@ -70,14 +70,30 @@ class Lotto { return TICKETS } - static setWinNum(string) { - const NUMS = string.trim().split(",").map(Number).sort((a, b) => a - b); + 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;