code
stringlengths
41
34.3k
lang
stringclasses
8 values
review
stringlengths
1
4.74k
@@ -0,0 +1,79 @@ +import React, { useState } from "react"; +import { useNavigate } from 'react-router-dom'; +import "../../../style/reset.scss"; +import "../../../style/common.scss"; +import "../../../style/variables.scss" +import "./Login.scss"; + + +const Login = () => { + + const [userInfo, setUserInfo] = useState({ + userId:"์•„์ด๋””", + userPw:"ํŒจ์Šค์›Œ๋“œ", + }); + const navigate = useNavigate(); + const goToMain = () => { + fetch("http://10.58.52.144:3000/users/signup", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: userInfo.userId, + password: userInfo.userPw, + }), + }) + .then((response) => response.json()) + .then((result) => { + if(result.accessToken) { + localStorage.setItem("token", result.accessToken); + navigate("/jinheekim-main"); + } + if(result.message === "invalid password") { + alert ("๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ ธ์Œ"); + }; + if(result.message === "specified user does not exist") { + alert ("์•„์ด๋”” ํ‹€๋ ธ์Œ"); + } + }) + + } + + const handleInput = (event) => { + const {value, id} = event.target; + setUserInfo({...userInfo, [id]:value}); + }; + + const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5; + + return ( + <div className="login"> + <div className="box"> + <h1>Westagram</h1> + <div className="inputTxt"> + <input + id="userId" + onChange={handleInput} + type="text" + className="id" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + id="userPw" + onChange={handleInput} + type="password" + className="pw" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋กœ๊ทธ์ธ</button> + <div className="findPw"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </div> + </div> + </div> + ); +}; + +export default Login; \ No newline at end of file
JavaScript
๐Ÿ‘ ์ข‹์Šต๋‹ˆ๋‹ค! ๊ณ„์†ํ•ด์„œ ์ด๋ ‡๊ฒŒ ์„ธ์…˜๋‚ด์šฉ ๋ฐ”๋กœ๋ฐ”๋กœ ์ ์šฉํ•ด์ฃผ๋ฉด์„œ ๋ณธ์ธ์˜ ์ง€์‹์œผ๋กœ ๊ฐ€์ ธ๊ฐ€์ฃผ์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,79 @@ +import React, { useState } from "react"; +import { useNavigate } from 'react-router-dom'; +import "../../../style/reset.scss"; +import "../../../style/common.scss"; +import "../../../style/variables.scss" +import "./Login.scss"; + + +const Login = () => { + + const [userInfo, setUserInfo] = useState({ + userId:"์•„์ด๋””", + userPw:"ํŒจ์Šค์›Œ๋“œ", + }); + const navigate = useNavigate(); + const goToMain = () => { + fetch("http://10.58.52.144:3000/users/signup", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: userInfo.userId, + password: userInfo.userPw, + }), + }) + .then((response) => response.json()) + .then((result) => { + if(result.accessToken) { + localStorage.setItem("token", result.accessToken); + navigate("/jinheekim-main"); + } + if(result.message === "invalid password") { + alert ("๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ ธ์Œ"); + }; + if(result.message === "specified user does not exist") { + alert ("์•„์ด๋”” ํ‹€๋ ธ์Œ"); + } + }) + + } + + const handleInput = (event) => { + const {value, id} = event.target; + setUserInfo({...userInfo, [id]:value}); + }; + + const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5; + + return ( + <div className="login"> + <div className="box"> + <h1>Westagram</h1> + <div className="inputTxt"> + <input + id="userId" + onChange={handleInput} + type="text" + className="id" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + id="userPw" + onChange={handleInput} + type="password" + className="pw" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋กœ๊ทธ์ธ</button> + <div className="findPw"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </div> + </div> + </div> + ); +}; + +export default Login; \ No newline at end of file
JavaScript
์ฝ”๋“œ์— ์ฃผ์„์ด ๋งŽ์Šต๋‹ˆ๋‹ค! ์ถ”ํ›„์— ๋‹ค์‹œ ํ•™์Šตํ•  ์šฉ๋„๋กœ ๋‚จ๊ฒจ๋†“์€ ์ฃผ์„์ด๋ผ๋ฉด, ํ•ด๋‹น๋‚ด์šฉ์€ ๋”ฐ๋กœ ๋ธ”๋กœ๊ทธ๋‚˜ ๊ฐœ์ธ ๋ฉ”๋ชจ์— ์ž‘์„ฑ์„ ํ•ด ์ฃผ์‹œ๊ณ , ์ฝ”๋“œ๋ฅผ github์„ ํ†ตํ•ด ์˜ฌ๋ ค์ฃผ์‹ค๋•Œ๋Š” ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๋ฅผ ์ตœ๋Œ€ํ•œ ์ค„์—ฌ์ฃผ์„ธ์š”!
@@ -0,0 +1,79 @@ +import React, { useState } from "react"; +import { useNavigate } from 'react-router-dom'; +import "../../../style/reset.scss"; +import "../../../style/common.scss"; +import "../../../style/variables.scss" +import "./Login.scss"; + + +const Login = () => { + + const [userInfo, setUserInfo] = useState({ + userId:"์•„์ด๋””", + userPw:"ํŒจ์Šค์›Œ๋“œ", + }); + const navigate = useNavigate(); + const goToMain = () => { + fetch("http://10.58.52.144:3000/users/signup", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: userInfo.userId, + password: userInfo.userPw, + }), + }) + .then((response) => response.json()) + .then((result) => { + if(result.accessToken) { + localStorage.setItem("token", result.accessToken); + navigate("/jinheekim-main"); + } + if(result.message === "invalid password") { + alert ("๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ ธ์Œ"); + }; + if(result.message === "specified user does not exist") { + alert ("์•„์ด๋”” ํ‹€๋ ธ์Œ"); + } + }) + + } + + const handleInput = (event) => { + const {value, id} = event.target; + setUserInfo({...userInfo, [id]:value}); + }; + + const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5; + + return ( + <div className="login"> + <div className="box"> + <h1>Westagram</h1> + <div className="inputTxt"> + <input + id="userId" + onChange={handleInput} + type="text" + className="id" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + id="userPw" + onChange={handleInput} + type="password" + className="pw" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋กœ๊ทธ์ธ</button> + <div className="findPw"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </div> + </div> + </div> + ); +}; + +export default Login; \ No newline at end of file
JavaScript
```suggestion <div className="inputTxt"> ``` ์œ„ ์ฝ”๋“œ ์ฒ˜๋Ÿผ js์˜ ๊ธฐ๋ณธ ์ปจ๋ฒค์…˜์€ `camelCase`์ž…๋‹ˆ๋‹ค! className์„ ์ž‘์„ฑํ•  ๋•Œ์—๋„ ์‹ ๊ฒฝ์จ์ฃผ์…”์•ผํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,79 @@ +import React, { useState } from "react"; +import { useNavigate } from 'react-router-dom'; +import "../../../style/reset.scss"; +import "../../../style/common.scss"; +import "../../../style/variables.scss" +import "./Login.scss"; + + +const Login = () => { + + const [userInfo, setUserInfo] = useState({ + userId:"์•„์ด๋””", + userPw:"ํŒจ์Šค์›Œ๋“œ", + }); + const navigate = useNavigate(); + const goToMain = () => { + fetch("http://10.58.52.144:3000/users/signup", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + email: userInfo.userId, + password: userInfo.userPw, + }), + }) + .then((response) => response.json()) + .then((result) => { + if(result.accessToken) { + localStorage.setItem("token", result.accessToken); + navigate("/jinheekim-main"); + } + if(result.message === "invalid password") { + alert ("๋น„๋ฐ€๋ฒˆํ˜ธ ํ‹€๋ ธ์Œ"); + }; + if(result.message === "specified user does not exist") { + alert ("์•„์ด๋”” ํ‹€๋ ธ์Œ"); + } + }) + + } + + const handleInput = (event) => { + const {value, id} = event.target; + setUserInfo({...userInfo, [id]:value}); + }; + + const isValue = userInfo.userId.includes('@') && userInfo.userPw.length >= 5; + + return ( + <div className="login"> + <div className="box"> + <h1>Westagram</h1> + <div className="inputTxt"> + <input + id="userId" + onChange={handleInput} + type="text" + className="id" + placeholder="์ „ํ™”๋ฒˆํ˜ธ, ์‚ฌ์šฉ์ž ์ด๋ฆ„ ๋˜๋Š” ์ด๋ฉ”์ผ" + /> + <input + id="userPw" + onChange={handleInput} + type="password" + className="pw" + placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ" + /> + <button type="button" onClick={goToMain} className={isValue ? "abled-button" : 'disabled-button'} disabled={!isValue}>๋กœ๊ทธ์ธ</button> + <div className="findPw"> + <a href="#">๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์žŠ์œผ์…จ๋‚˜์š”?</a> + </div> + </div> + </div> + </div> + ); +}; + +export default Login; \ No newline at end of file
JavaScript
ํ•ด๋‹น scssํŒŒ์ผ๋“ค์€ Index.js์—์„œ import ํ•ด์™€์„œ ์ „์—ญ์œผ๋กœ ์ ์šฉ์‹œํ‚ค๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,87 @@ +@import '../../../style/variables.scss'; +@import '../../../style/reset.scss'; + + +body { + font-size: 14px; +} +input { + outline: none; +} + +.login { + width: 100%; + height: 100vh; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + .box { + width: 400px; + padding: 40px 0 20px; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + border: 1px solid rgb(219, 219, 219); + + h1 { + font-size:40px; + font-family: 'Lobster'; + } + + .inputTxt { + width: 300px; + + input { + display: block; + width: 100%; + margin: 5px 0; + padding: 10px 0 10px 5px; + background-color: $grey-color-bg; + border-radius: 3px; + border: 1px solid $grey-color-border; + + &::placeholder{ + font-size: 12px; + } + } + } + } +} + +@mixin button-style { + margin-top: 20px; + display: inline-block; + width: 100%; + text-align: center; + text-decoration-line: none; + padding: 8px 0; + border-radius: 8px; + border: 0px; + font-weight: bold; + color: #fff; + cursor: pointer; + } + +.abled-button { + @include button-style; + background-color: $point-color-blue; +} + +.disabled-button { + @include button-style; + background-color: #666; +} + + +.findPw { + text-align: center; + margin: 60px 0; + + a { + font-size: 9px; + color: rgb(0, 55, 107); + text-decoration: none; + } +} \ No newline at end of file
Unknown
๋งˆ์ฐฌ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค! font๊ฐ™์€ ์†์„ฑ๋“ค๋„ common.scss์— ์ž‘์„ฑํ•ด์„œ ์ „์—ญ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,87 @@ +@import '../../../style/variables.scss'; +@import '../../../style/reset.scss'; + + +body { + font-size: 14px; +} +input { + outline: none; +} + +.login { + width: 100%; + height: 100vh; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + .box { + width: 400px; + padding: 40px 0 20px; + display: flex; + justify-content: center; + align-items: center; + flex-direction: column; + border: 1px solid rgb(219, 219, 219); + + h1 { + font-size:40px; + font-family: 'Lobster'; + } + + .inputTxt { + width: 300px; + + input { + display: block; + width: 100%; + margin: 5px 0; + padding: 10px 0 10px 5px; + background-color: $grey-color-bg; + border-radius: 3px; + border: 1px solid $grey-color-border; + + &::placeholder{ + font-size: 12px; + } + } + } + } +} + +@mixin button-style { + margin-top: 20px; + display: inline-block; + width: 100%; + text-align: center; + text-decoration-line: none; + padding: 8px 0; + border-radius: 8px; + border: 0px; + font-weight: bold; + color: #fff; + cursor: pointer; + } + +.abled-button { + @include button-style; + background-color: $point-color-blue; +} + +.disabled-button { + @include button-style; + background-color: #666; +} + + +.findPw { + text-align: center; + margin: 60px 0; + + a { + font-size: 9px; + color: rgb(0, 55, 107); + text-decoration: none; + } +} \ No newline at end of file
Unknown
variables.scssํŒŒ์ผ์— ์žˆ์œผ๋ฉด ๋  ๋ณ€์ˆ˜๋“ค์ด๋„ค์š”!
@@ -0,0 +1,163 @@ +import React, { useState, useEffect } from "react"; +import MainFeeds from "./MainFeeds"; +import "./Main.scss"; +import compass from "../assets/icon/compass.png"; +import heart from "../assets/icon/heart.png"; +import instagram from "../assets/icon/instagram.png"; +import search from "../assets/icon/search.png"; +import user from "../assets/icon/user.png"; +import userimg from "../assets/images/test.jpg"; +import feedimg from "../assets/images/test.jpg"; + +const INFO_LIST = [ + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, +]; + +const Main = () => { + return ( + <> + <nav className="nav"> + <div className="navLeft"> + <img className="navIcon iconInstagram" alt="." src={instagram} /> + <span className="logo">Westagram</span> + </div> + <div className="searchBar"> + <img className="iconSearch" alt="." src={search} /> + <input + className="searchBox" + type="text" + aria-label="๊ฒ€์ƒ‰์ฐฝ" + placeholder="๊ฒ€์ƒ‰" + /> + </div> + <div className="navRight"> + <img className="navIcon" alt="." src={compass} /> + <img className="navIcon" alt="." src={heart} /> + <img className="navIcon" alt="." src={user} /> + </div> + </nav> + <div className="main"> + <MainFeeds /> + <div className="mainRight"> + <div className="mainRightTop"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="stories"> + <div className="storiesTop"> + <p className="story">์Šคํ† ๋ฆฌ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + </div> + <div className="recommend"> + <div className="storiesTop"> + <p className="story">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + </div> + <div> + <ul> + {INFO_LIST.map((info) => { + return ( + <li key={info.id}> + <a href="{info.link}">{info.text}</a> + </li> + ); + })} + </ul> + <p className="copyright">โ“’ 2023 WESTAGRAM</p> + </div> + </div> + </div> + </> + ); +}; + +export default Main;
JavaScript
index.js์—์„œ ์ „์—ญ์œผ๋กœ importํ•œ๋‹ค๋ฉด ์ด๋ ‡๊ฒŒ ํŒŒ์ผ๋งˆ๋‹ค ๋งค๋ฒˆ import ํ•˜์ง€์•Š์•„๋„ ๋ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,163 @@ +import React, { useState, useEffect } from "react"; +import MainFeeds from "./MainFeeds"; +import "./Main.scss"; +import compass from "../assets/icon/compass.png"; +import heart from "../assets/icon/heart.png"; +import instagram from "../assets/icon/instagram.png"; +import search from "../assets/icon/search.png"; +import user from "../assets/icon/user.png"; +import userimg from "../assets/images/test.jpg"; +import feedimg from "../assets/images/test.jpg"; + +const INFO_LIST = [ + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, +]; + +const Main = () => { + return ( + <> + <nav className="nav"> + <div className="navLeft"> + <img className="navIcon iconInstagram" alt="." src={instagram} /> + <span className="logo">Westagram</span> + </div> + <div className="searchBar"> + <img className="iconSearch" alt="." src={search} /> + <input + className="searchBox" + type="text" + aria-label="๊ฒ€์ƒ‰์ฐฝ" + placeholder="๊ฒ€์ƒ‰" + /> + </div> + <div className="navRight"> + <img className="navIcon" alt="." src={compass} /> + <img className="navIcon" alt="." src={heart} /> + <img className="navIcon" alt="." src={user} /> + </div> + </nav> + <div className="main"> + <MainFeeds /> + <div className="mainRight"> + <div className="mainRightTop"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="stories"> + <div className="storiesTop"> + <p className="story">์Šคํ† ๋ฆฌ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + </div> + <div className="recommend"> + <div className="storiesTop"> + <p className="story">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + </div> + <div> + <ul> + {INFO_LIST.map((info) => { + return ( + <li key={info.id}> + <a href="{info.link}">{info.text}</a> + </li> + ); + })} + </ul> + <p className="copyright">โ“’ 2023 WESTAGRAM</p> + </div> + </div> + </div> + </> + ); +}; + +export default Main;
JavaScript
`comment`๋ผ๋Š” ๋ณ€์ˆ˜๋ช…์ด ์ž‘์„ฑ๋˜์–ด์žˆ๋Š” comment๋“ค์˜ ๋ชจ์Œ์ธ์ง€, ๋‚ด๊ฐ€ input์— ์ž…๋ ฅํ•œ ๊ฐ’์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ณ€์ˆ˜์ธ์ง€ ์ด๋ฆ„๋งŒ ๋ดค์„ ๋•Œ ๋ฐ”๋กœ ์ถ”์ธกํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์— commentArray๋ผ๋Š” ๋ณ€์ˆ˜๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์กฐ๊ธˆ ๋” ๋ณ€์ˆ˜๊ฐ€ ์–ด๋–ค state๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ๋ช…ํ™•ํ•œ ์ด๋ฆ„์œผ๋กœ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,163 @@ +import React, { useState, useEffect } from "react"; +import MainFeeds from "./MainFeeds"; +import "./Main.scss"; +import compass from "../assets/icon/compass.png"; +import heart from "../assets/icon/heart.png"; +import instagram from "../assets/icon/instagram.png"; +import search from "../assets/icon/search.png"; +import user from "../assets/icon/user.png"; +import userimg from "../assets/images/test.jpg"; +import feedimg from "../assets/images/test.jpg"; + +const INFO_LIST = [ + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, +]; + +const Main = () => { + return ( + <> + <nav className="nav"> + <div className="navLeft"> + <img className="navIcon iconInstagram" alt="." src={instagram} /> + <span className="logo">Westagram</span> + </div> + <div className="searchBar"> + <img className="iconSearch" alt="." src={search} /> + <input + className="searchBox" + type="text" + aria-label="๊ฒ€์ƒ‰์ฐฝ" + placeholder="๊ฒ€์ƒ‰" + /> + </div> + <div className="navRight"> + <img className="navIcon" alt="." src={compass} /> + <img className="navIcon" alt="." src={heart} /> + <img className="navIcon" alt="." src={user} /> + </div> + </nav> + <div className="main"> + <MainFeeds /> + <div className="mainRight"> + <div className="mainRightTop"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="stories"> + <div className="storiesTop"> + <p className="story">์Šคํ† ๋ฆฌ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + </div> + <div className="recommend"> + <div className="storiesTop"> + <p className="story">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + </div> + <div> + <ul> + {INFO_LIST.map((info) => { + return ( + <li key={info.id}> + <a href="{info.link}">{info.text}</a> + </li> + ); + })} + </ul> + <p className="copyright">โ“’ 2023 WESTAGRAM</p> + </div> + </div> + </div> + </> + ); +}; + +export default Main;
JavaScript
```suggestion setCommentArray(commentList => [ ...commentList,comment]); ``` ์ด ๋‘๊ฐœ์˜ ์ฐจ์ด์ ์„ ํ™•์‹คํžˆ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์ฃผ์…จ์œผ๋ฉด ์ข‹๊ฒ ์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,163 @@ +import React, { useState, useEffect } from "react"; +import MainFeeds from "./MainFeeds"; +import "./Main.scss"; +import compass from "../assets/icon/compass.png"; +import heart from "../assets/icon/heart.png"; +import instagram from "../assets/icon/instagram.png"; +import search from "../assets/icon/search.png"; +import user from "../assets/icon/user.png"; +import userimg from "../assets/images/test.jpg"; +import feedimg from "../assets/images/test.jpg"; + +const INFO_LIST = [ + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, +]; + +const Main = () => { + return ( + <> + <nav className="nav"> + <div className="navLeft"> + <img className="navIcon iconInstagram" alt="." src={instagram} /> + <span className="logo">Westagram</span> + </div> + <div className="searchBar"> + <img className="iconSearch" alt="." src={search} /> + <input + className="searchBox" + type="text" + aria-label="๊ฒ€์ƒ‰์ฐฝ" + placeholder="๊ฒ€์ƒ‰" + /> + </div> + <div className="navRight"> + <img className="navIcon" alt="." src={compass} /> + <img className="navIcon" alt="." src={heart} /> + <img className="navIcon" alt="." src={user} /> + </div> + </nav> + <div className="main"> + <MainFeeds /> + <div className="mainRight"> + <div className="mainRightTop"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="stories"> + <div className="storiesTop"> + <p className="story">์Šคํ† ๋ฆฌ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + </div> + <div className="recommend"> + <div className="storiesTop"> + <p className="story">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + </div> + <div> + <ul> + {INFO_LIST.map((info) => { + return ( + <li key={info.id}> + <a href="{info.link}">{info.text}</a> + </li> + ); + })} + </ul> + <p className="copyright">โ“’ 2023 WESTAGRAM</p> + </div> + </div> + </div> + </> + ); +}; + +export default Main;
JavaScript
Input์— ๊ฐ’์ด ๋น„์–ด์žˆ์„๋•Œ ํ•จ์ˆ˜๊ฐ€ ๊ทธ๋ƒฅ ์•„๋ฌด๋Ÿฐ ๋™์ž‘์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ธฐ๋Šฅ์ƒ ์—๋Ÿฌ์ธ์ง€, ์˜๋„๋œ ๋™์ž‘์ธ์ง€ ํŒŒ์•…ํ•˜๊ธฐ ์–ด๋ ต์Šต๋‹ˆ๋‹ค. `comment` ๊ฐ’์ด ๋น„์–ด์žˆ๋‹ค๋ฉด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ `alert`๋ฅผ ํ‘œ์‹œํ•ด์ฃผ๋Š” ๋“ฑ์˜ ์ถ”๊ฐ€์ ์ธ ์กฐ์น˜๊ฐ€ ์žˆ์œผ๋ฉด ์ข‹์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,163 @@ +import React, { useState, useEffect } from "react"; +import MainFeeds from "./MainFeeds"; +import "./Main.scss"; +import compass from "../assets/icon/compass.png"; +import heart from "../assets/icon/heart.png"; +import instagram from "../assets/icon/instagram.png"; +import search from "../assets/icon/search.png"; +import user from "../assets/icon/user.png"; +import userimg from "../assets/images/test.jpg"; +import feedimg from "../assets/images/test.jpg"; + +const INFO_LIST = [ + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, +]; + +const Main = () => { + return ( + <> + <nav className="nav"> + <div className="navLeft"> + <img className="navIcon iconInstagram" alt="." src={instagram} /> + <span className="logo">Westagram</span> + </div> + <div className="searchBar"> + <img className="iconSearch" alt="." src={search} /> + <input + className="searchBox" + type="text" + aria-label="๊ฒ€์ƒ‰์ฐฝ" + placeholder="๊ฒ€์ƒ‰" + /> + </div> + <div className="navRight"> + <img className="navIcon" alt="." src={compass} /> + <img className="navIcon" alt="." src={heart} /> + <img className="navIcon" alt="." src={user} /> + </div> + </nav> + <div className="main"> + <MainFeeds /> + <div className="mainRight"> + <div className="mainRightTop"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="stories"> + <div className="storiesTop"> + <p className="story">์Šคํ† ๋ฆฌ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + </div> + <div className="recommend"> + <div className="storiesTop"> + <p className="story">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + </div> + <div> + <ul> + {INFO_LIST.map((info) => { + return ( + <li key={info.id}> + <a href="{info.link}">{info.text}</a> + </li> + ); + })} + </ul> + <p className="copyright">โ“’ 2023 WESTAGRAM</p> + </div> + </div> + </div> + </> + ); +}; + +export default Main;
JavaScript
map ๋ฉ”์„œ๋“œ์— ์ฝœ๋ฐฑํ•จ์ˆ˜์˜ ๋‘๋ฒˆ ์งธ ์ธ์ž, ์ฆ‰ ์ง„ํฌ๋‹˜์˜ ์ฝ”๋“œ์—์„œ user๋ผ๊ณ ํ•˜๋Š” ๋ณ€์ˆ˜๋Š” ๊ธฐ์ค€๋ฐฐ์—ด(commnetArray)์˜ value(๋ฐฐ์—ด์˜ ๊ฐ๊ฐ์˜ ์š”์†Œ)๋งˆ๋‹ค์˜ index(๋ฐฐ์—ด๋‚ด์˜ ์ˆœ์„œ)๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๋ฆฌ์•กํŠธ์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ key ์†์„ฑ์„ ๊ผญ ๋ถ€์—ฌํ•˜๋ผ๊ณ  ํ•˜๋Š” ์ด์œ ๋Š” ํ•ด๋‹น์š”์†Œ๋ฅผ ๋ฆฌ์•กํŠธ๊ฐ€ ๋ช…ํ™•์ด ์ธ์‹ํ•˜๊ธฐ ์œ„ํ•จ์ธ๋ฐ, ๋ฐฐ์—ด์˜ index๋ฅผ key๊ฐ’์œผ๋กœ ๋ถ€์—ฌํ•˜๋Š”๊ฒƒ์€ ์กฐ๊ธˆ ์ง€์–‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์–ด๋–ค ๊ฐ’์œผ๋กœ key ์†์„ฑ์„ ๋ถ€์—ฌํ•ด์•ผํ•  ์ง€ ์กฐ๊ธˆ ๋” ๊ณ ๋ฏผํ•ด๋ณด์„ธ์š”!
@@ -0,0 +1,163 @@ +import React, { useState, useEffect } from "react"; +import MainFeeds from "./MainFeeds"; +import "./Main.scss"; +import compass from "../assets/icon/compass.png"; +import heart from "../assets/icon/heart.png"; +import instagram from "../assets/icon/instagram.png"; +import search from "../assets/icon/search.png"; +import user from "../assets/icon/user.png"; +import userimg from "../assets/images/test.jpg"; +import feedimg from "../assets/images/test.jpg"; + +const INFO_LIST = [ + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, + { id: 1, link: "https://instargram.com", text: "์†Œ๊ฐœ" }, +]; + +const Main = () => { + return ( + <> + <nav className="nav"> + <div className="navLeft"> + <img className="navIcon iconInstagram" alt="." src={instagram} /> + <span className="logo">Westagram</span> + </div> + <div className="searchBar"> + <img className="iconSearch" alt="." src={search} /> + <input + className="searchBox" + type="text" + aria-label="๊ฒ€์ƒ‰์ฐฝ" + placeholder="๊ฒ€์ƒ‰" + /> + </div> + <div className="navRight"> + <img className="navIcon" alt="." src={compass} /> + <img className="navIcon" alt="." src={heart} /> + <img className="navIcon" alt="." src={user} /> + </div> + </nav> + <div className="main"> + <MainFeeds /> + <div className="mainRight"> + <div className="mainRightTop"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="stories"> + <div className="storiesTop"> + <p className="story">์Šคํ† ๋ฆฌ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + </div> + </div> + <div className="recommend"> + <div className="storiesTop"> + <p className="story">ํšŒ์›๋‹˜์„ ์œ„ํ•œ ์ถ”์ฒœ</p> + <p>๋ชจ๋‘ ๋ณด๊ธฐ</p> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + <div className="storyPeople"> + <div className="photo"> + <img src={userimg} alt="."></img> + </div> + <div className="name"> + <p className="username">jini</p> + <p className="username2">์ง€๋‹ˆ์ง€๋‹ˆ</p> + </div> + <button>ํŒ”๋กœ์šฐ</button> + </div> + </div> + <div> + <ul> + {INFO_LIST.map((info) => { + return ( + <li key={info.id}> + <a href="{info.link}">{info.text}</a> + </li> + ); + })} + </ul> + <p className="copyright">โ“’ 2023 WESTAGRAM</p> + </div> + </div> + </div> + </> + ); +}; + +export default Main;
JavaScript
๊ตณ์ด ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€์•Š๋”๋ผ๋„ `<textarea></textarea>`ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด multiline input์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,264 @@ +@import '../../../style/reset.scss'; +@import '../../../style/variables.scss'; + +body { + font-size: 14px; +} +.logo { + font-family: "Lobster"; + src: url(../assets/Lobster-Regular.ttf); +} +input { + outline: none; +} + +.navIcon { + width: 24px; + margin-left: 20px; +} + +.navLeft{ + .iconInstagram { + margin-left: 0; + } +} +.nav { + display: flex; + width: 860px; + margin: 0 auto; + justify-content: space-between; + align-items: center; + padding: 40px 0; + + .logo { + font-size: 24px; + margin-left: 20px; + border-left: 1px solid black; + padding-left: 20px; + } +} +.navLeft, .searchBar, .navRight{ + display: flex; + align-items: center; +} + +.searchBar { + width: 300px; + background-color: $grey-color-bg; + border: 1px solid $grey-color-border; + border-radius: 5px; + display: flex; + justify-content: center; + align-items: center; + + .searchBox { + width: 50px; + padding : 8px 0; + border: none; + background-color: $grey-color-bg; + text-align: center; + margin-left: 10px; + overflow: auto; + outline: none; + &:focus { + outline: none; + } + } + + .iconSearch { + width: 16px; + } +} + +.bold {font-weight: bold;} +.main { + width: 100%; + height: 100vh; + display: flex; + justify-content: center; + padding: 80px 40px; + background-color: $grey-color-bg; + border-top: 2px solid black; + + .username { + font-weight: bold; + } + + .feeds { + border: 1px solid $grey-color-border; + background-color: white; + + .feedImg img { + width: 468px; + } + } + .feedsTop, .mainRightTop { + display: flex; + align-items: stretch; + padding: 15px; + } + + .name { + display: flex; + margin-left: 15px; + justify-content: center; + flex-direction: column; + } + .location, .username2 { + font-size: 12px; + color: gray; + } + + .photo img{ + width: 50px; + border-radius: 100%; + } + + .stories{ + .photo { + background: radial-gradient(circle at bottom left, #F58529 20%, #C42D91); + border-radius: 100%; + height: 57px; + width: 57px; + + img { + box-sizing: content-box; + border: 2px solid #fff; + margin: 1px; + } + } + } + + .feedsBottom { + padding: 10px; + + .photo img { + width: 24px; + margin-right: 10px; + padding: 1px; + border:1px solid rgb(188, 188, 188); + } + + .like { + margin-top:10px; + display: inline-flex; + } + + .feedsBottomIcons { + display: flex; + justify-content: space-between; + + .bottomRight > .navIcon { + margin-left:0; + } + + .bottomLeft .navIcon:first-child { + margin-left: 0; + } + } + + .comment { + margin-top: 10px; + + .writeTime { + color: grey; + margin-top: 10px; + } + .commentContainer{ + position: relative; + } + .commentButton { + position: absolute; + right: 0; + top: 20px; + border: none; + background-color: inherit; + color: grey; + } + } + + .description { + margin-top: 20px; + } + + .commentBox { + width: 100%; + resize: none; + margin-top: 20px; + border-style: none; + border-bottom: 1px solid grey; + + &:focus { + outline: none; + } + } + } + + .mainRight { + width: 300px; + padding: 0 20px; + + .stories, .recommend { + border: 1px solid $grey-color-border; + background-color: white; + padding: 15px; + height: 250px; + overflow: hidden; + } + + .storiesTop { + display: flex; + justify-content: space-between; + font-size: 12px; + } + + .recommend { + margin-top: 20px; + } + + .story { + color: grey; + } + + .storyPeople { + display: flex; + position: relative; + margin-top: 15px; + + button { + position: absolute; + right: 0; + margin-top: 15px; + border: none; + background-color: inherit; + color: $point-color-blue; + font-weight: bold; + + } + } + } + + ul { + padding: 0; + + li { + display: inline-block; + list-style-type: none; + color: $grey-color-193; + margin:5px 5px 0; + } + li:first-child { + margin-left: 0; + } + + a { + text-decoration: none; + color: $grey-color-193; + } + } + + .copyright { + color: $grey-color-193; + margin-top: 10px; + } +} \ No newline at end of file
Unknown
์•ž์„œ ๋‚จ๊ฒจ๋“œ๋ฆฐ ๋‚ด์šฉ์˜ ๋ฆฌ๋ทฐ์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค
@@ -0,0 +1,97 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class DiaryController { + private final DiaryService diaryService; + private final FeelingService feelingService; + + @ApiOperation(value = "Diary ๋“ฑ๋กํ•˜๊ธฐ") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation(value = "Diary ๋‹จ๊ฑด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/{id}") + public ResponseEntity<?> get(@PathVariable Long id) { + var response = diaryService.get(id); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/search-all") + public ResponseEntity<?> getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable + ) { + var response = diaryService.getAll(pageable); + return PageDto.ok(response); + } + + @ApiOperation(value = "Diary ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping("/{id}") // @PatchMapping + public ResponseEntity<?> update( + @PathVariable Long id, + @RequestBody DiaryUpdateRequest request + ) { + var response = diaryService.update(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/{id}") + public ResponseEntity<Void> delete( + @PathVariable Long id + ) { + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary ์›”๋ณ„ ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/month/{month}") + public ResponseEntity<Void> monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Feeling ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") + public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/feeling-all") + public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } +}
Java
์ด๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” month๋ฅผ Request Param์œผ๋กœ
@@ -0,0 +1,97 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class DiaryController { + private final DiaryService diaryService; + private final FeelingService feelingService; + + @ApiOperation(value = "Diary ๋“ฑ๋กํ•˜๊ธฐ") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation(value = "Diary ๋‹จ๊ฑด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/{id}") + public ResponseEntity<?> get(@PathVariable Long id) { + var response = diaryService.get(id); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/search-all") + public ResponseEntity<?> getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable + ) { + var response = diaryService.getAll(pageable); + return PageDto.ok(response); + } + + @ApiOperation(value = "Diary ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping("/{id}") // @PatchMapping + public ResponseEntity<?> update( + @PathVariable Long id, + @RequestBody DiaryUpdateRequest request + ) { + var response = diaryService.update(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/{id}") + public ResponseEntity<Void> delete( + @PathVariable Long id + ) { + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary ์›”๋ณ„ ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/month/{month}") + public ResponseEntity<Void> monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Feeling ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") + public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/feeling-all") + public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } +}
Java
์Œ ์—ฌ๋Ÿฌ ์กฐ๊ฑด์œผ๋กœ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๋งŒ๋“œ์‹  ๊ฒƒ ๊ฐ™์€๋ฐ, API๋ฅผ ๋‚˜๋ˆ„์ง€ ๋ง๊ณ , ๊ฒ€์ƒ‰ ๋ชจ๋“ˆ์„ ๋งŒ๋“œ๋Š”๊ฒŒ ์–ด๋–จ๊นŒ์š”?
@@ -0,0 +1,71 @@ +package com.selab.todo.entity; + +import com.selab.todo.common.BaseEntity; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import java.time.DayOfWeek; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.Year; + +@Entity(name = "diary") +@Getter +@Table(name = "diary") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Diary extends BaseEntity { + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "title") + private String title; + + @Column(name = "content") + private String content; + + @Column(name = "createdAt") + @CreatedDate + private LocalDateTime createdAt; + + @Column(name = "year") + private Year year; + + @Column(name = "month") + private Month month; + + @Column(name = "day") + private DayOfWeek day; + + @Column(name = "feel") + private String feel; + + + public Diary(String title, String content, String feel, Year year, Month month, DayOfWeek day) { + this.title = title; + this.content = content; + this.feel = feel; + this.year = year; + this.month = month; + this.day = day; + } + + public void update(String title, String content, String feel) { + this.title = title; + this.content = content; + this.feel = feel; + } + + public void feelingUpdate(String feel){ + this.feel = feel; + } +} \ No newline at end of file
Java
enumrated ์ถ”๊ฐ€ํ•ด์•ผ ๋  ๊ฒƒ ๊ฐ™์€๋ฐ์š”?
@@ -0,0 +1,97 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class DiaryController { + private final DiaryService diaryService; + private final FeelingService feelingService; + + @ApiOperation(value = "Diary ๋“ฑ๋กํ•˜๊ธฐ") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation(value = "Diary ๋‹จ๊ฑด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/{id}") + public ResponseEntity<?> get(@PathVariable Long id) { + var response = diaryService.get(id); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/search-all") + public ResponseEntity<?> getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable + ) { + var response = diaryService.getAll(pageable); + return PageDto.ok(response); + } + + @ApiOperation(value = "Diary ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping("/{id}") // @PatchMapping + public ResponseEntity<?> update( + @PathVariable Long id, + @RequestBody DiaryUpdateRequest request + ) { + var response = diaryService.update(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/{id}") + public ResponseEntity<Void> delete( + @PathVariable Long id + ) { + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary ์›”๋ณ„ ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/month/{month}") + public ResponseEntity<Void> monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Feeling ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") + public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/feeling-all") + public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } +}
Java
url path์— ๋Œ€ํ•ด ๋‹ค์‹œ ํ•œ๋ฒˆ ๋” ์ ๊ฒ€ ๋ถ€ํƒ๋“œ๋ฆ…๋‹ˆ๋‹ค! rest url ๊ทœ์น™ ํ™•์ธํ•˜๊ธฐ
@@ -0,0 +1,71 @@ +package com.selab.todo.entity; + +import com.selab.todo.common.BaseEntity; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.springframework.data.annotation.CreatedDate; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import java.time.DayOfWeek; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.Year; + +@Entity(name = "diary") +@Getter +@Table(name = "diary") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class Diary extends BaseEntity { + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(name = "title") + private String title; + + @Column(name = "content") + private String content; + + @Column(name = "createdAt") + @CreatedDate + private LocalDateTime createdAt; + + @Column(name = "year") + private Year year; + + @Column(name = "month") + private Month month; + + @Column(name = "day") + private DayOfWeek day; + + @Column(name = "feel") + private String feel; + + + public Diary(String title, String content, String feel, Year year, Month month, DayOfWeek day) { + this.title = title; + this.content = content; + this.feel = feel; + this.year = year; + this.month = month; + this.day = day; + } + + public void update(String title, String content, String feel) { + this.title = title; + this.content = content; + this.feel = feel; + } + + public void feelingUpdate(String feel){ + this.feel = feel; + } +} \ No newline at end of file
Java
LocalDateTime ํ˜น์€ ZoneDateTime๊ณผ ๊ฐ™์€ ์‹œ๊ฐ„์„ ๋‹ค๋ฃจ๋Š” ๊ฐ์ฒด ์‚ฌ์šฉ์„ ํ•˜์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”?
@@ -0,0 +1,23 @@ +package com.selab.todo.entity; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.*; + +@Entity(name = "feeling") +@Getter +@Table(name = "feeling") +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +public class Feeling { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") + private Long id; + + @Column(name = "feel") + private String feel; +}
Java
ํ•ด๋‹น ํด๋ž˜์Šค์˜ ์—ญํ• ์€ ๋ฌด์—‡์ผ๊นŒ์š”? id, baseentity ์ •๋„๋Š” ์ถ”๊ฐ€ํ•ด๋„ ๋  ๊ฒƒ ๊ฐ™์•„์š”
@@ -0,0 +1,97 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class DiaryController { + private final DiaryService diaryService; + private final FeelingService feelingService; + + @ApiOperation(value = "Diary ๋“ฑ๋กํ•˜๊ธฐ") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation(value = "Diary ๋‹จ๊ฑด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/{id}") + public ResponseEntity<?> get(@PathVariable Long id) { + var response = diaryService.get(id); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/search-all") + public ResponseEntity<?> getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable + ) { + var response = diaryService.getAll(pageable); + return PageDto.ok(response); + } + + @ApiOperation(value = "Diary ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping("/{id}") // @PatchMapping + public ResponseEntity<?> update( + @PathVariable Long id, + @RequestBody DiaryUpdateRequest request + ) { + var response = diaryService.update(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/{id}") + public ResponseEntity<Void> delete( + @PathVariable Long id + ) { + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary ์›”๋ณ„ ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/month/{month}") + public ResponseEntity<Void> monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Feeling ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") + public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/feeling-all") + public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } +}
Java
์ ์šฉํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,97 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class DiaryController { + private final DiaryService diaryService; + private final FeelingService feelingService; + + @ApiOperation(value = "Diary ๋“ฑ๋กํ•˜๊ธฐ") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation(value = "Diary ๋‹จ๊ฑด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/{id}") + public ResponseEntity<?> get(@PathVariable Long id) { + var response = diaryService.get(id); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/search-all") + public ResponseEntity<?> getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable + ) { + var response = diaryService.getAll(pageable); + return PageDto.ok(response); + } + + @ApiOperation(value = "Diary ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping("/{id}") // @PatchMapping + public ResponseEntity<?> update( + @PathVariable Long id, + @RequestBody DiaryUpdateRequest request + ) { + var response = diaryService.update(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/{id}") + public ResponseEntity<Void> delete( + @PathVariable Long id + ) { + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary ์›”๋ณ„ ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/month/{month}") + public ResponseEntity<Void> monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Feeling ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") + public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/feeling-all") + public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } +}
Java
default๋Š” ์™œ ํ•„์š”ํ•ด์š”??
@@ -0,0 +1,97 @@ +package com.selab.todo.controller; + +import com.selab.todo.common.dto.PageDto; +import com.selab.todo.common.dto.ResponseDto; +import com.selab.todo.dto.request.diary.DiaryRegisterRequest; +import com.selab.todo.dto.request.diary.DiaryUpdateRequest; +import com.selab.todo.dto.request.feel.FeelingUpdateRequest; +import com.selab.todo.service.DiaryService; +import com.selab.todo.service.FeelingService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api(tags = {"Diary API"}) +@RestController +@RequestMapping(value = "/api/v1/diaries", produces = MediaType.APPLICATION_JSON_VALUE) +@RequiredArgsConstructor +public class DiaryController { + private final DiaryService diaryService; + private final FeelingService feelingService; + + @ApiOperation(value = "Diary ๋“ฑ๋กํ•˜๊ธฐ") + @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/register") + public ResponseEntity<?> register(@RequestBody DiaryRegisterRequest request) { + var response = diaryService.register(request); + return ResponseDto.created(response); + } + + @ApiOperation(value = "Diary ๋‹จ๊ฑด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/{id}") + public ResponseEntity<?> get(@PathVariable Long id) { + var response = diaryService.get(id); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/search-all") + public ResponseEntity<?> getAll( + @PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable + ) { + var response = diaryService.getAll(pageable); + return PageDto.ok(response); + } + + @ApiOperation(value = "Diary ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping("/{id}") // @PatchMapping + public ResponseEntity<?> update( + @PathVariable Long id, + @RequestBody DiaryUpdateRequest request + ) { + var response = diaryService.update(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Diary ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/{id}") + public ResponseEntity<Void> delete( + @PathVariable Long id + ) { + diaryService.delete(id); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Diary ์›”๋ณ„ ์‚ญ์ œํ•˜๊ธฐ") + @DeleteMapping("/month/{month}") + public ResponseEntity<Void> monthDelete(@PathVariable int month) { + diaryService.monthDelete(month); + return ResponseDto.noContent(); + } + + @ApiOperation(value = "Feeling ์ˆ˜์ •ํ•˜๊ธฐ") + @PutMapping(consumes = MediaType.APPLICATION_JSON_VALUE, value = "/feeling/{id}") + public ResponseEntity<?> updateFeeling(@PathVariable Long id, @RequestBody FeelingUpdateRequest request) { + var response = feelingService.updateFeeling(id, request); + return ResponseDto.ok(response); + } + + @ApiOperation(value = "Feeling ์ „์ฒด ์กฐํšŒํ•˜๊ธฐ") + @GetMapping("/feeling-all") + public ResponseEntity<?> getAllFeeling(@PageableDefault Pageable pageable) { + var response = feelingService.getAllFeeling(pageable); + return PageDto.ok(response); + } +}
Java
REST API - POST : /api/v1/diaries - GET : /api/v1/diaries - GET : /api/v1/diaries/{id} - PUT : /api/v1/diaries/{id} - DELETE : /api/v1/diaries/{id} ์ด ํ˜•ํƒœ ์•„๋‹Œ๊ฐ€์š”? ๊ทธ๋ฆฌ๊ณ  url path์— ์™œ ๋Œ€๋ฌธ์ž๊ฐ€ ๋“ค์–ด๊ฐ€๋Š”๊ฑธ๊นŒ์šฉ?
@@ -0,0 +1,42 @@ +package racingcar.domain; + +import racingcar.exception.CarNameLengthException; + +public class Car { + + private static final int CAR_NAME_LENGTH = 5; + private static final int MOVABLE_MIN_NUMBER = 4; + private final String name; + private int position = 0; + + public Car(String name) { + validate(name); + this.name = name; + } + + private void validate(String name) { + validateLength(name); + } + + private void validateLength(String name) { + if (name.length() > CAR_NAME_LENGTH) { + throw new CarNameLengthException(); + } + } + + public void move(CarMoveNumberGenerator carMoveNumberGenerator) { + final int number = carMoveNumberGenerator.generate(); + + if (number >= MOVABLE_MIN_NUMBER) { + position++; + } + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } +}
Java
์ปจ๋ฒค์…˜์— ๋”ฐ๋ผ์„œ ์ƒ์ˆ˜์™€ ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜๋ฅผ ์ƒˆ ์ค„๋กœ ๊ตฌ๋ถ„ํ•ด์ฃผ์‹œ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!
@@ -0,0 +1,71 @@ +package racingcar.domain; + +import racingcar.exception.CarsDuplicatedNameException; +import racingcar.exception.CarsMaxScoreBlankException; + +import java.util.*; +import java.util.stream.Collectors; + +public class Cars { + + private static final String DELIMITER = ","; + private static final String DUPLICATED_DELIMITER_REGEX = ",+"; + private final List<Car> cars; + + private Cars(List<Car> cars) { + this.cars = Collections.unmodifiableList(cars); + } + + public static Cars createCarNameByWord(String input) { + List<Car> cars = new ArrayList<>(); + String[] words = divideWord(input); + validate(words); + + for (String carName : words) { + cars.add(new Car(carName)); + } + return new Cars(cars); + } + + private static void validate(String[] words) { + validateDuplicatedWord(words); + } + + private static void validateDuplicatedWord(String[] words) { + Set<String> uniqueWord = new HashSet<>(); + for (String word : words) { + if (!uniqueWord.add(word)) { + throw new CarsDuplicatedNameException(); + } + } + } + + private static String[] divideWord(String word) { + return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER); + } + + public void move(CarMoveNumberGenerator carMoveNumberGenerator) { + for (Car car : cars) { + car.move(carMoveNumberGenerator); + } + } + + public Winner findWinner() { + int maxScore = findMaxScore(); + return new Winner(cars.stream() + .filter(car -> (car.getPosition() == maxScore)) + .map(Car::getName) + .collect(Collectors.toList())); + } + + private int findMaxScore() { + return cars.stream() + .max(Comparator.comparingInt(Car::getPosition)) + .map(Car::getPosition) + .orElseThrow(CarsMaxScoreBlankException::new); + } + + public List<Car> getCars() { + return cars; + } +}
Java
๋ถˆ๋ณ€ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ์ƒ์„ฑ์ž ๋‹จ๊ณ„์—์„œ๋Š” ๋ฐฉ์–ด์  ๋ณต์‚ฌ๋ฅผ ๊ณ ๋ คํ•ด์ฃผ์‹œ๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!! - [๋ฐฉ์–ด์  ๋ณต์‚ฌ์™€ Unmodifiable Collections](https://tecoble.techcourse.co.kr/post/2021-04-26-defensive-copy-vs-unmodifiable/)
@@ -0,0 +1,16 @@ +package racingcar.domain; + +import java.util.List; + +public class Winner { + + private final List<String> winner; + + public Winner(List<String> winner) { + this.winner = winner; + } + + public List<String> getWinner() { + return winner; + } +}
Java
๋ฆฌ์ŠคํŠธ์˜ ๋„ค์ด๋ฐ์€ ๋ณต์ˆ˜ํ˜•์ด ๋” ์ ์ ˆํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค! `Winner` ํด๋ž˜์Šค ์ž์ฒด๋„ ์Šน๋ฆฌ์ž๋“ค์˜ ์ด๋ฆ„์„ ๋‹ด๊ณ ์žˆ๋Š” ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์ด๊ธฐ ๋•Œ๋ฌธ์— `Winners` ๋ผ๋Š” ๋„ค์ด๋ฐ์„ ๋” ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค!
@@ -0,0 +1,54 @@ +package racingcar.view; + +import racingcar.domain.Car; +import racingcar.domain.Cars; +import racingcar.domain.Winner; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OutputView { + + private static final String MESSAGE_INPUT_CAR_NAME = "๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"; + private static final String MESSAGE_INPUT_TRY = "์‹œ๋„ํ•  ํšŒ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"; + private static final String MESSAGE_RESULT = "์‹คํ–‰ ๊ฒฐ๊ณผ"; + private static final String MOVE_MARK = "-"; + private static final String JOIN_NAME_AND_POSITION = " : "; + private static final String WINNER_DELIMITER = ","; + private static final String MESSAGE_WINNER_PREFIX = "์ตœ์ข… ์šฐ์Šน์ž : "; + private static final String MESSAGE_WINNER_SUFFIX = ""; + + public void printInputCarName() { + System.out.println(MESSAGE_INPUT_CAR_NAME); + } + + public void printExceptionMessage(IllegalArgumentException exceptionMessage) { + System.out.println(exceptionMessage.getMessage()); + } + + public void printInputTry() { + System.out.println(MESSAGE_INPUT_TRY); + } + + public void printBlank() { + System.out.println(); + } + + public void printResult(Cars cars) { + System.out.println(MESSAGE_RESULT); + for(Car car : cars.getCars()){ + System.out.println(car.getName() + JOIN_NAME_AND_POSITION + convertMoveMark(car.getPosition())); + } + printBlank(); + } + + private String convertMoveMark(int position) { + return Stream.generate(() -> MOVE_MARK).limit(position).collect(Collectors.joining()); + } + + public void printWinner(Winner winner) { + String message = winner.getWinner().stream() + .collect(Collectors.joining(WINNER_DELIMITER, MESSAGE_WINNER_PREFIX, MESSAGE_WINNER_SUFFIX)); + System.out.println(message); + } +}
Java
String์—์„œ ์ œ๊ณตํ•˜๋Š” `repeat()` ๋ฉ”์„œ๋“œ๋ฅผ ํ™œ์šฉํ•ด๋ณด์‹œ๋Š” ๊ฒƒ๋„ ์ถ”์ฒœ๋“œ๋ฆฝ๋‹ˆ๋‹ค!
@@ -0,0 +1,71 @@ +package racingcar.domain; + +import racingcar.exception.CarsDuplicatedNameException; +import racingcar.exception.CarsMaxScoreBlankException; + +import java.util.*; +import java.util.stream.Collectors; + +public class Cars { + + private static final String DELIMITER = ","; + private static final String DUPLICATED_DELIMITER_REGEX = ",+"; + private final List<Car> cars; + + private Cars(List<Car> cars) { + this.cars = Collections.unmodifiableList(cars); + } + + public static Cars createCarNameByWord(String input) { + List<Car> cars = new ArrayList<>(); + String[] words = divideWord(input); + validate(words); + + for (String carName : words) { + cars.add(new Car(carName)); + } + return new Cars(cars); + } + + private static void validate(String[] words) { + validateDuplicatedWord(words); + } + + private static void validateDuplicatedWord(String[] words) { + Set<String> uniqueWord = new HashSet<>(); + for (String word : words) { + if (!uniqueWord.add(word)) { + throw new CarsDuplicatedNameException(); + } + } + } + + private static String[] divideWord(String word) { + return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER); + } + + public void move(CarMoveNumberGenerator carMoveNumberGenerator) { + for (Car car : cars) { + car.move(carMoveNumberGenerator); + } + } + + public Winner findWinner() { + int maxScore = findMaxScore(); + return new Winner(cars.stream() + .filter(car -> (car.getPosition() == maxScore)) + .map(Car::getName) + .collect(Collectors.toList())); + } + + private int findMaxScore() { + return cars.stream() + .max(Comparator.comparingInt(Car::getPosition)) + .map(Car::getPosition) + .orElseThrow(CarsMaxScoreBlankException::new); + } + + public List<Car> getCars() { + return cars; + } +}
Java
`CarMoveNumberGenerator` ๋ฅผ `Car` ๊ฐ์ฒด๊นŒ์ง€ ๋„˜๊ฒจ์ฃผ์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ๊ทธ๋Ÿฌ๋ฉด `car.move()` ๋ฉ”์„œ๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๊ธฐ๋„ ํ›จ์”ฌ ๊ฐ„ํŽธํ•ด์งˆ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,22 @@ +package racingcar.domain; + +public class RacingCarGame { + + private final Cars cars; + + public RacingCarGame(Cars cars) { + this.cars = cars; + } + + public void move() { + cars.move(new CarRandomMoveNumberGenerator()); + } + + public Winner findWinner() { + return cars.findWinner(); + } + + public Cars getCars() { + return cars; + } +}
Java
`RacingCarGame` ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋“ค์ด ๋Œ€๋ถ€๋ถ„ `Cars` ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์šฉ๋„๋กœ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”! `RacingCarGame` ํด๋ž˜์Šค๊ฐ€ ์ •๋ง ํ•„์š”ํ•œ์ง€, ์•„๋‹ˆ๋ฉด ์–ด๋–ค ์‹์œผ๋กœ `RacingCarGame` ๋งŒ์˜ ์ƒˆ๋กœ์šด ์ฑ…์ž„์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ์„ ์ง€ ์ƒ๊ฐํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!!
@@ -0,0 +1,47 @@ +package racingcar.domain; + +import racingcar.exception.TryCommandNumberException; +import racingcar.exception.TryCommandRangeException; + +public class TryCommand { + + private static final int MIN_TRY = 1; + private static final int MAX_TRY = 100000; + private int tryCount; + + private TryCommand(int tryCount) { + this.tryCount = tryCount; + } + + public static TryCommand createTryCommandByString(String input) { + int number = convertInt(input); + validate(number); + return new TryCommand(number); + } + + private static void validate(int number) { + validateRange(number); + } + + private static void validateRange(int number) { + if(number < MIN_TRY || number > MAX_TRY) { + throw new TryCommandRangeException(MIN_TRY, MAX_TRY); + } + } + + private static int convertInt(String input) { + try{ + return Integer.parseInt(input); + }catch (NumberFormatException exception) { + throw new TryCommandNumberException(); + } + } + + public boolean tryMove() { + if(tryCount > 0) { + tryCount--; + return true; + } + return false; + } +}
Java
๊ฐ’ ๊ฐ์ฒด์˜ ๋‚ด๋ถ€ ์ƒํƒœ๋ฅผ `final` ๋กœ ์„ ์–ธํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋”๋ผ๊ตฌ์š”! ์ฐธ๊ณ ํ•ด๋ณด์‹œ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! - [Value Object, Reference Obejct](http://aeternum.egloos.com/v/1111257)
@@ -0,0 +1,71 @@ +package racingcar.domain; + +import racingcar.exception.CarsDuplicatedNameException; +import racingcar.exception.CarsMaxScoreBlankException; + +import java.util.*; +import java.util.stream.Collectors; + +public class Cars { + + private static final String DELIMITER = ","; + private static final String DUPLICATED_DELIMITER_REGEX = ",+"; + private final List<Car> cars; + + private Cars(List<Car> cars) { + this.cars = Collections.unmodifiableList(cars); + } + + public static Cars createCarNameByWord(String input) { + List<Car> cars = new ArrayList<>(); + String[] words = divideWord(input); + validate(words); + + for (String carName : words) { + cars.add(new Car(carName)); + } + return new Cars(cars); + } + + private static void validate(String[] words) { + validateDuplicatedWord(words); + } + + private static void validateDuplicatedWord(String[] words) { + Set<String> uniqueWord = new HashSet<>(); + for (String word : words) { + if (!uniqueWord.add(word)) { + throw new CarsDuplicatedNameException(); + } + } + } + + private static String[] divideWord(String word) { + return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER); + } + + public void move(CarMoveNumberGenerator carMoveNumberGenerator) { + for (Car car : cars) { + car.move(carMoveNumberGenerator); + } + } + + public Winner findWinner() { + int maxScore = findMaxScore(); + return new Winner(cars.stream() + .filter(car -> (car.getPosition() == maxScore)) + .map(Car::getName) + .collect(Collectors.toList())); + } + + private int findMaxScore() { + return cars.stream() + .max(Comparator.comparingInt(Car::getPosition)) + .map(Car::getPosition) + .orElseThrow(CarsMaxScoreBlankException::new); + } + + public List<Car> getCars() { + return cars; + } +}
Java
์ŠคํŠธ๋ฆผ์œผ๋กœ ๋กœ์ง์„ ๋ฐ”๊พธ์‹œ๋ฉด ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ์–ธํ•˜์ง€ ์•Š๊ณ  ๋” ๊น”๋”ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,10 @@ +package racingcar.exception; + +public class CarNameLengthException extends IllegalArgumentException{ + + private static final String EXCEPTION_MESSAGE_CAR_NAME_LENGTH = "[ERROR] ์ž๋™์ฐจ์ด๋ฆ„์€ 5๊ธ€์ž ์ดํ•˜์ž…๋‹ˆ๋‹ค"; + + public CarNameLengthException() { + super(EXCEPTION_MESSAGE_CAR_NAME_LENGTH); + } +}
Java
ํŠน๋ณ„ํžˆ ์ปค์Šคํ…€ ์˜ˆ์™ธ๋ฅผ ์‚ฌ์šฉํ•˜์‹  ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ์š”?? ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,42 @@ +package racingcar.domain; + +import racingcar.exception.CarNameLengthException; + +public class Car { + + private static final int CAR_NAME_LENGTH = 5; + private static final int MOVABLE_MIN_NUMBER = 4; + private final String name; + private int position = 0; + + public Car(String name) { + validate(name); + this.name = name; + } + + private void validate(String name) { + validateLength(name); + } + + private void validateLength(String name) { + if (name.length() > CAR_NAME_LENGTH) { + throw new CarNameLengthException(); + } + } + + public void move(CarMoveNumberGenerator carMoveNumberGenerator) { + final int number = carMoveNumberGenerator.generate(); + + if (number >= MOVABLE_MIN_NUMBER) { + position++; + } + } + + public String getName() { + return name; + } + + public int getPosition() { + return position; + } +}
Java
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋†“์น˜๊ณ ์žˆ๋˜ ๋ถ€๋ถ„์ด๋„ค์š”..
@@ -0,0 +1,71 @@ +package racingcar.domain; + +import racingcar.exception.CarsDuplicatedNameException; +import racingcar.exception.CarsMaxScoreBlankException; + +import java.util.*; +import java.util.stream.Collectors; + +public class Cars { + + private static final String DELIMITER = ","; + private static final String DUPLICATED_DELIMITER_REGEX = ",+"; + private final List<Car> cars; + + private Cars(List<Car> cars) { + this.cars = Collections.unmodifiableList(cars); + } + + public static Cars createCarNameByWord(String input) { + List<Car> cars = new ArrayList<>(); + String[] words = divideWord(input); + validate(words); + + for (String carName : words) { + cars.add(new Car(carName)); + } + return new Cars(cars); + } + + private static void validate(String[] words) { + validateDuplicatedWord(words); + } + + private static void validateDuplicatedWord(String[] words) { + Set<String> uniqueWord = new HashSet<>(); + for (String word : words) { + if (!uniqueWord.add(word)) { + throw new CarsDuplicatedNameException(); + } + } + } + + private static String[] divideWord(String word) { + return word.replaceAll(DUPLICATED_DELIMITER_REGEX, DELIMITER).split(DELIMITER); + } + + public void move(CarMoveNumberGenerator carMoveNumberGenerator) { + for (Car car : cars) { + car.move(carMoveNumberGenerator); + } + } + + public Winner findWinner() { + int maxScore = findMaxScore(); + return new Winner(cars.stream() + .filter(car -> (car.getPosition() == maxScore)) + .map(Car::getName) + .collect(Collectors.toList())); + } + + private int findMaxScore() { + return cars.stream() + .max(Comparator.comparingInt(Car::getPosition)) + .map(Car::getPosition) + .orElseThrow(CarsMaxScoreBlankException::new); + } + + public List<Car> getCars() { + return cars; + } +}
Java
์˜ค... ์ข‹์€๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐฉ์–ด์ ๋ณต์‚ฌ๋Š” ์ฒ˜์Œ ์ ‘ํ•ด๋ณด๋Š” ๊ฐœ๋…์ด๋„ค์š”!
@@ -0,0 +1,54 @@ +package racingcar.view; + +import racingcar.domain.Car; +import racingcar.domain.Cars; +import racingcar.domain.Winner; + +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class OutputView { + + private static final String MESSAGE_INPUT_CAR_NAME = "๊ฒฝ์ฃผํ•  ์ž๋™์ฐจ ์ด๋ฆ„์„ ์ž…๋ ฅํ•˜์„ธ์š”.(์ด๋ฆ„์€ ์‰ผํ‘œ(,) ๊ธฐ์ค€์œผ๋กœ ๊ตฌ๋ถ„)"; + private static final String MESSAGE_INPUT_TRY = "์‹œ๋„ํ•  ํšŒ์ˆ˜๋Š” ๋ช‡ํšŒ์ธ๊ฐ€์š”?"; + private static final String MESSAGE_RESULT = "์‹คํ–‰ ๊ฒฐ๊ณผ"; + private static final String MOVE_MARK = "-"; + private static final String JOIN_NAME_AND_POSITION = " : "; + private static final String WINNER_DELIMITER = ","; + private static final String MESSAGE_WINNER_PREFIX = "์ตœ์ข… ์šฐ์Šน์ž : "; + private static final String MESSAGE_WINNER_SUFFIX = ""; + + public void printInputCarName() { + System.out.println(MESSAGE_INPUT_CAR_NAME); + } + + public void printExceptionMessage(IllegalArgumentException exceptionMessage) { + System.out.println(exceptionMessage.getMessage()); + } + + public void printInputTry() { + System.out.println(MESSAGE_INPUT_TRY); + } + + public void printBlank() { + System.out.println(); + } + + public void printResult(Cars cars) { + System.out.println(MESSAGE_RESULT); + for(Car car : cars.getCars()){ + System.out.println(car.getName() + JOIN_NAME_AND_POSITION + convertMoveMark(car.getPosition())); + } + printBlank(); + } + + private String convertMoveMark(int position) { + return Stream.generate(() -> MOVE_MARK).limit(position).collect(Collectors.joining()); + } + + public void printWinner(Winner winner) { + String message = winner.getWinner().stream() + .collect(Collectors.joining(WINNER_DELIMITER, MESSAGE_WINNER_PREFIX, MESSAGE_WINNER_SUFFIX)); + System.out.println(message); + } +}
Java
์ด๋Ÿฐ,, ์ŠคํŠธ๋ฆผ์„ ๋„ˆ๋ฌด ๋‚จ๋ฐœํ–ˆ๋„ค์š”.. `repeat()` ๋ผ๋Š” ์ข‹์€ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๋ง์ด์ฃ  ใ… 
@@ -0,0 +1,22 @@ +package racingcar.domain; + +public class RacingCarGame { + + private final Cars cars; + + public RacingCarGame(Cars cars) { + this.cars = cars; + } + + public void move() { + cars.move(new CarRandomMoveNumberGenerator()); + } + + public Winner findWinner() { + return cars.findWinner(); + } + + public Cars getCars() { + return cars; + } +}
Java
์›๋ž˜ `RacingCarGame` ์—์„œ ์‹œ๋„ํšŸ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ์–ด์„œ `Cars` ๋ฅผ ์‹œ๋„ํšŸ์ˆ˜๋งŒํผ ์›€์ง์ด๋Š” ๋กœ์ง์ด์žˆ์—ˆ๋Š”๋ฐ controller ์ชฝ์œผ๋กœ ํ•ด๋‹น ๋กœ์ง์„ ์˜ฎ๊ธฐ๋Š”๋ฐ”๋žŒ์— ์“ธ๋ชจ์—†๋Š” ํด๋ž˜์Šค๊ฐ€ ๋˜์—ˆ๋„ค์š”.. ์ข‹์€ ์ง€์  ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค..!
@@ -0,0 +1,47 @@ +package racingcar.domain; + +import racingcar.exception.TryCommandNumberException; +import racingcar.exception.TryCommandRangeException; + +public class TryCommand { + + private static final int MIN_TRY = 1; + private static final int MAX_TRY = 100000; + private int tryCount; + + private TryCommand(int tryCount) { + this.tryCount = tryCount; + } + + public static TryCommand createTryCommandByString(String input) { + int number = convertInt(input); + validate(number); + return new TryCommand(number); + } + + private static void validate(int number) { + validateRange(number); + } + + private static void validateRange(int number) { + if(number < MIN_TRY || number > MAX_TRY) { + throw new TryCommandRangeException(MIN_TRY, MAX_TRY); + } + } + + private static int convertInt(String input) { + try{ + return Integer.parseInt(input); + }catch (NumberFormatException exception) { + throw new TryCommandNumberException(); + } + } + + public boolean tryMove() { + if(tryCount > 0) { + tryCount--; + return true; + } + return false; + } +}
Java
๋ถˆ๋ณ€์œผ๋กœ ํ•ด๋„ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๊ตฐ์š”.. DDD ์—์„œ ๋‚˜์˜ค๋Š” ๊ฐœ๋…์ด๋ผ MVC ์—๋Š” ์–ด๋–ป๊ฒŒ ์ ์šฉํ• ์ง€๋Š” ๊ณ ๋ฏผ์„ ํ•ด๋ด์•ผ๊ฒ ๋„ค์š”.. ์ข‹์€๊ธ€ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,10 @@ +package racingcar.exception; + +public class CarNameLengthException extends IllegalArgumentException{ + + private static final String EXCEPTION_MESSAGE_CAR_NAME_LENGTH = "[ERROR] ์ž๋™์ฐจ์ด๋ฆ„์€ 5๊ธ€์ž ์ดํ•˜์ž…๋‹ˆ๋‹ค"; + + public CarNameLengthException() { + super(EXCEPTION_MESSAGE_CAR_NAME_LENGTH); + } +}
Java
[์ปค์Šคํ…€์˜ˆ์™ธ](https://tecoble.techcourse.co.kr/post/2020-08-17-custom-exception/) ์ปค์Šคํ…€ ์˜ˆ์™ธ๋“ค์€ ์žฅ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค! ๊ทธ๋ž˜์„œ ์ •๋‹ต์€ ์—†์ง€๋งŒ ์ œ๊ฐ€ ์‚ฌ์šฉํ•œ ์ด์œ ๋Š” ์ฒซ์งธ๋กœํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์–ด๋–ค ์˜ˆ์™ธ์ธ์ง€ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ๊ณ  ๋‘˜์งธ๋กœ ๋ณดํ†ต ๋„๋ฉ”์ธ ์ฝ”๋“œ๋ฅผ ๋ณผ๋•Œ ์ค‘์š”ํ•œ๊ฒƒ์€ ๊ทธ ๋„๋ฉ”์ธ ์—ญํ• ๊ณผ ๊ด€๋ จ๋œ ๋กœ์ง์ด์ง€, ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ•˜๋Š”์ง€๊ฐ€ ์ค‘์š”ํ•œ๊ฒŒ ์•„๋‹ˆ๊ธฐ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๋ฅผ ๋”ฐ๋กœ๋นผ๋‚ด์–ด ์ข€ ๋” ๋ณด๊ธฐ ํŽธํ•œ๊ฒŒ ๋งŒ๋“ค๊ธฐ ์œ„ํ•จ์ž…๋‹ˆ๋‹ค! ์…‹์งธ๋กœ ๋Œ€๋ถ€๋ถ„ Spring MVC ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ์ปค์Šคํ…€ ์˜ˆ์™ธ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์— ์—ฐ์Šต์ฐจ์›์—์„œ ์ปค์Šคํ…€ ์˜ˆ์™ธ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,32 @@ +package baseball.util + +// Message +const val PRINT_NOTHING_MESSAGE = "๋‚ซ์‹ฑ" +const val PRINT_STRIKE_MESSAGE = "%d์ŠคํŠธ๋ผ์ดํฌ" +const val PRINT_BALL_MESSAGE = "%d๋ณผ" +const val PRINT_STRIKE_BALL_MESSAGE = "%d๋ณผ %d์ŠคํŠธ๋ผ์ดํฌ" +const val START_GAME_MASSAGE = "์ˆซ์ž ์•ผ๊ตฌ ๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค." +const val QUIT_GAME_MASSAGE = "3๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ชจ๋‘ ๋งžํžˆ์…จ์Šต๋‹ˆ๋‹ค! ๊ฒŒ์ž„ ์ข…๋ฃŒ" +const val SELECT_COMMAND_MESSAGE = "๊ฒŒ์ž„์„ ์ƒˆ๋กœ ์‹œ์ž‘ํ•˜๋ ค๋ฉด 1, ์ข…๋ฃŒํ•˜๋ ค๋ฉด 2๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”." +const val USER_INPUT_NUMBER_MESSAGE = "์ˆซ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”: " +// Error +const val ERROR_INVALID_INPUT_MESSAGE = "์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค" +const val ERROR_INVALID_COMMAND_MESSAGE = "1๊ณผ 2๋งŒ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”." + +// Command +const val RETRY_COMMAND = "1" +const val QUIT_COMMAND = "2" + +// Code +const val END_CODE = 2 +const val PLAYING_CODE = 1 + +// Size, Number +const val MAX_SIZE = 3 +const val MAX_NUMBER = 9 +const val MIN_NUMBER = 1 + +// String +const val EMPTY_STRING = "" + +
Kotlin
util ํด๋”๋ฅผ ์‚ฌ์šฉํ•ด EMPTY_STRING๊ณผ ๊ฐ™์ด ์‚ฌ์†Œํ•œ ๊ฐ’๋“ค๊นŒ์ง€ ์ƒ์ˆ˜ํ™”๋ฅผ ํ•˜์‹  ๋•๋ถ„์— ๊ฐ€๋…์„ฑ์ด ์ข‹๋„ค์š”. ๋ฐฐ์›Œ๊ฐ‘๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +package baseball.game + +import baseball.game.service.Game +import baseball.model.AnswerBoard +import baseball.model.Computer +import baseball.util.* +import baseball.view.User +import baseball.view.validator.InputValidator + +class Baseball( + private val user: User, + private val computer: Computer, + private val answerBoard: AnswerBoard, +) : Game { + private var gameState = PLAYING_CODE + private var answer = computer.createAnswer() + + override fun play() { + println(START_GAME_MASSAGE) + process() + } + + // ๊ฒŒ์ž„ ์ง„ํ–‰ + override fun process() { + do { + val userNumber = user.createNumber() + + answerBoard.createResult(answer, userNumber) + println(answerBoard.printResult()) + + finish() + } while (isPlaying()) + } + + private fun isPlaying(): Boolean = gameState != END_CODE + + override fun quit() { + gameState = END_CODE + } + + override fun retry() { + answer = computer.createAnswer() + answerBoard.clearState() + } + + private fun printFinishMessage() = println(QUIT_GAME_MASSAGE + "\n" + SELECT_COMMAND_MESSAGE) + + private fun finish() { + if (answerBoard.isThreeStrike()) { + printFinishMessage() + when (InputValidator.validateUserCommand(readLine()!!)) { + RETRY_COMMAND -> retry() + QUIT_COMMAND -> quit() + } + } + } + +} +
Kotlin
MVC ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. Baseball Class๋Š” Controller์˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, Controller์—์„œ ์ง์ ‘ ์ž…๋ ฅ์„ ๋ฐ›๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ์‚ฌ์šฉ์ž์™€ ์ง์ ‘์ ์œผ๋กœ ๋งž๋‹ฟ๋Š” View์—์„œ ์ž…๋ ฅ์„ ๋ฐ›์•„์™€ Controller์—์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,12 @@ +package baseball.view + +import baseball.util.USER_INPUT_NUMBER_MESSAGE +import baseball.view.validator.InputValidator + +class User { + fun createNumber(): String { + print(USER_INPUT_NUMBER_MESSAGE) + val userNumber = readLine()!! + return InputValidator.validateUserNumber(userNumber) + } +} \ No newline at end of file
Kotlin
์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›์•„์™€ ๊ฒ€์‚ฌ๋œ ๊ฐ’์„ ๋Œ๋ ค๋ณด๋‚ด์ฃผ๋Š” ํ•จ์ˆ˜์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. create๋ผ๋Š” ํ•จ์ˆ˜๋ช…์„ ๋ณด์•˜์„ ๋•Œ ์ €๋Š” "์‚ฌ์šฉ์ž๊ฐ€ ๋ฒˆํ˜ธ๋ฅผ ๋งŒ๋“œ๋‚˜? ์‚ฌ์šฉ์ž๊ฐ€ ๊ธฐ๊ณ„์˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ธ๊ฐ€?"๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. input๊ณผ ์–ด์šธ๋ฆฌ๋Š” ํ•จ์ˆ˜๋ช…์ด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ, ์ €๋„ ์ด ๋ถ€๋ถ„์€ ์กฐ๊ธˆ ๊ณ ๋ฏผ๋˜๋Š” ๋ถ€๋ถ„์ด์ง€๋งŒ, MVC ํŒจํ„ด์˜ View์—์„œ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ ธ์™€ ๋ถ„๊ธฐ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๋กœ์ง์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํŒจํ„ด์˜ ๊ทœ์น™์— ๋งž์ง€ ์•Š๋Š”๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
@@ -0,0 +1,17 @@ +package baseball.view.validator + +import baseball.util.* + +object InputValidator { + + fun validateUserNumber(userNumber: String): String { + require(userNumber.length == MAX_SIZE) { ERROR_INVALID_INPUT_MESSAGE } + return userNumber + } + + fun validateUserCommand(command: String): String { + require(command == RETRY_COMMAND || command == QUIT_COMMAND) { ERROR_INVALID_COMMAND_MESSAGE } + return command + } +} +
Kotlin
๋ฏผ์žฌ๋‹˜๊ป˜์„œ ์ €์—๊ฒŒ require์— ๋Œ€ํ•ด ์•Œ๋ ค์ฃผ์…จ์ฃ ..! ์ž˜ ์“ฐ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค! ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,59 @@ +package baseball.model + +import baseball.util.* + + +class AnswerBoard { + // ์ƒํƒœ๋ฅผ ๋ชจ๋‘ ์•„์›ƒ์œผ๋กœ ์ดˆ๊ธฐํ™” + private val stateList = MutableList(MAX_SIZE) { BaseballState.OUT } + + + private fun createCount(): Triple<Int, Int, Int> { + var strikeCount = 0 + var ballCount = 0 + var outCount = 0 + + stateList.forEach { state -> + when (state) { + BaseballState.STRIKE -> strikeCount++ + BaseballState.BALL -> ballCount++ + BaseballState.OUT -> outCount++ + } + } + return Triple(strikeCount, ballCount, outCount) + } + + fun createResult(answer: String, userNumber: String) { + answer.forEachIndexed { answerIndex, answerNumber -> + if (answerNumber == userNumber[answerIndex]) { + stateList[answerIndex] = BaseballState.STRIKE + } + if (answerNumber != userNumber[answerIndex] && answer.contains(userNumber[answerIndex])) { + stateList[answerIndex] = BaseballState.BALL + } + if (answerNumber != userNumber[answerIndex] && !answer.contains(userNumber[answerIndex])) { + stateList[answerIndex] = BaseballState.OUT + } + } + } + + fun printResult(): String { + val (strikeCount, ballCount, outCount) = createCount() + var message = EMPTY_STRING + + // ์•„์›ƒ์ผ ๋•Œ ์ถœ๋ ฅ + if (outCount == stateList.size) message = PRINT_NOTHING_MESSAGE + // ์ŠคํŠธ๋ผ์ดํฌ์ผ๋•Œ ์ถœ๋ ฅ + if (ballCount == 0 && strikeCount != 0) message = PRINT_STRIKE_MESSAGE.format(strikeCount) + // ๋ณผ์ผ๋•Œ ์ถœ๋ ฅ + if (ballCount != 0 && strikeCount == 0) message = PRINT_BALL_MESSAGE.format(ballCount) + // ์ŠคํŠธ๋ผ์ดํฌ ๋ณผ์ผ ๋•Œ + if (ballCount != 0 && strikeCount != 0) message = PRINT_STRIKE_BALL_MESSAGE.format(ballCount, strikeCount) + + return message + } + + fun isThreeStrike(): Boolean = stateList.all { state -> state == BaseballState.STRIKE } + + fun clearState() = stateList.replaceAll { BaseballState.OUT } +}
Kotlin
createCount๋ผ๋Š” ํ•จ์ˆ˜๋ช…์€ "๊ฐœ์ˆ˜๋ฅผ **_๋งŒ๋“ ๋‹ค_**"๋ผ๋Š” ์˜๋ฏธ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ํ•จ์ˆ˜์˜ ์—ญํ• ์€ "์ŠคํŠธ๋ผ์ดํฌ, ๋ณผ, ์•„์›ƒ์˜ ๊ฐœ์ˆ˜๋ฅผ **_์„ผ๋‹ค_**"์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜๋ช…๊ณผ ์„ธ๊ฐ€์ง€์˜ ๊ธฐ๋Šฅ์„ ๋‚˜๋ˆ„๋Š” ๊ฒƒ์€ ์–ด๋–จ๊นŒ์š”? countStrike, countBall, countOut๊ณผ ๊ฐ™์ด์š”!
@@ -0,0 +1,59 @@ +package baseball.model + +import baseball.util.* + + +class AnswerBoard { + // ์ƒํƒœ๋ฅผ ๋ชจ๋‘ ์•„์›ƒ์œผ๋กœ ์ดˆ๊ธฐํ™” + private val stateList = MutableList(MAX_SIZE) { BaseballState.OUT } + + + private fun createCount(): Triple<Int, Int, Int> { + var strikeCount = 0 + var ballCount = 0 + var outCount = 0 + + stateList.forEach { state -> + when (state) { + BaseballState.STRIKE -> strikeCount++ + BaseballState.BALL -> ballCount++ + BaseballState.OUT -> outCount++ + } + } + return Triple(strikeCount, ballCount, outCount) + } + + fun createResult(answer: String, userNumber: String) { + answer.forEachIndexed { answerIndex, answerNumber -> + if (answerNumber == userNumber[answerIndex]) { + stateList[answerIndex] = BaseballState.STRIKE + } + if (answerNumber != userNumber[answerIndex] && answer.contains(userNumber[answerIndex])) { + stateList[answerIndex] = BaseballState.BALL + } + if (answerNumber != userNumber[answerIndex] && !answer.contains(userNumber[answerIndex])) { + stateList[answerIndex] = BaseballState.OUT + } + } + } + + fun printResult(): String { + val (strikeCount, ballCount, outCount) = createCount() + var message = EMPTY_STRING + + // ์•„์›ƒ์ผ ๋•Œ ์ถœ๋ ฅ + if (outCount == stateList.size) message = PRINT_NOTHING_MESSAGE + // ์ŠคํŠธ๋ผ์ดํฌ์ผ๋•Œ ์ถœ๋ ฅ + if (ballCount == 0 && strikeCount != 0) message = PRINT_STRIKE_MESSAGE.format(strikeCount) + // ๋ณผ์ผ๋•Œ ์ถœ๋ ฅ + if (ballCount != 0 && strikeCount == 0) message = PRINT_BALL_MESSAGE.format(ballCount) + // ์ŠคํŠธ๋ผ์ดํฌ ๋ณผ์ผ ๋•Œ + if (ballCount != 0 && strikeCount != 0) message = PRINT_STRIKE_BALL_MESSAGE.format(ballCount, strikeCount) + + return message + } + + fun isThreeStrike(): Boolean = stateList.all { state -> state == BaseballState.STRIKE } + + fun clearState() = stateList.replaceAll { BaseballState.OUT } +}
Kotlin
๋ณ€์ˆ˜๋ช…์ด ๋™์‚ฌ + ๋ช…์‚ฌ๋กœ ํ•œ๋‹ค๋ฉด ํ•จ์ˆ˜๋ช…๊ณผ ๋ถˆ์ผ์น˜ ํ•˜์ง€ ์•Š์„๊นŒ์š” ?!? ์ด๋ฆ„ ์ง“๊ธฐ์—์„œ ์ค‘์š”ํ•œ ๋ช…์‚ฌ๋Š” ์•ž์— ์“ฐ๋Š” ๊ฒƒ์ด ์ข‹๋‹ซ๊ณ  ํ•ฉ๋‹ˆ๋‹ค !! ex) countStrike -> strikeCount
@@ -0,0 +1,12 @@ +package baseball.view + +import baseball.util.USER_INPUT_NUMBER_MESSAGE +import baseball.view.validator.InputValidator + +class User { + fun createNumber(): String { + print(USER_INPUT_NUMBER_MESSAGE) + val userNumber = readLine()!! + return InputValidator.validateUserNumber(userNumber) + } +} \ No newline at end of file
Kotlin
์ด ๋ถ€๋ถ„๋„ ์ง€๊ธˆ๊นŒ์ง€๋„ ๊ณ ๋ฏผ์ด์˜€๋Š”๋ฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋”ฐ๋กœ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค !!
@@ -1,37 +1,20 @@ import java.util.List; +import java.util.Set; -import domain.LottoGame; -import domain.LottoResults; -import domain.WinningAnalyzer; -import domain.WinningStatistics; +import domain.ManualRequest; import view.LottoInputView; -import view.LottoOutputView; public class LottoController { - private LottoGame lottoGame; - private WinningAnalyzer winningAnalyzer; - public void playLottoGames(int money) { - lottoGame = new LottoGame(); - lottoGame.generateLottoResultsFromMoney(money); - } - - public void getLottoResults() { - List<List<Integer>> results = lottoGame.getLottoResults().lottoNumbersToInt(); - int gameCount = lottoGame.getCount(); - LottoOutputView.printLottoResults(gameCount, results); - } - - public void getWinningStatistics() { - LottoResults lottoResults = lottoGame.getLottoResults(); - winningAnalyzer = new WinningAnalyzer(lottoResults, LottoInputView.getWinningNumbers(), LottoInputView.getBonusNumber()); - WinningStatistics winningStatistics = winningAnalyzer.calculateWinningStatistics(); - LottoOutputView.printBeforeWinnings(); - LottoOutputView.printWinnings(winningStatistics); - } - - public void getReturnOnInvestment() { - int money = lottoGame.getMoney(); - LottoOutputView.printReturnOnInvestment(winningAnalyzer.getReturnOnInvestment(money)); + public static void main(String[] args) { + int money = LottoInputView.getMoney(); + int manualCount = LottoInputView.getManualCount(); + List<Set<Integer>> manualNumbers = LottoInputView.getManualLottos(manualCount); + LottoService lottoService = new LottoService(); + ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers); + lottoService.playLottoGames(manualRequest, money); + lottoService.lottoResults(); + lottoService.winningStatistics(); + lottoService.returnOnInvestment(); } }
Java
์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์จ๋ณด๋ฉด ์–ด๋–จ๊นŒ์š”?
@@ -1,13 +0,0 @@ -import view.LottoInputView; - -public class LottoMain { - - public static void main(String[] args) { - int money = LottoInputView.getMoney(); - LottoController lottoController = new LottoController(); - lottoController.playLottoGames(money); - lottoController.getLottoResults(); - lottoController.getWinningStatistics(); - lottoController.getReturnOnInvestment(); - } -}
Java
- ๊ฐ’ ๋Œ€์ž…, ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๊ณผ ๊ฐ™์ด ๊ฐœ๋…์  ์œ ์‚ฌ์„ฑ์ด ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค๊ฐ„์—๋Š” ๊ฐœํ–‰์œผ๋กœ ๊ฐ€๋…์„ฑ์„ ๋†’ํ˜€์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. - ์ปจํŠธ๋กค๋Ÿฌ์—๊ฒŒ ์„ธ๋ถ€ ๊ธฐ๋Šฅ๋“ค์„ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฑด ๋น„์ฆˆ๋‹ˆ์Šค๋กœ์ง ์˜ค์ผ€์ŠคํŠธ๋ ˆ์ด์…˜์œผ๋กœ ๋ณด์—ฌ์š”. ๊ทธ๋ƒฅ ์ ์ ˆํ•œ Request ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์ตœ์ข… ๊ฒฐ๊ณผ๋งŒ ๋ฐ˜ํ™˜๋ฐ›์•„์„œ ์ถœ๋ ฅ๊ณ„์ธต์—๊ฒŒ ์ „๋‹ฌํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์•„์š”. - ํ˜„์žฌ ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด LottoMain์ด ์ปจํŠธ๋กค๋Ÿฌ์˜ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ๊ณ , LottoController๊ฐ€ Service Layer์˜ ์—ญํ• ์„ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ์ฐจ๋ผ๋ฆฌ Controller์—์„œ View Layer๋ฅผ ๋ถ„๋ฆฌํ•ด LottoMain์œผ๋กœ ์˜ฎ๊ธฐ์‹œ๊ณ  LottoController์˜ ์ด๋ฆ„์„ LottoService๋กœ ๋ฐ”๊พธ๋Š”๊ฑด ์–ด๋–จ๊นŒ์š”
@@ -0,0 +1,15 @@ +package domain; + +public class GameCount { + private final int manualCount; + private final int automaticCount; + + public GameCount(int totalCount, int manualCount) { + this.manualCount = manualCount; + this.automaticCount = totalCount - manualCount; + } + + public int getAutomaticCount() { + return automaticCount; + } +}
Java
๋‚ด๋ถ€์ ์œผ๋กœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด final ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ถˆ๋ณ€์„ฑ ํ™•๋ณด๋ฅผ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”.
@@ -1,23 +1,47 @@ package domain; +import java.util.List; +import java.util.Set; + public class LottoGame { private LottoResults lottoResults; - private Money money; + private WinningAnalyzer winningAnalyzer; public LottoGame() { this.lottoResults = new LottoResults(); } - public void generateLottoResultsFromMoney(int money) { - this.money = new Money(money); - for (int i = 0; i < getCount(); i++) { - lottoResults.add(LottoNumGenerator.generateResult()); + public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) { + int manualCount = manualRequest.getManualCount(); + List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers(); + this.money = new Money(money, manualCount); + if (manualCount != manualNumbers.size()) { + throw new IllegalArgumentException("์ˆ˜๋™ ๊ตฌ๋งค๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + // ์ˆ˜๋™ ๊ตฌ๋งค + generateManualResults(manualCount, manualNumbers); + generateAutomaticResults(); + } + + private void generateAutomaticResults() { + for (int i = 0; i < getAutomaticCount(); i++) { + lottoResults.add(LottoNumGenerator.generateAutomaticResults()); + } + } + + private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) { + for (int i = 0; i < manualCount; i++) { + lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i))); } } public int getCount() { - return money.getCount(); + return money.getTotalCount(); + } + + public int getAutomaticCount() { + return money.getAutomaticCount(); } public int getMoney() { @@ -27,5 +51,19 @@ public int getMoney() { public LottoResults getLottoResults() { return lottoResults; } + + public List<List<Integer>> getLottoResultsToInt() { + return lottoResults.lottoResultsToInt(); + } + + public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) { + winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber); + return winningAnalyzer.calculateWinningStatistics(); + } + + public float getReturnOnInvestment() { + return winningAnalyzer.getReturnOnInvestment(money.getMoney()); + } + }
Java
์ด๋Ÿฐ ์ฐธ์กฐํƒ€์ž…๋„ ์ผ๊ธ‰ ์ปฌ๋ ‰์…˜์„ ์ด์šฉํ•ด์„œ ๋‚ด๋ถ€ ์ธ๋ฑ์Šค ์ œ์–ด๋ฅผ ์ฑ…์ž„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -1,23 +1,47 @@ package domain; +import java.util.List; +import java.util.Set; + public class LottoGame { private LottoResults lottoResults; - private Money money; + private WinningAnalyzer winningAnalyzer; public LottoGame() { this.lottoResults = new LottoResults(); } - public void generateLottoResultsFromMoney(int money) { - this.money = new Money(money); - for (int i = 0; i < getCount(); i++) { - lottoResults.add(LottoNumGenerator.generateResult()); + public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) { + int manualCount = manualRequest.getManualCount(); + List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers(); + this.money = new Money(money, manualCount); + if (manualCount != manualNumbers.size()) { + throw new IllegalArgumentException("์ˆ˜๋™ ๊ตฌ๋งค๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + // ์ˆ˜๋™ ๊ตฌ๋งค + generateManualResults(manualCount, manualNumbers); + generateAutomaticResults(); + } + + private void generateAutomaticResults() { + for (int i = 0; i < getAutomaticCount(); i++) { + lottoResults.add(LottoNumGenerator.generateAutomaticResults()); + } + } + + private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) { + for (int i = 0; i < manualCount; i++) { + lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i))); } } public int getCount() { - return money.getCount(); + return money.getTotalCount(); + } + + public int getAutomaticCount() { + return money.getAutomaticCount(); } public int getMoney() { @@ -27,5 +51,19 @@ public int getMoney() { public LottoResults getLottoResults() { return lottoResults; } + + public List<List<Integer>> getLottoResultsToInt() { + return lottoResults.lottoResultsToInt(); + } + + public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) { + winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber); + return winningAnalyzer.calculateWinningStatistics(); + } + + public float getReturnOnInvestment() { + return winningAnalyzer.getReturnOnInvestment(money.getMoney()); + } + }
Java
Money๊ฐ์ฒด์ธ๋ฐ count์ •๋ณด๊นŒ์ง€ ๋‹ด์•„๋ฒ„๋ฆฌ๋ฉด ์ผ๊ธ‰ ๊ฐ์ฒด๋กœ์จ์˜ ํšจ์šฉ์ด ๋–จ์–ด์ง€๊ณ  ๊ฐ์ฒด ์‹œ๊ทธ๋‹ˆ์ฒ˜๊ฐ€ ๋ถˆ๋ถ„๋ช…ํ•ด์งˆ ๊ฒƒ ๊ฐ™๋„ค์š” ๐Ÿค”
@@ -1,23 +1,47 @@ package domain; +import java.util.List; +import java.util.Set; + public class LottoGame { private LottoResults lottoResults; - private Money money; + private WinningAnalyzer winningAnalyzer; public LottoGame() { this.lottoResults = new LottoResults(); } - public void generateLottoResultsFromMoney(int money) { - this.money = new Money(money); - for (int i = 0; i < getCount(); i++) { - lottoResults.add(LottoNumGenerator.generateResult()); + public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) { + int manualCount = manualRequest.getManualCount(); + List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers(); + this.money = new Money(money, manualCount); + if (manualCount != manualNumbers.size()) { + throw new IllegalArgumentException("์ˆ˜๋™ ๊ตฌ๋งค๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + // ์ˆ˜๋™ ๊ตฌ๋งค + generateManualResults(manualCount, manualNumbers); + generateAutomaticResults(); + } + + private void generateAutomaticResults() { + for (int i = 0; i < getAutomaticCount(); i++) { + lottoResults.add(LottoNumGenerator.generateAutomaticResults()); + } + } + + private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) { + for (int i = 0; i < manualCount; i++) { + lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i))); } } public int getCount() { - return money.getCount(); + return money.getTotalCount(); + } + + public int getAutomaticCount() { + return money.getAutomaticCount(); } public int getMoney() { @@ -27,5 +51,19 @@ public int getMoney() { public LottoResults getLottoResults() { return lottoResults; } + + public List<List<Integer>> getLottoResultsToInt() { + return lottoResults.lottoResultsToInt(); + } + + public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) { + winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber); + return winningAnalyzer.calculateWinningStatistics(); + } + + public float getReturnOnInvestment() { + return winningAnalyzer.getReturnOnInvestment(money.getMoney()); + } + }
Java
์ž๋™,์ˆ˜๋™ ๋กœ๋˜๋ฉด ๊ทธ๋ƒฅ LottoNumber, Lotto, Lottos ์ •๋„๋กœ ๋„ค์ด๋ฐ์„ ์ง€์–ด๋„ ๋  ๊ฒƒ ๊ฐ™๋„ค์š” Result๋ผ๊ณ ํ•˜๋‹ˆ ๋ญ”๊ฐ€ ๋น„๊ต๊นŒ์ง€ ๋‹ค ๋๋‚œ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฑธ๋กœ ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -1,23 +1,47 @@ package domain; +import java.util.List; +import java.util.Set; + public class LottoGame { private LottoResults lottoResults; - private Money money; + private WinningAnalyzer winningAnalyzer; public LottoGame() { this.lottoResults = new LottoResults(); } - public void generateLottoResultsFromMoney(int money) { - this.money = new Money(money); - for (int i = 0; i < getCount(); i++) { - lottoResults.add(LottoNumGenerator.generateResult()); + public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) { + int manualCount = manualRequest.getManualCount(); + List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers(); + this.money = new Money(money, manualCount); + if (manualCount != manualNumbers.size()) { + throw new IllegalArgumentException("์ˆ˜๋™ ๊ตฌ๋งค๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + // ์ˆ˜๋™ ๊ตฌ๋งค + generateManualResults(manualCount, manualNumbers); + generateAutomaticResults(); + } + + private void generateAutomaticResults() { + for (int i = 0; i < getAutomaticCount(); i++) { + lottoResults.add(LottoNumGenerator.generateAutomaticResults()); + } + } + + private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) { + for (int i = 0; i < manualCount; i++) { + lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i))); } } public int getCount() { - return money.getCount(); + return money.getTotalCount(); + } + + public int getAutomaticCount() { + return money.getAutomaticCount(); } public int getMoney() { @@ -27,5 +51,19 @@ public int getMoney() { public LottoResults getLottoResults() { return lottoResults; } + + public List<List<Integer>> getLottoResultsToInt() { + return lottoResults.lottoResultsToInt(); + } + + public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) { + winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber); + return winningAnalyzer.calculateWinningStatistics(); + } + + public float getReturnOnInvestment() { + return winningAnalyzer.getReturnOnInvestment(money.getMoney()); + } + }
Java
์ž๋™์ƒ์„ฑ๊ณผ ์ˆ˜๋™์ƒ์„ฑ์„ ๋ฉ”์„œ๋“œ๋กœ ๊ตฌ๋ถ„ํ•˜๊ณ  ์žˆ๋Š”๋ฐ LottoGenerator -> LottoAutoGenerator, LottoManualGenerator ๋“ฑ์œผ๋กœ ์ „๋žตํŒจํ„ด์„ ์‚ฌ์šฉํ• ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -1,23 +1,47 @@ package domain; +import java.util.List; +import java.util.Set; + public class LottoGame { private LottoResults lottoResults; - private Money money; + private WinningAnalyzer winningAnalyzer; public LottoGame() { this.lottoResults = new LottoResults(); } - public void generateLottoResultsFromMoney(int money) { - this.money = new Money(money); - for (int i = 0; i < getCount(); i++) { - lottoResults.add(LottoNumGenerator.generateResult()); + public void generateLottoResultsFromMoney(ManualRequest manualRequest, int money) { + int manualCount = manualRequest.getManualCount(); + List<Set<Integer>> manualNumbers = manualRequest.getManualNumbers(); + this.money = new Money(money, manualCount); + if (manualCount != manualNumbers.size()) { + throw new IllegalArgumentException("์ˆ˜๋™ ๊ตฌ๋งค๊ฐ€ ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); + } + // ์ˆ˜๋™ ๊ตฌ๋งค + generateManualResults(manualCount, manualNumbers); + generateAutomaticResults(); + } + + private void generateAutomaticResults() { + for (int i = 0; i < getAutomaticCount(); i++) { + lottoResults.add(LottoNumGenerator.generateAutomaticResults()); + } + } + + private void generateManualResults(int manualCount, List<Set<Integer>> manualNumbers) { + for (int i = 0; i < manualCount; i++) { + lottoResults.add(LottoNumGenerator.generateManualResults(manualNumbers.get(i))); } } public int getCount() { - return money.getCount(); + return money.getTotalCount(); + } + + public int getAutomaticCount() { + return money.getAutomaticCount(); } public int getMoney() { @@ -27,5 +51,19 @@ public int getMoney() { public LottoResults getLottoResults() { return lottoResults; } + + public List<List<Integer>> getLottoResultsToInt() { + return lottoResults.lottoResultsToInt(); + } + + public WinningStatistics calculateWinningStatistics(Set<Integer> winningNumbers, int bonusNumber) { + winningAnalyzer = new WinningAnalyzer(lottoResults, winningNumbers, bonusNumber); + return winningAnalyzer.calculateWinningStatistics(); + } + + public float getReturnOnInvestment() { + return winningAnalyzer.getReturnOnInvestment(money.getMoney()); + } + }
Java
์ด ๋ฉ”์„œ๋“œ๋„ ProfitCalculator -> DefaultProfiltCalculator๋“ฑ์œผ๋กœ ๋ถ„๋ฆฌํ•ด์„œ ์ด์œจ ๊ณ„์‚ฐ๋„ ์ถ”์ƒํ™”ํ•˜๋ฉด ์œ ์ง€๋ณด์ˆ˜์„ฑ์ด ๋†’์•„์งˆ ๊ฒƒ ๊ฐ™์•„์š”
@@ -1,7 +1,9 @@ package domain; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Set; public class LottoResult { private List<LottoNumber> lottoResult; @@ -14,10 +16,11 @@ public LottoResult(List<LottoNumber> lottoNumbers) { this.lottoResult = lottoNumbers; } - public static LottoResult fromIntegers(List<Integer> lottoIntegers) { + public static LottoResult fromIntegers(Set<Integer> lottoIntegers) { LottoResult lottoResult = new LottoResult(); - for (int i = 0; i < lottoIntegers.size(); i++) { - lottoResult.lottoResult.add(LottoNumber.from(lottoIntegers.get(i))); + Iterator<Integer> iterator = lottoIntegers.iterator(); + while (iterator.hasNext()) { + lottoResult.lottoResult.add(LottoNumber.from(iterator.next())); } return lottoResult; } @@ -38,4 +41,14 @@ public List<Integer> getLottoResult() { } return lottoNumbers; } + + public int calculateCount(WinningNumbers winningNumbers) { + int count = 0; + return lottoResult.stream().mapToInt(num -> num.addCountIfContain(count, winningNumbers)).sum(); + } + + public boolean isBonusMatch(BonusNumber bonusNumber) { + return lottoResult.stream() + .anyMatch(lottoNumber -> bonusNumber.isBonusMatch(lottoNumber)); + } }
Java
๋žŒ๋‹ค์‹์—์„œ count๋ผ๋Š” ์™ธ๋ถ€ ๊ฐ’์— ์˜ํ–ฅ์„ ์ฃผ๊ณ  ์žˆ์–ด์š”. ๐Ÿค” ๋žŒ๋‹ค์‹์€ ์™ธ๋ถ€๊ฐ’์— ๋Œ€ํ•œ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์•„์•ผ ํ•˜๊ธฐ์— ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋กœ ์„ค๊ณ„๋˜์•ผํ•ฉ๋‹ˆ๋‹ค. ๋กœ์ง๋‚ด์—์„œ ์ง์ ‘ count๊ฐ™์€ ์ง€์—ญ๋ณ€์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ ํ•˜๋ฉด effective final variable์„ ์ˆ˜์ •ํ•˜๋ คํ•œ๋‹ค๊ณ  ์ปดํŒŒ์ผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ์ธ๋ฐ, ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•ด์„œ ๊ทธ ๋ฌธ์ œ๋ฅผ ํ”ผํ–ˆ์ง€๋งŒ, ๊ทธ๋ ‡๋‹ค๊ณ  ๋ฌธ์ œ๊ฐ€ ๋˜์ง€ ์•Š๋Š”๊ฑด ์•„๋‹™๋‹ˆ๋‹ค. filter์™€ count๋“ฑ์„ ์ด์šฉํ•ด์„œ๊ตฌํ˜„ํ• ์ˆ˜๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™๋„ค์š”
@@ -1,68 +1,75 @@ package domain; +import static domain.WinningStatistics.THRESHOLD; + import java.util.HashMap; import java.util.Map; +import java.util.function.Function; public enum WinningPrizes { - MISS(0, 0) { - - }, - FIFTH_PRIZE(5, 5_000), - FOURTH_PRIZE(4, 50_000), - THIRD_PRIZE(3, 1_500_000), - SECOND_PRIZE(2, 3_000_000), - FIRST_PRIZE(1, 2_000_000_000); - - public static final int OFFSET = 3; + ZERO(0, false, 0, 0, count -> THRESHOLD), + ONE(1, false,0, 0, count -> THRESHOLD), + TWO(2, false, 5, 5_000, count -> THRESHOLD), + THREE(3, false, 5, 5_000, count -> count), + FOUR(4, false, 4, 50_000, count -> count), + FIVE(5, false, 3, 1_500_000, count -> count), + FIVE_BONUS(5, true, 2, 3_000_000, count -> count), + SIX(6, false, 1, 2_000_000_000, count -> count); - private static final Map<Integer, WinningPrizes> WINNING_PRIZES_MAP = new HashMap<>(); + private static final int HASH_GENERATION_NUMBER = 31; + private static final Map<Integer, WinningPrizes> WINNING_PRIZE_MATCHERS_MAP = new HashMap<>(); - static { - for (WinningPrizes value : values()) { - WINNING_PRIZES_MAP.put(value.rank, value); - } - } + private int numberOfCount; + private boolean isBonusMatch; private int rank; - private final int prizeMoney; + private int prizeMoney; + private Function<Integer, Integer> countSupplier; - WinningPrizes(int rank, int prizeMoney) { + + WinningPrizes(int numberOfCount, boolean isBonusMatch, int rank, int prizeMoney, Function<Integer, Integer> countSupplier) { + this.numberOfCount = numberOfCount; + this.isBonusMatch = isBonusMatch; this.rank = rank; this.prizeMoney = prizeMoney; + this.countSupplier = countSupplier; } - public int calculatePrizeMoney(int count) { - return prizeMoney * count; + static { + for (WinningPrizes value : WinningPrizes.values()) { + WINNING_PRIZE_MATCHERS_MAP.put(getHashCode(value.numberOfCount, value.isBonusMatch), value); + } } - public int getPrizeMoney() { - return prizeMoney; + public static int getHashCode(int numberOfCount, boolean isBonusMatch) { + int bonusNum = 0; + if (isBonusMatch) { + bonusNum = 1; + } + return numberOfCount * HASH_GENERATION_NUMBER + bonusNum; } - public int getRank() { - return rank; + public static WinningPrizes valueOf(int numberOfCount, boolean isBonusMatch) { + isBonusMatch = checkBonus(numberOfCount, isBonusMatch); + return WINNING_PRIZE_MATCHERS_MAP.get(getHashCode(numberOfCount, isBonusMatch)); } - public static WinningPrizes valueOf(int countOfMatch, boolean matchBonus) { - if (countOfMatch < OFFSET) { - return WinningPrizes.MISS; - } - - if (WinningPrizes.SECOND_PRIZE.rank == countOfMatch) { - return decideSecondOrThirdPrizes(matchBonus); + private static boolean checkBonus(int numberOfCount, boolean isBonusMatch) { + if (FIVE_BONUS.getNumberOfCount() != numberOfCount && isBonusMatch) { + isBonusMatch = false; } + return isBonusMatch; + } - return WINNING_PRIZES_MAP.get(countOfMatch); + public int calculatePrizeMoney(int count) { + return prizeMoney * count; } - public static WinningPrizes valueOf(int rank) { - return WINNING_PRIZES_MAP.get(rank); + public int getPrizeMoney() { + return prizeMoney; } - private static WinningPrizes decideSecondOrThirdPrizes(boolean matchBonus) { - if (matchBonus) { - return WinningPrizes.SECOND_PRIZE; - } - return WinningPrizes.THIRD_PRIZE; + public int getNumberOfCount() { + return countSupplier.apply(numberOfCount); } }
Java
IntFunction์„ ํ™œ์šฉํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”.
@@ -2,25 +2,62 @@ import static org.assertj.core.api.Assertions.assertThat; +import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import org.junit.jupiter.api.Test; public class LottoGameTest { @Test - public void ๋กœ๋˜_๊ตฌ์ž…๊ธˆ์•ก์„_์ž…๋ ฅํ•˜๋ฉด_๊ตฌ์ž…๊ธˆ์•ก์—_ํ•ด๋‹นํ•˜๋Š”_๋กœ๋˜๋ฅผ_๋ฐœํ–‰ํ•œ๋‹ค() { + public void ๋กœ๋˜_๊ตฌ์ž…๊ธˆ์•ก๊ณผ_๋ฒˆํ˜ธ๋ฅผ_์ˆ˜๋™์œผ๋กœ_์ž…๋ ฅํ•˜๋ฉด_๊ตฌ์ž…๊ธˆ์•ก์—_ํ•ด๋‹นํ•˜๋Š”_๋กœ๋˜๋ฅผ_๋ฐœํ–‰ํ•œ๋‹ค() { //given int money = 14000; + Set<Integer> manualLottoNumber = new HashSet<>(); + manualLottoNumber.add(2); + manualLottoNumber.add(4); + manualLottoNumber.add(6); + manualLottoNumber.add(7); + manualLottoNumber.add(10); + manualLottoNumber.add(12); + + int manualCount = 1; LottoGame lottoGenerator = new LottoGame(); + List<Set<Integer>> manualNumbers = new ArrayList<>(); + manualNumbers.add(manualLottoNumber); + ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers); + //when - lottoGenerator.generateLottoResultsFromMoney(money); + lottoGenerator.generateLottoResultsFromMoney(manualRequest, money); LottoResults lottoResults = lottoGenerator.getLottoResults(); - List<List<Integer>> lottoResultList = lottoResults.lottoNumbersToInt(); + List<List<Integer>> lottoResultList = lottoResults.lottoResultsToInt(); //then assertThat(lottoResultList).hasSize(14); for (List<Integer> lottoNum : lottoResultList) { assertThat(lottoNum).hasSize(6); } } + +// @Test +// public void ๋กœ๋˜_๋ฒˆํ˜ธ๋ฅผ_์ˆ˜๋™์œผ๋กœ_์ž…๋ ฅํ• _๋•Œ_์ค‘๋ณต๋œ_๋ฒˆํ˜ธ๋ฅผ_์ž…๋ ฅํ•˜๋ฉด_์˜ˆ์™ธ๋ฅผ_๋˜์ง„๋‹ค() { +// //given +// int money = 1000; +// List<Integer> manualLottoNumber = Arrays.asList(2, 2, 6, 7, 10, 12); +// int manualCount = 1; +// LottoGame lottoGenerator = new LottoGame(); +// List<List<Integer>> manualNumbers = new ArrayList<>(); +// manualNumbers.add(manualLottoNumber); +// ManualRequest manualRequest = new ManualRequest(manualCount, manualNumbers); +// +// //when +// +// +// //then +// +// } + + + }
Java
์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ์ฃผ์„(์ฝ”๋“œ)์€ ์ œ๊ฑฐํ•ด์ฃผ์„ธ์š”!
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์„œ๋กœ ์„ฑ๊ฒฉ์ด ๋‹ค๋ฅธ ๋ณ€์ˆ˜ ์„ ์–ธ ๊ฐ„์—๋Š” ์ค„๋ฐ”๊ฟˆ์„ ํ•œ ๋ฒˆ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒŒ ๋” ๋ณด๊ธฐ ์ข‹์ง€ ์•Š์„๊นŒ์š”??๐Ÿค”
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์กฐ๊ฑด๋ฌธ์˜ ์ˆซ์ž์™€ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์ผ์น˜ํ•˜๋„ค์š”! ์ด๋Ÿฐ ์‹์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”??๐Ÿ˜ƒ ```suggestion answer = randomNumber(numberOfDigit[0]); ```
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
JS์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž˜ ํ™œ์šฉํ•˜์…จ๋„ค์š”! ๐Ÿ‘
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
๋งค์ง ๋„˜๋ฒ„ ๋ถ„๋ฆฌ์™€ ๋„ค์ด๋ฐ ์ปจ๋ฒค์…˜์„ ์ค€์ˆ˜ํ•˜์…จ๋„ค์š”! ๐Ÿ‘
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
ํ•˜๋‚˜์˜ ํ•จ์ˆ˜๋Š” ํ•œ ๊ฐ€์ง€ ์ฑ…์ž„๋งŒ ๊ฐ–๋„๋ก ํ•˜๋Š” ๊ฒŒ ์ข‹์•„์š”! randomNumber ํ•จ์ˆ˜์—์„œ๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋‚œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ๋ชฉํ‘œ์ธ๋ฐ, randomNumber ๋ณ€์ˆ˜์— ๋žœ๋ค๊ฐ’์„ ๋„ฃ๋Š” ํ–‰์œ„๋Š” ๊ทธ ์ฑ…์ž„๊ณผ๋Š” ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”. `let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER);` ๋ฅผ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”?? ๐Ÿ˜„ [์ฐธ๊ณ ์ž๋ฃŒ](https://inpa.tistory.com/entry/OOP-%F0%9F%92%A0-%EC%95%84%EC%A3%BC-%EC%89%BD%EA%B2%8C-%EC%9D%B4%ED%95%B4%ED%95%98%EB%8A%94-SRP-%EB%8B%A8%EC%9D%BC-%EC%B1%85%EC%9E%84-%EC%9B%90%EC%B9%99)
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
while๋ฌธ ํ˜ธ์ถœ ์ด์ „์— randomNumber์— ์œ ์˜๋ฏธํ•œ ๊ฐ’์„ ๋„ฃ๊ธฐ ์œ„ํ•ด์„œ ๋žœ๋ค๊ฐ’์„ ์ƒ์„ฑํ•˜์—ฌ ๋ฏธ๋ฆฌ ๋„ฃ์–ด์ฃผ๊ณ  while์„ ํ˜ธ์ถœํ•œ ๊ฒƒ ๊ฐ™๋„ค์š”. ํ•˜์ง€๋งŒ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด randomNumber์˜ ์ดˆ๊ธฐํ™”์‹์— ๋Œ€ํ•ด์„œ ์ฝ”๋“œ ์ค‘๋ณต์ด ์ผ์–ด๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค! do .. while ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ฝ”๋“œ ์ค‘๋ณต์„ ์—†์•จ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?? ๐Ÿ˜
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
newRow๋Š” HTML ์š”์†Œ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ $๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š”๊ฒŒ ๋งž์ง€ ์•Š์„๊นŒ์š”?? ๐Ÿค” ์ €๋„ ์ด๋ถ€๋ถ„์€ ํ™•์‹คํ•˜์ง€ ์•Š์œผ๋‹ˆ ์ง์ ‘ ๊ณ ๋ฏผํ•ด๋ณด์‹œ๋Š” ๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”! [์ฐธ๊ณ ์ž๋ฃŒ](https://choseongho93.tistory.com/213)
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์ด ๋ถ€๋ถ„๋“ค๋„ HTML ์š”์†Œ์ธ ๊ฒƒ ๊ฐ™์•„์š”!
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐํ•จ์„ ์œ„ํ•ด ์ข‹์€ ๋ฐฉ๋ฒ•์ด ๋  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ํ•œ ํ–‰์—์„œ๋Š” ํ•˜๋‚˜์˜ ๋ณ€๊ฒฝ๋งŒ์ด ์ผ์–ด๋‚˜์•ผ ํ•œ๋‹ค๋Š” ๋ง์„ ๋“ค์€ ์ ์ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์š”! ![image](https://user-images.githubusercontent.com/21010656/232725189-43544081-34d5-4f12-9bcb-b13548a0c97d.png)
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
๊ฒฝ๊ณ ๋ฌธ์˜ ๋‚ด์šฉ์ด ๊ฒน์น˜๋Š”๋ฐ, ๊ฒฝ๊ณ ๋ฌธ์„ ํ•จ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•˜๊ฑฐ๋‚˜ ๊ฒฝ๊ณ ๋ฌธ ๋‚ด์šฉ์„ ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋กœ ์ •์˜ํ•˜๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ๐Ÿ˜€
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
๋ณ€๊ฒฝํ•  innerText ๋‚ด์šฉ์€ ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๋Š” ๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด๋กœ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ์š”?? ๐Ÿ˜ƒ [์ฐธ๊ณ ์ž๋ฃŒ](https://www.delftstack.com/ko/howto/javascript/javascript-variable-in-string/)
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
``` score[0] -> ball score[1] -> strike ``` ์ธ ๊ฒƒ ๊ฐ™์€๋ฐ, ์ด๋ ‡๊ฒŒ ๋งค์ง ๋„˜๋ฒ„๋ฅผ ํ†ตํ•ด ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์ฝ”๋“œ๋ฅผ ๋ณด์•˜์„ ๋•Œ ์ธ๋ฑ์Šค ๋ณ„ ์—ญํ• ์— ๋Œ€ํ•ด ํ•ด์„ํ•˜๊ธฐ ํž˜๋“ค ๊ฒƒ ๊ฐ™์•„์š”! ์ธ๋ฑ์Šค์ธ 0๊ณผ 1์„ ๊ฐ๊ฐ ์ƒ์ˆ˜๋กœ ๋ถ„๋ฆฌํ•ด๋ณด๋Š” ๊ฑด ์–ด๋–จ๊นŒ์š”? ๐Ÿ˜„
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
![image](https://user-images.githubusercontent.com/21010656/232728861-7e7a068f-0d8d-4eaa-b1b0-ce69689ffdc0.png) ๊นƒํ—ˆ๋ธŒ์—์„œ `No newline at end of file`๋ผ๋Š” ์—๋Ÿฌ๊ฐ€ ๋‚˜ํƒ€๋‚˜๋„ค์š”! ์ด๋Ÿฐ ์—๋Ÿฌ๋Š” ์™œ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฑธ๊นŒ์š”?? [์ฐธ๊ณ ์ž๋ฃŒ](https://velog.io/@jesop/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EC%BD%94%EB%93%9C-%EB%A7%88%EC%A7%80%EB%A7%89%EC%97%90-%EB%B9%88-%EC%A4%84-%EC%82%BD%EC%9E%85%ED%95%98%EB%8A%94-%EC%9D%B4%EC%9C%A0)
@@ -0,0 +1,29 @@ +.container{ + display: flex; + flex-direction: column; + justify-content: center; + align-items: center; + min-height: 100vh; +} + +.btn-group, .input-content, .table-content{ + display: flex; + justify-content: center; + align-items: center; + margin: 10px; +} + +table{ + width: 500px; + border: 1px solid black; +} + +th, td{ + border: 1px solid black; + text-align: center; +} + +div input{ + margin-left: 5px; + margin-right: 5px; +} \ No newline at end of file
Unknown
์˜ค ์ด๋Ÿฐ ๋ฐฉ๋ฒ•๋„ ์žˆ๊ตฐ์š”...!! ๐Ÿ˜ฎ
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
๋„ต ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๋Šฆ๊ฒŒ ๋ฆฌ๋ทฐ๋‹ฌ์•„์„œ ์ •๋ง ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค...
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์•„ํ•˜ ์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์ฐธ๊ณ ์ž๋ฃŒ ์ž˜ ์ฝ์–ด๋ดค์Šต๋‹ˆ๋‹ค! SRP์— ๋Œ€ํ•ด ์ข€ ๋” ๊ณต๋ถ€๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™๋„ค์š”. ์ฐธ๊ณ ์ž๋ฃŒ๋ฅผ ์ฝ์œผ๋ฉด์„œ ์ข€ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์ด ์žˆ๋Š”๋ฐ "์‘์ง‘๋„๋ฅผ ๋†’๊ฒŒ, ๊ฒฐํ•ฉ๋„๋Š” ๋‚ฎ๊ฒŒ" ๋ผ๋Š” ๋ฌธ์žฅ์—์„œ ์‘์ง‘๋„์™€ ๊ฒฐํ•ฉ๋„์˜ ์ฐจ์ด์— ๋Œ€ํ•ด ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ฆฝ๋‹ˆ๋‹ค. ์‘์ง‘๋„๋Š” ํ•จ์ˆ˜ ์•ˆ์— ๊ตฌ์„ฑ์š”์†Œ๋“ค์˜ ์—ฐ๊ด€๋œ ์ •๋„, ๊ฒฐํ•ฉ๋„๋Š” ์„œ๋กœ ๋‹ค๋ฅธ ํ•จ์ˆ˜๊ฐ„์˜ ์—ฐ๊ด€๋œ ์ •๋„๋กœ ํ•ด์„ํ–ˆ๋Š”๋ฐ ์ด๊ฒŒ ๋งž๋Š” ํ•ด์„์ธ์ง€ ์•Œ๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
function getRandomNumber()๋กœ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ค‘๋ณต์„ ํ”ผํ•˜๋„๋ก ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
์ฃผ๋กœ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š”์ง€ ์•„๋‹ˆ๋ฉด ์ œ๊ฐ€ ํ•„์š”๋กœ ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์จ์•ผ ํ•˜๋Š”๊ฐ€ ๊ถ๊ธˆํ•ฉ๋‹ˆ๋‹ค! ์˜ˆ๋ฅผ ๋“ค์–ด existingNumber๋Š” ๋ฐฐ์—ด์ด๊ธฐ ๋•Œ๋ฌธ์— includesํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ randomNumber๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์ฐพ์•˜๋Š”๋ฐ, ๋งŒ์•ฝ ์ด ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค์ด ์ž์ฃผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์„œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•ด์•ผ ํ•˜๋Š”์ง€ ๋ฌป๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
do .. while ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ค‘๋ณต ์—†์• ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!! do .. while ๋ฌธ์„ ์ƒ๊ฐ ๋ชปํ–ˆ๋„ค์š”..
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
$๋ฅผ ๋ถ™์ด๋Š” ๊ฒƒ์ด ๋งž๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,96 @@ +const MAX_RANDOM_NUMBER = 9; +const MIN_RANDOM_NUMBER = 1; +const $inputNumber = document.querySelector('.input-number'); +const $gameTable = document.querySelector(".game-table"); + +let answer; + +function inputDigit(numberOfDigit){ + $gameTable.innerHTML = "<tr><td>์ˆœ์„œ</td><td>์ˆซ์ž</td><td>๊ฒฐ๊ณผ</td></tr>"; + if(numberOfDigit === "3์ž๋ฆฌ"){ + answer = randomNumber(3); + } else if(numberOfDigit === "4์ž๋ฆฌ"){ + answer = randomNumber(4); + } else if(numberOfDigit === "5์ž๋ฆฌ"){ + answer = randomNumber(5); + } +} + +function randomNumber(numberOfDigit){ + let result = 0; + let existingNumbers = []; + for (step = 0; step < numberOfDigit; step++) { + let randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + while (existingNumbers.includes(randomNumber)){ + randomNumber = Math.floor(Math.random() * (MAX_RANDOM_NUMBER - MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER); + } + existingNumbers.push(randomNumber); + } + + let weight = 1; + for(j = 0; j < numberOfDigit; j++){ + result += existingNumbers[j] * weight; + weight *= 10; + } + + console.log(result); + return result; +} + +let index = 1; +function addValue(){ + const newRow = $gameTable.insertRow(); + const pitchResult = checkBallAndStrike(answer); + const convertedAnswer = answer.toString(); + + let isValid = 0; + if($inputNumber.value.length === convertedAnswer.length){ + for(let i = 0; i < $inputNumber.value.length; i++){ + for(let j = 0; j < $inputNumber.value.length; j++){ + if(i !== j){ + if($inputNumber.value[i] === $inputNumber.value[j]){ + isValid = 1; + } + } + } + } + if(isValid === 0){ + const turn = newRow.insertCell(0); + const number = newRow.insertCell(1); + const result = newRow.insertCell(2); + turn.innerText = `${index++}`; + number.innerText = `${$inputNumber.value}` + if(pitchResult[1] === convertedAnswer.length){ + result.innerText = `O.K.`; + } else{ + result.innerText = `B: ${pitchResult[0]}, S: ${pitchResult[1]}`; + } + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + + } else{ + alert("์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์€ ๊ฐ’์ž…๋‹ˆ๋‹ค."); + } + +} + +function checkBallAndStrike(answer){ + const convertedAnswer = answer.toString(); + const convertedInputNumber = $inputNumber.value.toString(); + let k = 0; + let score = [0, 0]; + while(k < convertedAnswer.length){ + if(convertedAnswer[k] === convertedInputNumber[k]){ + score[1] += 1; + } else{ + for(let item of convertedAnswer){ + if(item === convertedInputNumber[k]){ + score[0] += 1; + } + } + } + k += 1; + } + return score; +} \ No newline at end of file
JavaScript
๊ทธ๋Ÿฌ๋ฉด index๊ฐ’์„ turn์— ๋„ฃ๋Š” ๊ฒƒ, index๊ฐ€ 1์”ฉ ์ฆ๊ฐ€ ํ•˜๋Š” ๊ฒƒ ๋‘ ๊ฐœ๋ฅผ ๋‚˜๋ˆ„๋„๋ก ์ˆ˜์ •ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,53 @@ +'use client'; + +import Button from '@/app/components/common/button/Button'; +import Modal from '@/app/components/common/modal/Modal'; +import IconAlert from '@/app/components/icons/IconAlert'; + +interface ConfirmModalInterface { + title: string; + cancelLabel?: string; + confirmLabel?: string; + isModalOpen: boolean; + handleCancel: () => void; + handleConfirm: () => void; +} + +function ConfirmModal({ + title, + cancelLabel, + confirmLabel, + isModalOpen, + handleCancel, + handleConfirm, +}: ConfirmModalInterface) { + return ( + <Modal isOpen={isModalOpen} closeModal={handleCancel}> + <div className="flex flex-col items-center"> + <IconAlert /> + <div className="mt-4 flex w-[239px] flex-col items-center"> + <h2 className="mb-4 text-lg font-light">{title}</h2> + </div> + + <div className="flex justify-end gap-4"> + <Button + onClick={handleCancel} + variant="secondary" + className="w-[8.5rem]" + > + {cancelLabel || '๋‹ซ๊ธฐ'} + </Button> + <Button + onClick={handleConfirm} + variant="danger" + className="w-[8.5rem]" + > + {confirmLabel || 'ํšŒ์› ํƒˆํ‡ด'} + </Button> + </div> + </div> + </Modal> + ); +} + +export default ConfirmModal;
Unknown
๊ณตํ†ต ์ปดํฌ๋„ŒํŠธ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!!!
@@ -10,6 +10,7 @@ import useModal from '@/app/hooks/useModal'; import DetailMemberModal from '@/app/components/team/DetailMemberModal'; import deleteMember from '@/app/lib/group/deleteMemeber'; import { useMutation, useQueryClient } from '@tanstack/react-query'; +import ConfirmModal from '@/app/components/common/modal/ConfirmModal'; interface GroupMember { role: 'ADMIN' | 'MEMBER'; @@ -24,6 +25,11 @@ function MemberCard({ member }: { member: GroupMember }) { const queryClient = useQueryClient(); const { isOpen, toggleDropdown, closeDropdown } = useDropdown(); const { isOpen: isModalOpen, openModal, closeModal } = useModal(); + const { + isOpen: isConfirmModalOpen, + openModal: openConfirmModal, + closeModal: closeConfirmModal, + } = useModal(); const { mutate: expelMember } = useMutation({ mutationFn: deleteMember, @@ -66,7 +72,7 @@ function MemberCard({ member }: { member: GroupMember }) { <DropdownItem onClick={openModal} onClose={closeDropdown}> ๋ฉค๋ฒ„ ์ •๋ณด </DropdownItem> - <DropdownItem onClick={handleExpel} onClose={closeDropdown}> + <DropdownItem onClick={openConfirmModal} onClose={closeDropdown}> ์ถ”๋ฐฉํ•˜๊ธฐ </DropdownItem> </DropdownList> @@ -77,6 +83,14 @@ function MemberCard({ member }: { member: GroupMember }) { isOpen={isModalOpen} closeModal={closeModal} /> + <ConfirmModal + isModalOpen={isConfirmModalOpen} + title={`${member.userName}๋‹˜์„ ์ถ”๋ฐฉํ•˜์‹œ๊ฒ ์–ด์š”?`} + cancelLabel="์ทจ์†Œ" + confirmLabel="์ถ”๋ฐฉ" + handleCancel={closeConfirmModal} + handleConfirm={handleExpel} + /> </> ); }
Unknown
`๋ฅผ ์ถ”๋ฐฉํ•˜์‹œ๊ฒ ์–ด์š”?` ๋ผ๊ณ  ๋ช…์‹œํ•˜๋ฉด ์ด๋ฆ„ ๋งˆ์ง€๋ง‰ ๊ธ€์ž์— ๋ฐ›์นจ์ด ์žˆ๋Š” ์‚ฌ๋žŒ์€ ๋ฌธ๊ตฌ๊ฐ€ ์–ด์ƒ‰ํ•˜๊ฒŒ ๋ณด์ผ ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ): ํ™์ง€์œค๋ฅผ ์ถ”๋ฐฉํ•˜์‹œ๊ฒ ์–ด์š”? ๊ทธ๋ž˜์„œ `${member.userName}๋‹˜์„ ์ถ”๋ฐฉํ•˜์‹œ๊ฒ ์–ด์š”?` ๊ฐ€ ๋” ๊ดœ์ฐฎ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -10,6 +10,7 @@ import useModal from '@/app/hooks/useModal'; import DetailMemberModal from '@/app/components/team/DetailMemberModal'; import deleteMember from '@/app/lib/group/deleteMemeber'; import { useMutation, useQueryClient } from '@tanstack/react-query'; +import ConfirmModal from '@/app/components/common/modal/ConfirmModal'; interface GroupMember { role: 'ADMIN' | 'MEMBER'; @@ -24,6 +25,11 @@ function MemberCard({ member }: { member: GroupMember }) { const queryClient = useQueryClient(); const { isOpen, toggleDropdown, closeDropdown } = useDropdown(); const { isOpen: isModalOpen, openModal, closeModal } = useModal(); + const { + isOpen: isConfirmModalOpen, + openModal: openConfirmModal, + closeModal: closeConfirmModal, + } = useModal(); const { mutate: expelMember } = useMutation({ mutationFn: deleteMember, @@ -66,7 +72,7 @@ function MemberCard({ member }: { member: GroupMember }) { <DropdownItem onClick={openModal} onClose={closeDropdown}> ๋ฉค๋ฒ„ ์ •๋ณด </DropdownItem> - <DropdownItem onClick={handleExpel} onClose={closeDropdown}> + <DropdownItem onClick={openConfirmModal} onClose={closeDropdown}> ์ถ”๋ฐฉํ•˜๊ธฐ </DropdownItem> </DropdownList> @@ -77,6 +83,14 @@ function MemberCard({ member }: { member: GroupMember }) { isOpen={isModalOpen} closeModal={closeModal} /> + <ConfirmModal + isModalOpen={isConfirmModalOpen} + title={`${member.userName}๋‹˜์„ ์ถ”๋ฐฉํ•˜์‹œ๊ฒ ์–ด์š”?`} + cancelLabel="์ทจ์†Œ" + confirmLabel="์ถ”๋ฐฉ" + handleCancel={closeConfirmModal} + handleConfirm={handleExpel} + /> </> ); }
Unknown
๋ฌธ๊ตฌ ๋””ํ…Œ์ผ์„ ๋†“์ณค๋„ค์š”. ๋ฐ˜์˜ํ•ด์„œ ์˜ฌ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค๐Ÿ˜Š
@@ -0,0 +1,31 @@ +package com.antique.dto.review; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ReviewRequestDTO { + @Schema(description = "๋ฆฌ๋ทฐ ์ž‘์„ฑ์ž ID") + @NotNull + private Long reviewerId; // ๋ฆฌ๋ทฐ ์ž‘์„ฑ์ž ID + + @Schema(description = "๋ฆฌ๋ทฐ ๋Œ€์ƒ์ž ID") + @NotNull + private Long reviewedUserId; // ๋ฆฌ๋ทฐ ๋Œ€์ƒ์ž ID + + @Schema(description = "์ œํ’ˆ ID") + @NotNull + private Long productId; // ์ œํ’ˆ ID + + @Schema(description = "ํ‰์ ") + @NotNull + private int rating; // ํ‰์  + + @Schema(description = "๋ฆฌ๋ทฐ ๋‚ด์šฉ") + private String content; // ๋ฆฌ๋ทฐ ๋‚ด์šฉ +}
Java
schema ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•ด์„œ ํ‘œํ˜„ํ•˜๋ฉด ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์•„..!!
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
๋ณ€์ˆ˜ ์„ ์–ธํ•  ๋•Œ๋Š” `scope` ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ํ•ญ์ƒ `const`, `let` ๋“ฑ์˜ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ๋Š”๊ฒŒ ์ข‹์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์—ฌ๊ธฐ๋„ ์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ณ€์ˆ˜ ์„ ์–ธ ์‹œ์— ์ ์ ˆํ•œ ํ‚ค์›Œ๋“œ๊ฐ€ ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
๋ฐ˜๋ณต๋ฌธ ์•ˆ์˜ `validType` ์€ ์žฌํ• ๋‹นํ•  ํ•„์š”๋Š” ์—†์–ด ๋ณด์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด `const` ๊ฐ€ ๋” ์ ์ ˆํ•˜์ง€ ์•Š์„๊นŒ์š”?
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์ „์ฒด์ ์œผ๋กœ String.prototype.match ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•˜์…จ๋Š”๋ฐ, Regex ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด๋Š” ๊ฒƒ์€ ์–ด๋–ฏ๊นŒ์š”?
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
`req.body` ์— ๋‹ค์Œ ํ‚ค๊ฐ’๋“ค ์ค‘ ํ•˜๋‚˜๊ฐ€ ์•„์˜ˆ ๋ˆ„๋ฝ๋œ ๊ฒฝ์šฐ, ๋Ÿฐํƒ€์ž„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ตฌ์กฐํ™” ํ• ๋‹น ์‹œ์— ์ „์ฒด์ ์œผ๋กœ ์ฃผ์˜ํ•˜๋Š”๊ฒŒ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์—ฌ๊ธฐ ๋ฐ˜๋ณต๋ฌธ ๋‚ด์˜ `info` ๋„ ์žฌํ• ๋‹นํ•  ์ผ์€ ์—†์–ด๋ณด์ด๋‹ˆ `const` ๊ฐ€ ๋” ์ ์ ˆํ•ด ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์—๋Ÿฌ๋ฅผ ๋˜์ง€๋Š” ๋ถ€๋ถ„์„ ๋ชจ๋“ˆํ™”ํ•˜๊ฒŒ ๋˜๋ฉด ๋” ๋†’์€ ์‚ฌ์šฉ์„ฑ์„ ๊ฐ–๊ฒŒ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๊ทธ๋ฆฌ๊ณ  ์ง์ ‘ ์—๋Ÿฌ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค ์—๋Ÿฌ๋ฅผ `throw` ํ•˜๊ณ , `catch` ๊ตฌ๋ฌธ์—์„œ ๋‹ค์Œ ๋ฏธ๋“ค์›จ์–ด๋กœ ๊ฑด๋‚ด๋ฉด์„œ ์ตœ์ข…์ ์œผ๋กœ ์—๋Ÿฌ๋ฅผ ํ—จ๋“ค๋งํ•˜๋Š” ํ•จ์ˆ˜์—์„œ ํ•œ๋ฒˆ์— ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ๋” ์ข‹์•„ ๋ณด์ž…๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์ „์ฒด์ ์œผ๋กœ ํ•˜๋‚˜์˜ ์ปจํŠธ๋กค๋Ÿฌ ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ ˆ์ฐจ๋“ค์ด ๋‚˜์—ด๋˜์–ด ์žˆ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ๋‹จ์œ„๋กœ ๋” ์ชผ๊ฒŒ๊ฒŒ๋˜๋ฉด, ๋‚˜์ค‘์— unit test ์‹œ์—๋„ ๋” ํŽธ๋ฆฌํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ๊ฒ€์ฆํ•˜๊ณ  ์žˆ๋Š” ์—ฌ๋Ÿฌ ๋กœ์ง๋“ค๋„, ์ ์ ˆํžˆ Service ์•ˆ์— ๋„ฃ์–ด๋‘˜ ํ•„์š”๋„ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ hash ํ•˜๋Š” ๋ถ€๋ถ„๋„ req, res ๋งŒ ๋‹ค๋ฃจ๊ฒŒ ๋˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋‹ค๋ฃจ๋Š” ๊ฒƒ์ด ์กฐ๊ธˆ ์–ด์ƒ‰ํ•ด ๋ณด์ด๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ „๋ฐ˜์ ์œผ๋กœ ์ด ๊ฐ ํ•จ์ˆ˜๊ฐ€ ํ•˜๋‚˜์˜ ๋ช…ํ™•ํ•œ ์ผ๋งŒ ํ•˜๋„๋ก ๊ตฌ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์‘๋‹ต ๊ฐ’๋„ ํด๋ผ์ด์–ธํŠธ์™€์˜ ํ˜‘์•ฝ์— ๋”ฐ๋ผ ์ •ํ•ด์ง„ ํผ์„ ํ•ญ์ƒ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ชจ๋“ˆํ™”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์œ ์ง€/๋ณด์ˆ˜์— ๋” ์šฉ์ดํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,13 @@ +const express = require('express'); +const router = express.Router(); +const middleware = require('../middlewares'); +const { userController } = require('../controllers'); +// Route ๋Š” ์˜ค์ง Controller ์—๋งŒ ์˜์กด ํ•ฉ๋‹ˆ๋‹ค. + +router.post( + '/signup', + [middleware.validateSignUpUserData, middleware.hashPassword], + userController.signUp, +); + +module.exports = router;
JavaScript
์ด ๋ถ€๋ถ„ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋‚˜์š”? ํ‚ค๊ฐ’์ด ๋‹ค๋ฅธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,48 @@ +const bcrypt = require('bcrypt'); +const { userDao } = require('../dao'); + +const validateEmail = (email) => { + const validEmailRegExp = /\w@\w+\.\w/i; + return validEmailRegExp.test(email); +}; + +const validatePw = (pw) => { + const pwValidation = { + regexUppercase: /[A-Z]/g, + regexLowercase: /[a-z]/g, + regexSpecialCharacter: /[!|@|#|$|%|^|&|*]/g, + regexDigit: /[0-9]/g, + }; + const MIN_PW_LENGTH = 8; + const pwLength = pw.length; + + if (pwLength < MIN_PW_LENGTH) return false; + + for (const validType in pwValidation) { + if (!pwValidation[validType].test(pw)) { + return false; + } + } + + return true; +}; + +const hashPassword = async (password) => { + return await bcrypt.hash(password, 10); +}; + +const findUser = async (fields) => { + return await userDao.findUser(fields); +}; + +const createUser = async (userData) => { + return await userDao.createUser(userData); +}; + +module.exports = { + validateEmail, + validatePw, + hashPassword, + findUser, + createUser, +};
JavaScript
์ด๋Ÿฐ ํ•จ์ˆ˜๋Š” ์•„์˜ˆ ๋ชจ๋“ˆํ™”ํ•ด์„œ ๋‹ค๋ฅธ ๊ณณ์—์„œ๋„ ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์ด ๋ถ€๋ถ„ ์ž‘๋™ ์ž˜ ํ•˜๋‚˜์š”? ํ‚ค ๊ฐ’์ด ์•ˆ ๋งž๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค! validation ๋กœ์ง์€ ์˜ˆ์ „ ์ œ ์ฝ”๋“œ ๊ธ์–ด์˜ค๋‹ค๊ฐ€ ๋†“์นœ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
ํ”„๋ก ํŠธ๋‹จ์—์„œ ๋ฐ›์€ `req.body`์— ์œ„ ํ‚ค๊ฐ’๋“ค์ด ๋ˆ„๋ฝ๋  ๊ฒฝ์šฐ ๊ทธ ๊ฐ’์ด undefined๋กœ ๋˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, ์ œ๊ฐ€ ๋†“์น˜๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด ์žˆ์„๊นŒ์š”?
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
`RegExp.prototype.test()`๋กœ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์›”์š”์ผ์— ๋ง์”€ํ•ด์ฃผ์‹ ๋Œ€๋กœ `return res` ํ˜•ํƒœ๊ฐ€ ์•„๋‹ˆ๋ผ ์—๋Ÿฌ ํ•ธ๋“ค๋ง์„ ๋ชจ๋“ˆํ™” ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค :)
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!!!!!!
@@ -0,0 +1,40 @@ +// Controller๋Š” ์˜ค์ง Service ๋ ˆ์ด์–ด์—๋งŒ ์˜์กดํ•ฉ๋‹ˆ๋‹ค. +const { userService } = require('../services'); +const { errorGenerator } = require('../erros'); + +const signUp = async (req, res, next) => { + try { + const { + email, + hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + } = req.body; + + /* + middleware์—์„œ email, hashedPassword, phoneNumber์˜ + ์กด์žฌ์— ๋Œ€ํ•œ ์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ–ˆ๋Š”๋ฐ, + ์—ฌ๊ธฐ์„œ๋„ ๋˜ ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ• ๊นŒ์š”? + */ + + const createdUser = await userService.createUser({ + email, + password: hashedPassword, + phoneNumber, + profileImageUrl, + introduction, + }); + + res.status(201).json({ + message: 'user created', + userId: createdUser.id, + }); + } catch (err) { + next(err); + } +}; + +module.exports = { + signUp, +};
JavaScript
์—๋Ÿฌ ํ•ธ๋“ค๋ง๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‘๋‹ต๋„ ๋ชจ๋“ˆํ™” ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค :)