From 616246a54fb282c26e0cb2ee92f574acc15b3cb7 Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Sun, 2 Nov 2025 22:11:50 +0900 Subject: [PATCH 01/20] =?UTF-8?q?feat:=20(=EC=9E=85=EB=A0=A5)=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EA=B5=AC=EC=9E=85=20=EA=B8=88=EC=95=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 24 ++++++++++++++++++++ src/main/java/validator/Validator.java | 28 ++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/main/java/lotto/LottoController.java create mode 100644 src/main/java/validator/Validator.java diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java new file mode 100644 index 0000000000..402f47a9cf --- /dev/null +++ b/src/main/java/lotto/LottoController.java @@ -0,0 +1,24 @@ +package lotto; + +import camp.nextstep.edu.missionutils.Console; +import validator.Validator; + +public class LottoController { + public static void main(String[] args){ + // 입력1: 로또 구입 금액 + System.out.println("구입금액을 입력해 주세요."); + int purchaseAmount = Validator.checkDivisibility(Console.readLine(), 1000); + + // 입력2: 당첨 번호 + + // 입력3: 보너스 번호 + + // 출력1: 발행 로또 수량 + + // 출력2: 발행 로또 번호 + + // 출력3: 당첨 내역 + + // 출력4: 수익률 + } +} diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java new file mode 100644 index 0000000000..45dcf8193f --- /dev/null +++ b/src/main/java/validator/Validator.java @@ -0,0 +1,28 @@ +package validator; + +public class Validator { + + // 1000원 단위로 나눠떨어지는지 확인 + public static int checkDivisibility(String number, int minUnit){ + + int amount = convertToNumber(number); + + if(amount % minUnit != 0){ + throw new IllegalArgumentException("[ERROR] 최소 입력 단위는 "+minUnit+"원입니다."); + } + return amount; + } + + // 숫자인지 확인 + public static int convertToNumber(String number){ + if(number.isEmpty() || number.trim().isEmpty()){ + throw new IllegalArgumentException("[ERROR] 빈 값을 입력할 수 없습니다."); + } + + try{ + return Integer.parseInt(number); + }catch(Exception e){ + throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다."); + } + } +} From 99f8fec7e263a8e86c13d9eada034dfd4ba3a7cc Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Sun, 2 Nov 2025 22:15:53 +0900 Subject: [PATCH 02/20] =?UTF-8?q?feat:=20(=EC=B6=9C=EB=A0=A5)=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89=20=EB=A1=9C=EB=98=90=20=EC=88=98=EB=9F=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 402f47a9cf..a744d968c5 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -9,12 +9,14 @@ public static void main(String[] args){ System.out.println("구입금액을 입력해 주세요."); int purchaseAmount = Validator.checkDivisibility(Console.readLine(), 1000); + // 출력1: 발행 로또 수량 + System.out.printf("%d개를 구매했습니다.\n",purchaseAmount/1000); + // 입력2: 당첨 번호 + System.out.println("구입금액을 입력해 주세요."); // 입력3: 보너스 번호 - // 출력1: 발행 로또 수량 - // 출력2: 발행 로또 번호 // 출력3: 당첨 내역 From 3b20056be749149ca2b586c066977358278bfddd Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Sun, 2 Nov 2025 23:48:05 +0900 Subject: [PATCH 03/20] =?UTF-8?q?feat:=20(=EC=9E=85=EB=A0=A5)=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EB=B2=88=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 5 ++++- src/main/java/validator/Validator.java | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index a744d968c5..c92112a279 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -3,6 +3,8 @@ import camp.nextstep.edu.missionutils.Console; import validator.Validator; +import java.util.List; + public class LottoController { public static void main(String[] args){ // 입력1: 로또 구입 금액 @@ -13,7 +15,8 @@ public static void main(String[] args){ System.out.printf("%d개를 구매했습니다.\n",purchaseAmount/1000); // 입력2: 당첨 번호 - System.out.println("구입금액을 입력해 주세요."); + System.out.println("당첨 번호를 입력해 주세요."); + List winningNumbers = Validator.convertToList(Console.readLine(),","); // 입력3: 보너스 번호 diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java index 45dcf8193f..47f51785a0 100644 --- a/src/main/java/validator/Validator.java +++ b/src/main/java/validator/Validator.java @@ -1,5 +1,8 @@ package validator; +import java.util.ArrayList; +import java.util.List; + public class Validator { // 1000원 단위로 나눠떨어지는지 확인 @@ -25,4 +28,24 @@ public static int convertToNumber(String number){ throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다."); } } + + // 구분자 기준 숫자 리스트 변환 + public static List convertToList(String LottoNumbers,String separtor){ + + try{ + // 구분자 기준 분리 + String[] tempNums = LottoNumbers.split(separtor); + + // String[] → List로 변환 + List numberList = new ArrayList<>(); + for(String number:tempNums){ + numberList.add(Integer.parseInt(number)); + } + return numberList; + + }catch(Exception e){ + throw new IllegalArgumentException("[ERROR] 숫자, 구분자(" + +separtor+") 이외의 값은 입력할 수 없습니다."); + } + } } From 061479c6416dcaedf2e09616ef7ed76f76fcbd16 Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Mon, 3 Nov 2025 00:06:28 +0900 Subject: [PATCH 04/20] =?UTF-8?q?refactor:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EC=88=98=EB=9F=89=20=EB=B3=80=EC=88=98=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index c92112a279..61b609186c 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -9,10 +9,11 @@ public class LottoController { public static void main(String[] args){ // 입력1: 로또 구입 금액 System.out.println("구입금액을 입력해 주세요."); - int purchaseAmount = Validator.checkDivisibility(Console.readLine(), 1000); + int purchasePrice = Validator.checkDivisibility(Console.readLine(), 1000); // 출력1: 발행 로또 수량 - System.out.printf("%d개를 구매했습니다.\n",purchaseAmount/1000); + int purchaseAmount = purchasePrice/1000; + System.out.printf("%d개를 구매했습니다.\n",purchaseAmount); // 입력2: 당첨 번호 System.out.println("당첨 번호를 입력해 주세요."); From a3b548d8d1cce53ef23f6320c88c83c5805abc39 Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Mon, 3 Nov 2025 00:11:49 +0900 Subject: [PATCH 05/20] =?UTF-8?q?feat:=20(=EC=9E=85=EB=A0=A5)=20=EB=B3=B4?= =?UTF-8?q?=EB=84=88=EC=8A=A4=20=EB=B2=88=ED=98=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 61b609186c..ca5d0975e6 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -20,6 +20,8 @@ public static void main(String[] args){ List winningNumbers = Validator.convertToList(Console.readLine(),","); // 입력3: 보너스 번호 + System.out.println("보너스 번호를 입력해 주세요."); + int bonusNumber = Validator.convertToNumber(Console.readLine()); // 출력2: 발행 로또 번호 From 6757743005fa94f2a1758e0a083293aaa97cbe27 Mon Sep 17 00:00:00 2001 From: Yujin Date: Mon, 3 Nov 2025 09:55:05 +0900 Subject: [PATCH 06/20] =?UTF-8?q?feat:=201~45=20=EC=82=AC=EC=9D=B4=20?= =?UTF-8?q?=EC=88=AB=EC=9E=90=20=EA=B2=80=EC=A6=9D=20=EB=A1=9C=EC=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 70 ++-- README.md | 2 +- build.gradle | 50 +-- gradle/wrapper/gradle-wrapper.properties | 14 +- gradlew | 502 +++++++++++------------ settings.gradle | 8 +- src/main/java/lotto/Application.java | 14 +- src/main/java/lotto/Lotto.java | 40 +- src/main/java/lotto/LottoController.java | 64 +-- src/main/java/lotto/LottoService.java | 7 + src/main/java/validator/Validator.java | 108 ++--- src/test/java/lotto/ApplicationTest.java | 122 +++--- src/test/java/lotto/LottoTest.java | 50 +-- 13 files changed, 532 insertions(+), 519 deletions(-) create mode 100644 src/main/java/lotto/LottoService.java diff --git a/.gitignore b/.gitignore index 5dca701a77..0a0ce971c0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,35 +1,35 @@ -HELP.md -.gradle -build/ -!gradle/wrapper/gradle-wrapper.jar -!**/src/main/** -!**/src/test/** - -### STS ### -.apt_generated -.classpath -.factorypath -.project -.settings -.springBeans -.sts4-cache - -### IntelliJ IDEA ### -.idea -*.iws -*.iml -*.ipr -out/ - -### NetBeans ### -/nbproject/private/ -/nbbuild/ -/dist/ -/nbdist/ -/.nb-gradle/ - -### VS Code ### -.vscode/ - -### Mac OS ### -.DS_Store +HELP.md +.gradle +build/ +!gradle/wrapper/gradle-wrapper.jar +!**/src/main/** +!**/src/test/** + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr +out/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store diff --git a/README.md b/README.md index 5fa2560b46..e995005d86 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# java-lotto-precourse +# java-lotto-precourse diff --git a/build.gradle b/build.gradle index ae93bdf87f..c1cd8df4fa 100644 --- a/build.gradle +++ b/build.gradle @@ -1,25 +1,25 @@ -plugins { - id 'java' -} - -group = 'camp.nextstep.edu' -version = '1.0-SNAPSHOT' - -java { - toolchain { - languageVersion = JavaLanguageVersion.of(21) - } -} - -repositories { - mavenCentral() - maven { url 'https://jitpack.io' } -} - -dependencies { - implementation 'com.github.woowacourse-projects:mission-utils:1.2.0' -} - -test { - useJUnitPlatform() -} +plugins { + id 'java' +} + +group = 'camp.nextstep.edu' +version = '1.0-SNAPSHOT' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} + +repositories { + mavenCentral() + maven { url 'https://jitpack.io' } +} + +dependencies { + implementation 'com.github.woowacourse-projects:mission-utils:1.2.0' +} + +test { + useJUnitPlatform() +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 37f853b1c8..7036d2e7d5 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,7 +1,7 @@ -distributionBase=GRADLE_USER_HOME -distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip -networkTimeout=10000 -validateDistributionUrl=true -zipStoreBase=GRADLE_USER_HOME -zipStorePath=wrapper/dists +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew index faf93008b7..ba132c9bba 100755 --- a/gradlew +++ b/gradlew @@ -1,251 +1,251 @@ -#!/bin/sh - -# -# Copyright © 2015-2021 the original authors. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -# SPDX-License-Identifier: Apache-2.0 -# - -############################################################################## -# -# Gradle start up script for POSIX generated by Gradle. -# -# Important for running: -# -# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is -# noncompliant, but you have some other compliant shell such as ksh or -# bash, then to run this script, type that shell name before the whole -# command line, like: -# -# ksh Gradle -# -# Busybox and similar reduced shells will NOT work, because this script -# requires all of these POSIX shell features: -# * functions; -# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», -# «${var#prefix}», «${var%suffix}», and «$( cmd )»; -# * compound commands having a testable exit status, especially «case»; -# * various built-in commands including «command», «set», and «ulimit». -# -# Important for patching: -# -# (2) This script targets any POSIX shell, so it avoids extensions provided -# by Bash, Ksh, etc; in particular arrays are avoided. -# -# The "traditional" practice of packing multiple parameters into a -# space-separated string is a well documented source of bugs and security -# problems, so this is (mostly) avoided, by progressively accumulating -# options in "$@", and eventually passing that to Java. -# -# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, -# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; -# see the in-line comments for details. -# -# There are tweaks for specific operating systems such as AIX, CygWin, -# Darwin, MinGW, and NonStop. -# -# (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt -# within the Gradle project. -# -# You can find Gradle at https://github.com/gradle/gradle/. -# -############################################################################## - -# Attempt to set APP_HOME - -# Resolve links: $0 may be a link -app_path=$0 - -# Need this for daisy-chained symlinks. -while - APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path - [ -h "$app_path" ] -do - ls=$( ls -ld "$app_path" ) - link=${ls#*' -> '} - case $link in #( - /*) app_path=$link ;; #( - *) app_path=$APP_HOME$link ;; - esac -done - -# This is normally unused -# shellcheck disable=SC2034 -APP_BASE_NAME=${0##*/} -# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit - -# Use the maximum available, or set MAX_FD != -1 to use that value. -MAX_FD=maximum - -warn () { - echo "$*" -} >&2 - -die () { - echo - echo "$*" - echo - exit 1 -} >&2 - -# OS specific support (must be 'true' or 'false'). -cygwin=false -msys=false -darwin=false -nonstop=false -case "$( uname )" in #( - CYGWIN* ) cygwin=true ;; #( - Darwin* ) darwin=true ;; #( - MSYS* | MINGW* ) msys=true ;; #( - NONSTOP* ) nonstop=true ;; -esac - -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar - - -# Determine the Java command to use to start the JVM. -if [ -n "$JAVA_HOME" ] ; then - if [ -x "$JAVA_HOME/jre/sh/java" ] ; then - # IBM's JDK on AIX uses strange locations for the executables - JAVACMD=$JAVA_HOME/jre/sh/java - else - JAVACMD=$JAVA_HOME/bin/java - fi - if [ ! -x "$JAVACMD" ] ; then - die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -else - JAVACMD=java - if ! command -v java >/dev/null 2>&1 - then - die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. - -Please set the JAVA_HOME variable in your environment to match the -location of your Java installation." - fi -fi - -# Increase the maximum file descriptors if we can. -if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then - case $MAX_FD in #( - max*) - # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC2039,SC3045 - MAX_FD=$( ulimit -H -n ) || - warn "Could not query maximum file descriptor limit" - esac - case $MAX_FD in #( - '' | soft) :;; #( - *) - # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. - # shellcheck disable=SC2039,SC3045 - ulimit -n "$MAX_FD" || - warn "Could not set maximum file descriptor limit to $MAX_FD" - esac -fi - -# Collect all arguments for the java command, stacking in reverse order: -# * args from the command line -# * the main class name -# * -classpath -# * -D...appname settings -# * --module-path (only if needed) -# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. - -# For Cygwin or MSYS, switch paths to Windows format before running java -if "$cygwin" || "$msys" ; then - APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) - - JAVACMD=$( cygpath --unix "$JAVACMD" ) - - # Now convert the arguments - kludge to limit ourselves to /bin/sh - for arg do - if - case $arg in #( - -*) false ;; # don't mess with options #( - /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath - [ -e "$t" ] ;; #( - *) false ;; - esac - then - arg=$( cygpath --path --ignore --mixed "$arg" ) - fi - # Roll the args list around exactly as many times as the number of - # args, so each arg winds up back in the position where it started, but - # possibly modified. - # - # NB: a `for` loop captures its iteration list before it begins, so - # changing the positional parameters here affects neither the number of - # iterations, nor the values presented in `arg`. - shift # remove old arg - set -- "$@" "$arg" # push replacement arg - done -fi - - -# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' - -# Collect all arguments for the java command: -# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, -# and any embedded shellness will be escaped. -# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be -# treated as '${Hostname}' itself on the command line. - -set -- \ - "-Dorg.gradle.appname=$APP_BASE_NAME" \ - -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ - "$@" - -# Stop when "xargs" is not available. -if ! command -v xargs >/dev/null 2>&1 -then - die "xargs is not available" -fi - -# Use "xargs" to parse quoted args. -# -# With -n1 it outputs one arg per line, with the quotes and backslashes removed. -# -# In Bash we could simply go: -# -# readarray ARGS < <( xargs -n1 <<<"$var" ) && -# set -- "${ARGS[@]}" "$@" -# -# but POSIX shell has neither arrays nor command substitution, so instead we -# post-process each arg (as a line of input to sed) to backslash-escape any -# character that might be a shell metacharacter, then use eval to reverse -# that process (while maintaining the separation between arguments), and wrap -# the whole thing up as a single "set" statement. -# -# This will of course break if any of these variables contains a newline or -# an unmatched quote. -# - -eval "set -- $( - printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | - xargs -n1 | - sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | - tr '\n' ' ' - )" '"$@"' - -exec "$JAVACMD" "$@" +#!/bin/sh + +# +# Copyright © 2015-2021 the original authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# + +############################################################################## +# +# Gradle start up script for POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD=maximum + +warn () { + echo "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "$( uname )" in #( + CYGWIN* ) cygwin=true ;; #( + Darwin* ) darwin=true ;; #( + MSYS* | MINGW* ) msys=true ;; #( + NONSTOP* ) nonstop=true ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD=$JAVA_HOME/jre/sh/java + else + JAVACMD=$JAVA_HOME/bin/java + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD=java + if ! command -v java >/dev/null 2>&1 + then + die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/settings.gradle b/settings.gradle index 364494c424..c7e6e3af86 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ -plugins { - id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0' -} -rootProject.name = 'java-lotto' +plugins { + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0' +} +rootProject.name = 'java-lotto' diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index d190922ba4..dec7a9bb9e 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -1,7 +1,7 @@ -package lotto; - -public class Application { - public static void main(String[] args) { - // TODO: 프로그램 구현 - } -} +package lotto; + +public class Application { + public static void main(String[] args) { + // TODO: 프로그램 구현 + } +} diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 88fc5cf12b..7d2d4704c8 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,20 +1,20 @@ -package lotto; - -import java.util.List; - -public class Lotto { - private final List numbers; - - public Lotto(List numbers) { - validate(numbers); - this.numbers = numbers; - } - - private void validate(List numbers) { - if (numbers.size() != 6) { - throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); - } - } - - // TODO: 추가 기능 구현 -} +package lotto; + +import java.util.List; + +public class Lotto { + private final List numbers; + + public Lotto(List numbers) { + validate(numbers); + this.numbers = numbers; + } + + private void validate(List numbers) { + if (numbers.size() != 6) { + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } + } + + // TODO: 추가 기능 구현 +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index ca5d0975e6..09e525c361 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -1,32 +1,32 @@ -package lotto; - -import camp.nextstep.edu.missionutils.Console; -import validator.Validator; - -import java.util.List; - -public class LottoController { - public static void main(String[] args){ - // 입력1: 로또 구입 금액 - System.out.println("구입금액을 입력해 주세요."); - int purchasePrice = Validator.checkDivisibility(Console.readLine(), 1000); - - // 출력1: 발행 로또 수량 - int purchaseAmount = purchasePrice/1000; - System.out.printf("%d개를 구매했습니다.\n",purchaseAmount); - - // 입력2: 당첨 번호 - System.out.println("당첨 번호를 입력해 주세요."); - List winningNumbers = Validator.convertToList(Console.readLine(),","); - - // 입력3: 보너스 번호 - System.out.println("보너스 번호를 입력해 주세요."); - int bonusNumber = Validator.convertToNumber(Console.readLine()); - - // 출력2: 발행 로또 번호 - - // 출력3: 당첨 내역 - - // 출력4: 수익률 - } -} +package lotto; + +import camp.nextstep.edu.missionutils.Console; +import validator.Validator; + +import java.util.List; + +public class LottoController { + public static void main(String[] args){ + // 입력1: 로또 구입 금액 + System.out.println("구입금액을 입력해 주세요."); + int purchasePrice = Validator.checkDivisibility(Console.readLine(), 1000); + + // 출력1: 발행 로또 수량 + int purchaseAmount = purchasePrice/1000; + System.out.printf("%d개를 구매했습니다.\n",purchaseAmount); + + // 입력2: 당첨 번호 + System.out.println("당첨 번호를 입력해 주세요."); + List winningNumbers = Validator.convertToList(Console.readLine(),","); + + // 입력3: 보너스 번호 + System.out.println("보너스 번호를 입력해 주세요."); + int bonusNumber = Validator.convertToNumber(Console.readLine()); + + // 출력2: 발행 로또 번호 + + // 출력3: 당첨 내역 + + // 출력4: 수익률 + } +} diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java new file mode 100644 index 0000000000..f39bc580cc --- /dev/null +++ b/src/main/java/lotto/LottoService.java @@ -0,0 +1,7 @@ +package lotto; + +public class LottoService { + private Lotto lotto; + + // 로또 번호 객체 생성 +} diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java index 47f51785a0..29cd081793 100644 --- a/src/main/java/validator/Validator.java +++ b/src/main/java/validator/Validator.java @@ -1,51 +1,57 @@ -package validator; - -import java.util.ArrayList; -import java.util.List; - -public class Validator { - - // 1000원 단위로 나눠떨어지는지 확인 - public static int checkDivisibility(String number, int minUnit){ - - int amount = convertToNumber(number); - - if(amount % minUnit != 0){ - throw new IllegalArgumentException("[ERROR] 최소 입력 단위는 "+minUnit+"원입니다."); - } - return amount; - } - - // 숫자인지 확인 - public static int convertToNumber(String number){ - if(number.isEmpty() || number.trim().isEmpty()){ - throw new IllegalArgumentException("[ERROR] 빈 값을 입력할 수 없습니다."); - } - - try{ - return Integer.parseInt(number); - }catch(Exception e){ - throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다."); - } - } - - // 구분자 기준 숫자 리스트 변환 - public static List convertToList(String LottoNumbers,String separtor){ - - try{ - // 구분자 기준 분리 - String[] tempNums = LottoNumbers.split(separtor); - - // String[] → List로 변환 - List numberList = new ArrayList<>(); - for(String number:tempNums){ - numberList.add(Integer.parseInt(number)); - } - return numberList; - - }catch(Exception e){ - throw new IllegalArgumentException("[ERROR] 숫자, 구분자(" - +separtor+") 이외의 값은 입력할 수 없습니다."); - } - } -} +package validator; + +import java.util.ArrayList; +import java.util.List; + +public class Validator { + + // 1000원 단위로 나눠떨어지는지 확인 + public static int checkDivisibility(String number, int minUnit){ + + int amount = convertToNumber(number); + + if(amount % minUnit != 0){ + throw new IllegalArgumentException("[ERROR] 최소 입력 단위는 "+minUnit+"원입니다."); + } + return amount; + } + + // 1~45 사이의 숫자인지 확인 + public static int convertToNumber(String number){ + if(number.isEmpty() || number.trim().isEmpty()){ + throw new IllegalArgumentException("[ERROR] 빈 값을 입력할 수 없습니다."); + } + + int num; + try{ + num = Integer.parseInt(number); + }catch(Exception e){ + throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다."); + } + + int MIN_VALUE = 1; + int MAX_VALUE = 45; + if(num < MIN_VALUE || num > MAX_VALUE){ + throw new IllegalArgumentException("[ERROR] "+MIN_VALUE+"~"+MAX_VALUE+"사이의 숫자만 입력 가능합니다."); + } + + return num; + } + + // 구분자 기준 숫자 리스트 변환 + public static List convertToList(String LottoNumbers,String separtor){ + + // 구분자 기준 분리 + String[] tempNums = LottoNumbers.split(separtor); + + // String[] → List로 변환 + List numberList = new ArrayList<>(); + for(String number:tempNums){ + int num = convertToNumber(number); + //if(isNumberInRange(num, 1, 45)) { + numberList.add(num); + //} + } + return numberList; + } +} diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index a15c7d1f52..84daa4964b 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -1,61 +1,61 @@ -package lotto; - -import camp.nextstep.edu.missionutils.test.NsTest; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; -import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; -import static org.assertj.core.api.Assertions.assertThat; - -class ApplicationTest extends NsTest { - private static final String ERROR_MESSAGE = "[ERROR]"; - - @Test - void 기능_테스트() { - assertRandomUniqueNumbersInRangeTest( - () -> { - run("8000", "1,2,3,4,5,6", "7"); - assertThat(output()).contains( - "8개를 구매했습니다.", - "[8, 21, 23, 41, 42, 43]", - "[3, 5, 11, 16, 32, 38]", - "[7, 11, 16, 35, 36, 44]", - "[1, 8, 11, 31, 41, 42]", - "[13, 14, 16, 38, 42, 45]", - "[7, 11, 30, 40, 42, 43]", - "[2, 13, 22, 32, 38, 45]", - "[1, 3, 5, 14, 22, 45]", - "3개 일치 (5,000원) - 1개", - "4개 일치 (50,000원) - 0개", - "5개 일치 (1,500,000원) - 0개", - "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", - "6개 일치 (2,000,000,000원) - 0개", - "총 수익률은 62.5%입니다." - ); - }, - List.of(8, 21, 23, 41, 42, 43), - List.of(3, 5, 11, 16, 32, 38), - List.of(7, 11, 16, 35, 36, 44), - List.of(1, 8, 11, 31, 41, 42), - List.of(13, 14, 16, 38, 42, 45), - List.of(7, 11, 30, 40, 42, 43), - List.of(2, 13, 22, 32, 38, 45), - List.of(1, 3, 5, 14, 22, 45) - ); - } - - @Test - void 예외_테스트() { - assertSimpleTest(() -> { - runException("1000j"); - assertThat(output()).contains(ERROR_MESSAGE); - }); - } - - @Override - public void runMain() { - Application.main(new String[]{}); - } -} +package lotto; + +import camp.nextstep.edu.missionutils.test.NsTest; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static camp.nextstep.edu.missionutils.test.Assertions.assertRandomUniqueNumbersInRangeTest; +import static camp.nextstep.edu.missionutils.test.Assertions.assertSimpleTest; +import static org.assertj.core.api.Assertions.assertThat; + +class ApplicationTest extends NsTest { + private static final String ERROR_MESSAGE = "[ERROR]"; + + @Test + void 기능_테스트() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "1,2,3,4,5,6", "7"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 62.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } + + @Test + void 예외_테스트() { + assertSimpleTest(() -> { + runException("1000j"); + assertThat(output()).contains(ERROR_MESSAGE); + }); + } + + @Override + public void runMain() { + Application.main(new String[]{}); + } +} diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 309f4e50ae..2de84e6cf2 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -1,25 +1,25 @@ -package lotto; - -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class LottoTest { - @Test - void 로또_번호의_개수가_6개가_넘어가면_예외가_발생한다() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) - .isInstanceOf(IllegalArgumentException.class); - } - - @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") - @Test - void 로또_번호에_중복된_숫자가_있으면_예외가_발생한다() { - assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) - .isInstanceOf(IllegalArgumentException.class); - } - - // TODO: 추가 기능 구현에 따른 테스트 코드 작성 -} +package lotto; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class LottoTest { + @Test + void 로또_번호의_개수가_6개가_넘어가면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 6, 7))) + .isInstanceOf(IllegalArgumentException.class); + } + + @DisplayName("로또 번호에 중복된 숫자가 있으면 예외가 발생한다.") + @Test + void 로또_번호에_중복된_숫자가_있으면_예외가_발생한다() { + assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5))) + .isInstanceOf(IllegalArgumentException.class); + } + + // TODO: 추가 기능 구현에 따른 테스트 코드 작성 +} From 2e8830fc61963bbfb7c85c39f13c370867d4cdad Mon Sep 17 00:00:00 2001 From: Yujin Date: Mon, 3 Nov 2025 14:04:15 +0900 Subject: [PATCH 07/20] =?UTF-8?q?feat:=EB=A1=9C=EB=98=90=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 1 - src/main/java/lotto/LottoService.java | 20 +++++++++++++++++++- src/main/java/validator/Validator.java | 2 -- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 7d2d4704c8..9245bf6fc1 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -16,5 +16,4 @@ private void validate(List numbers) { } } - // TODO: 추가 기능 구현 } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index f39bc580cc..f7b8e1312b 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -1,7 +1,25 @@ package lotto; +import camp.nextstep.edu.missionutils.Randoms; + +import java.util.List; + public class LottoService { - private Lotto lotto; + private int purchaseAmount; + private Lotto winningNumber; + private List issueNumbers; // 로또 번호 객체 생성 + public LottoService(int pca, List list){ + this.purchaseAmount = pca; + this.winningNumber = new Lotto(list); + } + + // 로또 발행 + public void issueLotto(){ + for(int i=0; i convertToList(String LottoNumbers,String separtor){ List numberList = new ArrayList<>(); for(String number:tempNums){ int num = convertToNumber(number); - //if(isNumberInRange(num, 1, 45)) { numberList.add(num); - //} } return numberList; } From 29f72ee12012c73cbc6fd09863705cd1694f88bc Mon Sep 17 00:00:00 2001 From: Yujin Date: Mon, 3 Nov 2025 14:43:25 +0900 Subject: [PATCH 08/20] =?UTF-8?q?feat:=20(=EC=B6=9C=EB=A0=A5)=20=EB=B0=9C?= =?UTF-8?q?=ED=96=89=EB=90=9C=20=EB=A1=9C=EB=98=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Application.java | 4 +++- src/main/java/lotto/Lotto.java | 3 +++ src/main/java/lotto/LottoController.java | 10 +++++++++- src/main/java/lotto/LottoService.java | 5 +++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/Application.java b/src/main/java/lotto/Application.java index dec7a9bb9e..155d0ca846 100644 --- a/src/main/java/lotto/Application.java +++ b/src/main/java/lotto/Application.java @@ -2,6 +2,8 @@ public class Application { public static void main(String[] args) { - // TODO: 프로그램 구현 + + LottoController lottoController = new LottoController(); + lottoController.run(); } } diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 9245bf6fc1..2a93786f0f 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -16,4 +16,7 @@ private void validate(List numbers) { } } + public List getLotto(){ + return numbers; + } } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 09e525c361..49cbadcffe 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -6,7 +6,7 @@ import java.util.List; public class LottoController { - public static void main(String[] args){ + public void run(){ // 입력1: 로또 구입 금액 System.out.println("구입금액을 입력해 주세요."); int purchasePrice = Validator.checkDivisibility(Console.readLine(), 1000); @@ -23,7 +23,15 @@ public static void main(String[] args){ System.out.println("보너스 번호를 입력해 주세요."); int bonusNumber = Validator.convertToNumber(Console.readLine()); + // 로또 발행 + LottoService lottoService = new LottoService(purchaseAmount, winningNumbers); + lottoService.issueLotto(); + // 출력2: 발행 로또 번호 + for(int i=0; i Date: Mon, 3 Nov 2025 15:12:52 +0900 Subject: [PATCH 09/20] =?UTF-8?q?refactor:=20Validator=20=EC=88=AB?= =?UTF-8?q?=EC=9E=90=20=EC=97=AC=EB=B6=80=20=EB=B0=8F=201~45=20=EB=B2=94?= =?UTF-8?q?=EC=9C=84=20=EC=88=AB=EC=9E=90=20=ED=99=95=EC=9D=B8=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/validator/Validator.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java index 49568a91f3..d910696e00 100644 --- a/src/main/java/validator/Validator.java +++ b/src/main/java/validator/Validator.java @@ -16,26 +16,28 @@ public static int checkDivisibility(String number, int minUnit){ return amount; } - // 1~45 사이의 숫자인지 확인 + // 숫자인지 확인 public static int convertToNumber(String number){ if(number.isEmpty() || number.trim().isEmpty()){ throw new IllegalArgumentException("[ERROR] 빈 값을 입력할 수 없습니다."); } - int num; try{ - num = Integer.parseInt(number); + return Integer.parseInt(number); }catch(Exception e){ throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다."); } + } + // 1~45 사이의 숫자인지 확인 + public static int convertToNumberInRange(int number){ int MIN_VALUE = 1; int MAX_VALUE = 45; - if(num < MIN_VALUE || num > MAX_VALUE){ + if(number < MIN_VALUE || number > MAX_VALUE){ throw new IllegalArgumentException("[ERROR] "+MIN_VALUE+"~"+MAX_VALUE+"사이의 숫자만 입력 가능합니다."); } - return num; + return number; } // 구분자 기준 숫자 리스트 변환 @@ -47,7 +49,7 @@ public static List convertToList(String LottoNumbers,String separtor){ // String[] → List로 변환 List numberList = new ArrayList<>(); for(String number:tempNums){ - int num = convertToNumber(number); + int num = convertToNumberInRange(convertToNumber(number)); numberList.add(num); } return numberList; From e401a64d1733760a1a04aa13c9a2ebed7ccc19e7 Mon Sep 17 00:00:00 2001 From: Yujin Date: Mon, 3 Nov 2025 15:16:57 +0900 Subject: [PATCH 10/20] =?UTF-8?q?fix:=20=EB=A1=9C=EB=98=90=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=206=EA=B0=9C=20=EC=A0=9C=ED=95=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/validator/Validator.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java index d910696e00..0262f05566 100644 --- a/src/main/java/validator/Validator.java +++ b/src/main/java/validator/Validator.java @@ -52,6 +52,10 @@ public static List convertToList(String LottoNumbers,String separtor){ int num = convertToNumberInRange(convertToNumber(number)); numberList.add(num); } + + if(numberList.size() != 6){ + throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다."); + } return numberList; } } From 031fd6bda666d1ef567f803ad5d0df594147e5e7 Mon Sep 17 00:00:00 2001 From: Yujin Date: Mon, 3 Nov 2025 15:26:34 +0900 Subject: [PATCH 11/20] =?UTF-8?q?fix:=20=EB=B0=9C=ED=96=89=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EB=AA=A9=EB=A1=9D=20NullpointException(=EA=B0=9D?= =?UTF-8?q?=EC=B2=B4=20=EB=AF=B8=EC=83=9D=EC=84=B1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 2e3f3765f6..d8ee7ce17b 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -2,6 +2,7 @@ import camp.nextstep.edu.missionutils.Randoms; +import java.util.ArrayList; import java.util.List; public class LottoService { @@ -13,6 +14,7 @@ public class LottoService { public LottoService(int pca, List list){ this.purchaseAmount = pca; this.winningNumber = new Lotto(list); + this.issueNumbers = new ArrayList<>(); } // 로또 발행 From e6708a31e76d8ed476279a72aaca70bcf66f7f17 Mon Sep 17 00:00:00 2001 From: Yujin Date: Mon, 3 Nov 2025 17:35:32 +0900 Subject: [PATCH 12/20] =?UTF-8?q?refactor:=20=EB=A1=9C=EB=98=90=20?= =?UTF-8?q?=EB=B2=88=ED=98=B8=20=EC=98=A4=EB=A6=84=EC=B0=A8=EC=88=9C=20?= =?UTF-8?q?=EC=A0=95=EB=A0=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 6 ++++++ src/main/java/lotto/LottoResult.java | 25 +++++++++++++++++++++++++ src/main/java/lotto/LottoService.java | 7 +++++++ 3 files changed, 38 insertions(+) create mode 100644 src/main/java/lotto/LottoResult.java diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index 2a93786f0f..c76b243ed0 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,5 +1,6 @@ package lotto; +import java.util.Collections; import java.util.List; public class Lotto { @@ -19,4 +20,9 @@ private void validate(List numbers) { public List getLotto(){ return numbers; } + + // 로또번호 정렬 + public void sortNumbers(){ + Collections.sort(numbers); + } } diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java new file mode 100644 index 0000000000..3e09f97f4c --- /dev/null +++ b/src/main/java/lotto/LottoResult.java @@ -0,0 +1,25 @@ +package lotto; + +public enum LottoResult { + FIRST(6, 2000000000), + SECOND(6, 30000000), + THIRD(5, 1500000), + FOURTH(4, 50000), + FIFTH(3, 5000); + + private int correctNumbers; + private int reward; + + private LottoResult(int corNums, int reward){ + this.correctNumbers = corNums; + this.reward = reward; + } + + public int getCorrectNumbers() { + return correctNumbers; + } + + public int getReward() { + return reward; + } +} diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index d8ee7ce17b..e1e8e28295 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -3,6 +3,7 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class LottoService { @@ -21,6 +22,7 @@ public LottoService(int pca, List list){ public void issueLotto(){ for(int i=0; i Date: Mon, 3 Nov 2025 21:29:37 +0900 Subject: [PATCH 13/20] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EB=B6=84=EB=A5=98=20=EB=B0=8F=20=EA=B2=B0=EA=B3=BC?= =?UTF-8?q?=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 10 ++--- src/main/java/lotto/LottoResult.java | 32 +++++++++++----- src/main/java/lotto/LottoService.java | 49 +++++++++++++++++++----- 3 files changed, 67 insertions(+), 24 deletions(-) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 49cbadcffe..9520e9a6d0 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -11,10 +11,6 @@ public void run(){ System.out.println("구입금액을 입력해 주세요."); int purchasePrice = Validator.checkDivisibility(Console.readLine(), 1000); - // 출력1: 발행 로또 수량 - int purchaseAmount = purchasePrice/1000; - System.out.printf("%d개를 구매했습니다.\n",purchaseAmount); - // 입력2: 당첨 번호 System.out.println("당첨 번호를 입력해 주세요."); List winningNumbers = Validator.convertToList(Console.readLine(),","); @@ -23,8 +19,12 @@ public void run(){ System.out.println("보너스 번호를 입력해 주세요."); int bonusNumber = Validator.convertToNumber(Console.readLine()); + // 출력1: 발행 로또 수량 + int purchaseAmount = purchasePrice/1000; + System.out.printf("%d개를 구매했습니다.\n",purchaseAmount); + // 로또 발행 - LottoService lottoService = new LottoService(purchaseAmount, winningNumbers); + LottoService lottoService = new LottoService(purchaseAmount, winningNumbers, bonusNumber); lottoService.issueLotto(); // 출력2: 발행 로또 번호 diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java index 3e09f97f4c..88fc7f1013 100644 --- a/src/main/java/lotto/LottoResult.java +++ b/src/main/java/lotto/LottoResult.java @@ -1,25 +1,39 @@ package lotto; public enum LottoResult { - FIRST(6, 2000000000), - SECOND(6, 30000000), - THIRD(5, 1500000), - FOURTH(4, 50000), - FIFTH(3, 5000); + FIRST(6, false, 2000000000), + SECOND(5, true, 30000000 ), + THIRD(5, false, 1500000), + FOURTH(4, false, 50000), + FIFTH(3, false, 5000), + NONE(0, false, 0); - private int correctNumbers; - private int reward; + private final int correctNumbers; + private final boolean isBonus; + private final int reward; - private LottoResult(int corNums, int reward){ + private LottoResult(int corNums, boolean isBonus, int reward){ this.correctNumbers = corNums; + this.isBonus = isBonus; this.reward = reward; } public int getCorrectNumbers() { return correctNumbers; } - + public boolean getBonus(){ return isBonus;} public int getReward() { return reward; } + + public static LottoResult getResult(int corNums, boolean isBonus){ + for(LottoResult result:values()){ + if(corNums == 3 || corNums == 4 || corNums == 6){ + return result; + } + if(corNums == 5 && isBonus) return result; + if(corNums == 5) return result; + } + return NONE; + } } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index e1e8e28295..0bbd26ccdf 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -3,19 +3,23 @@ import camp.nextstep.edu.missionutils.Randoms; import java.util.ArrayList; -import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class LottoService { private int purchaseAmount; - private Lotto winningNumber; - private List issueNumbers; + private Lotto winningLotto; + private List issueLottos; + private int bonusNumber; + private Map result; // 로또 번호 객체 생성 - public LottoService(int pca, List list){ + public LottoService(int pca, List list, int bonusNumber){ this.purchaseAmount = pca; - this.winningNumber = new Lotto(list); - this.issueNumbers = new ArrayList<>(); + this.winningLotto = new Lotto(list); + this.issueLottos = new ArrayList<>(); + this.bonusNumber = bonusNumber; } // 로또 발행 @@ -23,17 +27,42 @@ public void issueLotto(){ for(int i=0; i saveLottoResult(){ + result = new HashMap<>(); + + for(Lotto lotto:issueLottos){ + int winningNumbers = confirmWinningNumbers(lotto); + boolean bonus = confirmWinningBonus(lotto); + + LottoResult lottoResult = LottoResult.getResult(winningNumbers, bonus); + result.compute(lottoResult, (k, prevResult) -> prevResult + 1); + } + return result; } } From cb7a2f6e581cea2bd8182916c6582a1c3f1fafee Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Mon, 3 Nov 2025 22:17:01 +0900 Subject: [PATCH 14/20] =?UTF-8?q?refactor:=20=EB=8B=B9=EC=B2=A8=20?= =?UTF-8?q?=EA=B2=B0=EA=B3=BC=20=EC=A0=80=EC=9E=A5(HashMap=20=E2=86=92=20E?= =?UTF-8?q?numMap)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 6 ++++++ src/main/java/lotto/LottoService.java | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 9520e9a6d0..0e19f4a636 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -4,6 +4,7 @@ import validator.Validator; import java.util.List; +import java.util.Map; public class LottoController { public void run(){ @@ -34,6 +35,11 @@ public void run(){ } // 출력3: 당첨 내역 + Map result = lottoService.saveLottoResult(); + + System.out.println("당첨 통계"); + System.out.println("---"); + // 출력4: 수익률 } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 0bbd26ccdf..06fdbdde2a 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -2,10 +2,7 @@ import camp.nextstep.edu.missionutils.Randoms; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; public class LottoService { private int purchaseAmount; @@ -54,7 +51,12 @@ public boolean confirmWinningBonus(Lotto lotto){ // 당첨 결과 저장 public Map saveLottoResult(){ - result = new HashMap<>(); + result = new EnumMap<>(LottoResult.class); + + // Map 초기화 + for(LottoResult init:LottoResult.values()){ + result.put(init,0); + } for(Lotto lotto:issueLottos){ int winningNumbers = confirmWinningNumbers(lotto); From da57f0ec9ca6f7d68a955c2603814de02e3d2b77 Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Mon, 3 Nov 2025 22:46:15 +0900 Subject: [PATCH 15/20] =?UTF-8?q?feat:=20=EB=A1=9C=EB=98=90=20=EB=8B=B9?= =?UTF-8?q?=EC=B2=A8=20=EA=B2=B0=EA=B3=BC=20=EB=B0=8F=20=EC=88=98=EC=9D=B5?= =?UTF-8?q?=EB=A5=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 0e19f4a636..ae0b19aa50 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -3,6 +3,8 @@ import camp.nextstep.edu.missionutils.Console; import validator.Validator; +import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.List; import java.util.Map; @@ -33,6 +35,7 @@ public void run(){ Lotto issuedLotto = lottoService.getIssueNumbers(i); System.out.println(issuedLotto.getLotto()); } + System.out.println(); // 출력3: 당첨 내역 Map result = lottoService.saveLottoResult(); @@ -40,7 +43,21 @@ public void run(){ System.out.println("당첨 통계"); System.out.println("---"); + int winningPrize = 0; + for(Map.Entry entry:result.entrySet()){ + if(entry.getKey().getCorrectNumbers() == 0) continue; + System.out.print(entry.getKey().getCorrectNumbers()+"개 일치 "); + System.out.printf("(%,d원) - ", entry.getKey().getReward()); + System.out.println(entry.getValue() +"개"); + winningPrize += entry.getKey().getReward() * entry.getValue(); + } // 출력4: 수익률 + float roi = (float) winningPrize /purchasePrice*100; + + // 소수점 둘째 자리 반올림 + BigDecimal bd = new BigDecimal(String.valueOf(roi)); + BigDecimal final_roi = bd.setScale(2, RoundingMode.HALF_UP); + System.out.printf("총 수익률은 %s%%입니다.%n",final_roi.toString()); } } From 8f4f6c537d10ec7f86d78dc22df21973b130ca38 Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Mon, 3 Nov 2025 23:56:53 +0900 Subject: [PATCH 16/20] =?UTF-8?q?fix:=20LottoResult=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoController.java | 36 ++++++++++++++---------- src/main/java/lotto/LottoResult.java | 9 +++--- src/main/java/lotto/LottoService.java | 12 +++++--- src/test/java/lotto/LottoTest.java | 1 + 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index ae0b19aa50..cb3276b4e3 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -14,20 +14,12 @@ public void run(){ System.out.println("구입금액을 입력해 주세요."); int purchasePrice = Validator.checkDivisibility(Console.readLine(), 1000); - // 입력2: 당첨 번호 - System.out.println("당첨 번호를 입력해 주세요."); - List winningNumbers = Validator.convertToList(Console.readLine(),","); - - // 입력3: 보너스 번호 - System.out.println("보너스 번호를 입력해 주세요."); - int bonusNumber = Validator.convertToNumber(Console.readLine()); - // 출력1: 발행 로또 수량 int purchaseAmount = purchasePrice/1000; System.out.printf("%d개를 구매했습니다.\n",purchaseAmount); // 로또 발행 - LottoService lottoService = new LottoService(purchaseAmount, winningNumbers, bonusNumber); + LottoService lottoService = new LottoService(purchaseAmount);//, winningNumbers, bonusNumber); lottoService.issueLotto(); // 출력2: 발행 로또 번호 @@ -37,6 +29,17 @@ public void run(){ } System.out.println(); + // 입력2: 당첨 번호 + System.out.println("당첨 번호를 입력해 주세요."); + List winningNumbers = Validator.convertToList(Console.readLine(),","); + + // 입력3: 보너스 번호 + System.out.println("보너스 번호를 입력해 주세요."); + int bonusNumber = Validator.convertToNumber(Console.readLine()); + + // 당첨 번호 저장 + lottoService.setUpWinningLotto(winningNumbers, bonusNumber); + // 출력3: 당첨 내역 Map result = lottoService.saveLottoResult(); @@ -45,11 +48,14 @@ public void run(){ int winningPrize = 0; for(Map.Entry entry:result.entrySet()){ - if(entry.getKey().getCorrectNumbers() == 0) continue; - System.out.print(entry.getKey().getCorrectNumbers()+"개 일치 "); - System.out.printf("(%,d원) - ", entry.getKey().getReward()); - System.out.println(entry.getValue() +"개"); - winningPrize += entry.getKey().getReward() * entry.getValue(); + LottoResult key = entry.getKey(); + int count = entry.getValue(); + + if(key.getCorrectNumbers() == 0) continue; + System.out.print(key.getCorrectNumbers()+"개 일치 "); + System.out.printf("(%,d원) - ", key.getReward()); + System.out.println(count +"개"); + winningPrize += key.getReward() * entry.getValue(); } // 출력4: 수익률 @@ -57,7 +63,7 @@ public void run(){ // 소수점 둘째 자리 반올림 BigDecimal bd = new BigDecimal(String.valueOf(roi)); - BigDecimal final_roi = bd.setScale(2, RoundingMode.HALF_UP); + BigDecimal final_roi = bd.setScale(1, RoundingMode.HALF_UP); System.out.printf("총 수익률은 %s%%입니다.%n",final_roi.toString()); } } diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java index 88fc7f1013..8dfeb77637 100644 --- a/src/main/java/lotto/LottoResult.java +++ b/src/main/java/lotto/LottoResult.java @@ -28,11 +28,12 @@ public int getReward() { public static LottoResult getResult(int corNums, boolean isBonus){ for(LottoResult result:values()){ - if(corNums == 3 || corNums == 4 || corNums == 6){ - return result; + if(result.getCorrectNumbers() != corNums){ + continue; } - if(corNums == 5 && isBonus) return result; - if(corNums == 5) return result; + if(result.getCorrectNumbers() == 5 && result.isBonus == isBonus) return result; + //if(result.getCorrectNumbers() == 5 && result.isBonus != isBonus) return result; + return result; } return NONE; } diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index 06fdbdde2a..ed9204f05d 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -11,11 +11,15 @@ public class LottoService { private int bonusNumber; private Map result; - // 로또 번호 객체 생성 - public LottoService(int pca, List list, int bonusNumber){ + // 발행 로또 번호 객체 생성 + public LottoService(int pca){ this.purchaseAmount = pca; - this.winningLotto = new Lotto(list); this.issueLottos = new ArrayList<>(); + } + + // 당첨 로또 번호 저장 + public void setUpWinningLotto(List list, int bonusNumber){ + this.winningLotto = new Lotto(list); this.bonusNumber = bonusNumber; } @@ -46,7 +50,7 @@ public int confirmWinningNumbers(Lotto lotto){ // 보너스 번호 당첨 여부 확인 public boolean confirmWinningBonus(Lotto lotto){ - return winningLotto.getLotto().contains(bonusNumber); + return lotto.getLotto().contains(bonusNumber); } // 당첨 결과 저장 diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index 2de84e6cf2..d3f5a8395b 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -22,4 +22,5 @@ class LottoTest { } // TODO: 추가 기능 구현에 따른 테스트 코드 작성 + } From 7fce98d7827983ec9f2779740107776a65e45a8d Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Tue, 4 Nov 2025 20:18:15 +0900 Subject: [PATCH 17/20] =?UTF-8?q?feat:=20=EB=8B=B9=EC=B2=A8=20=EB=A1=9C?= =?UTF-8?q?=EB=98=90=20=EB=B0=8F=20=EB=B3=B4=EB=84=88=EC=8A=A4=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=A4=91=EB=B3=B5=20=EB=B0=A9=EC=A7=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/Lotto.java | 13 +++++++++++++ src/main/java/lotto/LottoController.java | 2 +- src/main/java/validator/Validator.java | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/lotto/Lotto.java b/src/main/java/lotto/Lotto.java index c76b243ed0..f8c09541f0 100644 --- a/src/main/java/lotto/Lotto.java +++ b/src/main/java/lotto/Lotto.java @@ -1,13 +1,16 @@ package lotto; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class Lotto { private final List numbers; public Lotto(List numbers) { validate(numbers); + unRepeatedNumber(numbers); this.numbers = numbers; } @@ -17,6 +20,15 @@ private void validate(List numbers) { } } + // 로또 번호 중복 여부 확인 + private void unRepeatedNumber(List numbers){ + Set nonRepeatedNumbers = new HashSet<>(numbers); + + if(nonRepeatedNumbers.size() != numbers.size()){ + throw new IllegalArgumentException("[ERROR] 중복된 당첨번호를 가질 수 없습니다."); + } + } + public List getLotto(){ return numbers; } @@ -25,4 +37,5 @@ public List getLotto(){ public void sortNumbers(){ Collections.sort(numbers); } + } diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index cb3276b4e3..20c50893d3 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -35,7 +35,7 @@ public void run(){ // 입력3: 보너스 번호 System.out.println("보너스 번호를 입력해 주세요."); - int bonusNumber = Validator.convertToNumber(Console.readLine()); + int bonusNumber = Validator.unRepeatedNumber(winningNumbers, Console.readLine()); // 당첨 번호 저장 lottoService.setUpWinningLotto(winningNumbers, bonusNumber); diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java index 0262f05566..db31e31103 100644 --- a/src/main/java/validator/Validator.java +++ b/src/main/java/validator/Validator.java @@ -1,7 +1,9 @@ package validator; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; public class Validator { @@ -58,4 +60,14 @@ public static List convertToList(String LottoNumbers,String separtor){ } return numberList; } + + // 보너스 번호와 로또 번호 중복 여부 확인 + public static int unRepeatedNumber(List winningNumbers, String bonus){ + int bonusNumber = convertToNumber(bonus); + + if(winningNumbers.contains(bonusNumber)) { + throw new IllegalArgumentException("[ERROR] 보너스 번호는 당첨 번호와 일치할 수 없습니다."); + } + return bonusNumber; + } } From ab3bde062d51cd697c96cf6dcff5084e3528ac99 Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Tue, 4 Nov 2025 20:56:20 +0900 Subject: [PATCH 18/20] =?UTF-8?q?fix:=20=EB=8B=B9=EC=B2=A8=20=EA=B2=B0?= =?UTF-8?q?=EA=B3=BC=20=EC=B6=9C=EB=A0=A5=20=EC=88=9C=EC=84=9C=20=EB=B0=8F?= =?UTF-8?q?=20=ED=8F=AC=EB=A7=B7=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Output/Formatter.java | 15 +++++++++++++++ src/main/java/lotto/LottoController.java | 5 ++--- src/main/java/lotto/LottoResult.java | 10 +++++----- src/main/java/lotto/LottoService.java | 2 +- 4 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 src/main/java/Output/Formatter.java diff --git a/src/main/java/Output/Formatter.java b/src/main/java/Output/Formatter.java new file mode 100644 index 0000000000..31a9e351df --- /dev/null +++ b/src/main/java/Output/Formatter.java @@ -0,0 +1,15 @@ +package Output; + +public class Formatter { + public static void printStatics(int correctNumber, boolean isBonus, int reward, int count){ + System.out.print(correctNumber+"개 일치"); + if(isBonus) { + System.out.print(", 보너스 볼 일치 "); + } + if(!isBonus){ + System.out.print(" "); + } + System.out.printf("(%,d원) - ", reward); + System.out.println(count +"개"); + } +} diff --git a/src/main/java/lotto/LottoController.java b/src/main/java/lotto/LottoController.java index 20c50893d3..95827f0c31 100644 --- a/src/main/java/lotto/LottoController.java +++ b/src/main/java/lotto/LottoController.java @@ -1,5 +1,6 @@ package lotto; +import Output.Formatter; import camp.nextstep.edu.missionutils.Console; import validator.Validator; @@ -52,9 +53,7 @@ public void run(){ int count = entry.getValue(); if(key.getCorrectNumbers() == 0) continue; - System.out.print(key.getCorrectNumbers()+"개 일치 "); - System.out.printf("(%,d원) - ", key.getReward()); - System.out.println(count +"개"); + Formatter.printStatics(key.getCorrectNumbers(), key.getBonus(), key.getReward(), count); winningPrize += key.getReward() * entry.getValue(); } diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java index 8dfeb77637..5ff1fc8c6c 100644 --- a/src/main/java/lotto/LottoResult.java +++ b/src/main/java/lotto/LottoResult.java @@ -1,12 +1,12 @@ package lotto; public enum LottoResult { - FIRST(6, false, 2000000000), - SECOND(5, true, 30000000 ), - THIRD(5, false, 1500000), - FOURTH(4, false, 50000), + NONE(0, false, 0), FIFTH(3, false, 5000), - NONE(0, false, 0); + FOURTH(4, false, 50000), + THIRD(5, false, 1500000), + SECOND(5, true, 30000000 ), + FIRST(6, false, 2000000000); private final int correctNumbers; private final boolean isBonus; diff --git a/src/main/java/lotto/LottoService.java b/src/main/java/lotto/LottoService.java index ed9204f05d..4cf2813aac 100644 --- a/src/main/java/lotto/LottoService.java +++ b/src/main/java/lotto/LottoService.java @@ -27,7 +27,7 @@ public void setUpWinningLotto(List list, int bonusNumber){ public void issueLotto(){ for(int i=0; i Date: Tue, 4 Nov 2025 21:55:44 +0900 Subject: [PATCH 19/20] =?UTF-8?q?test:=205=EA=B0=9C=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20=EB=B0=8F=20=EB=B3=B4=EB=84=88=EC=8A=A4=20=EB=B2=88=ED=98=B8?= =?UTF-8?q?=20=EC=9D=BC=EC=B9=98/=EB=B6=88=EC=9D=BC=EC=B9=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/lotto/LottoResult.java | 2 +- src/main/java/validator/Validator.java | 2 +- src/test/java/lotto/ApplicationTest.java | 68 ++++++++++++++++++++++++ src/test/java/lotto/LottoTest.java | 8 +++ 4 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/main/java/lotto/LottoResult.java b/src/main/java/lotto/LottoResult.java index 5ff1fc8c6c..a82372a3c5 100644 --- a/src/main/java/lotto/LottoResult.java +++ b/src/main/java/lotto/LottoResult.java @@ -33,7 +33,7 @@ public static LottoResult getResult(int corNums, boolean isBonus){ } if(result.getCorrectNumbers() == 5 && result.isBonus == isBonus) return result; //if(result.getCorrectNumbers() == 5 && result.isBonus != isBonus) return result; - return result; + if(result.getCorrectNumbers() != 5) return result; } return NONE; } diff --git a/src/main/java/validator/Validator.java b/src/main/java/validator/Validator.java index db31e31103..491e84f0b4 100644 --- a/src/main/java/validator/Validator.java +++ b/src/main/java/validator/Validator.java @@ -25,7 +25,7 @@ public static int convertToNumber(String number){ } try{ - return Integer.parseInt(number); + return Integer.parseInt(number.trim()); }catch(Exception e){ throw new IllegalArgumentException("[ERROR] 숫자만 입력 가능합니다."); } diff --git a/src/test/java/lotto/ApplicationTest.java b/src/test/java/lotto/ApplicationTest.java index 84daa4964b..e2a4e1bb7a 100644 --- a/src/test/java/lotto/ApplicationTest.java +++ b/src/test/java/lotto/ApplicationTest.java @@ -46,6 +46,74 @@ class ApplicationTest extends NsTest { ); } + @Test + void 기능_테스트_5개번호와_보너스번호_일치() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "8,21,23,41,42,45", "43"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 0개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 1개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 375062.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } + + @Test + void 기능_테스트_5개번호와_보너스번호_불일치() { + assertRandomUniqueNumbersInRangeTest( + () -> { + run("8000", "8,21,23,41,42,45", "40"); + assertThat(output()).contains( + "8개를 구매했습니다.", + "[8, 21, 23, 41, 42, 43]", + "[3, 5, 11, 16, 32, 38]", + "[7, 11, 16, 35, 36, 44]", + "[1, 8, 11, 31, 41, 42]", + "[13, 14, 16, 38, 42, 45]", + "[7, 11, 30, 40, 42, 43]", + "[2, 13, 22, 32, 38, 45]", + "[1, 3, 5, 14, 22, 45]", + "3개 일치 (5,000원) - 1개", + "4개 일치 (50,000원) - 0개", + "5개 일치 (1,500,000원) - 1개", + "5개 일치, 보너스 볼 일치 (30,000,000원) - 0개", + "6개 일치 (2,000,000,000원) - 0개", + "총 수익률은 18812.5%입니다." + ); + }, + List.of(8, 21, 23, 41, 42, 43), + List.of(3, 5, 11, 16, 32, 38), + List.of(7, 11, 16, 35, 36, 44), + List.of(1, 8, 11, 31, 41, 42), + List.of(13, 14, 16, 38, 42, 45), + List.of(7, 11, 30, 40, 42, 43), + List.of(2, 13, 22, 32, 38, 45), + List.of(1, 3, 5, 14, 22, 45) + ); + } + @Test void 예외_테스트() { assertSimpleTest(() -> { diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index d3f5a8395b..e60148c3d6 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -5,6 +5,7 @@ import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; class LottoTest { @@ -22,5 +23,12 @@ class LottoTest { } // TODO: 추가 기능 구현에 따른 테스트 코드 작성 + /* + @Test + void 당첨_번호_개수(){ + assertThat((new Lotto(List.of(1,2,3,4,5,6)) -> ) + ); + } + */ } From 55b30b732b344affb72af6700bf76fd58715ff9e Mon Sep 17 00:00:00 2001 From: Yujin1039 Date: Tue, 4 Nov 2025 22:51:59 +0900 Subject: [PATCH 20/20] =?UTF-8?q?test:=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8(=EB=B3=B4=EB=84=88=EC=8A=A4=20=EB=B2=88?= =?UTF-8?q?=ED=98=B8=20=EC=A4=91=EB=B3=B5=20=EC=97=AC=EB=B6=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/lotto/LottoTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/lotto/LottoTest.java b/src/test/java/lotto/LottoTest.java index e60148c3d6..e4f856a042 100644 --- a/src/test/java/lotto/LottoTest.java +++ b/src/test/java/lotto/LottoTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import validator.Validator; import java.util.List; @@ -23,6 +24,15 @@ class LottoTest { } // TODO: 추가 기능 구현에 따른 테스트 코드 작성 + @DisplayName("보너스 번호가 로또 번호와 중복되면 예외가 발생한다.") + @Test + void checkBonusNumber_중복(){ + List winningNumbers = List.of(1, 2, 3, 4, 5, 6); + + assertThatThrownBy(() -> Validator.unRepeatedNumber(winningNumbers, "6")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("[ERROR] 보너스 번호는 당첨 번호와 일치할 수 없습니다."); + } /* @Test void 당첨_번호_개수(){