code stringlengths 41 34.3k | lang stringclasses 8
values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,261 @@
+//
+// LoginViewController_TVING.swift
+// GO_SOPT_Seminar_Assingment
+//
+// Created by ๊น๋ค์ on 2023/04/11.
+//
+
+import UIKit
+
+import SnapKit
+import Then
+
+struct TvingUserInfo {
+ var id: String?
+ var password: String?
+ var nickName: String?
+}
+
+final class LoginViewController_TVING: BaseViewController {
+
+ // MARK: - Property
+
+ private let mainView = LoginView()
+ private let nickNameBottomSheet = AddNickNameBottomSheetUIView()
+
+ var user = TvingUserInfo()
+
+ private var bottomSheetKeyboardEnable: Bool = false
+
+ // MARK: - Target
+
+ private func target() {
+ mainView.idTextField.delegate = self
+ mainView.passwordTextField.delegate = self
+
+ mainView.idTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+ mainView.passwordTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ mainView.logInBtn.addTarget(self, action: #selector(tappedLogInBtn), for: .touchUpInside)
+ mainView.goToMakeNicknameBtn.addTarget(self, action: #selector(tappedMakeNickNameBtn), for: .touchUpInside)
+ }
+
+ private func targetBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.delegate = self
+
+ nickNameBottomSheet.nickNameTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ nickNameBottomSheet.saveNickNameBtn.addTarget(self, action: #selector(tappedSavedNickNameBtn), for: .touchUpInside)
+
+ let bottomSheetBackgroundTap = UITapGestureRecognizer(target: self, action: #selector(TappedBottomSheetBackground))
+ nickNameBottomSheet.dimmendView.addGestureRecognizer(bottomSheetBackgroundTap)
+ }
+
+ // MARK: - Lift Cycle
+
+ override func loadView() {
+ self.view = mainView
+ view.addSubview(nickNameBottomSheet)
+
+ nickNameBottomSheet.snp.makeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ }
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+
+ target()
+ targetBottomSheet()
+ setKeyboardObserver() // ํค๋ณด๋ ํ์ฑํ์ ๋ฐ๋ฅธ bottomSheet ๋์ด ์กฐ์ ์ํด
+ }
+
+ override func viewWillAppear(_ animated: Bool) {
+ initView()
+ }
+
+}
+
+private extension LoginViewController_TVING {
+
+ // MARK: - objc func
+
+ @objc
+ func tappedLogInBtn() {
+ saveUserEmail()
+ let welcomViewController = WelcomeViewController()
+ welcomViewController.idDataBind(idOrNick: getNickNameOrId())
+ self.navigationController?.pushViewController(welcomViewController, animated: true)
+ }
+
+ @objc
+ func tappedMakeNickNameBtn() {
+ showBottomSheet()
+ }
+
+ @objc
+ func tappedSavedNickNameBtn() {
+ saveUserNickName()
+ hideBottomSheet()
+ }
+
+ @objc func TappedBottomSheetBackground(_ tapRecognizer: UITapGestureRecognizer) {
+ hideBottomSheet()
+ }
+
+ @objc func keyboardWillShow(notification: NSNotification) {
+ guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - (nickNameBottomSheet.bottomSheetHeight + keyboardSize.height)
+ }
+ }
+
+ @objc func keyboardWillHide(notification: NSNotification) {
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - nickNameBottomSheet.bottomSheetHeight
+ }
+ }
+
+ // MARK: - custom func
+
+ func saveUserEmail(){
+ guard let id = mainView.idTextField.text else { return }
+ user.id = id
+ }
+
+ func saveUserNickName() {
+ guard let nickName = nickNameBottomSheet.nickNameTextField.text else { return }
+ user.nickName = nickName
+ }
+
+ func getNickNameOrId() -> String {
+ if let nickName = user.nickName {
+ return nickName
+ } else {
+ guard let id = user.id else { return "" }
+ return id
+ }
+ }
+
+ func isIDValid() -> Bool {
+ guard let id = mainView.idTextField.text else { return false }
+ return id.isValidEmail()
+ }
+
+ func showBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = true
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.edges.equalToSuperview()
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)
+ })
+ }
+
+ func hideBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = false
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height)
+ }
+ )
+ }
+
+ func initView() {
+ mainView.idTextField.text = nil
+ mainView.passwordTextField.text = nil
+ nickNameBottomSheet.nickNameTextField.text = nil
+ user = TvingUserInfo()
+ mainView.logInBtn.enableDisableButtonSet(isEnable: false, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ func setKeyboardObserver() {
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
+ }
+
+}
+
+// MARK: - TextFieldDelegate
+
+extension LoginViewController_TVING: UITextFieldDelegate {
+
+ // textField๊ฐ ํ์ฑํ๋๋ฉด
+ func textFieldDidBeginEditing(_ textField: UITextField) {
+ textField.layer.borderColor = UIColor.tvingGray2.cgColor
+ textField.layer.borderWidth = 0.7
+ }
+
+ // textField ๋นํ์ฑํ๋๋ฉด
+ func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
+ textField.layer.borderWidth = 0
+
+ idValidPrint()
+
+ return true
+ }
+
+ @objc
+ private func textFieldDidChange(_ textField: UITextField) {
+
+ if let nickNameText = nickNameBottomSheet.nickNameTextField.text {
+ if (!nickNameText.isValidNickName()) {
+ nickNameBottomSheet.nickNameTextField.text = nil
+ }
+ }
+
+ let idPwIsFull: Bool = mainView.idTextField.hasText && !mainView.passwordTextField.hasText
+ let saveIdPwBtnEnable: Bool = idPwIsFull && isIDValid()
+
+ if (saveIdPwBtnEnable) {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ let saveNickNameBtnEnable = !nickNameBottomSheet.nickNameTextField.hasText
+
+ if (saveNickNameBtnEnable) {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+ }
+
+ private func idValidPrint() {
+
+ if (!(mainView.idTextField.text?.isValidEmail() ?? false) && !mainView.idTextField.hasText) {
+ mainView.idTextField.layer.borderColor = UIColor.tvingRed.cgColor
+ mainView.idTextField.layer.borderWidth = 0.7
+
+ mainView.idInvalidLabel.isHidden = false
+ mainView.idInvalidLabel.snp.remakeConstraints {
+ $0.leading.equalTo(mainView.idTextField.snp.leading)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(5)
+ }
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(30)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ } else {
+ mainView.idTextField.layer.borderWidth = 0
+
+ mainView.idInvalidLabel.isHidden = true
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(7)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ }
+ }
+
+} | Swift | ๋ฒํผ ํ์ฑํ์ ์ ๊ท์์ ๋ง๋์ง ํ์ธํ๋ ๋ถ๋ถ์ ๋๋ ์ฃผ๋ฉด ๋ ์ข์๊ฑฐ ๊ฐ์์
๋ฒํผ ํ์ฑํ๋ -> textFeild์ ๊ธ์ด ์ฐจ ์์ ๊ฒฝ์ฐ -> textFeildDidChange๋ฅผ ์ฌ์ฉํ๋ฉด ์ข์๊ฑฐ ๊ฐ๊ณ
์ ๊ท์ ํ์ธ -> ๋ฒํผ์ ๋๋ ์ ๋ -> ๋ฒํผ์ addTarget์ ํด์ ํ์ธํด์ฃผ๋ toastMessage๋ print๋ฌธ์ ์ฐ์ด์ฃผ๋๊ฒ ์ข์๊ฑฐ ๊ฐ์ต๋๋ค~ |
@@ -0,0 +1,119 @@
+//
+// AddNickNameBottomSheetUIView.swift
+// GO_SOPT_Seminar_Assingment
+//
+// Created by ๊น๋ค์ on 2023/04/15.
+//
+
+import UIKit
+
+import SnapKit
+import Then
+
+final class AddNickNameBottomSheetUIView: UIView {
+
+ let bottomSheetHeight = UIScreen.main.bounds.height / 2
+
+ let dimmendView = UIView().then {
+ $0.backgroundColor = .black.withAlphaComponent(0.5)
+ }
+
+ private lazy var dragIndicatior = UIView().then {
+ $0.backgroundColor = .tvingGray1
+ $0.layer.cornerRadius = 3
+ }
+
+ let bottomSheetView = UIView().then {
+ $0.backgroundColor = .white
+ $0.layer.cornerRadius = 12
+ $0.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner] // ์ข์ฐ์ธก ํ๋จ์ ๊ทธ๋๋ก
+ }
+
+ private let nickNameMainLabel = UILabel().then {
+ $0.text = "๋๋ค์์ ์
๋ ฅํด์ฃผ์ธ์"
+ $0.font = .tvingMedium(ofSize: 23)
+ }
+
+ let nickNameTextField = CustomTextField().then {
+ $0.placeholder = "๋๋ค์"
+ $0.setPlaceholderColor(.tvingGray1)
+ $0.textColor = .black
+ $0.backgroundColor = .tvingGray2
+ $0.font = .tvingMedium(ofSize: 14)
+ }
+
+ lazy var saveNickNameBtn = UIButton().then {
+ $0.setTitle("์ ์ฅํ๊ธฐ", for: .normal)
+ $0.titleLabel?.font = .tvingMedium(ofSize: 16)
+ $0.titleLabel?.textAlignment = .center
+ $0.titleLabel?.textColor = .tvingGray2
+ $0.backgroundColor = .black
+ $0.layer.cornerRadius = 12
+ $0.isEnabled = false
+ }
+
+ override init(frame: CGRect) {
+ super.init(frame: frame)
+
+ style()
+ hierarchy()
+ setLayout()
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+}
+
+private extension AddNickNameBottomSheetUIView {
+
+ func style() {
+
+ }
+
+ func hierarchy() {
+ self.addSubviews(dimmendView,
+ dragIndicatior,
+ bottomSheetView)
+ bottomSheetView.addSubviews(nickNameMainLabel,
+ nickNameTextField,
+ saveNickNameBtn)
+ }
+
+ func setLayout() {
+
+ dimmendView.snp.makeConstraints {
+ $0.edges.equalToSuperview()
+ }
+
+ bottomSheetView.snp.makeConstraints {
+ $0.height.equalTo(bottomSheetHeight)
+ $0.bottom.left.right.equalToSuperview()
+ $0.top.equalToSuperview().inset(UIScreen.main.bounds.height - bottomSheetHeight)
+ }
+
+ dragIndicatior.snp.makeConstraints {
+ $0.height.equalTo(5)
+ $0.leading.trailing.equalToSuperview().inset(120)
+ $0.bottom.equalTo(bottomSheetView.snp.top).inset(-10)
+ }
+
+ nickNameMainLabel.snp.makeConstraints {
+ $0.top.equalToSuperview().inset(45)
+ $0.leading.equalToSuperview().inset(20)
+ }
+
+ nickNameTextField.snp.makeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(nickNameMainLabel.snp.bottom).offset(21)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+
+ saveNickNameBtn.snp.makeConstraints {
+ $0.height.equalTo(52)
+ $0.bottom.equalToSuperview().inset(20)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ }
+
+} | Swift | ์ ๊ทผ ์ ์ด์๋ฅผ ํ์ฉํ ๋๋ ๋ด๊ฐ ํด๋น ๋ด์ฉ์(? ๋ช
์นญ์ด ์ ๋งคํจ) ์ฌ์ฉํ ์ต์ํ์ ๋ฒ์๋ก ์ฌ์ฉํ๋๊ฒ ์ข์์! ๋ง์ฝ ํด๋น ํ์ผ์์๋ง ์ฌ์ฉํ component๋ผ๋ฉด private์ผ๋ก ๋ฐ๊พธ๋๊ฒ๋ ์ข๊ฒ ์ฃ ? |
@@ -0,0 +1,205 @@
+//
+// LoginView.swift
+// GO_SOPT_Seminar_Assingment
+//
+// Created by ๊น๋ค์ on 2023/04/16.
+//
+
+import UIKit
+
+class LoginView: UIView {
+
+ // MARK: - Object Setting
+
+ private let mainLabel = UILabel().then {
+ $0.text = "TVING ID ๋ก๊ทธ์ธ"
+ $0.font = .tvingMedium(ofSize: 23)
+ $0.textColor = .tvingGray1
+ $0.textAlignment = .center
+ }
+
+ let idTextField = CustomTextField().then {
+ $0.placeholder = "์ด๋ฉ์ผ"
+ $0.setPlaceholderColor(.tvingGray2)
+ $0.font = .tvingMedium(ofSize: 15)
+ $0.textColor = .tvingGray2
+ $0.backgroundColor = .tvingGray4
+ $0.keyboardType = .emailAddress
+ }
+
+ let idInvalidLabel = UILabel().then {
+ $0.textColor = .systemRed
+ $0.font = .systemFont(ofSize: 12)
+ $0.text = "* ์ฌ๋ฐ๋ฅด์ง ์์ ์ด๋ฉ์ผ ํ์์
๋๋ค ๐ญ"
+ $0.isHidden = true
+ }
+
+ let passwordTextField = CustomPasswordTextField().then {
+ $0.placeholder = "๋น๋ฐ๋ฒํธ"
+ $0.setPlaceholderColor(.tvingGray2)
+ $0.font = .tvingMedium(ofSize: 15)
+ $0.textColor = .tvingGray2
+ $0.backgroundColor = .tvingGray4
+ $0.keyboardType = .asciiCapable
+ }
+
+ lazy var logInBtn = UIButton().then {
+ $0.setTitle("๋ก๊ทธ์ธํ๊ธฐ", for: .normal)
+ $0.setTitleColor(.tvingGray2, for: .normal)
+ $0.titleLabel?.font = .tvingMedium(ofSize: 14)
+ $0.titleLabel?.textAlignment = .center
+ $0.layer.borderColor = UIColor.tvingGray4.cgColor
+ $0.layer.borderWidth = 1
+ $0.layer.cornerRadius = 3
+ $0.isEnabled = false
+ }
+
+ private let idPasswordStackView = UIStackView().then {
+ $0.axis = .horizontal
+ $0.distribution = .fill
+ $0.alignment = .center
+ $0.spacing = 40
+ }
+
+ lazy var findIdBtn = UIButton().then {
+ $0.setTitle("์์ด๋ ์ฐพ๊ธฐ", for: .normal)
+ $0.setTitleColor(.tvingGray2, for: .normal)
+ $0.titleLabel?.font = .tvingMedium(ofSize: 14)
+ $0.titleLabel?.textAlignment = .center
+ }
+
+ lazy var findPasswordBtn = UIButton().then {
+ $0.setTitle("๋น๋ฐ๋ฒํธ ์ฐพ๊ธฐ", for: .normal)
+ $0.setTitleColor(.tvingGray2, for: .normal)
+ $0.titleLabel?.font = .tvingMedium(ofSize: 14)
+ $0.titleLabel?.textAlignment = .center
+ }
+
+ private let makeAccountStackView = UIStackView().then {
+ $0.axis = .horizontal
+ $0.distribution = .fill
+ $0.alignment = .center
+ $0.spacing = 20
+ }
+
+ let askExistAccountLabel = UILabel().then {
+ $0.text = "์์ง ๊ณ์ ์ด ์์ผ์ ๊ฐ์?"
+ $0.font = .tvingRegular(ofSize: 14)
+ $0.textColor = .tvingGray3
+ $0.textAlignment = .center
+ }
+
+ lazy var goToMakeNicknameBtn = UIButton().then {
+ $0.setTitle("๋๋ค์ ๋ง๋ค๋ฌ๊ฐ๊ธฐ", for: .normal)
+ $0.setTitleColor(.tvingGray2, for: .normal)
+ $0.titleLabel?.font = .tvingRegular(ofSize: 14)
+ $0.titleLabel?.textAlignment = .center
+ $0.setUnderline()
+ }
+
+ lazy var backBtn = UIButton().then {
+ $0.setImage(UIImage(named: "btn_before"), for: .normal)
+ }
+
+ override init(frame: CGRect) {
+ super.init(frame: frame)
+
+ style()
+ hierarchy()
+ setLayout()
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+}
+
+private extension LoginView {
+
+ func style() {
+ self.backgroundColor = .black
+ }
+
+ func hierarchy() {
+ self.addSubviews(mainLabel,
+ idTextField,
+ idInvalidLabel,
+ passwordTextField,
+ logInBtn,
+ idPasswordStackView,
+ makeAccountStackView,
+ backBtn)
+ idPasswordStackView.addArrangedSubviews(findIdBtn,
+ findPasswordBtn)
+ makeAccountStackView.addArrangedSubviews(askExistAccountLabel,
+ goToMakeNicknameBtn)
+ }
+
+
+ func setLayout() {
+
+ mainLabel.snp.makeConstraints{
+ $0.height.equalTo(37)
+ $0.top.equalTo(self.safeAreaLayoutGuide).inset(50)
+ $0.leading.trailing.equalToSuperview().inset(100)
+ }
+
+ idTextField.snp.makeConstraints{
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainLabel.snp.bottom).offset(31)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+
+ passwordTextField.snp.makeConstraints{
+ $0.height.equalTo(52)
+ $0.top.equalTo(idTextField.snp.bottom).offset(7)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+
+ logInBtn.snp.makeConstraints{
+ $0.height.equalTo(52)
+ $0.top.equalTo(passwordTextField.snp.bottom).offset(21)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+
+ idPasswordStackView.snp.makeConstraints {
+ $0.height.equalTo(25)
+ $0.top.equalTo(logInBtn.snp.bottom).offset(30)
+ $0.centerX.equalToSuperview().inset(80)
+ }
+
+ makeAccountStackView.snp.makeConstraints {
+ $0.height.equalTo(25)
+ $0.top.equalTo(idPasswordStackView.snp.bottom).offset(30)
+ $0.centerX.equalToSuperview().inset(50)
+ }
+
+ backBtn.snp.makeConstraints{
+ $0.height.equalTo(15)
+ $0.width.equalTo(8)
+ $0.top.equalTo(self.safeAreaLayoutGuide).inset(25)
+ $0.leading.equalToSuperview().inset(24)
+ }
+
+ findIdBtn.snp.makeConstraints{
+ $0.width.equalTo(70)
+ $0.height.equalTo(22)
+ }
+
+ findPasswordBtn.snp.makeConstraints{
+ $0.width.equalTo(80)
+ $0.height.equalTo(22)
+ }
+
+ askExistAccountLabel.snp.makeConstraints{
+ $0.width.equalTo(140)
+ $0.height.equalTo(22)
+ }
+
+ goToMakeNicknameBtn.snp.makeConstraints{
+ $0.width.equalTo(128)
+ $0.height.equalTo(22)
+ }
+ }
+} | Swift | ์ด์ฉ์ง ๋ ์ด์์ ์ก์ผ๋ฉด์๋ ์ด๊ฒ ๋ง๋.. ์ถ์์ด๋ฏธ๋ค
์คํ๋ทฐ๋ก ๋ค์ ์ง๋ดค์ด์!!!!!! |
@@ -0,0 +1,261 @@
+//
+// LoginViewController_TVING.swift
+// GO_SOPT_Seminar_Assingment
+//
+// Created by ๊น๋ค์ on 2023/04/11.
+//
+
+import UIKit
+
+import SnapKit
+import Then
+
+struct TvingUserInfo {
+ var id: String?
+ var password: String?
+ var nickName: String?
+}
+
+final class LoginViewController_TVING: BaseViewController {
+
+ // MARK: - Property
+
+ private let mainView = LoginView()
+ private let nickNameBottomSheet = AddNickNameBottomSheetUIView()
+
+ var user = TvingUserInfo()
+
+ private var bottomSheetKeyboardEnable: Bool = false
+
+ // MARK: - Target
+
+ private func target() {
+ mainView.idTextField.delegate = self
+ mainView.passwordTextField.delegate = self
+
+ mainView.idTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+ mainView.passwordTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ mainView.logInBtn.addTarget(self, action: #selector(tappedLogInBtn), for: .touchUpInside)
+ mainView.goToMakeNicknameBtn.addTarget(self, action: #selector(tappedMakeNickNameBtn), for: .touchUpInside)
+ }
+
+ private func targetBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.delegate = self
+
+ nickNameBottomSheet.nickNameTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ nickNameBottomSheet.saveNickNameBtn.addTarget(self, action: #selector(tappedSavedNickNameBtn), for: .touchUpInside)
+
+ let bottomSheetBackgroundTap = UITapGestureRecognizer(target: self, action: #selector(TappedBottomSheetBackground))
+ nickNameBottomSheet.dimmendView.addGestureRecognizer(bottomSheetBackgroundTap)
+ }
+
+ // MARK: - Lift Cycle
+
+ override func loadView() {
+ self.view = mainView
+ view.addSubview(nickNameBottomSheet)
+
+ nickNameBottomSheet.snp.makeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ }
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+
+ target()
+ targetBottomSheet()
+ setKeyboardObserver() // ํค๋ณด๋ ํ์ฑํ์ ๋ฐ๋ฅธ bottomSheet ๋์ด ์กฐ์ ์ํด
+ }
+
+ override func viewWillAppear(_ animated: Bool) {
+ initView()
+ }
+
+}
+
+private extension LoginViewController_TVING {
+
+ // MARK: - objc func
+
+ @objc
+ func tappedLogInBtn() {
+ saveUserEmail()
+ let welcomViewController = WelcomeViewController()
+ welcomViewController.idDataBind(idOrNick: getNickNameOrId())
+ self.navigationController?.pushViewController(welcomViewController, animated: true)
+ }
+
+ @objc
+ func tappedMakeNickNameBtn() {
+ showBottomSheet()
+ }
+
+ @objc
+ func tappedSavedNickNameBtn() {
+ saveUserNickName()
+ hideBottomSheet()
+ }
+
+ @objc func TappedBottomSheetBackground(_ tapRecognizer: UITapGestureRecognizer) {
+ hideBottomSheet()
+ }
+
+ @objc func keyboardWillShow(notification: NSNotification) {
+ guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - (nickNameBottomSheet.bottomSheetHeight + keyboardSize.height)
+ }
+ }
+
+ @objc func keyboardWillHide(notification: NSNotification) {
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - nickNameBottomSheet.bottomSheetHeight
+ }
+ }
+
+ // MARK: - custom func
+
+ func saveUserEmail(){
+ guard let id = mainView.idTextField.text else { return }
+ user.id = id
+ }
+
+ func saveUserNickName() {
+ guard let nickName = nickNameBottomSheet.nickNameTextField.text else { return }
+ user.nickName = nickName
+ }
+
+ func getNickNameOrId() -> String {
+ if let nickName = user.nickName {
+ return nickName
+ } else {
+ guard let id = user.id else { return "" }
+ return id
+ }
+ }
+
+ func isIDValid() -> Bool {
+ guard let id = mainView.idTextField.text else { return false }
+ return id.isValidEmail()
+ }
+
+ func showBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = true
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.edges.equalToSuperview()
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)
+ })
+ }
+
+ func hideBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = false
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height)
+ }
+ )
+ }
+
+ func initView() {
+ mainView.idTextField.text = nil
+ mainView.passwordTextField.text = nil
+ nickNameBottomSheet.nickNameTextField.text = nil
+ user = TvingUserInfo()
+ mainView.logInBtn.enableDisableButtonSet(isEnable: false, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ func setKeyboardObserver() {
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
+ }
+
+}
+
+// MARK: - TextFieldDelegate
+
+extension LoginViewController_TVING: UITextFieldDelegate {
+
+ // textField๊ฐ ํ์ฑํ๋๋ฉด
+ func textFieldDidBeginEditing(_ textField: UITextField) {
+ textField.layer.borderColor = UIColor.tvingGray2.cgColor
+ textField.layer.borderWidth = 0.7
+ }
+
+ // textField ๋นํ์ฑํ๋๋ฉด
+ func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
+ textField.layer.borderWidth = 0
+
+ idValidPrint()
+
+ return true
+ }
+
+ @objc
+ private func textFieldDidChange(_ textField: UITextField) {
+
+ if let nickNameText = nickNameBottomSheet.nickNameTextField.text {
+ if (!nickNameText.isValidNickName()) {
+ nickNameBottomSheet.nickNameTextField.text = nil
+ }
+ }
+
+ let idPwIsFull: Bool = mainView.idTextField.hasText && !mainView.passwordTextField.hasText
+ let saveIdPwBtnEnable: Bool = idPwIsFull && isIDValid()
+
+ if (saveIdPwBtnEnable) {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ let saveNickNameBtnEnable = !nickNameBottomSheet.nickNameTextField.hasText
+
+ if (saveNickNameBtnEnable) {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+ }
+
+ private func idValidPrint() {
+
+ if (!(mainView.idTextField.text?.isValidEmail() ?? false) && !mainView.idTextField.hasText) {
+ mainView.idTextField.layer.borderColor = UIColor.tvingRed.cgColor
+ mainView.idTextField.layer.borderWidth = 0.7
+
+ mainView.idInvalidLabel.isHidden = false
+ mainView.idInvalidLabel.snp.remakeConstraints {
+ $0.leading.equalTo(mainView.idTextField.snp.leading)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(5)
+ }
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(30)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ } else {
+ mainView.idTextField.layer.borderWidth = 0
+
+ mainView.idInvalidLabel.isHidden = true
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(7)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ }
+ }
+
+} | Swift | bottomSheet ๋๋ฌธ์ ์ด์ฉ๋ค๋ณด๋..
์ฅ๋จ์ ์ด ์๋ ๊ฒ ๊ฐ์์!!! ์ฝ๋๋ ์ง์ง ๊ฐ๊ฒฐํด์ก์ฃต |
@@ -0,0 +1,261 @@
+//
+// LoginViewController_TVING.swift
+// GO_SOPT_Seminar_Assingment
+//
+// Created by ๊น๋ค์ on 2023/04/11.
+//
+
+import UIKit
+
+import SnapKit
+import Then
+
+struct TvingUserInfo {
+ var id: String?
+ var password: String?
+ var nickName: String?
+}
+
+final class LoginViewController_TVING: BaseViewController {
+
+ // MARK: - Property
+
+ private let mainView = LoginView()
+ private let nickNameBottomSheet = AddNickNameBottomSheetUIView()
+
+ var user = TvingUserInfo()
+
+ private var bottomSheetKeyboardEnable: Bool = false
+
+ // MARK: - Target
+
+ private func target() {
+ mainView.idTextField.delegate = self
+ mainView.passwordTextField.delegate = self
+
+ mainView.idTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+ mainView.passwordTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ mainView.logInBtn.addTarget(self, action: #selector(tappedLogInBtn), for: .touchUpInside)
+ mainView.goToMakeNicknameBtn.addTarget(self, action: #selector(tappedMakeNickNameBtn), for: .touchUpInside)
+ }
+
+ private func targetBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.delegate = self
+
+ nickNameBottomSheet.nickNameTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ nickNameBottomSheet.saveNickNameBtn.addTarget(self, action: #selector(tappedSavedNickNameBtn), for: .touchUpInside)
+
+ let bottomSheetBackgroundTap = UITapGestureRecognizer(target: self, action: #selector(TappedBottomSheetBackground))
+ nickNameBottomSheet.dimmendView.addGestureRecognizer(bottomSheetBackgroundTap)
+ }
+
+ // MARK: - Lift Cycle
+
+ override func loadView() {
+ self.view = mainView
+ view.addSubview(nickNameBottomSheet)
+
+ nickNameBottomSheet.snp.makeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ }
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+
+ target()
+ targetBottomSheet()
+ setKeyboardObserver() // ํค๋ณด๋ ํ์ฑํ์ ๋ฐ๋ฅธ bottomSheet ๋์ด ์กฐ์ ์ํด
+ }
+
+ override func viewWillAppear(_ animated: Bool) {
+ initView()
+ }
+
+}
+
+private extension LoginViewController_TVING {
+
+ // MARK: - objc func
+
+ @objc
+ func tappedLogInBtn() {
+ saveUserEmail()
+ let welcomViewController = WelcomeViewController()
+ welcomViewController.idDataBind(idOrNick: getNickNameOrId())
+ self.navigationController?.pushViewController(welcomViewController, animated: true)
+ }
+
+ @objc
+ func tappedMakeNickNameBtn() {
+ showBottomSheet()
+ }
+
+ @objc
+ func tappedSavedNickNameBtn() {
+ saveUserNickName()
+ hideBottomSheet()
+ }
+
+ @objc func TappedBottomSheetBackground(_ tapRecognizer: UITapGestureRecognizer) {
+ hideBottomSheet()
+ }
+
+ @objc func keyboardWillShow(notification: NSNotification) {
+ guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - (nickNameBottomSheet.bottomSheetHeight + keyboardSize.height)
+ }
+ }
+
+ @objc func keyboardWillHide(notification: NSNotification) {
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - nickNameBottomSheet.bottomSheetHeight
+ }
+ }
+
+ // MARK: - custom func
+
+ func saveUserEmail(){
+ guard let id = mainView.idTextField.text else { return }
+ user.id = id
+ }
+
+ func saveUserNickName() {
+ guard let nickName = nickNameBottomSheet.nickNameTextField.text else { return }
+ user.nickName = nickName
+ }
+
+ func getNickNameOrId() -> String {
+ if let nickName = user.nickName {
+ return nickName
+ } else {
+ guard let id = user.id else { return "" }
+ return id
+ }
+ }
+
+ func isIDValid() -> Bool {
+ guard let id = mainView.idTextField.text else { return false }
+ return id.isValidEmail()
+ }
+
+ func showBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = true
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.edges.equalToSuperview()
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)
+ })
+ }
+
+ func hideBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = false
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height)
+ }
+ )
+ }
+
+ func initView() {
+ mainView.idTextField.text = nil
+ mainView.passwordTextField.text = nil
+ nickNameBottomSheet.nickNameTextField.text = nil
+ user = TvingUserInfo()
+ mainView.logInBtn.enableDisableButtonSet(isEnable: false, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ func setKeyboardObserver() {
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
+ }
+
+}
+
+// MARK: - TextFieldDelegate
+
+extension LoginViewController_TVING: UITextFieldDelegate {
+
+ // textField๊ฐ ํ์ฑํ๋๋ฉด
+ func textFieldDidBeginEditing(_ textField: UITextField) {
+ textField.layer.borderColor = UIColor.tvingGray2.cgColor
+ textField.layer.borderWidth = 0.7
+ }
+
+ // textField ๋นํ์ฑํ๋๋ฉด
+ func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
+ textField.layer.borderWidth = 0
+
+ idValidPrint()
+
+ return true
+ }
+
+ @objc
+ private func textFieldDidChange(_ textField: UITextField) {
+
+ if let nickNameText = nickNameBottomSheet.nickNameTextField.text {
+ if (!nickNameText.isValidNickName()) {
+ nickNameBottomSheet.nickNameTextField.text = nil
+ }
+ }
+
+ let idPwIsFull: Bool = mainView.idTextField.hasText && !mainView.passwordTextField.hasText
+ let saveIdPwBtnEnable: Bool = idPwIsFull && isIDValid()
+
+ if (saveIdPwBtnEnable) {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ let saveNickNameBtnEnable = !nickNameBottomSheet.nickNameTextField.hasText
+
+ if (saveNickNameBtnEnable) {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+ }
+
+ private func idValidPrint() {
+
+ if (!(mainView.idTextField.text?.isValidEmail() ?? false) && !mainView.idTextField.hasText) {
+ mainView.idTextField.layer.borderColor = UIColor.tvingRed.cgColor
+ mainView.idTextField.layer.borderWidth = 0.7
+
+ mainView.idInvalidLabel.isHidden = false
+ mainView.idInvalidLabel.snp.remakeConstraints {
+ $0.leading.equalTo(mainView.idTextField.snp.leading)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(5)
+ }
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(30)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ } else {
+ mainView.idTextField.layer.borderWidth = 0
+
+ mainView.idInvalidLabel.isHidden = true
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(7)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ }
+ }
+
+} | Swift | bottomSheet๋ฅผ ์ฌ๋ฆฌ๋ ค๊ณ ์ฌ์ฉํ๋๋ฐ ์ด๊ฒ ๋ง๋์ง๋ ๋ฉ๊ฒ ใ
๋ค์..ใ
ใ
|
@@ -0,0 +1,261 @@
+//
+// LoginViewController_TVING.swift
+// GO_SOPT_Seminar_Assingment
+//
+// Created by ๊น๋ค์ on 2023/04/11.
+//
+
+import UIKit
+
+import SnapKit
+import Then
+
+struct TvingUserInfo {
+ var id: String?
+ var password: String?
+ var nickName: String?
+}
+
+final class LoginViewController_TVING: BaseViewController {
+
+ // MARK: - Property
+
+ private let mainView = LoginView()
+ private let nickNameBottomSheet = AddNickNameBottomSheetUIView()
+
+ var user = TvingUserInfo()
+
+ private var bottomSheetKeyboardEnable: Bool = false
+
+ // MARK: - Target
+
+ private func target() {
+ mainView.idTextField.delegate = self
+ mainView.passwordTextField.delegate = self
+
+ mainView.idTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+ mainView.passwordTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ mainView.logInBtn.addTarget(self, action: #selector(tappedLogInBtn), for: .touchUpInside)
+ mainView.goToMakeNicknameBtn.addTarget(self, action: #selector(tappedMakeNickNameBtn), for: .touchUpInside)
+ }
+
+ private func targetBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.delegate = self
+
+ nickNameBottomSheet.nickNameTextField.addTarget(self, action: #selector(textFieldDidChange), for: .allEditingEvents)
+
+ nickNameBottomSheet.saveNickNameBtn.addTarget(self, action: #selector(tappedSavedNickNameBtn), for: .touchUpInside)
+
+ let bottomSheetBackgroundTap = UITapGestureRecognizer(target: self, action: #selector(TappedBottomSheetBackground))
+ nickNameBottomSheet.dimmendView.addGestureRecognizer(bottomSheetBackgroundTap)
+ }
+
+ // MARK: - Lift Cycle
+
+ override func loadView() {
+ self.view = mainView
+ view.addSubview(nickNameBottomSheet)
+
+ nickNameBottomSheet.snp.makeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ }
+
+ override func viewDidLoad() {
+ super.viewDidLoad()
+
+ target()
+ targetBottomSheet()
+ setKeyboardObserver() // ํค๋ณด๋ ํ์ฑํ์ ๋ฐ๋ฅธ bottomSheet ๋์ด ์กฐ์ ์ํด
+ }
+
+ override func viewWillAppear(_ animated: Bool) {
+ initView()
+ }
+
+}
+
+private extension LoginViewController_TVING {
+
+ // MARK: - objc func
+
+ @objc
+ func tappedLogInBtn() {
+ saveUserEmail()
+ let welcomViewController = WelcomeViewController()
+ welcomViewController.idDataBind(idOrNick: getNickNameOrId())
+ self.navigationController?.pushViewController(welcomViewController, animated: true)
+ }
+
+ @objc
+ func tappedMakeNickNameBtn() {
+ showBottomSheet()
+ }
+
+ @objc
+ func tappedSavedNickNameBtn() {
+ saveUserNickName()
+ hideBottomSheet()
+ }
+
+ @objc func TappedBottomSheetBackground(_ tapRecognizer: UITapGestureRecognizer) {
+ hideBottomSheet()
+ }
+
+ @objc func keyboardWillShow(notification: NSNotification) {
+ guard let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return }
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - (nickNameBottomSheet.bottomSheetHeight + keyboardSize.height)
+ }
+ }
+
+ @objc func keyboardWillHide(notification: NSNotification) {
+ if (nickNameBottomSheet.nickNameTextField.isSelected){
+ bottomSheetKeyboardEnable = true
+ nickNameBottomSheet.bottomSheetView.frame.origin.y = UIScreen.main.bounds.height - nickNameBottomSheet.bottomSheetHeight
+ }
+ }
+
+ // MARK: - custom func
+
+ func saveUserEmail(){
+ guard let id = mainView.idTextField.text else { return }
+ user.id = id
+ }
+
+ func saveUserNickName() {
+ guard let nickName = nickNameBottomSheet.nickNameTextField.text else { return }
+ user.nickName = nickName
+ }
+
+ func getNickNameOrId() -> String {
+ if let nickName = user.nickName {
+ return nickName
+ } else {
+ guard let id = user.id else { return "" }
+ return id
+ }
+ }
+
+ func isIDValid() -> Bool {
+ guard let id = mainView.idTextField.text else { return false }
+ return id.isValidEmail()
+ }
+
+ func showBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = true
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.edges.equalToSuperview()
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: 0.0, width: self.view.frame.width, height: self.view.frame.height)
+ })
+ }
+
+ func hideBottomSheet() {
+ nickNameBottomSheet.nickNameTextField.isSelected = false
+ nickNameBottomSheet.snp.remakeConstraints {
+ $0.width.equalToSuperview()
+ $0.top.equalTo(view.snp.bottom)
+ }
+ UIView.animate(withDuration: 0.5, animations: {
+ self.nickNameBottomSheet.frame = CGRect(x: 0.0, y: self.view.frame.height, width: self.view.frame.width, height: self.view.frame.height)
+ }
+ )
+ }
+
+ func initView() {
+ mainView.idTextField.text = nil
+ mainView.passwordTextField.text = nil
+ nickNameBottomSheet.nickNameTextField.text = nil
+ user = TvingUserInfo()
+ mainView.logInBtn.enableDisableButtonSet(isEnable: false, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ func setKeyboardObserver() {
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
+ NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
+ }
+
+}
+
+// MARK: - TextFieldDelegate
+
+extension LoginViewController_TVING: UITextFieldDelegate {
+
+ // textField๊ฐ ํ์ฑํ๋๋ฉด
+ func textFieldDidBeginEditing(_ textField: UITextField) {
+ textField.layer.borderColor = UIColor.tvingGray2.cgColor
+ textField.layer.borderWidth = 0.7
+ }
+
+ // textField ๋นํ์ฑํ๋๋ฉด
+ func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
+ textField.layer.borderWidth = 0
+
+ idValidPrint()
+
+ return true
+ }
+
+ @objc
+ private func textFieldDidChange(_ textField: UITextField) {
+
+ if let nickNameText = nickNameBottomSheet.nickNameTextField.text {
+ if (!nickNameText.isValidNickName()) {
+ nickNameBottomSheet.nickNameTextField.text = nil
+ }
+ }
+
+ let idPwIsFull: Bool = mainView.idTextField.hasText && !mainView.passwordTextField.hasText
+ let saveIdPwBtnEnable: Bool = idPwIsFull && isIDValid()
+
+ if (saveIdPwBtnEnable) {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ mainView.logInBtn.enableDisableButtonSet(isEnable: saveIdPwBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+
+ let saveNickNameBtnEnable = !nickNameBottomSheet.nickNameTextField.hasText
+
+ if (saveNickNameBtnEnable) {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .tvingRed, setTextColor: .white)
+ }else {
+ nickNameBottomSheet.saveNickNameBtn.enableDisableButtonSet(isEnable: saveNickNameBtnEnable, setColor: .black, setTextColor: .tvingGray2)
+ }
+ }
+
+ private func idValidPrint() {
+
+ if (!(mainView.idTextField.text?.isValidEmail() ?? false) && !mainView.idTextField.hasText) {
+ mainView.idTextField.layer.borderColor = UIColor.tvingRed.cgColor
+ mainView.idTextField.layer.borderWidth = 0.7
+
+ mainView.idInvalidLabel.isHidden = false
+ mainView.idInvalidLabel.snp.remakeConstraints {
+ $0.leading.equalTo(mainView.idTextField.snp.leading)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(5)
+ }
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(30)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ } else {
+ mainView.idTextField.layer.borderWidth = 0
+
+ mainView.idInvalidLabel.isHidden = true
+
+ mainView.passwordTextField.snp.remakeConstraints {
+ $0.height.equalTo(52)
+ $0.top.equalTo(mainView.idTextField.snp.bottom).offset(7)
+ $0.leading.trailing.equalToSuperview().inset(20)
+ }
+ }
+ }
+
+} | Swift | ๋ด์ ๋ฆฌ๋ทฐ ๋ฐ๋ก ๋ฌ๋ฌ๊ฐ๊ฒ์ฉ~๐ |
@@ -0,0 +1,36 @@
+package rentcar.domain;
+
+public class Avante extends Car {
+
+ private static final String NAME = "Avante";
+ private static final double DISTANCE_PER_LITER = 15;
+ private static final String VALIDATE_TRIP_DISTANCE_MESSAGE = "[ERROR] ์ฌํ๊ฑฐ๋ฆฌ๋ 0 ์ด์์
๋๋ค.";
+
+ private double tripDistance;
+
+ public Avante(double tripDistance) {
+ validateTripDistance(tripDistance);
+ this.tripDistance = tripDistance;
+ }
+
+ private void validateTripDistance(double tripDistance) {
+ if (tripDistance < 0) {
+ throw new IllegalArgumentException(VALIDATE_TRIP_DISTANCE_MESSAGE);
+ }
+ }
+
+ @Override
+ double getDistancePerLiter() {
+ return DISTANCE_PER_LITER;
+ }
+
+ @Override
+ double getTripDistance() {
+ return tripDistance;
+ }
+
+ @Override
+ String getName() {
+ return NAME;
+ }
+} | Java | Car (์ถ์) ํด๋์ค์ ํ์ ๊ตฌํ์ฒด๋ค์ ๋ชจ๋ ์ด ๋ก์ง์ด ๋ฐ๋ณต๋๊ณ ์์ด์. ๐
๋ฐ๋ณต๋๋ ์ฝ๋๋ฅผ Car ํด๋์ค์ ๊ตฌํํ๋ฉด ๋ฐ๋ณต๋๋ ์ฝ๋๋ฅผ ์ค์ผ ์ ์์ ๊ฒ ๊ฐ์์. ๐ค |
@@ -0,0 +1,36 @@
+package rentcar.domain;
+
+public class Avante extends Car {
+
+ private static final String NAME = "Avante";
+ private static final double DISTANCE_PER_LITER = 15;
+ private static final String VALIDATE_TRIP_DISTANCE_MESSAGE = "[ERROR] ์ฌํ๊ฑฐ๋ฆฌ๋ 0 ์ด์์
๋๋ค.";
+
+ private double tripDistance;
+
+ public Avante(double tripDistance) {
+ validateTripDistance(tripDistance);
+ this.tripDistance = tripDistance;
+ }
+
+ private void validateTripDistance(double tripDistance) {
+ if (tripDistance < 0) {
+ throw new IllegalArgumentException(VALIDATE_TRIP_DISTANCE_MESSAGE);
+ }
+ }
+
+ @Override
+ double getDistancePerLiter() {
+ return DISTANCE_PER_LITER;
+ }
+
+ @Override
+ double getTripDistance() {
+ return tripDistance;
+ }
+
+ @Override
+ String getName() {
+ return NAME;
+ }
+} | Java | `0`์ด ์ด๋ค ์ซ์์ธ์ง ์์๋ก ์ถ์ถํ์ฌ ์๋ฏธ๋ฅผ ๋ช
ํํ๊ฒ ํด์ฃผ๋ฉด ์ข์ ๊ฒ ๊ฐ์์. ๐ค |
@@ -0,0 +1,7 @@
+package rentcar.domain;
+
+public interface Reportable {
+
+ String generateReport();
+
+} | Java | ์ธํฐํ์ด์ค ํ์ฉ ๐๐ |
@@ -0,0 +1,92 @@
+package blackjack.controller;
+
+import blackjack.domain.card.CardFactory;
+import blackjack.domain.card.Deck;
+import blackjack.domain.report.GameReports;
+import blackjack.domain.request.DrawRequest;
+import blackjack.domain.request.UserNamesRequest;
+import blackjack.domain.user.Dealer;
+import blackjack.domain.user.Player;
+import blackjack.domain.user.Players;
+import blackjack.view.InputView;
+import blackjack.view.OutputView;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class BlackJack {
+
+ private final Players players;
+ private final Deck deck;
+
+ private BlackJack(Players players, Deck deck) {
+ this.players = players;
+ this.deck = deck;
+ }
+
+ public static BlackJack init(UserNamesRequest playerNames) {
+ Dealer dealer = new Dealer();
+ Players candidates = playerNames.userNames()
+ .stream()
+ .map(Player::new)
+ .collect(Collectors.collectingAndThen(Collectors.toList(),
+ players -> new Players(dealer, players)));
+ CardFactory cardFactory = CardFactory.getInstance();
+ Deck deck = new Deck(cardFactory.createCards());
+ return new BlackJack(candidates, deck);
+ }
+
+ public void runGame() {
+ spreadStartCards();
+ if (!players.hasBlackJack()) {
+ drawPlayers();
+ drawDealer();
+ }
+ GameReports reports = getResult();
+ showResult(reports);
+ }
+
+ private void spreadStartCards() {
+ players.drawStartCards(deck);
+ OutputView.printAllPlayersCard(players);
+ }
+
+ private void drawPlayers() {
+ for (Player player : players.findOnlyPlayers()) {
+ drawEachPlayer(player);
+ }
+ }
+
+ private void drawEachPlayer(Player player) {
+ while (player.isDrawable() && getPlayerRequest(player)) {
+ player.drawCard(deck.spreadCard());
+ OutputView.printEachCardInfo(player);
+ }
+ }
+
+ private boolean getPlayerRequest(Player player) {
+ String drawRequest = InputView.getDrawRequest(player);
+ return DrawRequest.valueOf(drawRequest)
+ .isDrawable();
+ }
+
+ private void drawDealer() {
+ Dealer dealer = players.findDealer();
+ while (dealer.isDrawable()) {
+ dealer.drawCard(deck.spreadCard());
+ OutputView.printDealerGetCard();
+ }
+ }
+
+ private GameReports getResult() {
+ Dealer dealer = players.findDealer();
+ List<Player> candidates = players.findOnlyPlayers();
+ return candidates.stream()
+ .map(dealer::createReport)
+ .collect(Collectors.collectingAndThen(Collectors.toList(), GameReports::new));
+ }
+
+ private void showResult(GameReports reports) {
+ OutputView.printResultStatus(players.all());
+ OutputView.printReports(reports);
+ }
+} | Java | `collectingAndThen` ํจ์๋ ์ ๋ ๋ชฐ๋๋๋ฐ, ์ ์ฉํ ๊ฒ ๊ฐ์์! ๐คญ๐ |
@@ -0,0 +1,92 @@
+package blackjack.controller;
+
+import blackjack.domain.card.CardFactory;
+import blackjack.domain.card.Deck;
+import blackjack.domain.report.GameReports;
+import blackjack.domain.request.DrawRequest;
+import blackjack.domain.request.UserNamesRequest;
+import blackjack.domain.user.Dealer;
+import blackjack.domain.user.Player;
+import blackjack.domain.user.Players;
+import blackjack.view.InputView;
+import blackjack.view.OutputView;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class BlackJack {
+
+ private final Players players;
+ private final Deck deck;
+
+ private BlackJack(Players players, Deck deck) {
+ this.players = players;
+ this.deck = deck;
+ }
+
+ public static BlackJack init(UserNamesRequest playerNames) {
+ Dealer dealer = new Dealer();
+ Players candidates = playerNames.userNames()
+ .stream()
+ .map(Player::new)
+ .collect(Collectors.collectingAndThen(Collectors.toList(),
+ players -> new Players(dealer, players)));
+ CardFactory cardFactory = CardFactory.getInstance();
+ Deck deck = new Deck(cardFactory.createCards());
+ return new BlackJack(candidates, deck);
+ }
+
+ public void runGame() {
+ spreadStartCards();
+ if (!players.hasBlackJack()) {
+ drawPlayers();
+ drawDealer();
+ }
+ GameReports reports = getResult();
+ showResult(reports);
+ }
+
+ private void spreadStartCards() {
+ players.drawStartCards(deck);
+ OutputView.printAllPlayersCard(players);
+ }
+
+ private void drawPlayers() {
+ for (Player player : players.findOnlyPlayers()) {
+ drawEachPlayer(player);
+ }
+ }
+
+ private void drawEachPlayer(Player player) {
+ while (player.isDrawable() && getPlayerRequest(player)) {
+ player.drawCard(deck.spreadCard());
+ OutputView.printEachCardInfo(player);
+ }
+ }
+
+ private boolean getPlayerRequest(Player player) {
+ String drawRequest = InputView.getDrawRequest(player);
+ return DrawRequest.valueOf(drawRequest)
+ .isDrawable();
+ }
+
+ private void drawDealer() {
+ Dealer dealer = players.findDealer();
+ while (dealer.isDrawable()) {
+ dealer.drawCard(deck.spreadCard());
+ OutputView.printDealerGetCard();
+ }
+ }
+
+ private GameReports getResult() {
+ Dealer dealer = players.findDealer();
+ List<Player> candidates = players.findOnlyPlayers();
+ return candidates.stream()
+ .map(dealer::createReport)
+ .collect(Collectors.collectingAndThen(Collectors.toList(), GameReports::new));
+ }
+
+ private void showResult(GameReports reports) {
+ OutputView.printResultStatus(players.all());
+ OutputView.printReports(reports);
+ }
+} | Java | `BlackJack` ํด๋์ค๋ Controller์ ์ฉ๋๋ก ์ฌ์ฉํ๋ ค๋ ๊ฒ ๊ฐ์ผ๋ ๋น์ฆ๋์ค ๋ก์ง์ด ์๋น์ ์กด์ฌํ๋ ๊ฒ ๊ฐ์์. ๐ค
Controller๋ ์ํ ๊ฐ์ ๊ฐ์ง์ง ์์์ผํด์. ๐
`BlackJackController` ํด๋์ค์ ๋น์ฆ๋์ค ๋ชจ๋ธ์ธ `BlackJack` ํด๋์ค๋ก ๋ถ๋ฆฌํด๋ณผ ์ ์์ ๊ฒ ๊ฐ์์. ๐ค |
@@ -0,0 +1,34 @@
+package blackjack.domain.card;
+
+public enum Number {
+
+ ACE(1, "A"),
+ TWO(2, "2"),
+ THREE(3, "3"),
+ FOUR(4, "4"),
+ FIVE(5, "5"),
+ SIX(6, "6"),
+ SEVEN(7, "7"),
+ EIGHT(8, "8"),
+ NINE(9, "9"),
+ TEN(10, "10"),
+ JACK(10, "J"),
+ QUEEN(10, "Q"),
+ KING(10, "K");
+
+ private final int score;
+ private final String message;
+
+ Number(int score, String message) {
+ this.score = score;
+ this.message = message;
+ }
+
+ public int score() {
+ return score;
+ }
+
+ public String message() {
+ return message;
+ }
+} | Java | enum ํด๋์ค๋ช
์ด `ACE, JACK, QUEEN, KING`์ ํฌํจํ๋ ์๋ฏธ๋ผ๊ณ ๋ณผ ์ ์์๊น์? ๐ค
`Type` or `CardType`๊ณผ ๊ฐ์ ๋ค์ด๋ฐ์ ์ด๋จ๊น์? ๐ |
@@ -0,0 +1,34 @@
+package blackjack.domain.card;
+
+public enum Number {
+
+ ACE(1, "A"),
+ TWO(2, "2"),
+ THREE(3, "3"),
+ FOUR(4, "4"),
+ FIVE(5, "5"),
+ SIX(6, "6"),
+ SEVEN(7, "7"),
+ EIGHT(8, "8"),
+ NINE(9, "9"),
+ TEN(10, "10"),
+ JACK(10, "J"),
+ QUEEN(10, "Q"),
+ KING(10, "K");
+
+ private final int score;
+ private final String message;
+
+ Number(int score, String message) {
+ this.score = score;
+ this.message = message;
+ }
+
+ public int score() {
+ return score;
+ }
+
+ public String message() {
+ return message;
+ }
+} | Java | ๊ฐ์ฒด์ ์ํ ๊ฐ์ ์ธ๋ถ๋ก ํธ์ถํ๋ ๋ฉ์๋๋ `getter` ๋ฉ์๋๋ฅผ ์ ์ํด์ฃผ์ธ์. ๐
getter, setter์ ๊ฐ์ ๊ด์ต์ ์ธ ๋ฉ์๋ ์์ฑ์ด ๋ฏ์ค๊ฑฐ๋ ์ด๋ ต๋ค๋ฉด `โ + N` ๋จ์ถํค๋ฅผ ํตํด์ ์ด๋ฐ ๋ฉ์๋๋ค์ ์ฝ๊ฒ ์์ฑํ ์ ์์ด์. ๐ค |
@@ -0,0 +1,35 @@
+package blackjack.domain.card;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class CardFactory {
+
+ public static CardFactory INSTANCE;
+
+ private CardFactory() {
+ }
+
+ public static CardFactory getInstance() {
+ if (INSTANCE == null) {
+ INSTANCE = new CardFactory();
+ }
+ return INSTANCE;
+ }
+
+ public List<Card> createCards() {
+ List<Card> cards = new ArrayList<>();
+ for (Suit suit : Suit.values()) {
+ createBySuit(cards, suit);
+ }
+ Collections.shuffle(cards);
+ return cards;
+ }
+
+ private void createBySuit(List<Card> cards, Suit suit) {
+ for (Number number : Number.values()) {
+ cards.add(new Card(suit, number));
+ }
+ }
+} | Java | Deck์ผ๋ก ์ฐ์ด๋ ์นด๋๋ค์ ์นด๋์ ํํ์ ๊ฐฏ์๋ค์ด ์ ํด์ ธ์๊ณ ์ฌ์ฌ์ฉํ ํ๋ฅ ์ด ๋๋ค๊ณ ์๊ฐํด์. ๐
๋ฏธ๋ฆฌ ๋ง๋ค์ด๋๋ค๊ฐ ํ์ํ ์์ ์ ์ฌ์ฌ์ฉํด๋ณด๋ฉด ์ด๋จ๊น์? ๐ค
[์ฐธ๊ณ ์๋ฃ](https://tecoble.techcourse.co.kr/post/2020-06-24-caching-instance/) |
@@ -0,0 +1,22 @@
+package blackjack.domain.card;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+
+public class Deck {
+
+ private final Queue<Card> deck;
+
+ public Deck(List<Card> cards) {
+ this.deck = new LinkedList<>(cards);
+ }
+
+ public Card spreadCard() {
+ return deck.poll();
+ }
+
+ public int remainCardSize() {
+ return deck.size();
+ }
+} | Java | ์ ๋ Stack์ด ์กฐ๊ธ ๋ ์นด๋ ๊ฒ์์ ์ด์ธ๋ฆฐ๋ค๊ณ ์๊ฐํ๋๋ฐ, Queue๋ ์ข์ ์ ๊ทผ์ธ ๊ฒ ๊ฐ์์. ๐ค
`spreadCard` ๋ฉ์๋์์๋ deck์ด ๋น์ด์๋ ๊ฒฝ์ฐ์ ๋ํ ์์ธ ์ฒ๋ฆฌ๋ ํ ์ ์์ ๊ฒ ๊ฐ์์. ๐ |
@@ -0,0 +1,42 @@
+package blackjack.domain.card;
+
+import blackjack.domain.score.ScoreCalculator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class CardBundle {
+
+ private static final int BLACK_JACK_SCORE = 21;
+
+ private final List<Card> cards;
+
+ private CardBundle() {
+ this.cards = new ArrayList<>();
+ }
+
+ public static CardBundle emptyBundle() {
+ return new CardBundle();
+ }
+
+ public void addCard(Card card) {
+ cards.add(card);
+ }
+
+ public int calculateScore() {
+ return ScoreCalculator.findByCards(cards)
+ .calculateScore(cards);
+ }
+
+ public List<Card> getCards() {
+ return Collections.unmodifiableList(cards);
+ }
+
+ public boolean isBurst() {
+ return calculateScore() > BLACK_JACK_SCORE;
+ }
+
+ public boolean isBlackJack() {
+ return calculateScore() == BLACK_JACK_SCORE;
+ }
+} | Java | CardBundle.`empty()` ๋ก๋ ์ถฉ๋ถํ ์๋ฏธ๊ฐ ์ ๋ฌ๋๋ ๊ฒ ๊ฐ์์. ๐ |
@@ -0,0 +1,42 @@
+package blackjack.domain.card;
+
+import blackjack.domain.score.ScoreCalculator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class CardBundle {
+
+ private static final int BLACK_JACK_SCORE = 21;
+
+ private final List<Card> cards;
+
+ private CardBundle() {
+ this.cards = new ArrayList<>();
+ }
+
+ public static CardBundle emptyBundle() {
+ return new CardBundle();
+ }
+
+ public void addCard(Card card) {
+ cards.add(card);
+ }
+
+ public int calculateScore() {
+ return ScoreCalculator.findByCards(cards)
+ .calculateScore(cards);
+ }
+
+ public List<Card> getCards() {
+ return Collections.unmodifiableList(cards);
+ }
+
+ public boolean isBurst() {
+ return calculateScore() > BLACK_JACK_SCORE;
+ }
+
+ public boolean isBlackJack() {
+ return calculateScore() == BLACK_JACK_SCORE;
+ }
+} | Java | `CardBundle` ๊ฐ์ฒด๋ **์นด๋์ ์ ์๋ฅผ ํฉ์ฐํ๋ ์ญํ **์ ์ํํ๋ค๊ณ ์๊ฐํด์. ๐ค
`ScoreCalculator` enum class ๋ด๋ถ ์ฝ๋๋ ๋งค์ฐ ํฅ๋ฏธ๋กญ๊ฒ ๋ดค์ด์. ๐ ๐
ํ์ง๋ง `AceScoreStrategy`, `DefaultScoreStrategy`์ ๋ด๋ถ ์ฝ๋๋ฅผ ๋ณด์์ ๋, ์ด๋ฐ ์์ผ๋ก (์นด๋ ์ ์ ํฉ์ฐ) ์ ๋ต์ ๊ตฌ๋ถํ์ฌ ๊ตฌํํ๋ ๊ฒ๋ณด๋ค ์นด๋ ์ ์ ํฉ์ฐ ๋ก์ง์ด ํ์ํ `CardBundle ๊ฐ์ฒด ๋ด๋ถ`์ ์ง์ ๊ตฌํํ๋ ๊ฒ์ด ๋ ์์ง์ฑ์ด ๋๋ค๊ณ ์๊ฐํด์. ๐
์ง๊ธ์ ์ฝ๋์์ `์ ์๋ฅผ ํฉ์ฐ`ํ๋ ๊ธฐ๋ฅ์ ํ์
ํ๊ธฐ๊ฐ ์ด๋ ค์ด ๊ฒ ๊ฐ์์. ๐ค (์๋์ ๊ฐ์ด ์ฝ๋๋ฅผ ํ์ํด์ผ ํด์. ๐ฑ)
`calculateScore()` -> `ScoreCalculator#findByCards(cards)` -> `ScoreCalculator#isSupportable(cards)` -> `ScoreCalculator#calculateScore(cards)` -> (AceScoreStrategy & DefaultScoreStrategy)`scoreStrategy.calculateScore(cards)`
๋ํ `ScoreCalculator`์ ํจ์๋ฅผ ํธ์ถํ ๋๋ง๋ค, ๊ณ์ํด์ ๋ฉ์๋ ํ๋ผ๋ฏธํฐ(= ์ธ๋ถ)๋ก cards๋ฅผ ์ ๋ฌํ๊ณ ์์ด์. ๐
์ด๋ฐ ๋ถ๋ถ์์ `์นด๋ ์ ์๋ฅผ ํฉ์ฐ`ํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ฒด์ ์ญํ ์ด ์๋๋ผ CardBundle์ ์ญํ ์ด๋ผ๊ณ ์๊ฐ๋ผ์. ๐ค |
@@ -0,0 +1,51 @@
+package blackjack.domain.report;
+
+import java.util.Objects;
+
+public class GameReport {
+
+ private final String name;
+ private final GameResult result;
+
+ public GameReport(String name, GameResult result) {
+ this.name = name;
+ this.result = result;
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public String message() {
+ return result.message();
+ }
+
+ public boolean isWin() {
+ return result == GameResult.WIN;
+ }
+
+ public boolean isDraw() {
+ return result == GameResult.DRAW;
+ }
+
+ public boolean isLose() {
+ return result == GameResult.LOSE;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ GameReport report = (GameReport) o;
+ return Objects.equals(name, report.name) && result == report.result;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, result);
+ }
+} | Java | GameReport ๊ฐ์ฒด์ `๋๋ฑ์ฑ`์ ์ ์ํ ํ์๊ฐ ์๋์? ๐ค
ํน์ ํ์ํ ์์น๊ฐ ์์๊น์? ๐ (์ ๊ฐ ๋ชป ์ฐพ๋ ๊ฑธ๊น์? ๐) |
@@ -0,0 +1,52 @@
+package blackjack.domain.report;
+
+import blackjack.domain.card.CardBundle;
+import java.util.Arrays;
+
+public enum GameResult {
+
+ WIN(1, "์น"),
+ DRAW(0, "๋ฌด"),
+ LOSE(-1, "ํจ");
+
+ private final int result;
+ private final String message;
+
+ GameResult(int result, String message) {
+ this.result = result;
+ this.message = message;
+ }
+
+ public static GameResult comparing(CardBundle playerCardBundle, CardBundle dealerCardBundle) {
+ isComparable(playerCardBundle, dealerCardBundle);
+ if (playerCardBundle.isBurst()) {
+ return GameResult.LOSE;
+ }
+ if (dealerCardBundle.isBurst()) {
+ return GameResult.WIN;
+ }
+ int result = Integer.compare(playerCardBundle.calculateScore(),
+ dealerCardBundle.calculateScore());
+ return findResult(result);
+ }
+
+ public String message() {
+ return message;
+ }
+
+ private static void isComparable(CardBundle dealerCardBundle, CardBundle playerCardBundle) {
+ if (dealerCardBundle == null) {
+ throw new IllegalArgumentException("[ERROR] ๋๋ฌ์ ์นด๋๊ฐ ๋น์์ต๋๋ค");
+ }
+ if (playerCardBundle == null) {
+ throw new IllegalArgumentException("[ERROR] ํ๋ ์ด์ด์ ์นด๋๊ฐ ๋น์์ต๋๋ค.");
+ }
+ }
+
+ private static GameResult findResult(int result) {
+ return Arrays.stream(values())
+ .filter(gameResult -> gameResult.result == result)
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException("[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ ์
๋๋ค"));
+ }
+} | Java | getter (getter ์ญํ ์ ํ๋ ๋ฉ์๋), setter, compareTo, toString ๋ฑ์ ๋ฉ์๋๋ ํด๋์ค์ ์ตํ๋จ์ ์์น์์ผ์ฃผ์ธ์! |
@@ -0,0 +1,52 @@
+package blackjack.domain.report;
+
+import blackjack.domain.card.CardBundle;
+import java.util.Arrays;
+
+public enum GameResult {
+
+ WIN(1, "์น"),
+ DRAW(0, "๋ฌด"),
+ LOSE(-1, "ํจ");
+
+ private final int result;
+ private final String message;
+
+ GameResult(int result, String message) {
+ this.result = result;
+ this.message = message;
+ }
+
+ public static GameResult comparing(CardBundle playerCardBundle, CardBundle dealerCardBundle) {
+ isComparable(playerCardBundle, dealerCardBundle);
+ if (playerCardBundle.isBurst()) {
+ return GameResult.LOSE;
+ }
+ if (dealerCardBundle.isBurst()) {
+ return GameResult.WIN;
+ }
+ int result = Integer.compare(playerCardBundle.calculateScore(),
+ dealerCardBundle.calculateScore());
+ return findResult(result);
+ }
+
+ public String message() {
+ return message;
+ }
+
+ private static void isComparable(CardBundle dealerCardBundle, CardBundle playerCardBundle) {
+ if (dealerCardBundle == null) {
+ throw new IllegalArgumentException("[ERROR] ๋๋ฌ์ ์นด๋๊ฐ ๋น์์ต๋๋ค");
+ }
+ if (playerCardBundle == null) {
+ throw new IllegalArgumentException("[ERROR] ํ๋ ์ด์ด์ ์นด๋๊ฐ ๋น์์ต๋๋ค.");
+ }
+ }
+
+ private static GameResult findResult(int result) {
+ return Arrays.stream(values())
+ .filter(gameResult -> gameResult.result == result)
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException("[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฐ ์
๋๋ค"));
+ }
+} | Java | ๋ CardBundle (์ฌ์ค์ ๋ ๋ฒ์งธ, ํ๋ผ๋ฏธํฐ๋ก ๋ฐ๋ CardBundle์ Dealer์ CardBundle๋ก ๊ณ ์ ๋๋ ๊ฒ ๊ฐ์์. ๐)์ ๋ฐ์์ `๊ฒ์ ๊ฒฐ๊ณผ`๋ฅผ ๋ฐํํ๋ ์ญํ ์ `Dealer`์ ์ญํ ์ด๋ผ๊ณ ๋ณผ ์ ์์ง ์์๊น์? ๐ค (๊ทธ๋ฌ๋ฉด ์ฒซ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ์ ๋ ๋ฒ์งธ ํ๋ผ๋ฏธํฐ์ ์์๋ฅผ ์๋ชป ๋์
ํ๋ค๊ฑฐ๋ ํ๋ ์ค์๋ฅผ ๋ง์ ์๋ ์์ ๊ฒ ๊ฐ์์. ๐) |
@@ -0,0 +1,45 @@
+package blackjack.domain.request;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class UserNamesRequest {
+
+ private static final String USER_NAME_DELIMITER = ",";
+
+ private final List<String> userNames;
+
+ private UserNamesRequest(List<String> userNames) {
+ this.userNames = userNames;
+ }
+
+ public static UserNamesRequest from(String userNames) {
+ validateUserNames(userNames);
+ List<String> splitUserNames = Arrays.stream(userNames.split(USER_NAME_DELIMITER))
+ .map(String::trim)
+ .collect(Collectors.toList());
+ validateDuplicate(splitUserNames);
+ return new UserNamesRequest(splitUserNames);
+ }
+
+ public List<String> userNames() {
+ return Collections.unmodifiableList(userNames);
+ }
+
+ private static void validateUserNames(String userNames) {
+ if (userNames == null || userNames.trim().isEmpty()) {
+ throw new IllegalArgumentException("[ERROR] ์ด๋ฆ์ ์
๋ ฅํด ์ฃผ์ธ์");
+ }
+ }
+
+ private static void validateDuplicate(List<String> splitUserNames) {
+ Set<String> removeDuplicate = new HashSet<>(splitUserNames);
+ if (removeDuplicate.size() != splitUserNames.size()) {
+ throw new IllegalArgumentException("[ERROR] ํ๋ ์ด์ด ์ด๋ฆ์ ์ค๋ณต๋ ์ ์์ต๋๋ค");
+ }
+ }
+} | Java | ์ด ์ ํจ์ฑ ๊ฒ์ฆ ๋ก์ง์ ๋ธ๋์ญ ๋๋ฉ์ธ ๋ชจ๋ธ์ ๊ผญ ํ์ํ ์ ํจ์ฑ ๊ฒ์ฆ์ธ ๊ฒ ๊ฐ์์. ๐
๋จผ์ Player์ ์ํ ๊ฐ์ธ ์์ ๊ฐ name์ ํฌ์ฅํด๋ณด๋ฉด ์ด๋จ๊น์? ๐ค |
@@ -0,0 +1,55 @@
+package blackjack.domain.user;
+
+import blackjack.domain.card.Card;
+import blackjack.domain.card.CardBundle;
+import java.util.List;
+
+public class Player {
+
+ protected final CardBundle cardBundle;
+ private final String name;
+
+ public Player(String name) {
+ validateName(name);
+ this.name = name;
+ this.cardBundle = CardBundle.emptyBundle();
+ }
+
+ private void validateName(String name) {
+ if (name == null || name.trim().length() == 0) {
+ throw new IllegalArgumentException("[ERROR] ์ฌ๋ฐ๋ฅด์ง ์์ ์ด๋ฆ์ ์
๋ ฅ ๊ฐ์
๋๋ค.");
+ }
+ }
+
+ public void drawCard(Card card) {
+ this.cardBundle.addCard(card);
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public boolean isPlayer() {
+ return true;
+ }
+
+ public boolean isDealer() {
+ return false;
+ }
+
+ public int score() {
+ return cardBundle.calculateScore();
+ }
+
+ public boolean isDrawable() {
+ return !cardBundle.isBlackJack() && !cardBundle.isBurst();
+ }
+
+ public List<Card> getCardBundle() {
+ return cardBundle.getCards();
+ }
+
+ public boolean isBlackJack() {
+ return cardBundle.isBlackJack();
+ }
+} | Java | String์ `isEmpty` ํจ์๋ฅผ ๋์ ์ฌ์ฉํด๋ณด๋ฉด ์ด๋จ๊น์? ๐
```suggestion
if (name == null || name.trim().isEmpty()) {
``` |
@@ -0,0 +1,33 @@
+package blackjack.domain.user;
+
+import blackjack.domain.report.GameResult;
+import blackjack.domain.report.GameReport;
+
+public class Dealer extends Player {
+
+ private static final int DEALER_MUST_DRAW_SCORE = 16;
+
+ public Dealer() {
+ super("๋๋ฌ");
+ }
+
+ public GameReport createReport(Player player) {
+ GameResult gameResult = GameResult.comparing(player.cardBundle, this.cardBundle);
+ return new GameReport(player.name(), gameResult);
+ }
+
+ @Override
+ public boolean isPlayer() {
+ return false;
+ }
+
+ @Override
+ public boolean isDealer() {
+ return true;
+ }
+
+ @Override
+ public boolean isDrawable() {
+ return score() <= DEALER_MUST_DRAW_SCORE;
+ }
+} | Java | Dealer๊ฐ ์จ์ ํ Player ํด๋์ค๋ฅผ ๋ฐ๋ก ์์๋ฐ์๊ธฐ ๋๋ฌธ์ ์ด๋ค ๋ฉ์๋๋ฅผ ์ฌ์ ์ํด์ผ ํ๋์ง ํ์
ํ๊ธฐ ์ด๋ ค์ด ๊ฒ ๊ฐ์์. ๐ค
๊ณตํต์ผ๋ก ์ฌ์ฉํ๋ ๋ฉ์๋๋ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๊ฒ ํ๋, ํ์ ๊ตฌํ์ฒด์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ ๊ธฐ๋ฅ๋ค์ `์ถ์ ๋ฉ์๋`๋ก ์ ์ํด์ ํ์ ๊ตฌํ์ฒด์์๋ ์ฌ์ ์๋ฅผ ๊ฐ์ ํ๋๋ก ํ๋ฉด ์ข์ง ์์๊น์? ๐ |
@@ -0,0 +1,33 @@
+package blackjack.domain.user;
+
+import blackjack.domain.report.GameResult;
+import blackjack.domain.report.GameReport;
+
+public class Dealer extends Player {
+
+ private static final int DEALER_MUST_DRAW_SCORE = 16;
+
+ public Dealer() {
+ super("๋๋ฌ");
+ }
+
+ public GameReport createReport(Player player) {
+ GameResult gameResult = GameResult.comparing(player.cardBundle, this.cardBundle);
+ return new GameReport(player.name(), gameResult);
+ }
+
+ @Override
+ public boolean isPlayer() {
+ return false;
+ }
+
+ @Override
+ public boolean isDealer() {
+ return true;
+ }
+
+ @Override
+ public boolean isDrawable() {
+ return score() <= DEALER_MUST_DRAW_SCORE;
+ }
+} | Java | GameReport ๊ฐ์ฒด์ ์ํ ๊ฐ์ผ๋ก Player์ ์ด๋ฆ์ ๋ฐ๋๊ฒ ์๋๋ผ `Player`๋ฅผ ๊ฐ์ง๋ฉด ์ด๋จ๊น์? ๐ค |
@@ -0,0 +1,82 @@
+package blackjack.view;
+
+import static blackjack.domain.user.Players.START_CARD_INIT_SIZE;
+
+import blackjack.domain.report.GameReports;
+import blackjack.domain.card.Card;
+import blackjack.domain.user.Player;
+import blackjack.domain.user.Players;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private OutputView() {
+ }
+
+ public static void printAllPlayersCard(Players players) {
+ List<Player> candiates = players.findOnlyPlayers();
+ Player dealer = players.findDealer();
+ System.out.printf("\n๋๋ฌ์ %s์๊ฒ %d์ฅ ๋๋์์ต๋๋ค.\n", collectPlayerNames(candiates),
+ START_CARD_INIT_SIZE);
+ System.out.printf("%s : %s\n", dealer.name(), collectDealerCard(dealer));
+ candiates.forEach(OutputView::printEachCardInfo);
+ System.out.println();
+ }
+
+ public static void printEachCardInfo(Player player) {
+ System.out.printf("%s : %s\n", player.name(), collectPlayerCard(player));
+ }
+
+ public static void printDealerGetCard() {
+ System.out.println("\n๋๋ฌ๋ 16์ดํ๋ผ ํ์ฅ์ ์นด๋๋ฅผ ๋ ๋ฐ์์ต๋๋ค.");
+ }
+
+ public static void printResultStatus(List<Player> players) {
+ System.out.println();
+ players.forEach(OutputView::showEachResult);
+ }
+
+ public static void printReports(GameReports reports) {
+ System.out.println("\n## ์ต์ข
์นํจ");
+ showDealerReports(reports);
+ reports.reports()
+ .stream()
+ .map(report -> String.format("%s: %s", report.name(), report.message()))
+ .forEach(System.out::println);
+ }
+
+ private static void showDealerReports(GameReports reports) {
+ int dealerWinCount = reports.getPlayerLoseCount();
+ int drawCount = reports.getPlayerDrawCount();
+ int dealerLoseCount = reports.getPlayerWinCount();
+ System.out.printf("๋๋ฌ: %d์น %d๋ฌด %dํจ\n", dealerWinCount, drawCount, dealerLoseCount);
+ }
+
+ private static String collectPlayerNames(List<Player> candiates) {
+ return candiates.stream()
+ .map(Player::name)
+ .collect(Collectors.joining(","));
+ }
+
+ private static String collectDealerCard(Player dealer) {
+ List<Card> cards = dealer.getCardBundle();
+ return makeCardInfo(cards.get(0));
+ }
+
+ private static String makeCardInfo(Card card) {
+ return String.join("", card.message(), card.suit());
+ }
+
+ private static String collectPlayerCard(Player player) {
+ List<Card> cards = player.getCardBundle();
+ return cards.stream()
+ .map(OutputView::makeCardInfo)
+ .collect(Collectors.joining(", "));
+ }
+
+ private static void showEachResult(Player player) {
+ System.out.printf("%s์นด๋: %s - ๊ฒฐ๊ณผ : %d\n", player.name(), collectPlayerCard(player),
+ player.score());
+ }
+} | Java | `findOnlyPlayers`์ `findDealer`๋ ๋น์ฆ๋์ค ๋ก์ง์ด๊ธฐ ๋๋ฌธ์ Model์ ์์ญ์ด๋ผ๊ณ ๋ณผ ์ ์์ด์. ๐
Controller์์ ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ค, ๋ฉ์๋ ํ๋ผ๋ฏธํฐ๋ก ๊ทธ ๊ฒฐ๊ณผ ๊ฐ๋ค์ ์ ๋ฌ๋ฐ๋๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์! ๐ |
@@ -0,0 +1,34 @@
+package blackjack.domain.card;
+
+public enum Number {
+
+ ACE(1, "A"),
+ TWO(2, "2"),
+ THREE(3, "3"),
+ FOUR(4, "4"),
+ FIVE(5, "5"),
+ SIX(6, "6"),
+ SEVEN(7, "7"),
+ EIGHT(8, "8"),
+ NINE(9, "9"),
+ TEN(10, "10"),
+ JACK(10, "J"),
+ QUEEN(10, "Q"),
+ KING(10, "K");
+
+ private final int score;
+ private final String message;
+
+ Number(int score, String message) {
+ this.score = score;
+ this.message = message;
+ }
+
+ public int score() {
+ return score;
+ }
+
+ public String message() {
+ return message;
+ }
+} | Java | ์ ๋ `getXXX`๊ณผ ๊ฐ์ ๋ค์ด๋ฐ์ด ์คํ๋ ค ๋ด๋ถ ์์ฑ์ ๋
ธ์ถํ๋ค๊ณ ์๊ฐํด์
`์๋ฏธ์๋ ๋ค์ด๋ฐ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋๊ฒ ๋ ๋ซ์ง ์์๊น?` ๋ผ๋ ์๊ฐ์ผ๋ก ๋ฉ์๋์ ๋ค์ด๋ฐ์ ์์ฑํด๋ณด์๋๋ฐ
ํด๋น์ ๋ํด์๋ `getXXX`์ผ๋ก ํต์ผํ๋๊ฒ ๋ ์ข์๊น์? |
@@ -0,0 +1,42 @@
+package blackjack.domain.card;
+
+import blackjack.domain.score.ScoreCalculator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class CardBundle {
+
+ private static final int BLACK_JACK_SCORE = 21;
+
+ private final List<Card> cards;
+
+ private CardBundle() {
+ this.cards = new ArrayList<>();
+ }
+
+ public static CardBundle emptyBundle() {
+ return new CardBundle();
+ }
+
+ public void addCard(Card card) {
+ cards.add(card);
+ }
+
+ public int calculateScore() {
+ return ScoreCalculator.findByCards(cards)
+ .calculateScore(cards);
+ }
+
+ public List<Card> getCards() {
+ return Collections.unmodifiableList(cards);
+ }
+
+ public boolean isBurst() {
+ return calculateScore() > BLACK_JACK_SCORE;
+ }
+
+ public boolean isBlackJack() {
+ return calculateScore() == BLACK_JACK_SCORE;
+ }
+} | Java | `empty()`๋ ๊ด์ฐฎ์ ๋ค์ด๋ฐ์ด๋ผ๊ณ ์๊ฐํ์ง๋ง,
ํด๋น ๋ฒ๋ค์ด ๋น์ด์๋ค๋ ์๋ฏธ(`isEmpty()`)์ ์ ์ฌํ๊ฒ ํด์๋ ์๋ ์๋ค๊ณ ์๊ฐํ์ต๋๋ค. |
@@ -0,0 +1,51 @@
+package blackjack.domain.report;
+
+import java.util.Objects;
+
+public class GameReport {
+
+ private final String name;
+ private final GameResult result;
+
+ public GameReport(String name, GameResult result) {
+ this.name = name;
+ this.result = result;
+ }
+
+ public String name() {
+ return name;
+ }
+
+ public String message() {
+ return result.message();
+ }
+
+ public boolean isWin() {
+ return result == GameResult.WIN;
+ }
+
+ public boolean isDraw() {
+ return result == GameResult.DRAW;
+ }
+
+ public boolean isLose() {
+ return result == GameResult.LOSE;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ GameReport report = (GameReport) o;
+ return Objects.equals(name, report.name) && result == report.result;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, result);
+ }
+} | Java | ์ ์ฒ์์ GameReports์ `Set`์ ์๋ํ๊ณ ๊ตฌํํ๋๋ฐ ์ง๊ธ์ ๋ถํ์ํ ๋ฉ์๋๋ผ๊ณ ์๊ฐ๋์ด ์ญ์ ํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,21 @@
+package lotto.constants;
+
+public enum ErrorMessage {
+ DUPLICATION("[ERROR] ๊ฐ์ด ์ค๋ณต๋์์ต๋๋ค"),
+ ISNOTINTEGER("[ERROR] ์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"),
+ SIXNUMBER("[ERROR] ๋ฒํธ 6๊ฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"),
+ OUTFRANGE("[ERROR] ๋ก๋ ๋ฒํธ๋ 1๋ถํฐ 45 ์ฌ์ด์ ์ซ์์ฌ์ผ ํฉ๋๋ค."),
+ ONENUMBER("[ERROR] ์ซ์ ๋ฒํธ 1๊ฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"),
+ NOTTHOUSAND("[ERROR] 1000์ ๋จ์๋ก ์
๋ ฅํด ์ฃผ์ธ์");
+
+
+ private final String message;
+
+ ErrorMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | ์ค๋ณต๋๋ ๋ถ๋ถ์ ๋ํด์ ์์์ฒ๋ฆฌ ํด์ค๋ ์ข์๊ฑฐ๊ฐ์์ |
@@ -0,0 +1,50 @@
+package lotto.controller;
+
+import lotto.model.Lotto;
+import lotto.model.LottoNumberMaker;
+import lotto.model.LottoPercentageCalculation;
+import lotto.model.constants.LottoPrize;
+import lotto.view.LottoInput;
+import lotto.view.LottoOutput;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static lotto.model.constants.LottoPrize.*;
+
+public class LottoController {
+ private static final LottoNumberMaker lottoNumberMaker = new LottoNumberMaker();
+ private static final LottoInput lottoInput = new LottoInput();
+ private static final LottoOutput lottoOutput = new LottoOutput();
+ private static final LottoPercentageCalculation lottoPercentageCalculation = new LottoPercentageCalculation();
+ public static void setPrice() {
+ lottoNumberMaker.checkInt();
+ }
+
+// static List<LottoPrize> LottoPrizelist= asList(FIFTH_PRIZE,FOURTH_PRIZE,THIRD_PRIZE,SECOND_PRIZE,FIRST_PRIZE);
+ public static void setBuyLottoNumberPrint() {
+ lottoOutput.buyLottoNumberPrint(lottoNumberMaker.getLottoNumber());
+ }
+
+ public static void setPrizeNumberInput() {
+ Lotto lotto = new Lotto(lottoInput.prizeNumberInput());
+ lotto.checkSame(lottoInput.bonusNumberInput(),lottoNumberMaker.getLottoNumber());
+ }
+
+ public static void winningStatistic() {
+ List<String> lottoPrizes = new ArrayList<>();
+ for(LottoPrize lottoPrize: LottoPrize.values()){
+ lottoPrizes.add(lottoPrize.getText()+lottoPrize.getWinCount()+lottoPrize.getUnit());
+ }
+ LottoOutput.seeWinningStatstic(lottoPrizes);
+ }
+
+ public static void PerformanceCalculation() {
+ lottoOutput.seePercentage(lottoPercentageCalculation.percentageCalculation(LottoPrize.values(),lottoNumberMaker.getBuyPrice()));
+ }
+
+ public String askPrice() {
+ return lottoInput.askPrice();
+ }
+}
\ No newline at end of file | Java | ์ปจํธ๋กค๋ฌ ์์ ๋๋ถ๋ถ์ ๋ฉ์๋์ ๋ณ์๋ฅผ static ์ฒ๋ฆฌํ ๋ณ๋์ ์ด์ ๊ฐ ์๋์? |
@@ -0,0 +1,79 @@
+package lotto.model;
+
+import lotto.model.constants.LottoPrize;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import static lotto.constants.ErrorMessage.DUPLICATION;
+import static lotto.constants.ErrorMessage.SIXNUMBER;
+import static lotto.model.constants.LottoPrize.*;
+public class Lotto {
+ private final List<Integer> numbers;
+
+ public Lotto(List<Integer> numbers) {
+ validate(numbers);
+ duplicate(numbers);
+ this.numbers = numbers;
+ }
+
+ private void duplicate(List<Integer> numbers) {
+ List<Integer> duplication = numbers.stream()
+ .distinct()
+ .toList();
+ if (duplication.size() != numbers.size()) {
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+ }
+
+ private void validate(List<Integer> numbers) {
+ if (numbers.size() != 6) {
+ throw new IllegalArgumentException(SIXNUMBER.getMessage());
+ }
+ }
+ public void checkSame(Integer bonusNumber, List<List<Integer>> lottoNumber) {
+ bonusDuplication(bonusNumber);
+ for (List<Integer> integers : lottoNumber) {
+ List<Integer> Result = integers.stream()
+ .filter(i -> this.numbers.stream().anyMatch(Predicate.isEqual(i)))
+ .toList();
+ long bonusResult = integers.stream()
+ .filter(bonusNumber::equals).count();
+ CheckPrize(Result.size(), bonusResult).addWinCount();
+
+ }
+ }
+
+ private void bonusDuplication(Integer bonusNumber) {
+ List<Integer> Result = this.numbers.stream()
+ .filter(i-> !Objects.equals(i, bonusNumber))
+ .toList();
+ if(Result.size()!=6){
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+
+ }
+
+ public LottoPrize CheckPrize(int result, long bonusResult){
+ if(result==6){
+ return FIRST_PRIZE;
+ }
+ if(result==5&&bonusResult==1){
+ return SECOND_PRIZE;
+ }
+ if(result==5&&bonusResult==0){
+ return THIRD_PRIZE;
+ }
+ if(result==4){
+ return FOURTH_PRIZE;
+ }
+ if(result==3){
+ return FIFTH_PRIZE;
+ }
+ if(result<3){
+ return LOOSE;
+ }
+ throw new IllegalArgumentException("์กด์ฌ ํ์ง ์๋ ์์์
๋๋ค.");
+ }
+} | Java | bonus์ ๊ฒฝ์ฐ longํ์ ์ฌ์ฉํ ์ด์ ๊ฐ ์๋์?
๊ทธ๋ฆฌ๊ณ ํด๋น ๋ฉ์๋๊ฐ lotto ๋๋ฉ์ธ์์ ๊ผญ ์ ์ธ๋์ผ ํ๋ ์ด์ ๊ฐ ์๋์ง ๊ถ๊ธํฉ๋๋ค !!(lotto๊ฐ์ฒด๊ฐ ๊ฐ์ง๋ ๋ณ์๋ฅผ ์ฌ์ฉํ์ง ์์๊ฑฐ ๊ฐ์์์ !!) |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | Lotto๊ฐ List'<'Integer'>'ํ์ ๊ฐ์ง๋๋ฐ List'<'Lotto'>'๋ก ๋ง๋ค์๋ค๋ฉด ์ด๋จ๊น์ ?! |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ๋๋ฉ์ธ์์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฌ์ฉํ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ๋ก๋ ๊ตฌ์
๊ธ์ก๋ํ 1000์ผ๋ก ๊ณ ์ ์ํค๋ ๊ฒ ๋ณด๋ค ํ์ฅ์ฑ ์ธก๋ฉด์์ ๊ณ ๋ คํ์ฌ ์์ ์ฒ๋ฆฌํด์ ์ฌ์ฉํด๋ ์ข์๊ฑฐ๊ฐ์์ |
@@ -1,5 +1,7 @@
package lotto;
+import lotto.model.Lotto;
+import lotto.model.LottoNumberMaker;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@@ -22,6 +24,11 @@ void createLottoByDuplicatedNumber() {
assertThatThrownBy(() -> new Lotto(List.of(1, 2, 3, 4, 5, 5)))
.isInstanceOf(IllegalArgumentException.class);
}
+ @DisplayName("๋ก๋ ๋ฒํธ๊ฐ 6๊ฐ ๋ฏธ๋ง์ด๋ฉด ์์ธ๊ฐ ๋ฐ์ํ๋ค.")
+ @Test
+ void createLottoByMinSize() {
+ assertThatThrownBy(() -> new Lotto(List.of(1)))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
- // ์๋์ ์ถ๊ฐ ํ
์คํธ ์์ฑ ๊ฐ๋ฅ
}
\ No newline at end of file | Java | ๋จ์ ํ
์คํธ๋ ๋๋ฉ์ธ ๋ณ๋ก ๋ค์ํ๊ฒ ์งํํด๋ณด๋ฉด ๋ ์ข์๊ฑฐ ๊ฐ์์ :) |
@@ -1,7 +1,13 @@
package lotto;
+import lotto.controller.LottoController;
+
public class Application {
public static void main(String[] args) {
- // TODO: ํ๋ก๊ทธ๋จ ๊ตฌํ
+ LottoController.setPrice();
+ LottoController.setBuyLottoNumberPrint();
+ LottoController.setPrizeNumberInput();
+ LottoController.winningStatistic();
+ LottoController.PerformanceCalculation();
}
} | Java | ์์๋๋ก ๋ชจ๋ ์คํํด์ผํ๋ค๋ฉด controller์์์ ํ๋์ ๋ฉ์๋๋ก ์คํํ๋ฉด ์กฐ๊ธ ๋ ์ฝ๋๊ฐ ๊น๋ํด์ง ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,79 @@
+package lotto.model;
+
+import lotto.model.constants.LottoPrize;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import static lotto.constants.ErrorMessage.DUPLICATION;
+import static lotto.constants.ErrorMessage.SIXNUMBER;
+import static lotto.model.constants.LottoPrize.*;
+public class Lotto {
+ private final List<Integer> numbers;
+
+ public Lotto(List<Integer> numbers) {
+ validate(numbers);
+ duplicate(numbers);
+ this.numbers = numbers;
+ }
+
+ private void duplicate(List<Integer> numbers) {
+ List<Integer> duplication = numbers.stream()
+ .distinct()
+ .toList();
+ if (duplication.size() != numbers.size()) {
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+ }
+
+ private void validate(List<Integer> numbers) {
+ if (numbers.size() != 6) {
+ throw new IllegalArgumentException(SIXNUMBER.getMessage());
+ }
+ }
+ public void checkSame(Integer bonusNumber, List<List<Integer>> lottoNumber) {
+ bonusDuplication(bonusNumber);
+ for (List<Integer> integers : lottoNumber) {
+ List<Integer> Result = integers.stream()
+ .filter(i -> this.numbers.stream().anyMatch(Predicate.isEqual(i)))
+ .toList();
+ long bonusResult = integers.stream()
+ .filter(bonusNumber::equals).count();
+ CheckPrize(Result.size(), bonusResult).addWinCount();
+
+ }
+ }
+
+ private void bonusDuplication(Integer bonusNumber) {
+ List<Integer> Result = this.numbers.stream()
+ .filter(i-> !Objects.equals(i, bonusNumber))
+ .toList();
+ if(Result.size()!=6){
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+
+ }
+
+ public LottoPrize CheckPrize(int result, long bonusResult){
+ if(result==6){
+ return FIRST_PRIZE;
+ }
+ if(result==5&&bonusResult==1){
+ return SECOND_PRIZE;
+ }
+ if(result==5&&bonusResult==0){
+ return THIRD_PRIZE;
+ }
+ if(result==4){
+ return FOURTH_PRIZE;
+ }
+ if(result==3){
+ return FIFTH_PRIZE;
+ }
+ if(result<3){
+ return LOOSE;
+ }
+ throw new IllegalArgumentException("์กด์ฌ ํ์ง ์๋ ์์์
๋๋ค.");
+ }
+} | Java | set์ ์ด์ฉํ์ง ์๋ ๋ฐฉ๋ฒ๋ ์์๋ค์. |
@@ -0,0 +1,79 @@
+package lotto.model;
+
+import lotto.model.constants.LottoPrize;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import static lotto.constants.ErrorMessage.DUPLICATION;
+import static lotto.constants.ErrorMessage.SIXNUMBER;
+import static lotto.model.constants.LottoPrize.*;
+public class Lotto {
+ private final List<Integer> numbers;
+
+ public Lotto(List<Integer> numbers) {
+ validate(numbers);
+ duplicate(numbers);
+ this.numbers = numbers;
+ }
+
+ private void duplicate(List<Integer> numbers) {
+ List<Integer> duplication = numbers.stream()
+ .distinct()
+ .toList();
+ if (duplication.size() != numbers.size()) {
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+ }
+
+ private void validate(List<Integer> numbers) {
+ if (numbers.size() != 6) {
+ throw new IllegalArgumentException(SIXNUMBER.getMessage());
+ }
+ }
+ public void checkSame(Integer bonusNumber, List<List<Integer>> lottoNumber) {
+ bonusDuplication(bonusNumber);
+ for (List<Integer> integers : lottoNumber) {
+ List<Integer> Result = integers.stream()
+ .filter(i -> this.numbers.stream().anyMatch(Predicate.isEqual(i)))
+ .toList();
+ long bonusResult = integers.stream()
+ .filter(bonusNumber::equals).count();
+ CheckPrize(Result.size(), bonusResult).addWinCount();
+
+ }
+ }
+
+ private void bonusDuplication(Integer bonusNumber) {
+ List<Integer> Result = this.numbers.stream()
+ .filter(i-> !Objects.equals(i, bonusNumber))
+ .toList();
+ if(Result.size()!=6){
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+
+ }
+
+ public LottoPrize CheckPrize(int result, long bonusResult){
+ if(result==6){
+ return FIRST_PRIZE;
+ }
+ if(result==5&&bonusResult==1){
+ return SECOND_PRIZE;
+ }
+ if(result==5&&bonusResult==0){
+ return THIRD_PRIZE;
+ }
+ if(result==4){
+ return FOURTH_PRIZE;
+ }
+ if(result==3){
+ return FIFTH_PRIZE;
+ }
+ if(result<3){
+ return LOOSE;
+ }
+ throw new IllegalArgumentException("์กด์ฌ ํ์ง ์๋ ์์์
๋๋ค.");
+ }
+} | Java | ํด๋น ๋ถ๋ถ์ LottoPrize์์ ํด์ฃผ๋ฉด lotto์ ์ญํ ์ ์ค์ผ ์ ์์ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋๋ฐ ์ด๋ ์ ๊ฐ์?
stream์ ์ด์ฉํด์ filter๋ฅผ ํ๋ฉด ์กฐ๊ธ ๋ ๊น๋ํด ์ง ๊ฒ ๊ฐ์์. |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ๋งค์ง ๋๋ฒ ํด์ฃผ์๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,62 @@
+package lotto.view;
+
+import camp.nextstep.edu.missionutils.Console;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static lotto.constants.ErrorMessage.*;
+import static lotto.view.ConstantsMessage.*;
+
+public class LottoInput {
+ public String askPrice() {
+ System.out.println(ASK_BUY_PRICE.getMessage());
+ String input = Console.readLine();
+ return input;
+
+ }
+
+ public List<Integer> prizeNumberInput() {
+ while (true) {
+ try {
+ printNewLine();
+ System.out.println(ASK_PRIZE_NUMBER.getMessage());
+ return changeInt(Arrays.asList(Console.readLine().split(",")));
+ }catch (IllegalArgumentException e){
+ System.out.println(OUTFRANGE.getMessage());
+ }
+ }
+ }
+ public Integer bonusNumberInput() {
+ try{
+ printNewLine();
+ System.out.println(ASK_BONUS_NUMBER.getMessage());
+ String input = Console.readLine();
+
+ return Integer.parseInt(input);
+ }catch (NumberFormatException e){
+ throw new IllegalArgumentException(ONENUMBER.getMessage());
+ }
+
+ }
+ private List<Integer> changeInt(List<String> prizeNumbers) {
+ try{
+ List<Integer> numbers = prizeNumbers.stream()
+ .map(Integer::parseInt)
+ .filter(i->i>0&&i<45)
+ .toList();
+ if(numbers.size() != prizeNumbers.size()){
+ throw new NumberFormatException();
+ }
+ return numbers;
+ }catch (NumberFormatException e){
+ throw new IllegalArgumentException(OUTFRANGE.getMessage());
+ }
+
+ }
+
+ private void printNewLine() {
+ System.out.println();
+ }
+} | Java | ๋ฉ์์ง ์์ %n๋ฅผ ์ด์ฉํด์ printf๋ฅผ ์ด์ฉํ๋ค๋ฉด printNewLine ์ฝ๋๋ฅผ ์์จ๋ ๋ ๊ฒ ๊ฐ์๋ฐ ์ด ๋ถ๋ถ์ ๋ํด์ ์ด๋ป๊ฒ ์๊ฐํ์๋์ง ๊ถ๊ธํฉ๋๋ค. |
@@ -0,0 +1,21 @@
+package lotto.constants;
+
+public enum ErrorMessage {
+ DUPLICATION("[ERROR] ๊ฐ์ด ์ค๋ณต๋์์ต๋๋ค"),
+ ISNOTINTEGER("[ERROR] ์ซ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"),
+ SIXNUMBER("[ERROR] ๋ฒํธ 6๊ฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"),
+ OUTFRANGE("[ERROR] ๋ก๋ ๋ฒํธ๋ 1๋ถํฐ 45 ์ฌ์ด์ ์ซ์์ฌ์ผ ํฉ๋๋ค."),
+ ONENUMBER("[ERROR] ์ซ์ ๋ฒํธ 1๊ฐ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์"),
+ NOTTHOUSAND("[ERROR] 1000์ ๋จ์๋ก ์
๋ ฅํด ์ฃผ์ธ์");
+
+
+ private final String message;
+
+ ErrorMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+} | Java | constants ํจํค์ง๊ฐ ๋๊ฐ๊ฐ ์กด์ฌํ๋๋ฐ ์ด๋ฆ์ ๋ฌ๋ฆฌํด์ ๊ตฌ๋ถํ๋๊ฒ ์กฐ๊ธ ๋ ๊ฐ๋
์ฑ์๋ ์ข์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,34 @@
+# ๋ก๋
+
+## ๊ธฐ๋ฅ ๋ชฉ๋ก
+- [V] ๋๋ค ํ๊ฒ ์์ฑํ ๋ก๋ ๋ฒํธ์ ์ฌ์ฉ์๊ฐ ์์๋ก ์ ์ ํ ๋น์ฒจ ๋ฒํธ๋ฅผ ๋น๊ตํ์ฌ
+ ๋น์ฒจ๋ ๋ฑ์์ ์์ต๋ฅ ์ ๊ณ์ฐํ๋ค.
+ - [V] ์ฌ์ฉ์๋ก๋ถํฐ ๋ก๋ ๊ตฌ์
๊ธ์ก์ ์
๋ ฅ ๋ฐ๋๋ค.
+ - [V] ์
๋ ฅ ๋ฐ์ ๊ธ์ก์ ๋ฐ๋ผ ๋๋คํ๊ฒ 6๊ฐ์ ์ซ์๋ฅผ ์์ฑํ์ฌ ๋ก๋ ๋ฒํธ๋ฅผ ์์ฑํ๋ค.(1~45๋ฒ์, ์ค๋ณตX, ์ค๋ฆ์ฐจ์ ์ ๋ ฌ)
+ - [V] ์ฌ์ฉ์๋ก๋ถํฐ ๋น์ฒจ ๋ฒํธ์ ๋ณด๋์ค ๋ฒํธ๋ฅผ ์
๋ ฅ ๋ฐ์ ํ, ์์ฑํ ๋ก๋ ๋ฒํธ์ ๋น๊ตํ์ฌ ๋ฑ์๋ฅผ ์ถ๋ ฅํ๋ค.
+ - [V] ์์ต๋ฅ ์ ๊ณ์ฐํ์ฌ ์ถ๋ ฅํ๋ค.(์์์ ๋๋ ์๋ฆฌ ๋ฐ์ฌ๋ฆผ)
+ -[]์
๋ ฅ๊ฐ ์์ธ์ฒ๋ฆฌ
+ -[V]๋ก๋ ๋ฒํธ ์ค๋ณต ์์ธ์ฒ๋ฆฌ
+ -[V]๋ก๋ ๋ฒํธ ๋ฒ์ ๋ฒ์ด๋จ ์์ธ์ฒ๋ฆฌ
+ -[] ๋ณด๋์ค ๋ฒํธ ์์ธ์ฒ๋ฆฌ
+## ๊ธฐ๋ฅ ์๊ตฌ ์ฌํญ
+- ๋ก๋ ๋ฒํธ์ ์ซ์ ๋ฒ์๋ 1~45๊น์ง์ด๋ค.
+- 1๊ฐ์ ๋ก๋๋ฅผ ๋ฐํํ ๋ ์ค๋ณต๋์ง ์๋ 6๊ฐ์ ์ซ์๋ฅผ ๋ฝ๋๋ค.
+- ๋น์ฒจ ๋ฒํธ ์ถ์ฒจ ์ ์ค๋ณต๋์ง ์๋ ์ซ์ 6๊ฐ์ ๋ณด๋์ค ๋ฒํธ 1๊ฐ๋ฅผ ๋ฝ๋๋ค.
+- ๋น์ฒจ์ 1๋ฑ๋ถํฐ 5๋ฑ๊น์ง ์๋ค. ๋น์ฒจ ๊ธฐ์ค๊ณผ ๊ธ์ก์ ์๋์ ๊ฐ๋ค.
+ - 1๋ฑ: 6๊ฐ ๋ฒํธ ์ผ์น / 2,000,000,000์
+ - 2๋ฑ: 5๊ฐ ๋ฒํธ + ๋ณด๋์ค ๋ฒํธ ์ผ์น / 30,000,000์
+ - 3๋ฑ: 5๊ฐ ๋ฒํธ ์ผ์น / 1,500,000์
+ - 4๋ฑ: 4๊ฐ ๋ฒํธ ์ผ์น / 50,000์
+ - 5๋ฑ: 3๊ฐ ๋ฒํธ ์ผ์น / 5,000์
+
+### ์ถ๊ฐ๋ ์๊ตฌ ์ฌํญ
+-[V] ํจ์(๋๋ ๋ฉ์๋)์ ๊ธธ์ด๊ฐ 15๋ผ์ธ์ ๋์ด๊ฐ์ง ์๋๋ก ๊ตฌํํ๋ค.
+- ํจ์(๋๋ ๋ฉ์๋)๊ฐ ํ ๊ฐ์ง ์ผ๋ง ์ ํ๋๋ก ๊ตฌํํ๋ค.
+-[V] else ์์ฝ์ด๋ฅผ ์ฐ์ง ์๋๋ค.
+- ํํธ: if ์กฐ๊ฑด์ ์์ ๊ฐ์ returnํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํ๋ฉด else๋ฅผ ์ฌ์ฉํ์ง ์์๋ ๋๋ค.
+- else๋ฅผ ์ฐ์ง ๋ง๋ผ๊ณ ํ๋ switch/case๋ก ๊ตฌํํ๋ ๊ฒฝ์ฐ๊ฐ ์๋๋ฐ switch/case๋ ํ์ฉํ์ง ์๋๋ค.
+-[V] Java Enum์ ์ ์ฉํ๋ค.
+-[] ๋๋ฉ์ธ ๋ก์ง์ ๋จ์ ํ
์คํธ๋ฅผ ๊ตฌํํด์ผ ํ๋ค. ๋จ, UI(System.out, System.in, Scanner) ๋ก์ง์ ์ ์ธํ๋ค.
+ - ํต์ฌ ๋ก์ง์ ๊ตฌํํ๋ ์ฝ๋์ UI๋ฅผ ๋ด๋นํ๋ ๋ก์ง์ ๋ถ๋ฆฌํด ๊ตฌํํ๋ค.
+ - ๋จ์ ํ
์คํธ ์์ฑ์ด ์ต์ํ์ง ์๋ค๋ฉดย `test/java/lotto/LottoTest`๋ฅผ ์ฐธ๊ณ ํ์ฌ ํ์ตํ ํ ํ
์คํธ๋ฅผ ๊ตฌํํ๋ค.
\ No newline at end of file | Unknown | ์๋ฃ๋ ๋ถ๋ถ ์ฒดํฌํ์ฌ ๋ฆฌ๋๋ฏธ๋ ๊ผผ๊ผผํ๊ฒ ๊ด๋ฆฌํด์ฃผ์๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค~ |
@@ -0,0 +1,50 @@
+package lotto.controller;
+
+import lotto.model.Lotto;
+import lotto.model.LottoNumberMaker;
+import lotto.model.LottoPercentageCalculation;
+import lotto.model.constants.LottoPrize;
+import lotto.view.LottoInput;
+import lotto.view.LottoOutput;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import static java.util.Arrays.asList;
+import static lotto.model.constants.LottoPrize.*;
+
+public class LottoController {
+ private static final LottoNumberMaker lottoNumberMaker = new LottoNumberMaker();
+ private static final LottoInput lottoInput = new LottoInput();
+ private static final LottoOutput lottoOutput = new LottoOutput();
+ private static final LottoPercentageCalculation lottoPercentageCalculation = new LottoPercentageCalculation();
+ public static void setPrice() {
+ lottoNumberMaker.checkInt();
+ }
+
+// static List<LottoPrize> LottoPrizelist= asList(FIFTH_PRIZE,FOURTH_PRIZE,THIRD_PRIZE,SECOND_PRIZE,FIRST_PRIZE);
+ public static void setBuyLottoNumberPrint() {
+ lottoOutput.buyLottoNumberPrint(lottoNumberMaker.getLottoNumber());
+ }
+
+ public static void setPrizeNumberInput() {
+ Lotto lotto = new Lotto(lottoInput.prizeNumberInput());
+ lotto.checkSame(lottoInput.bonusNumberInput(),lottoNumberMaker.getLottoNumber());
+ }
+
+ public static void winningStatistic() {
+ List<String> lottoPrizes = new ArrayList<>();
+ for(LottoPrize lottoPrize: LottoPrize.values()){
+ lottoPrizes.add(lottoPrize.getText()+lottoPrize.getWinCount()+lottoPrize.getUnit());
+ }
+ LottoOutput.seeWinningStatstic(lottoPrizes);
+ }
+
+ public static void PerformanceCalculation() {
+ lottoOutput.seePercentage(lottoPercentageCalculation.percentageCalculation(LottoPrize.values(),lottoNumberMaker.getBuyPrice()));
+ }
+
+ public String askPrice() {
+ return lottoInput.askPrice();
+ }
+}
\ No newline at end of file | Java | set์ด๋ผ๋ ๋ฉ์๋๋ช
์ Setter์ ๋๋์ด ๊ฐํ๋ฐ ํน์ ๋ค๋ฅธ ๋ณ์๋ช
์ ๊ณ ๋ คํด๋ณด์๋๊ฑด ์ด๋ ์ค๊น์? |
@@ -0,0 +1,79 @@
+package lotto.model;
+
+import lotto.model.constants.LottoPrize;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import static lotto.constants.ErrorMessage.DUPLICATION;
+import static lotto.constants.ErrorMessage.SIXNUMBER;
+import static lotto.model.constants.LottoPrize.*;
+public class Lotto {
+ private final List<Integer> numbers;
+
+ public Lotto(List<Integer> numbers) {
+ validate(numbers);
+ duplicate(numbers);
+ this.numbers = numbers;
+ }
+
+ private void duplicate(List<Integer> numbers) {
+ List<Integer> duplication = numbers.stream()
+ .distinct()
+ .toList();
+ if (duplication.size() != numbers.size()) {
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+ }
+
+ private void validate(List<Integer> numbers) {
+ if (numbers.size() != 6) {
+ throw new IllegalArgumentException(SIXNUMBER.getMessage());
+ }
+ }
+ public void checkSame(Integer bonusNumber, List<List<Integer>> lottoNumber) {
+ bonusDuplication(bonusNumber);
+ for (List<Integer> integers : lottoNumber) {
+ List<Integer> Result = integers.stream()
+ .filter(i -> this.numbers.stream().anyMatch(Predicate.isEqual(i)))
+ .toList();
+ long bonusResult = integers.stream()
+ .filter(bonusNumber::equals).count();
+ CheckPrize(Result.size(), bonusResult).addWinCount();
+
+ }
+ }
+
+ private void bonusDuplication(Integer bonusNumber) {
+ List<Integer> Result = this.numbers.stream()
+ .filter(i-> !Objects.equals(i, bonusNumber))
+ .toList();
+ if(Result.size()!=6){
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+
+ }
+
+ public LottoPrize CheckPrize(int result, long bonusResult){
+ if(result==6){
+ return FIRST_PRIZE;
+ }
+ if(result==5&&bonusResult==1){
+ return SECOND_PRIZE;
+ }
+ if(result==5&&bonusResult==0){
+ return THIRD_PRIZE;
+ }
+ if(result==4){
+ return FOURTH_PRIZE;
+ }
+ if(result==3){
+ return FIFTH_PRIZE;
+ }
+ if(result<3){
+ return LOOSE;
+ }
+ throw new IllegalArgumentException("์กด์ฌ ํ์ง ์๋ ์์์
๋๋ค.");
+ }
+} | Java | ์ ๋ ๋์น ๋ถ๋ถ์ด๊ธดํ๋ฐ ๋งค์ง๋๋ฒ ์ฒ๋ฆฌํด์ฃผ์๋๊ฒ ์ข์๋ฏ ํฉ๋๋ค! |
@@ -0,0 +1,79 @@
+package lotto.model;
+
+import lotto.model.constants.LottoPrize;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import static lotto.constants.ErrorMessage.DUPLICATION;
+import static lotto.constants.ErrorMessage.SIXNUMBER;
+import static lotto.model.constants.LottoPrize.*;
+public class Lotto {
+ private final List<Integer> numbers;
+
+ public Lotto(List<Integer> numbers) {
+ validate(numbers);
+ duplicate(numbers);
+ this.numbers = numbers;
+ }
+
+ private void duplicate(List<Integer> numbers) {
+ List<Integer> duplication = numbers.stream()
+ .distinct()
+ .toList();
+ if (duplication.size() != numbers.size()) {
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+ }
+
+ private void validate(List<Integer> numbers) {
+ if (numbers.size() != 6) {
+ throw new IllegalArgumentException(SIXNUMBER.getMessage());
+ }
+ }
+ public void checkSame(Integer bonusNumber, List<List<Integer>> lottoNumber) {
+ bonusDuplication(bonusNumber);
+ for (List<Integer> integers : lottoNumber) {
+ List<Integer> Result = integers.stream()
+ .filter(i -> this.numbers.stream().anyMatch(Predicate.isEqual(i)))
+ .toList();
+ long bonusResult = integers.stream()
+ .filter(bonusNumber::equals).count();
+ CheckPrize(Result.size(), bonusResult).addWinCount();
+
+ }
+ }
+
+ private void bonusDuplication(Integer bonusNumber) {
+ List<Integer> Result = this.numbers.stream()
+ .filter(i-> !Objects.equals(i, bonusNumber))
+ .toList();
+ if(Result.size()!=6){
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+
+ }
+
+ public LottoPrize CheckPrize(int result, long bonusResult){
+ if(result==6){
+ return FIRST_PRIZE;
+ }
+ if(result==5&&bonusResult==1){
+ return SECOND_PRIZE;
+ }
+ if(result==5&&bonusResult==0){
+ return THIRD_PRIZE;
+ }
+ if(result==4){
+ return FOURTH_PRIZE;
+ }
+ if(result==3){
+ return FIFTH_PRIZE;
+ }
+ if(result<3){
+ return LOOSE;
+ }
+ throw new IllegalArgumentException("์กด์ฌ ํ์ง ์๋ ์์์
๋๋ค.");
+ }
+} | Java | ```suggestion
.filter(i -> this.numbers
.stream()
.anyMatch(Predicate.isEqual(i)))
```
๋ค์๊ณผ ๊ฐ์ด ๊ฐํ์ฒ๋ฆฌ ํด์ฃผ์๋๊ฒ ๊ฐ๋
์ฑ์ ์ข์๋ณด์
๋๋ค! |
@@ -0,0 +1,79 @@
+package lotto.model;
+
+import lotto.model.constants.LottoPrize;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Predicate;
+
+import static lotto.constants.ErrorMessage.DUPLICATION;
+import static lotto.constants.ErrorMessage.SIXNUMBER;
+import static lotto.model.constants.LottoPrize.*;
+public class Lotto {
+ private final List<Integer> numbers;
+
+ public Lotto(List<Integer> numbers) {
+ validate(numbers);
+ duplicate(numbers);
+ this.numbers = numbers;
+ }
+
+ private void duplicate(List<Integer> numbers) {
+ List<Integer> duplication = numbers.stream()
+ .distinct()
+ .toList();
+ if (duplication.size() != numbers.size()) {
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+ }
+
+ private void validate(List<Integer> numbers) {
+ if (numbers.size() != 6) {
+ throw new IllegalArgumentException(SIXNUMBER.getMessage());
+ }
+ }
+ public void checkSame(Integer bonusNumber, List<List<Integer>> lottoNumber) {
+ bonusDuplication(bonusNumber);
+ for (List<Integer> integers : lottoNumber) {
+ List<Integer> Result = integers.stream()
+ .filter(i -> this.numbers.stream().anyMatch(Predicate.isEqual(i)))
+ .toList();
+ long bonusResult = integers.stream()
+ .filter(bonusNumber::equals).count();
+ CheckPrize(Result.size(), bonusResult).addWinCount();
+
+ }
+ }
+
+ private void bonusDuplication(Integer bonusNumber) {
+ List<Integer> Result = this.numbers.stream()
+ .filter(i-> !Objects.equals(i, bonusNumber))
+ .toList();
+ if(Result.size()!=6){
+ throw new IllegalArgumentException(DUPLICATION.getMessage());
+ }
+
+ }
+
+ public LottoPrize CheckPrize(int result, long bonusResult){
+ if(result==6){
+ return FIRST_PRIZE;
+ }
+ if(result==5&&bonusResult==1){
+ return SECOND_PRIZE;
+ }
+ if(result==5&&bonusResult==0){
+ return THIRD_PRIZE;
+ }
+ if(result==4){
+ return FOURTH_PRIZE;
+ }
+ if(result==3){
+ return FIFTH_PRIZE;
+ }
+ if(result<3){
+ return LOOSE;
+ }
+ throw new IllegalArgumentException("์กด์ฌ ํ์ง ์๋ ์์์
๋๋ค.");
+ }
+} | Java | ๋งค์ง๋๋ฒ ์ฒ๋ฆฌ๋ฅผ ํด์ฃผ์
์ผ ํ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ์ด๋ถ๋ถ๋ง ๋ฐ๋ก ์ ์ญ ๋ฉ์๋๋ก ์ ์ธํ์ ์ด์ ๊ฐ ์์ผ์ค๊น์??? |
@@ -0,0 +1,67 @@
+package lotto.model;
+
+import camp.nextstep.edu.missionutils.Randoms;
+import lotto.controller.LottoController;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static lotto.constants.ErrorMessage.ISNOTINTEGER;
+import static lotto.constants.ErrorMessage.NOTTHOUSAND;
+
+public class LottoNumberMaker {
+ private static int buyPrice;
+ private static final List<List<Integer>> numbers = new ArrayList<>();
+ private static final LottoController lottoController = new LottoController();
+
+ public static int getBuyPrice() {
+ return buyPrice;
+ }
+
+ public void makeLottoNumber(Integer count) {
+ buyPrice = count;
+ checkThousand(count);
+ IntStream.range(0, count / 1000)
+ .forEach(i -> numbers.add(makeRandomNumber()));
+ }
+
+ private void checkThousand(Integer count) {
+ if(count%1000!=0){
+ throw new IllegalArgumentException(NOTTHOUSAND.getMessage());
+ }
+ return;
+ }
+
+ private List<Integer> makeRandomNumber() {
+ return Randoms.pickUniqueNumbersInRange(1, 45, 6).stream()
+ .sorted()
+ .collect(Collectors.toList());
+ }
+
+ public void checkInt() {
+ while (true) {
+ try {
+ int number = checkCount();
+ makeLottoNumber(number);
+ break;
+ } catch (IllegalArgumentException e) {
+ System.out.println(ISNOTINTEGER.getMessage());
+ System.out.println(NOTTHOUSAND.getMessage());
+ }
+ }
+ }
+
+ private int checkCount() {
+ try {
+ return Integer.parseInt(lottoController.askPrice());
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(ISNOTINTEGER.getMessage());
+ }
+ }
+
+ public List<List<Integer>> getLottoNumber() {
+ return numbers;
+ }
+} | Java | ํด๋น ํด๋์ค๊ฐ ๋๋ฌด ๋ง์ ์ฑ
์์ ๊ฐ์ง๊ณ ์๋๊ฒ ๊ฐ์ต๋๋ค. ๊ตฌ๋งค๊ธ์ก์ ๊ฒ์ฆํ๋ ๋ถ๋ถ๊ณผ ๋ก๋ ๋ฒํธ๋ฅผ ๊ฒ์ฆํ๋ ๋ถ๋ถ์ ๋ถ๋ฆฌํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ผ๊ฑฐ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,39 @@
+package lotto.model.constants;
+
+public enum LottoPrize {
+ FIRST_PRIZE(0,"6๊ฐ ์ผ์น (2,000,000,000์) - ","๊ฐ",2000000000),
+ SECOND_PRIZE(0,"5๊ฐ ์ผ์น, ๋ณด๋์ค ๋ณผ ์ผ์น (30,000,000์) - ","๊ฐ", 30000000),
+ THIRD_PRIZE(0,"5๊ฐ ์ผ์น (1,500,000์) - ","๊ฐ", 1500000),
+ FOURTH_PRIZE(0,"4๊ฐ ์ผ์น (50,000์) - ","๊ฐ", 50000),
+ FIFTH_PRIZE(0,"3๊ฐ ์ผ์น (5,000์) - ","๊ฐ", 5000),
+ LOOSE(0,"","๊ฐ", 0);
+
+
+ private int winCount;
+ private final String text;
+ private final String unit;
+ private int prizeMoney;
+
+ LottoPrize(int winCount, String text, String unit, int prizemoney) {
+ this.winCount = winCount;
+ this.text = text;
+ this.unit = unit;
+ this.prizeMoney = prizemoney;
+ }
+ public int getWinCount() {
+ return winCount;
+ }
+ public String getText() {
+ return text;
+ }
+ public String getUnit() {
+ return unit;
+ }
+ public int getPrizeMoney() {
+ return prizeMoney;
+ }
+
+ public void addWinCount() {
+ this.winCount ++;
+ }
+} | Java | ์ถ๋ ฅ ํ์๊ณผ ๊ด๋ จ๋ ๋ถ๋ถ์ view์์ ์ฒ๋ฆฌํด์ผํ ๊ฑฐ ๊ฐ์๋ฐ ์ด์ ๋ํด์ ์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,100 @@
+# Domain ๊ตฌํ
+
+## ์งํ ํ๋ก์ธ์ค (์์คํ
์ฑ
์)
+
+1. ๊ฒ์ด๋จธ๋ ์๋ํ ๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์ค์ ํ๋ค.
+2. ๊ฒ์ด๋จธ๋ ํ๋ ์ดํ ํ๋ ์ด์ด์ ์ด๋ฆ๊ณผ HP, MP๋ฅผ ์ค์ ํ๋ค.
+3. ๊ฒ์์ด ์์๋๋ค.
+4. ๊ฒ์์ ํด์ ์ด๋ฉฐ, ๋งค ํด๋ง๋ค ๋ค์๊ณผ ๊ฐ์ ํ๋์ด ๋ฐ๋ณต๋๋ค.
+ 1) ๋ณด์ค ๋ชฌ์คํฐ์ ํ๋ ์ด์ด์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ 2) ํ๋ ์ด์ด๊ฐ ๊ณต๊ฒฉํ ๋ฐฉ์์ ์ ํํ๋ค.
+ 3) ํ๋ ์ด์ด๊ฐ ์ ํ๋ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋ค. (๋ณด์ค ๋ชฌ์คํฐ์๊ฒ ๋ฐ๋ฏธ์ง๋ฅผ ์
ํ)
+ 4) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ๋ณด์ค ๋ชฌ์คํฐ์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 5) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+ 6) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด์์๋ค๋ฉด ํ๋ ์ด์ด๋ฅผ ๊ณต๊ฒฉํ๋ค.
+ 7) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ํ๋ ์ด์ด์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 8) ํ๋ ์ด์ด๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+5. ๊ฒ์ ์ข
๋ฃ ํ ์น์๋ฅผ ์๋ดํ๋ค.
+ - ํ๋ ์ด์ด๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ์งํํ ํด ์๋ฅผ ์๋ ค์ค๋ค.
+ - ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ํ๋ ์ด์ด์ ๋ณด์ค ๋ชฌ์คํฐ์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+
+## Domain ๊ฐ์ฒด ์ ์ ๋ฐ ์ฑ
์๊ณผ ํ๋ ฅ ํ ๋น
+
+1. **Raid Game**
+ - ๊ฒ์ ์์ ์ ์ฃผ์ ์ธ๋ฌผ์ ๋ํด ์
ํ
ํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Boss Monster**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Player**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๋งค ํด์ ์คํํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ํ๋ฅผ ํ์ธํ ์ฑ
์์ ๊ฐ์ง (๊ฒ์ ์ํ๊ฐ '์ข
๋ฃ'์ผ ๊ฒฝ์ฐ ํด์ ์คํํ์ง ์์)
+ - ํด ์๋ฅผ ์ฆ๊ฐ์ํฌ ์ฑ
์์ ๊ฐ์ง
+ - **Player**๊ฐ **Boss Monster**๋ฅผ ์ ํ๋ ๊ณต๊ฒฉ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง
+ - **Player**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Boss Monster**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ๊ณต๊ฒฉ๊ณผ **Boss Monster**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - **Boss Monster**๊ฐ **Player**๋ฅผ ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Player**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Player**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - ํด๋น ํด์ ๋ํ ๊ฒ์ ๊ธฐ๋ก์ ์์ฑํ ์ฑ
์์ ๊ฐ์ง (**GameHistory**์๊ฒ ํด์ ๋ํ ๊ธฐ๋ก์ ์์ฑํ๋ ํ๋ ฅ ํ์)
+
+2. **Boss Monster**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด HP, ํ์ฌ HP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (0~20์ ๋๋ค ๋ฐ๋ฏธ์ง)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+3. **Player**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด๋ฆ, ์ด HP, ํ์ฌ HP, ์ด MP, ํ์ฌ MP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (**Attack Type**์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์, MP ๊ฐ์)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+4. **Attack Type**
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ข
๋ฅ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๊ณต๊ฒฉ ๋ณํธ, ๊ณต๊ฒฉ ๋ฐฉ์ ์ด๋ฆ)
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๋ฐ๋ฏธ์ง, MP ์๋ชจ๋, MP ํ๋ณต๋)
+
+5. **Game History**
+ - ๋งค ํด์ ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ ์ฑ
์์ ๊ฐ์ง
+ - ์ถํ Controller์ Veiw๋ฅผ ์ํ ์์ฒญ์ ์๋ต ๊ฐ๋ฅ
+
+## ์ฃผ์ ๋ฉ์์ง ๊ตฌ์ฑ (๊ณต์ฉ ์ธํฐํ์ด์ค)
+
+1. ๋ณด์ค ๋ชฌ์คํฐ ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Boss Monster
+ - ์ธ์ : int hp
+ - ์๋ต : void, Boss Monster ๊ฐ์ฒด ์์ฑ
+
+2. ํ๋ ์ด์ด ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Player
+ - ์ธ์ : String name, int hp, int mp
+ - ์๋ต : void, Player ๊ฐ์ฒด ์์ฑ
+
+3. ํด ์งํ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Start Game
+ - ์ธ์ : -
+ - ์๋ต : ๊ตฌ์ฑ๋ ํด ๋ก์ง ์คํ
+
+4. ๊ฒ์ ์ํ ํ์ธ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Check Game Status
+ - ์ธ์ : -
+ - ์๋ต : return Game Status (true or False)
+
+5. ํด ํ์ ์ฆ๊ฐ๋ฅผ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Increase Turns
+ - ์ธ์ : -
+ - ์๋ต : void, Turn 1 ์ฆ๊ฐ
+
+6. ๊ณต๊ฒฉ ํ๋ ์ํ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : Attack
+ - ์ธ์ : -
+ - ์๋ต : return ๋ฐ๋ฏธ์ง, ๊ณต๊ฒฉ ๋ฐฉ์์ ๋ฐ๋ผ MP ๊ฐ์(Player)
+
+7. ๊ณต๊ฒฉ ํผํด ์ ๋ฌ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : take Damage
+ - ์ธ์ : int damage
+ - ์๋ต : return ํ์ฌ HP
+
+ | Unknown | ์์ฒญ ์ฌํญ์ ๊ต์ฅํ ๊น๋ํ๊ฒ ์ ์ด์ฃผ์ ๊ฒ ๊ฐ์์!
ํ์ง๋ง ์ฐ๋ ค๋๋ ์ ๋ ์๋๋ฐ์. ์ ์ฑ์ค๋ ์์ฑํด์ฃผ์ ๋ฌธ์์ง๋ง ๋๋ฃ ์
์ฅ์์๋ ํจ์ ์ด๋ฆ์ด๋ ์ธ์ ๋ฑ์ ๊ธ๋ก ํํํ๋ ๊ฒ๋ณด๋ค ํจ์ ํํ๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด ๊ฐ๋
์ฑ์ด ๋ ์ข์ ์๋ ์๊ฒ ๋ค๋ ์๊ฐ์ ํ์ด์!
์ฑ
๋ค๋ ๋ณด๋ฉด ๊ธ๋ณด๋ค๋ ์ฝ๋๋ก ๋จผ์ ๋ณด์ฌ์ฃผ์์์:)
```java
public void start() {
// ๊ฒ์ ์์ ๋ก์ง
}
```
์ด๋ฐ ๋๋์ผ๋ก์!
์ด๋ป๊ฒ ์๊ฐํ์๋์? |
@@ -0,0 +1,17 @@
+package bossmonster.exception;
+
+import bossmonster.view.OutputView;
+import java.util.function.Supplier;
+
+public class ExceptionHandler {
+
+ public static <T> T retryInput(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (Exception exception) {
+ OutputView.printError(exception);
+ }
+ }
+ }
+} | Java | ์ด๋ ๊ฒ ๋ง๋ค ์๊ฐ์ ํ์ง ๋ชปํ๋๋ฐ, ์ ๋ค๋ฆญ์ ์ ๋ง ์ ํ์ฉํ์ ๊ฒ ๊ฐ์์!
ํ์ง๋ง ํจ์๋ช
์์ ์คํด์ ์์ง๊ฐ ์๋ค๊ณ ์๊ฐํด์. ์ฒซ ์๋๋ฅผ ํ ๋๋ ํด๋น ํจ์๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ธ๋ฐ์. ์ค๋ฅ๊ฐ ๋์ง ์์๋๊น์ง ๊ณ์ ์ฌ์
๋ ฅ ๋ฐ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ค๋ฅธ ๋ค์ด๋ฐ์ด ์ด์ธ๋ฆด ๊ฒ ๊ฐ์์! ์... ๋ง๋
ํ ๋ ์ค๋ฅด๋ ๋ค์ด๋ฐ์ ์๋ค์ใ
ใ
|
@@ -0,0 +1,17 @@
+package bossmonster.exception;
+
+import bossmonster.view.OutputView;
+import java.util.function.Supplier;
+
+public class ExceptionHandler {
+
+ public static <T> T retryInput(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (Exception exception) {
+ OutputView.printError(exception);
+ }
+ }
+ }
+} | Java | ์ถ๊ฐ์ ์ผ๋ก ํด๋์ค๋ช
์ด ์ด์ธ๋ฆฌ์ง ์๋๋ฏํ ๋๋์ ๋ฐ์์ด์. ์คํ๋ง์ ์๊ฐํด์ ๋ค์ด๋ฐ์ ์ ํ์ ๊ฒ ๊ฐ์์ ์คํ๋ง์ ExceptionHandler์ ๋น๊ตํด์ ๋ง์๋๋ฆฌ๊ฒ ์ต๋๋ค!
`ExceptionHandler`์ ๊ฒฝ์ฐ ๋ง ๊ทธ๋๋ก ์์ธ๋ฅผ ํธ๋ค๋งํด์ฃผ๋ ์ญํ ์
๋๋ค. A๋ผ๋ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ExceptionHandler๊ฐ ๊ทธ์ ๋ง๊ฒ ์ฒ๋ฆฌํด์ฃผ๊ณ , B๋ผ๋ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด ๊ทธ๊ฒ๋ ๊ทธ๊ฒ์ ExceptionHandler์ ๋ง๊ฒ ์ฒ๋ฆฌํด์ฃผ๋ ์ญํ ์ด์ฃ . ์คํ๋ง์ ExceptionHandler์ฒ๋ผ ์์ธ๋ฅผ ํธ๋ค๋งํ๋ ๊ทธ ์์ฒด์๋ง ๋ชฉ์ ์ด ์์ด์ผํ๋ค๊ณ ์๊ฐํด์.
ํ์ง๋ง ์์ฑํ์ ์ฝ๋์์๋ ์์ธ๊ฐ ๋ฐ์ํ์ ๋ ์ด๋ฒคํธ๊ฐ ์์ธ๋ฅผ ๊ฐ์งํด ์์ธ๋ฅผ ํธ๋ค๋งํ๋ ๊ฒ์ด ์๋๋ผ, ๋ง์น ์์ธ๊ฐ ๋ฐ์ํ ๋ฒํ ํจ์๋ค์ ๋์ ์คํํด์ฃผ๋ ์ญํ ๊ฐ์์. ๋ต, ๊ทธ์ ํจ์ํ ์ธํฐํ์ด์ค์ฃ . ๊ทธ๋ ๋ค๋ฉด ์ด๊ฒ์ ๋ง๋ ๋ค์ด๋ฐ์ผ๋ก ํ๋ ๊ฒ์ด ์ข์ง ์์๊น์?
๋ง์ฝ `ExceptionHandler`๋ฅผ ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด ์๋์ ๊ฐ์ด ๋์ด์ผํ ๊ฒ ๊ฐ์ต๋๋ค.
```java
public static T handle(Exception e) {
// ํธ๋ค๋ง
}
``` |
@@ -0,0 +1,78 @@
+package bossmonster.exception;
+
+import bossmonster.view.constants.Message;
+
+public class Validator {
+
+ public static String validateInputOfNumber(String inputValue) {
+ validateEmpty(inputValue);
+ validateInteger(inputValue);
+ return inputValue;
+ }
+
+ public static String validatePlayerName(String inputValue) {
+ validateEmpty(inputValue);
+ validateRangeOfPlayerName(inputValue);
+ return inputValue;
+ }
+
+ public static String[] validateInputOfNumbers(String inputValue) {
+ validateSeparated(inputValue);
+ String[] separatedValue = inputValue.split(",");
+ validateArray(separatedValue);
+ for (String value : separatedValue) {
+ validateEmpty(value);
+ validateInteger(value);
+ }
+ return separatedValue;
+ }
+
+ public static int validateBossMonster(int hp) {
+ return validateRangeOfMonster(hp);
+ }
+
+ public static void validatePlayerStatus(int hp, int mp) {
+ if (hp + mp != 200) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_STATUS.getErrorMessage());
+ }
+ }
+
+ private static void validateEmpty(String inputValue) {
+ if (inputValue.isEmpty()) {
+ throw new IllegalArgumentException(Message.ERROR_EMPTY.getErrorMessage());
+ }
+ }
+
+ private static void validateInteger(String inputValue) {
+ try {
+ Integer.parseInt(inputValue);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_INTEGER.getErrorMessage());
+ }
+ }
+
+ private static void validateSeparated(String inputValue) {
+ if (!inputValue.contains(",")) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_COMMA.getErrorMessage());
+ }
+ }
+
+ private static void validateArray(String[] inputValue) {
+ if (inputValue.length != 2) {
+ throw new IllegalArgumentException(Message.ERROR_ARRAY_SIZE.getErrorMessage());
+ }
+ }
+
+ private static int validateRangeOfMonster(int hp) {
+ if (hp < 100 || hp > 300) {
+ throw new IllegalArgumentException(Message.ERROR_BOSS_MONSTER_HP.getErrorMessage());
+ }
+ return hp;
+ }
+
+ private static void validateRangeOfPlayerName(String name) {
+ if (name.length() > 5) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_NAME.getErrorMessage());
+ }
+ }
+} | Java | ํด๋น ํด๋์ค๋ฅผ exception ํจํค์ง์ ๋ฃ์ผ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค!
hp + mp๋ 200์ด์ด์ผํ๋ค ๋ฑ... ๋น์ฆ๋์ค ๋ก์ง์ด ๋ด๊ธด ๋ถ๋ถ์ด ์๋๋ฐ domain ํจํค์ง์ ๋ถ๋ฆฌ๋์ด ์์ด์์! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | controller์์ Validator๋ฅผ ์ฌ์ฉํ ๋๋ ์๊ณ , domain์์ ์ฌ์ฉํ ๋๋ ์๊ตฐ์.
์ด๋ค ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌํ์
จ๋์ง ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,70 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GameHistory {
+
+ private class Turns {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public Turns(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ this.turnCount = turnCount;
+ this.playerName = player.getName();
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = player.getMaxHP();
+ this.playerCurrentHP = player.getCurrentHP();
+ this.playerMaxMP = player.getMaxMP();
+ this.playerCurrentMP = player.getCurrentMP();
+ this.monsterMaxHP = bossMonster.getMaxHP();
+ this.monsterCurrentHP = bossMonster.getCurrentHP();
+ this.gameStatus = gameStatus;
+ }
+ }
+
+ private static final String DEFAULT_ATTACK_TYPE = "";
+ private static final int DEFAULT_DAMAGE = 0;
+ private int recentRecord;
+ private List<Turns> raidHistory = new ArrayList<>();
+
+ public void addHistory(int turnCount, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, DEFAULT_ATTACK_TYPE, DEFAULT_DAMAGE, DEFAULT_DAMAGE, gameStatus, player,
+ bossMonster));
+ }
+
+ public void addHistory(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, playerAttackType, playerAttackDamage, monsterAttackDamage, gameStatus, player,
+ bossMonster));
+ }
+
+ public GameHistoryDto requestRaidHistory() {
+ setRecentRecord();
+ Turns turns = raidHistory.get(recentRecord);
+ return new GameHistoryDto(turns.turnCount, turns.playerName, turns.playerAttackType, turns.playerAttackDamage,
+ turns.monsterAttackDamage, turns.playerMaxHP, turns.playerCurrentHP, turns.playerMaxMP,
+ turns.playerCurrentMP, turns.monsterMaxHP, turns.monsterCurrentHP, turns.gameStatus);
+ }
+
+ private void setRecentRecord() {
+ recentRecord = raidHistory.size() - 1;
+ }
+} | Java | ํด๋น ๊ฐ์ฒด๋ฅผ ๋ง๋์ ๋ชฉ์ ์ด ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,82 @@
+package bossmonster.domain.dto;
+
+public class GameHistoryDto {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public GameHistoryDto(int turnCount, String playerName, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, int playerMaxHP, int playerCurrentHP, int playerMaxMP,
+ int playerCurrentMP, int monsterMaxHP, int monsterCurrentHP, boolean gameStatus) {
+ this.turnCount = turnCount;
+ this.playerName = playerName;
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = playerMaxHP;
+ this.playerCurrentHP = playerCurrentHP;
+ this.playerMaxMP = playerMaxMP;
+ this.playerCurrentMP = playerCurrentMP;
+ this.monsterMaxHP = monsterMaxHP;
+ this.monsterCurrentHP = monsterCurrentHP;
+ this.gameStatus = gameStatus;
+ }
+
+ public int getTurnCount() {
+ return turnCount;
+ }
+
+ public String getPlayerName() {
+ return playerName;
+ }
+
+ public String getPlayerAttackType() {
+ return playerAttackType;
+ }
+
+ public int getPlayerAttackDamage() {
+ return playerAttackDamage;
+ }
+
+ public int getMonsterAttackDamage() {
+ return monsterAttackDamage;
+ }
+
+ public int getPlayerMaxHP() {
+ return playerMaxHP;
+ }
+
+ public int getPlayerCurrentHP() {
+ return playerCurrentHP;
+ }
+
+ public int getPlayerMaxMP() {
+ return playerMaxMP;
+ }
+
+ public int getPlayerCurrentMP() {
+ return playerCurrentMP;
+ }
+
+ public int getMonsterMaxHP() {
+ return monsterMaxHP;
+ }
+
+ public int getMonsterCurrentHP() {
+ return monsterCurrentHP;
+ }
+
+ public boolean isGameStatus() {
+ return gameStatus;
+ }
+} | Java | `isFinish()`๋ `isPlaying()`์ ๊ฐ์ ๊ฒ์ ์ด๋จ๊น์? |
@@ -0,0 +1,119 @@
+package bossmonster.controller;
+
+import bossmonster.domain.AttackType;
+import bossmonster.domain.BossMonster;
+import bossmonster.domain.Player;
+import bossmonster.domain.RaidGame;
+import bossmonster.domain.dto.GameHistoryDto;
+import bossmonster.exception.ExceptionHandler;
+import bossmonster.exception.ManaShortageException;
+import bossmonster.exception.Validator;
+import bossmonster.view.InputView;
+import bossmonster.view.OutputView;
+import bossmonster.view.constants.Message;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.NoSuchElementException;
+
+public class Controller {
+
+ public void startGame() {
+ RaidGame raidGame = createRaid();
+ OutputView.printMessage(Message.OUTPUT_START_RAID);
+
+ while (isGamePossible(raidGame)) {
+ showGameStatus(raidGame);
+ raidGame.executeTurn(selectAttackType(raidGame));
+ showTurnResult(raidGame);
+ }
+ showGameOver(raidGame);
+ }
+
+ private RaidGame createRaid() {
+ OutputView.printMessage(Message.INPUT_MONSTER_HP);
+ BossMonster bossMonster = ExceptionHandler.retryInput(this::createBossMonster);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_NAME);
+ String playerName = ExceptionHandler.retryInput(this::requestPlayerName);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_HP_MP);
+ Player player = ExceptionHandler.retryInput(() -> createPlayer(playerName));
+
+ return new RaidGame(bossMonster, player);
+ }
+
+ private BossMonster createBossMonster() {
+ int bossMonsterHP = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ return new BossMonster(bossMonsterHP);
+ }
+
+ private String requestPlayerName() {
+ return Validator.validatePlayerName(InputView.readLine());
+ }
+
+ private Player createPlayer(String playerName) {
+ String[] playerStatus = Validator.validateInputOfNumbers(InputView.readLine());
+ int playerHP = Integer.parseInt(playerStatus[0]);
+ int playerMP = Integer.parseInt(playerStatus[1]);
+ return new Player(playerName, playerHP, playerMP);
+ }
+
+ private boolean isGamePossible(RaidGame raidGame) {
+ return raidGame.getGameHistory().isGameStatus();
+ }
+
+ private void showGameStatus(RaidGame raidGame) {
+ OutputView.printGameStatus(raidGame.getGameHistory());
+ }
+
+ private AttackType selectAttackType(RaidGame raidGame) {
+ OutputView.printAttackType(provideAttackType());
+ return ExceptionHandler.retryInput(() -> requestAttackType(raidGame));
+ }
+
+ private LinkedHashMap<Integer, String> provideAttackType() {
+ LinkedHashMap<Integer, String> attackType = new LinkedHashMap<>();
+ for (AttackType type : AttackType.values()) {
+ if (type.getNumber() != 0) {
+ attackType.put(type.getNumber(), type.getName());
+ }
+ }
+ return attackType;
+ }
+
+ private AttackType requestAttackType(RaidGame raidGame) {
+ int valueInput = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ AttackType attackType = Arrays.stream(AttackType.values())
+ .filter(type -> type.getNumber() == valueInput && type.getNumber() != 0)
+ .findFirst()
+ .orElseThrow(() -> new NoSuchElementException(Message.ERROR_PLAYER_ATTACK_TYPE.getErrorMessage()));
+ checkPlayerMP(raidGame, attackType);
+ return attackType;
+ }
+
+ private void checkPlayerMP(RaidGame raidGame, AttackType attackType) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getPlayerCurrentMP() < attackType.getMpUsage()) {
+ throw new ManaShortageException(Message.ERROR_PLAYER_MANA_SHORTAGE.getErrorMessage());
+ }
+ }
+
+ private void showTurnResult(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ OutputView.printPlayerTurnResult(gameHistoryDto);
+ if (gameHistoryDto.getMonsterCurrentHP() != 0) {
+ OutputView.printBossMonsterTurnResult(gameHistoryDto);
+ }
+ }
+
+ private void showGameOver(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getMonsterCurrentHP() == 0) {
+ OutputView.printPlayerWin(gameHistoryDto);
+ }
+ if (gameHistoryDto.getPlayerCurrentHP() == 0) {
+ OutputView.printGameStatus(gameHistoryDto);
+ OutputView.printPlayerLose(gameHistoryDto);
+ }
+ }
+} | Java | DTO๋ก ๋ฐ์๋ค๋ฉด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ํ๋ ์ด์ด ์ ๋ณด๋ฅผ ๋ฐ์์ ์ ์์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,73 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+
+public class RaidGame {
+
+ private static final int DEFAULT_VALUE = 0;
+
+ private BossMonster bossMonster;
+ private Player player;
+ private GameHistory gameHistory;
+ private boolean status;
+ private int turns;
+
+ public RaidGame(BossMonster bossMonster, Player player) {
+ this.bossMonster = bossMonster;
+ this.player = player;
+ this.gameHistory = new GameHistory();
+ this.status = true;
+ this.turns = DEFAULT_VALUE;
+
+ gameHistory.addHistory(turns, status, player, bossMonster);
+ }
+
+ public void executeTurn(AttackType attackType) {
+ if (isGameProgress()) {
+ increaseTurn();
+ int playerAttackDamage = DEFAULT_VALUE;
+ int monsterAttackDamage = DEFAULT_VALUE;
+ playerAttackDamage = executePlayerTurn(attackType);
+ if (bossMonster.isAlive()) {
+ monsterAttackDamage = executeBossMonsterTurn();
+ }
+ setStatus();
+ createTurnHistory(attackType, playerAttackDamage, monsterAttackDamage);
+ }
+ }
+
+ public GameHistoryDto getGameHistory() {
+ return gameHistory.requestRaidHistory();
+ }
+
+ private boolean isGameProgress() {
+ setStatus();
+ return status;
+ }
+
+ private void setStatus() {
+ status = player.isAlive() && bossMonster.isAlive();
+ }
+
+ private void increaseTurn() {
+ turns ++;
+ }
+
+ private int executePlayerTurn(AttackType attackType) {
+ int playerAttackDamage = player.attack(attackType);
+ bossMonster.takeDamage(playerAttackDamage);
+ return playerAttackDamage;
+ }
+
+ private int executeBossMonsterTurn() {
+ int monsterAttackDamage = bossMonster.attack();
+ player.takeDamage(monsterAttackDamage);
+ return monsterAttackDamage;
+ }
+
+ private void createTurnHistory(AttackType playerAttackType, int playerAttackDamage, int monsterAttackDamage) {
+ gameHistory.addHistory(turns, playerAttackType.getName(), playerAttackDamage, monsterAttackDamage, status,
+ player,
+ bossMonster);
+ }
+} | Java | ์ปจํธ๋กค๋ฌ์ ๋๋ฉ์ธ์ ์ ๋ง ์ ๋ถ๋ฆฌํ์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | ์ด ๋ถ๋ถ ์ด๋ ค์ฐ์
จ์ํ
๋ฐ ์ญํ ๋ถ๋ฐฐ ์ํ์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,63 @@
+package bossmonster.view.constants;
+
+public enum Message {
+
+ /* Input Request */
+ INPUT_MONSTER_HP("๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."),
+ INPUT_PLAYER_NAME("\n" + "ํ๋ ์ด์ด์ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์"),
+ INPUT_PLAYER_HP_MP("\n" + "ํ๋ ์ด์ด์ HP์ MP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.(,๋ก ๊ตฌ๋ถ)"),
+ INPUT_ATTACK_TYPE_SELECT("\n" + "์ด๋ค ๊ณต๊ฒฉ์ ํ์๊ฒ ์ต๋๊น?"),
+ INPUT_ATTACK_TYPE("%d. %s"),
+
+ /* Output Message */
+ OUTPUT_START_RAID("\n" + "๋ณด์ค ๋ ์ด๋๋ฅผ ์์ํฉ๋๋ค!"),
+ OUTPUT_LINE_ONE("============================"),
+ OUTPUT_BOSS_STATUS("BOSS HP [%d/%d]"),
+ OUTPUT_LINE_TWO("____________________________"),
+ OUTPUT_BOSS_DEFAULT(" ^-^" + "\n" +
+ " / 0 0 \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ - /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_DAMAGED(" ^-^" + "\n" +
+ " / x x \\" + "\n" +
+ "( \"\\ )" + "\n" +
+ " \\ ^ /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_WIN(" ^-^" + "\n" +
+ " / ^ ^ \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ 3 /" + "\n" +
+ " - ^ -"),
+ OUTPUT_PLAYER_STATUS("\n" + "%s HP [%d/%d] MP [%d/%d]"),
+ OUTPUT_TURN_RESULT_PLAYER("\n" + "%s์ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_TURN_RESULT_BOSS("๋ณด์ค๊ฐ ๊ณต๊ฒฉ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_GAME_OVER_PLAYER_WIN("\n" + "%s ๋์ด %d๋ฒ์ ์ ํฌ ๋์ ๋ณด์ค ๋ชฌ์คํฐ๋ฅผ ์ก์์ต๋๋ค."),
+ OUTPUT_GAME_OVER_PLAYER_LOSE("\n" + "%s์ HP๊ฐ %d์ด ๋์์ต๋๋ค." + "\n" + "๋ณด์ค ๋ ์ด๋์ ์คํจํ์ต๋๋ค."),
+
+ /* Error Message */
+ ERROR_EMPTY("์
๋ ฅ๊ฐ์ด ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_INTEGER("์ซ์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_COMMA("์ฝค๋ง(',')๋ฅผ ํตํด ๊ตฌ๋ถํด์ ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_ARRAY_SIZE("์
๋ ฅ๋ ํญ๋ชฉ์ด ๋ง์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_BOSS_MONSTER_HP("๋ณด์ค๋ชฌ์คํฐ์ HP๋ 100 ์ด์ 300 ์ดํ๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_NAME("ํ๋ ์ด์ด์ ์ด๋ฆ์ 5์ ์ดํ๋ง ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_STATUS("ํ๋ ์ด์ด์ HP์ MP์ ํฉ์ 200์ผ๋ก๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_ATTACK_TYPE("์กด์ฌํ์ง ์๋ ๊ณต๊ฒฉ ๋ฐฉ์์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_MANA_SHORTAGE("๋ง๋๊ฐ ๋ถ์กฑํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+
+ public static final String ERROR_PREFIX = "[ERROR] ";
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getErrorMessage() {
+ return ERROR_PREFIX + message;
+ }
+} | Java | ์ ๋ ์์๋ฅผ ํ ํ์ผ๋ก ๊ด๋ฆฌํ์๋๋ฐ์.
์์๋ฅผ ํ ํ์ผ๋ก ๊ด๋ฆฌํ๊ฒ ๋๋ฉด ํ ํ๋ก์ ํธ๋ฅผ ์งํํ ๋ ์์ ํ์ผ์์ ์ถฉ๋์ด ๋ง์ด ์ผ์ด๋๋๋ผ๊ตฌ์.
์ด๋ฐ ๋ฌธ์ ๋ฅผ ์ด๋ป๊ฒ ํด๊ฒฐํ๋ฉด ์ข์๊น์? |
@@ -0,0 +1,63 @@
+package bossmonster.view.constants;
+
+public enum Message {
+
+ /* Input Request */
+ INPUT_MONSTER_HP("๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."),
+ INPUT_PLAYER_NAME("\n" + "ํ๋ ์ด์ด์ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์"),
+ INPUT_PLAYER_HP_MP("\n" + "ํ๋ ์ด์ด์ HP์ MP๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.(,๋ก ๊ตฌ๋ถ)"),
+ INPUT_ATTACK_TYPE_SELECT("\n" + "์ด๋ค ๊ณต๊ฒฉ์ ํ์๊ฒ ์ต๋๊น?"),
+ INPUT_ATTACK_TYPE("%d. %s"),
+
+ /* Output Message */
+ OUTPUT_START_RAID("\n" + "๋ณด์ค ๋ ์ด๋๋ฅผ ์์ํฉ๋๋ค!"),
+ OUTPUT_LINE_ONE("============================"),
+ OUTPUT_BOSS_STATUS("BOSS HP [%d/%d]"),
+ OUTPUT_LINE_TWO("____________________________"),
+ OUTPUT_BOSS_DEFAULT(" ^-^" + "\n" +
+ " / 0 0 \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ - /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_DAMAGED(" ^-^" + "\n" +
+ " / x x \\" + "\n" +
+ "( \"\\ )" + "\n" +
+ " \\ ^ /" + "\n" +
+ " - ^ -"),
+ OUTPUT_BOSS_WIN(" ^-^" + "\n" +
+ " / ^ ^ \\" + "\n" +
+ "( \" )" + "\n" +
+ " \\ 3 /" + "\n" +
+ " - ^ -"),
+ OUTPUT_PLAYER_STATUS("\n" + "%s HP [%d/%d] MP [%d/%d]"),
+ OUTPUT_TURN_RESULT_PLAYER("\n" + "%s์ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_TURN_RESULT_BOSS("๋ณด์ค๊ฐ ๊ณต๊ฒฉ ํ์ต๋๋ค. (์
ํ ๋ฐ๋ฏธ์ง: %d)"),
+ OUTPUT_GAME_OVER_PLAYER_WIN("\n" + "%s ๋์ด %d๋ฒ์ ์ ํฌ ๋์ ๋ณด์ค ๋ชฌ์คํฐ๋ฅผ ์ก์์ต๋๋ค."),
+ OUTPUT_GAME_OVER_PLAYER_LOSE("\n" + "%s์ HP๊ฐ %d์ด ๋์์ต๋๋ค." + "\n" + "๋ณด์ค ๋ ์ด๋์ ์คํจํ์ต๋๋ค."),
+
+ /* Error Message */
+ ERROR_EMPTY("์
๋ ฅ๊ฐ์ด ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_INTEGER("์ซ์๋ง ์
๋ ฅ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_NOT_COMMA("์ฝค๋ง(',')๋ฅผ ํตํด ๊ตฌ๋ถํด์ ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_ARRAY_SIZE("์
๋ ฅ๋ ํญ๋ชฉ์ด ๋ง์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_BOSS_MONSTER_HP("๋ณด์ค๋ชฌ์คํฐ์ HP๋ 100 ์ด์ 300 ์ดํ๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_NAME("ํ๋ ์ด์ด์ ์ด๋ฆ์ 5์ ์ดํ๋ง ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_STATUS("ํ๋ ์ด์ด์ HP์ MP์ ํฉ์ 200์ผ๋ก๋ง ์ค์ ๊ฐ๋ฅํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_ATTACK_TYPE("์กด์ฌํ์ง ์๋ ๊ณต๊ฒฉ ๋ฐฉ์์
๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์."),
+ ERROR_PLAYER_MANA_SHORTAGE("๋ง๋๊ฐ ๋ถ์กฑํฉ๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.");
+
+ public static final String ERROR_PREFIX = "[ERROR] ";
+ private final String message;
+
+ Message(String message) {
+ this.message = message;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public String getErrorMessage() {
+ return ERROR_PREFIX + message;
+ }
+} | Java | ์
์ถ๋ ฅ์ ์ฌ์ฉ๋๋ ๋ฉ์์ง๋ค์ InputView์ OutputView์์ ๊ฐ๊ฐ ์ฌ์ฉํ๋ค ๋ณด๋ ๋ฉ์์ง๊ฐ ๋ง์ ๊ฒฝ์ฐ์๋ ์๋ฆฌ๋ฅผ ๋ง์ด ์ฐจ์งํ๋ ๊ฒ ๊ฐ์์ ํ ํ์ผ๋ก ๊ด๋ฆฌํ๊ณ ๊ฐ์ ธ๋ค ์ฐ๋ ํํ๋ก ํด์ผ๊ฒ ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์ ๊ฐ ๊ณต๋ถํ์ง ์ผ๋ง ๋์ง ์์ ํ ํ๋ก์ ํธ๋ฅผ ๊ฒฝํํด๋ณธ ์ ์ด ์์ง ์์ด ์ด๋ค ๋ถ๋ถ์์ ์ถฉ๋์ด ์ผ์ด๋๋์ง๋ ์ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค. ๋ถ๋ช
์์๋ฅผ ํ ํ์ผ๋ก ๊ด๋ฆฌํ๋ค๋ณด๋ฉด ๋ฌธ์ ๊ฐ ์๊ธธ ์๋ ์์ ๊ฒ ๊ฐ์๋ฐ์. ์ด ๋ถ๋ถ์ ์ฌ๋ฌ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ์กฐ๊ธ ๋ ๊ณ ๋ฏผํด ๋ด์ผ ํ ๋ถ๋ถ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,78 @@
+package bossmonster.exception;
+
+import bossmonster.view.constants.Message;
+
+public class Validator {
+
+ public static String validateInputOfNumber(String inputValue) {
+ validateEmpty(inputValue);
+ validateInteger(inputValue);
+ return inputValue;
+ }
+
+ public static String validatePlayerName(String inputValue) {
+ validateEmpty(inputValue);
+ validateRangeOfPlayerName(inputValue);
+ return inputValue;
+ }
+
+ public static String[] validateInputOfNumbers(String inputValue) {
+ validateSeparated(inputValue);
+ String[] separatedValue = inputValue.split(",");
+ validateArray(separatedValue);
+ for (String value : separatedValue) {
+ validateEmpty(value);
+ validateInteger(value);
+ }
+ return separatedValue;
+ }
+
+ public static int validateBossMonster(int hp) {
+ return validateRangeOfMonster(hp);
+ }
+
+ public static void validatePlayerStatus(int hp, int mp) {
+ if (hp + mp != 200) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_STATUS.getErrorMessage());
+ }
+ }
+
+ private static void validateEmpty(String inputValue) {
+ if (inputValue.isEmpty()) {
+ throw new IllegalArgumentException(Message.ERROR_EMPTY.getErrorMessage());
+ }
+ }
+
+ private static void validateInteger(String inputValue) {
+ try {
+ Integer.parseInt(inputValue);
+ } catch (NumberFormatException e) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_INTEGER.getErrorMessage());
+ }
+ }
+
+ private static void validateSeparated(String inputValue) {
+ if (!inputValue.contains(",")) {
+ throw new IllegalArgumentException(Message.ERROR_NOT_COMMA.getErrorMessage());
+ }
+ }
+
+ private static void validateArray(String[] inputValue) {
+ if (inputValue.length != 2) {
+ throw new IllegalArgumentException(Message.ERROR_ARRAY_SIZE.getErrorMessage());
+ }
+ }
+
+ private static int validateRangeOfMonster(int hp) {
+ if (hp < 100 || hp > 300) {
+ throw new IllegalArgumentException(Message.ERROR_BOSS_MONSTER_HP.getErrorMessage());
+ }
+ return hp;
+ }
+
+ private static void validateRangeOfPlayerName(String name) {
+ if (name.length() > 5) {
+ throw new IllegalArgumentException(Message.ERROR_PLAYER_NAME.getErrorMessage());
+ }
+ }
+} | Java | ์ฒ์์๋ ์
๋ ฅ ๋จ๊ณ์์ ๊ฒ์ฆํ๋ ๋ถ๋ถ๋ง ๋ฐ๋ก ๋ฃ๊ณ , ๋๋ฉ์ธ ๋ถ๋ถ์์ ๊ฒ์ฆํ ๋ก์ง์ ๊ฐ ๋๋ฉ์ธ ํด๋์ค์์ ์งํํ๋ ๊ฒ์ผ๋ก ํ์๋๋ฐ์. ํ๋ค๋ณด๋ ๊ฒน์น๋ ๋ถ๋ถ์ด ์๋ ๊ฒ ๊ฐ์์ ์ ํ์ฉํ์ง ๋ชปํ์ต๋๋ค. ์์์ ์๊ฒฌ์ ์ฃผ์ ๊ฒ ์ฒ๋ผ DTO๋ฅผ ํ์ฉํ๋ฉด ๋ ๊น๋ํ๊ฒ ๊ฒ์ฆ ๋ก์ง์ ๋ถ๋ฐฐํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,17 @@
+package bossmonster.exception;
+
+import bossmonster.view.OutputView;
+import java.util.function.Supplier;
+
+public class ExceptionHandler {
+
+ public static <T> T retryInput(Supplier<T> supplier) {
+ while (true) {
+ try {
+ return supplier.get();
+ } catch (Exception exception) {
+ OutputView.printError(exception);
+ }
+ }
+ }
+} | Java | ์ ๋ ์๋ฌ ๋ฐ์ ์, ์ฌ์
๋ ฅ์ ๋ฐ์์ผ ํ๋ ๋ถ๋ถ์ ๋ํด์ ๊ณ ๋ฏผ์ ๋ง์ด ํ๋ค๊ฐ.. ๋ค๋ฅธ ๋ถ๋ค์ ์ฝ๋ ๋ฆฌ๋ทฐ๋ฅผ ๋ณด๊ณ ์ฌ๋ฌ๊ฐ์ง ์ฐพ์๋ณด๋ค ์ด๋ฐ ๋ฐฉ๋ฒ๋ ์๋ค๋ ๊ฒ์ ์๊ฒ ๋์์ต๋๋ค. ํ๋ ๋ง๋ค์ด ๋๋ฉด ํ์ฉ์ฑ์ด ์ข์ ๊ฒ ๊ฐ์์. ๋ค์ด๋ฐ์ ๋ํด์๋ ๋ง์ ์ฃผ์ ๋ถ๋ถ์ฒ๋ผ ๊ฐ๋
์ฑ์ด ์ ์ข์ ์ง ์ ์๊ธฐ ๋๋ฌธ์ ๋ ๊ณ ๋ฏผ์ด ํ์ํ ๊ฒ ๊ฐ์ต๋๋ค. ๋ง์ ๋ค์ด๋ฐ์ ๋ํด์๋ ๋ง์ด ์ ๊ฒฝ์ ์ฐ์ง ๋ชปํ๋ ๊ฒ ๊ฐ๋ค์.
ํด๋์ค๋ช
์ ๊ดํด ์๊ฒฌ ์ฃผ์ ๋ถ๋ถ์ ๋ํด์๋ ๊ฐ์ฌํฉ๋๋ค. ์คํ๋ง์ ๋ํ ๊ฐ๋
์ด ์์ง ์์ด์ ์คํ๋ง์ ExceptionHandler๊ฐ ์ด๋ป๊ฒ ์ฌ์ฉ๋๋์ง ์ฒ์ ์์๋ค์.. ์ ๋ ๋จ์ํ ์์ธ๋ฅผ ๊ด๋ฆฌํ๋ค๋ ๋ถ๋ถ์์ ์ ๋ ๊ฒ ์ฌ์ฉํ๋๋ฐ ๋ค๋ฅธ ๋ถ๋ค์ด ์ฝ๊ธฐ์๋ ํท๊ฐ๋ฆด ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. ์ด ๋ถ๋ถ๋ ์ฐธ๊ณ ํ๊ฒ ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,82 @@
+package bossmonster.domain.dto;
+
+public class GameHistoryDto {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public GameHistoryDto(int turnCount, String playerName, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, int playerMaxHP, int playerCurrentHP, int playerMaxMP,
+ int playerCurrentMP, int monsterMaxHP, int monsterCurrentHP, boolean gameStatus) {
+ this.turnCount = turnCount;
+ this.playerName = playerName;
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = playerMaxHP;
+ this.playerCurrentHP = playerCurrentHP;
+ this.playerMaxMP = playerMaxMP;
+ this.playerCurrentMP = playerCurrentMP;
+ this.monsterMaxHP = monsterMaxHP;
+ this.monsterCurrentHP = monsterCurrentHP;
+ this.gameStatus = gameStatus;
+ }
+
+ public int getTurnCount() {
+ return turnCount;
+ }
+
+ public String getPlayerName() {
+ return playerName;
+ }
+
+ public String getPlayerAttackType() {
+ return playerAttackType;
+ }
+
+ public int getPlayerAttackDamage() {
+ return playerAttackDamage;
+ }
+
+ public int getMonsterAttackDamage() {
+ return monsterAttackDamage;
+ }
+
+ public int getPlayerMaxHP() {
+ return playerMaxHP;
+ }
+
+ public int getPlayerCurrentHP() {
+ return playerCurrentHP;
+ }
+
+ public int getPlayerMaxMP() {
+ return playerMaxMP;
+ }
+
+ public int getPlayerCurrentMP() {
+ return playerCurrentMP;
+ }
+
+ public int getMonsterMaxHP() {
+ return monsterMaxHP;
+ }
+
+ public int getMonsterCurrentHP() {
+ return monsterCurrentHP;
+ }
+
+ public boolean isGameStatus() {
+ return gameStatus;
+ }
+} | Java | ๋จ์ํ ํ์ฌ ๊ฒ์ ์ํ๋ฅผ ๋ฌป๊ธฐ ์ํด ์ผ๋ ๊ฒ ๊ฐ์๋ฐ ์๊ฒฌ ์ฃผ์ ๋ฉ์๋๋ช
์ด ๋ฐํ๋๋ ๊ฐ์ ์ดํดํ๊ธฐ์ ๋ ์ง๊ด์ ์ด๋ค์! |
@@ -0,0 +1,73 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+
+public class RaidGame {
+
+ private static final int DEFAULT_VALUE = 0;
+
+ private BossMonster bossMonster;
+ private Player player;
+ private GameHistory gameHistory;
+ private boolean status;
+ private int turns;
+
+ public RaidGame(BossMonster bossMonster, Player player) {
+ this.bossMonster = bossMonster;
+ this.player = player;
+ this.gameHistory = new GameHistory();
+ this.status = true;
+ this.turns = DEFAULT_VALUE;
+
+ gameHistory.addHistory(turns, status, player, bossMonster);
+ }
+
+ public void executeTurn(AttackType attackType) {
+ if (isGameProgress()) {
+ increaseTurn();
+ int playerAttackDamage = DEFAULT_VALUE;
+ int monsterAttackDamage = DEFAULT_VALUE;
+ playerAttackDamage = executePlayerTurn(attackType);
+ if (bossMonster.isAlive()) {
+ monsterAttackDamage = executeBossMonsterTurn();
+ }
+ setStatus();
+ createTurnHistory(attackType, playerAttackDamage, monsterAttackDamage);
+ }
+ }
+
+ public GameHistoryDto getGameHistory() {
+ return gameHistory.requestRaidHistory();
+ }
+
+ private boolean isGameProgress() {
+ setStatus();
+ return status;
+ }
+
+ private void setStatus() {
+ status = player.isAlive() && bossMonster.isAlive();
+ }
+
+ private void increaseTurn() {
+ turns ++;
+ }
+
+ private int executePlayerTurn(AttackType attackType) {
+ int playerAttackDamage = player.attack(attackType);
+ bossMonster.takeDamage(playerAttackDamage);
+ return playerAttackDamage;
+ }
+
+ private int executeBossMonsterTurn() {
+ int monsterAttackDamage = bossMonster.attack();
+ player.takeDamage(monsterAttackDamage);
+ return monsterAttackDamage;
+ }
+
+ private void createTurnHistory(AttackType playerAttackType, int playerAttackDamage, int monsterAttackDamage) {
+ gameHistory.addHistory(turns, playerAttackType.getName(), playerAttackDamage, monsterAttackDamage, status,
+ player,
+ bossMonster);
+ }
+} | Java | ๊ฐ์ฌํฉ๋๋ค. ๊ฐ์ฒด์งํฅ์ ๊ณต๋ถํ๋ฉด์ MVC ํจํด์ ๊ตฌ์ ๋ฐ์ง ์๊ณ ๋๋ฉ์ธ ๋ก์ง์ ๋๋ฉ์ธ ๋ก์ง์์๋ง์ผ๋ก๋ ๋์๊ฐ ์ ์๋๋ก ๊ตฌํํ๊ณ ์ ์ฐ์ตํ๊ณ ์์ต๋๋ค. ์ปจํธ๋กค๋ฌ๊ฐ ์์ด๋ ๋๋ฉ์ธ ๋ด์์๋ ๋๋ฉ์ธ ๋ก์ง์ด ์ ๋์๊ฐ์ผ ํ๋ค๊ณ ์๊ฐํ๋ฉด์ ๊ณ ๋ฏผํ๋ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | ์ ๋์๋ค๋ ๋คํ์ด๋ค์ ์ข๊ฒ ๋ด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,61 @@
+package bossmonster.domain;
+
+import bossmonster.exception.Validator;
+
+public class Player {
+
+ private String name;
+ private PlayerStatus status;
+
+ public Player(String name, int maxHP, int maxMP) {
+ this.name = Validator.validatePlayerName(name);
+ Validator.validatePlayerStatus(maxHP, maxMP);
+ this.status = new PlayerStatus(maxHP, maxMP);
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public boolean isAlive() {
+ return status.isAlive();
+ }
+
+ public int getMaxHP() {
+ return status.getMaxHP();
+ }
+
+ public int getCurrentHP() {
+ return status.getCurrentHP();
+ }
+
+ public int getMaxMP() {
+ return status.getMaxMP();
+ }
+
+ public int getCurrentMP() {
+ return status.getCurrentMP();
+ }
+
+ public int attack(AttackType attackType) {
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_NORMAL.getName())) {
+ recoverMP(attackType.getMpRecover());
+ }
+ if (attackType.getName().equals(AttackType.ATTACK_TYPE_MAGIC.getName())) {
+ consumeMP(attackType.getMpUsage());
+ }
+ return attackType.getDamage();
+ }
+
+ public void takeDamage(int damage) {
+ status.setCurrentHP(status.getCurrentHP() - damage);
+ }
+
+ private void recoverMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() + mp);
+ }
+
+ private void consumeMP(int mp) {
+ status.setCurrentMP(status.getCurrentMP() - mp);
+ }
+} | Java | ๋ฐ์ Validator์ ์ฌ์ฉ์ ๋ํด ์ฃผ์ ์๊ฒฌ๊ณผ ๋์ผํ ๋ฌธ์ ๋ค์.. ์
์ถ๋ ฅ์ ๊ด๋ จ๋ ๊ฒ์ฆ๊ณผ ๋๋ฉ์ธ ๋ถ๋ถ์ ๊ฒ์ฆ์ ๋ถ๋ฆฌํด์ ์ฌ์ฉํ๋๊ฒ ๋ง์ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,70 @@
+package bossmonster.domain;
+
+import bossmonster.domain.dto.GameHistoryDto;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GameHistory {
+
+ private class Turns {
+
+ private int turnCount;
+ private String playerName;
+ private String playerAttackType;
+ private int playerAttackDamage;
+ private int monsterAttackDamage;
+ private int playerMaxHP;
+ private int playerCurrentHP;
+ private int playerMaxMP;
+ private int playerCurrentMP;
+ private int monsterMaxHP;
+ private int monsterCurrentHP;
+ private boolean gameStatus;
+
+ public Turns(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ this.turnCount = turnCount;
+ this.playerName = player.getName();
+ this.playerAttackType = playerAttackType;
+ this.playerAttackDamage = playerAttackDamage;
+ this.monsterAttackDamage = monsterAttackDamage;
+ this.playerMaxHP = player.getMaxHP();
+ this.playerCurrentHP = player.getCurrentHP();
+ this.playerMaxMP = player.getMaxMP();
+ this.playerCurrentMP = player.getCurrentMP();
+ this.monsterMaxHP = bossMonster.getMaxHP();
+ this.monsterCurrentHP = bossMonster.getCurrentHP();
+ this.gameStatus = gameStatus;
+ }
+ }
+
+ private static final String DEFAULT_ATTACK_TYPE = "";
+ private static final int DEFAULT_DAMAGE = 0;
+ private int recentRecord;
+ private List<Turns> raidHistory = new ArrayList<>();
+
+ public void addHistory(int turnCount, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, DEFAULT_ATTACK_TYPE, DEFAULT_DAMAGE, DEFAULT_DAMAGE, gameStatus, player,
+ bossMonster));
+ }
+
+ public void addHistory(int turnCount, String playerAttackType, int playerAttackDamage,
+ int monsterAttackDamage, boolean gameStatus, Player player, BossMonster bossMonster) {
+ raidHistory.add(
+ new Turns(turnCount, playerAttackType, playerAttackDamage, monsterAttackDamage, gameStatus, player,
+ bossMonster));
+ }
+
+ public GameHistoryDto requestRaidHistory() {
+ setRecentRecord();
+ Turns turns = raidHistory.get(recentRecord);
+ return new GameHistoryDto(turns.turnCount, turns.playerName, turns.playerAttackType, turns.playerAttackDamage,
+ turns.monsterAttackDamage, turns.playerMaxHP, turns.playerCurrentHP, turns.playerMaxMP,
+ turns.playerCurrentMP, turns.monsterMaxHP, turns.monsterCurrentHP, turns.gameStatus);
+ }
+
+ private void setRecentRecord() {
+ recentRecord = raidHistory.size() - 1;
+ }
+} | Java | ์ปจํธ๋กค๋ฌ์์ ๊ฒ์์ ์ ์ดํ๋ ๊ฒ ๋ณด๋ค RaidGame ํด๋์ค์์ ์ ์ฒด์ ์ธ ๊ฒ์ ๋ก์ง์ด ๋์๊ฐ๋ ํํ๋ก ์๊ฐํ๊ณ ๊ตฌํํ๋ค ๋ณด๋ ๊ฒ์์ด ์คํ๋๋ฉด์ ํ์คํ ๋ฆฌ๋ฅผ ๋จ๊ธฐ๊ณ ์ปจํธ๋กค๋ฌ์์ ์ถ๋ ฅ์ ์ํด์๋ ๊ธฐ๋ก์ ๋จ๊ฒจ์ง ๋ถ๋ถ์ ๊ฐ์ ธ๋ค๊ฐ ๋ณด์ฌ์ค์ผ ์์กด์ฑ์ด ๋จ์ด์ง ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํ์ต๋๋ค. ์ค์ ๋ก ํด ์ ๊ฒ์์ ํ๋ฉด ๋งค ํด๋ง๋ค ํด์ ๋ํ ๊ธฐ๋ก์ด ๊ฒ์์์๋ ๋จ๊ฒ ๋๋๋ฐ ์ด๊ฑธ ์๊ฐํด์ ๋ง๋ค์๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,119 @@
+package bossmonster.controller;
+
+import bossmonster.domain.AttackType;
+import bossmonster.domain.BossMonster;
+import bossmonster.domain.Player;
+import bossmonster.domain.RaidGame;
+import bossmonster.domain.dto.GameHistoryDto;
+import bossmonster.exception.ExceptionHandler;
+import bossmonster.exception.ManaShortageException;
+import bossmonster.exception.Validator;
+import bossmonster.view.InputView;
+import bossmonster.view.OutputView;
+import bossmonster.view.constants.Message;
+import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.NoSuchElementException;
+
+public class Controller {
+
+ public void startGame() {
+ RaidGame raidGame = createRaid();
+ OutputView.printMessage(Message.OUTPUT_START_RAID);
+
+ while (isGamePossible(raidGame)) {
+ showGameStatus(raidGame);
+ raidGame.executeTurn(selectAttackType(raidGame));
+ showTurnResult(raidGame);
+ }
+ showGameOver(raidGame);
+ }
+
+ private RaidGame createRaid() {
+ OutputView.printMessage(Message.INPUT_MONSTER_HP);
+ BossMonster bossMonster = ExceptionHandler.retryInput(this::createBossMonster);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_NAME);
+ String playerName = ExceptionHandler.retryInput(this::requestPlayerName);
+
+ OutputView.printMessage(Message.INPUT_PLAYER_HP_MP);
+ Player player = ExceptionHandler.retryInput(() -> createPlayer(playerName));
+
+ return new RaidGame(bossMonster, player);
+ }
+
+ private BossMonster createBossMonster() {
+ int bossMonsterHP = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ return new BossMonster(bossMonsterHP);
+ }
+
+ private String requestPlayerName() {
+ return Validator.validatePlayerName(InputView.readLine());
+ }
+
+ private Player createPlayer(String playerName) {
+ String[] playerStatus = Validator.validateInputOfNumbers(InputView.readLine());
+ int playerHP = Integer.parseInt(playerStatus[0]);
+ int playerMP = Integer.parseInt(playerStatus[1]);
+ return new Player(playerName, playerHP, playerMP);
+ }
+
+ private boolean isGamePossible(RaidGame raidGame) {
+ return raidGame.getGameHistory().isGameStatus();
+ }
+
+ private void showGameStatus(RaidGame raidGame) {
+ OutputView.printGameStatus(raidGame.getGameHistory());
+ }
+
+ private AttackType selectAttackType(RaidGame raidGame) {
+ OutputView.printAttackType(provideAttackType());
+ return ExceptionHandler.retryInput(() -> requestAttackType(raidGame));
+ }
+
+ private LinkedHashMap<Integer, String> provideAttackType() {
+ LinkedHashMap<Integer, String> attackType = new LinkedHashMap<>();
+ for (AttackType type : AttackType.values()) {
+ if (type.getNumber() != 0) {
+ attackType.put(type.getNumber(), type.getName());
+ }
+ }
+ return attackType;
+ }
+
+ private AttackType requestAttackType(RaidGame raidGame) {
+ int valueInput = Integer.parseInt(Validator.validateInputOfNumber(InputView.readLine()));
+ AttackType attackType = Arrays.stream(AttackType.values())
+ .filter(type -> type.getNumber() == valueInput && type.getNumber() != 0)
+ .findFirst()
+ .orElseThrow(() -> new NoSuchElementException(Message.ERROR_PLAYER_ATTACK_TYPE.getErrorMessage()));
+ checkPlayerMP(raidGame, attackType);
+ return attackType;
+ }
+
+ private void checkPlayerMP(RaidGame raidGame, AttackType attackType) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getPlayerCurrentMP() < attackType.getMpUsage()) {
+ throw new ManaShortageException(Message.ERROR_PLAYER_MANA_SHORTAGE.getErrorMessage());
+ }
+ }
+
+ private void showTurnResult(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ OutputView.printPlayerTurnResult(gameHistoryDto);
+ if (gameHistoryDto.getMonsterCurrentHP() != 0) {
+ OutputView.printBossMonsterTurnResult(gameHistoryDto);
+ }
+ }
+
+ private void showGameOver(RaidGame raidGame) {
+ GameHistoryDto gameHistoryDto = raidGame.getGameHistory();
+ if (gameHistoryDto.getMonsterCurrentHP() == 0) {
+ OutputView.printPlayerWin(gameHistoryDto);
+ }
+ if (gameHistoryDto.getPlayerCurrentHP() == 0) {
+ OutputView.printGameStatus(gameHistoryDto);
+ OutputView.printPlayerLose(gameHistoryDto);
+ }
+ }
+} | Java | ์ ๊ฐ ์์ฑํ ์ฝ๋์ InputView๋ ๋จ์ํ ์
๋ ฅ์ ๋ฐ๋ ๋ฉ์๋๋ง ์กด์ฌ ํ๋๋ฐ์. ์ด๋ ๊ฒ ๊ตฌ์ฑํ๋ค ๋ณด๋ ์ปจํธ๋กค๋ฌ์์ ์
๋ ฅ๋ฐ์ ๋ถ๋ถ์ ๋ํด์ ๊ฒ์ฆ์ ํ๊ณ ๋ณํํด์ ๋๋ฉ์ธ์ ์์ฑํ๊ฑฐ๋ ๋ฐ์ดํฐ๋ฅผ ๋๊ฒจ์ค์ผ ํด์ ํ ์ผ์ด ๋ง์์ง๋ ๋จ์ ์ด ์๋ค์. ๋ง์์ฃผ์ ๊ฒ์ฒ๋ผ InputView ์ชฝ์์ ์
๋ ฅ์ ๋ฐ๊ณ ๋ฐ๋ก ์
๋ ฅ์ ๋ํ ๋ถ๋ถ์ ๊ฒ์ฆํด์ DTO๋ฅผ ์์ฑํ์ฌ ๊ฑด๋ค์ฃผ๋ ๋ฐฉ์์ ์ฐ์ตํด๋ด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. ์ข์ ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,100 @@
+# Domain ๊ตฌํ
+
+## ์งํ ํ๋ก์ธ์ค (์์คํ
์ฑ
์)
+
+1. ๊ฒ์ด๋จธ๋ ์๋ํ ๋ณด์ค ๋ชฌ์คํฐ์ HP๋ฅผ ์ค์ ํ๋ค.
+2. ๊ฒ์ด๋จธ๋ ํ๋ ์ดํ ํ๋ ์ด์ด์ ์ด๋ฆ๊ณผ HP, MP๋ฅผ ์ค์ ํ๋ค.
+3. ๊ฒ์์ด ์์๋๋ค.
+4. ๊ฒ์์ ํด์ ์ด๋ฉฐ, ๋งค ํด๋ง๋ค ๋ค์๊ณผ ๊ฐ์ ํ๋์ด ๋ฐ๋ณต๋๋ค.
+ 1) ๋ณด์ค ๋ชฌ์คํฐ์ ํ๋ ์ด์ด์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+ 2) ํ๋ ์ด์ด๊ฐ ๊ณต๊ฒฉํ ๋ฐฉ์์ ์ ํํ๋ค.
+ 3) ํ๋ ์ด์ด๊ฐ ์ ํ๋ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋ค. (๋ณด์ค ๋ชฌ์คํฐ์๊ฒ ๋ฐ๋ฏธ์ง๋ฅผ ์
ํ)
+ 4) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ๋ณด์ค ๋ชฌ์คํฐ์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 5) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+ 6) ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด์์๋ค๋ฉด ํ๋ ์ด์ด๋ฅผ ๊ณต๊ฒฉํ๋ค.
+ 7) ๋ฐ๋ฏธ์ง๋ฅผ ์
์ ํ๋ ์ด์ด์ ์ํ๋ฅผ ํ์ธํ๋ค.
+ 8) ํ๋ ์ด์ด๊ฐ ์ฃฝ์๋ค๋ฉด ๊ฒ์์ด ์ข
๋ฃ๋๋ค.
+5. ๊ฒ์ ์ข
๋ฃ ํ ์น์๋ฅผ ์๋ดํ๋ค.
+ - ํ๋ ์ด์ด๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ์งํํ ํด ์๋ฅผ ์๋ ค์ค๋ค.
+ - ๋ณด์ค ๋ชฌ์คํฐ๊ฐ ์ด๊ฒผ์ ๊ฒฝ์ฐ : ํ๋ ์ด์ด์ ๋ณด์ค ๋ชฌ์คํฐ์ ํ์ฌ ์ํ๋ฅผ ๋ณด์ฌ์ค๋ค.
+
+## Domain ๊ฐ์ฒด ์ ์ ๋ฐ ์ฑ
์๊ณผ ํ๋ ฅ ํ ๋น
+
+1. **Raid Game**
+ - ๊ฒ์ ์์ ์ ์ฃผ์ ์ธ๋ฌผ์ ๋ํด ์
ํ
ํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Boss Monster**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ธ๋ฌผ์ธ **Player**๋ฅผ ์์ฑํ ์ฑ
์์ ๊ฐ์ง
+ - ๋งค ํด์ ์คํํ ์ฑ
์์ ๊ฐ์ง
+ - ๊ฒ์ ์ํ๋ฅผ ํ์ธํ ์ฑ
์์ ๊ฐ์ง (๊ฒ์ ์ํ๊ฐ '์ข
๋ฃ'์ผ ๊ฒฝ์ฐ ํด์ ์คํํ์ง ์์)
+ - ํด ์๋ฅผ ์ฆ๊ฐ์ํฌ ์ฑ
์์ ๊ฐ์ง
+ - **Player**๊ฐ **Boss Monster**๋ฅผ ์ ํ๋ ๊ณต๊ฒฉ ๋ฐฉ์์ผ๋ก ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง
+ - **Player**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Boss Monster**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ๊ณต๊ฒฉ๊ณผ **Boss Monster**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - **Boss Monster**๊ฐ **Player**๋ฅผ ๊ณต๊ฒฉํ๋๋ก ํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Boss Monster**์ ๊ณต๊ฒฉ ํ๋์ ๋ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณต๋ฐ๊ณ , **Player**์๊ฒ ์ ๋ฌํ ์ฑ
์์ ๊ฐ์ง (**Boss Monster**์ ๊ณต๊ฒฉ๊ณผ **Player**์ ํผํด ํ๋์ ๋ํ ํ๋ ฅ ํ์)
+ - **Player**์ ์์กด์ฌ๋ถ์ ๋ฐ๋ผ ๊ฒ์ ์ํ๋ฅผ ๋ณ๊ฒฝํ ์ฑ
์์ ๊ฐ์ง (**Player**์ ์ํ๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์)
+ - ํด๋น ํด์ ๋ํ ๊ฒ์ ๊ธฐ๋ก์ ์์ฑํ ์ฑ
์์ ๊ฐ์ง (**GameHistory**์๊ฒ ํด์ ๋ํ ๊ธฐ๋ก์ ์์ฑํ๋ ํ๋ ฅ ํ์)
+
+2. **Boss Monster**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด HP, ํ์ฌ HP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (0~20์ ๋๋ค ๋ฐ๋ฏธ์ง)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+3. **Player**
+ - ํ์ฌ ์ํ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (์ด๋ฆ, ์ด HP, ํ์ฌ HP, ์ด MP, ํ์ฌ MP)
+ - ๊ณต๊ฒฉ ๋ช
๋ น์ ์ํํ ์ฑ
์์ ๊ฐ์ง (**Attack Type**์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณต๋ฐ๋ ํ๋ ฅ ํ์, MP ๊ฐ์)
+ - ๊ณต๊ฒฉ ํผํด ์
์์ ์ํํ ์ฑ
์์ ๊ฐ์ง (ํ์ฌ HP ๊ฐ์)
+
+4. **Attack Type**
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ข
๋ฅ๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๊ณต๊ฒฉ ๋ณํธ, ๊ณต๊ฒฉ ๋ฐฉ์ ์ด๋ฆ)
+ - ๊ณต๊ฒฉ ๋ฐฉ์ ์ํ์ ๋ํ ์ ๋ณด๋ฅผ ์ ๊ณตํด์ค ์ฑ
์์ ๊ฐ์ง (๋ฐ๋ฏธ์ง, MP ์๋ชจ๋, MP ํ๋ณต๋)
+
+5. **Game History**
+ - ๋งค ํด์ ๋ํ ๊ธฐ๋ก์ ์ ์ฅํ ์ฑ
์์ ๊ฐ์ง
+ - ์ถํ Controller์ Veiw๋ฅผ ์ํ ์์ฒญ์ ์๋ต ๊ฐ๋ฅ
+
+## ์ฃผ์ ๋ฉ์์ง ๊ตฌ์ฑ (๊ณต์ฉ ์ธํฐํ์ด์ค)
+
+1. ๋ณด์ค ๋ชฌ์คํฐ ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Boss Monster
+ - ์ธ์ : int hp
+ - ์๋ต : void, Boss Monster ๊ฐ์ฒด ์์ฑ
+
+2. ํ๋ ์ด์ด ์ค์ ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Create Player
+ - ์ธ์ : String name, int hp, int mp
+ - ์๋ต : void, Player ๊ฐ์ฒด ์์ฑ
+
+3. ํด ์งํ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Start Game
+ - ์ธ์ : -
+ - ์๋ต : ๊ตฌ์ฑ๋ ํด ๋ก์ง ์คํ
+
+4. ๊ฒ์ ์ํ ํ์ธ์ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Check Game Status
+ - ์ธ์ : -
+ - ์๋ต : return Game Status (true or False)
+
+5. ํด ํ์ ์ฆ๊ฐ๋ฅผ ์์ฒญ
+ - ์์ ์ : Raid Game
+ - ์ด๋ฆ : Increase Turns
+ - ์ธ์ : -
+ - ์๋ต : void, Turn 1 ์ฆ๊ฐ
+
+6. ๊ณต๊ฒฉ ํ๋ ์ํ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : Attack
+ - ์ธ์ : -
+ - ์๋ต : return ๋ฐ๋ฏธ์ง, ๊ณต๊ฒฉ ๋ฐฉ์์ ๋ฐ๋ผ MP ๊ฐ์(Player)
+
+7. ๊ณต๊ฒฉ ํผํด ์ ๋ฌ์ ์์ฒญ
+ - ์์ ์ : Player, Boss Monster
+ - ์ด๋ฆ : take Damage
+ - ์ธ์ : int damage
+ - ์๋ต : return ํ์ฌ HP
+
+ | Unknown | ์์ง์ ๊ธฐ๋ฅ ๋ชฉ๋ก ๋ฌธ์์ ๋ํด์ ๋๊ตฐ๊ฐ ๋ณธ๋ค๋ ์๊ฐ๋ณด๋ค๋ ์ ๊ฐ ๊ตฌํ์ ์์์ ๊ณต๋ถํ ๋ด์ฉ์ผ๋ก ์ดํดํ๊ธฐ ์ฝ๊ฒ ์ ๋ฆฌํ๋ ๊ฒ์ ๋ชฉ์ ์ผ๋ก ์์ฑํ์๋๋ฐ์. ๊ฐ๋ฐ์ ํผ์ ํ๋ ๊ฒ์ด ์๋๊ธฐ์ ๋ง์ ์ฃผ์ ๋ถ๋ถ์ ๋ํด์๋ ์ ๊ฒฝ์ ์ฐ๋ฉด์ ์์ฑ์ ํด์ผ ํ ๊ฒ ๊ฐ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค.
์ ๊ฐ ๋ฐ์์๋ถํฐ ๋ฆฌ๋ทฐ์ ๋ํ ๋ต๊ธ์ ๋จ๊ฒผ์ต๋๋ค. ๋ฆฌ๋ทฐ๋ฅผ ์์ฒญํ๊ณ ๋ฐ์ ๋ณด๋๊ฒ ์ฒ์์ด๋ผ ์ด๋ ๊ฒ ๋์์ด ๋ง์ด ๋๋์ง ๋ชฐ๋๋ค์. ์์ง ๊ณต๋ถ๋ฅผ ์์ํ์ง ์ผ๋ง ๋์ง ์์ ์ฝ๋ ์ฝ๊ธฐ๊ฐ ๋ง์ด ํ๋์
จ์ํ
๋ฐ ๋ฆ์ ์๊ฐ์๋ ๋ฆฌ๋ทฐ ํด์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค. ์ฃผ์ ์๊ฒฌ๋ค ๋ชจ๋ ์ฐธ๊ณ ํด์ ์ ๊ฐ ๊ฐ์ ํด ๋๊ฐ์ผ ํ ๋ฐฉํฅ์ธ ๊ฒ ๊ฐ์ต๋๋ค. ๋ฆฌ๋ทฐ ๊ฐ์ฌ๋๋ฆฝ๋๋ค. ์ข์ ํ๋ฃจ ๋์ธ์! |
@@ -0,0 +1,9 @@
+# ํ์ด 1 - Counter ์ด์ฉ
+from collections import Counter
+
+def solution(participant, completion):
+ # Counter๋ฅผ ์ด์ฉํ๋ฉด ๋ฆฌ์คํธ ๋ด์ ์ฑ๋ถ์ ์ซ์๋ฅผ ๋์
๋๋ฆฌ ํํ๋ก ๋ฐํํด์ค๋ค
+ # ex) list1 =['a', 'b', 'c', 'a'] => Counter(list1) : {'a':2, 'b':1, 'c':1}
+ not_completion = Counter(participant) - Counter(completion) # Counter์ ๊ฒฝ์ฐ ์ฌ์น์ฐ์ฐ๋ ๊ฐ๋ฅ
+
+ return list(not_completion.keys())[0] | Python | `Counter`๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ์ด ๊ต์ฅํ ์ ์ฉํ๊ตฐ์ ! ๐ฏ |
@@ -1,4 +1,4 @@
-import { EstimationInputDTO, Estimation } from '#estimations/estimation.types.js';
+import { EstimationInputDTO, Estimation, IsActivate } from '#estimations/estimation.types.js';
import { IEstimationRepository } from '#estimations/interfaces/estimation.repository.interface.js';
import { PrismaService } from '#global/prisma.service.js';
import { FindOptions } from '#types/options.type.js';
@@ -88,4 +88,52 @@ export class EstimationRepository implements IEstimationRepository {
take: pageSize,
});
}
+
+ // confirmedForId ๊ฐ์ด null์ด ์๋๋ฉด์ ์ด์ฌํ์ ๋๋๊ฒ์ด๋ค..~!
+ async findReviewable(userId: string, moveInfoIds: string[], page: number, pageSize: number) {
+ const estimations = await this.prisma.estimation.findMany({
+ where: {
+ confirmedForId: { in: moveInfoIds }, // ์๋ฃ๋ ์ด์ฌ์ ์ฐ๊ฒฐ๋ ๊ฒฌ์ ์ ์กฐํ
+ },
+ include: {
+ driver: true, // ๊ฒฌ์ ๊ณผ ๊ด๋ จ๋ ๋๋ผ์ด๋ฒ ์ ๋ณด
+ moveInfo: true, // ๊ฒฌ์ ๊ณผ ๊ด๋ จ๋ ์ด์ฌ ์ ๋ณด
+ reviews: true, // ๊ฒฌ์ ์ ์์ฑ๋ ๋ฆฌ๋ทฐ๋ค
+ },
+ skip: (page - 1) * pageSize,
+ take: pageSize,
+ });
+
+ // ๋ฆฌ๋ทฐ๊ฐ ์๋ ๊ฒฌ์ ๋ง ํํฐ๋ง
+ return estimations.filter(estimation => !estimation.reviews.some(review => review.ownerId === userId));
+ }
+
+ // ์ ์ ์ ์ด์ฌ ์ ๋ณด ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
+ async getUserMoveInfos(userId: string) {
+ return this.prisma.moveInfo.findMany({
+ where: { ownerId: userId }, // ๋ก๊ทธ์ธํ ์ ์ ์ ์ด์ฌ๋ง ์กฐํํ๊ธฐ
+ select: { id: true }, // ์ด์ฌ ID๋ง ์ ํ
+ });
+ }
+
+ // ์ง์ ๊ฒฌ์ ์์ฒญ ์ฌ๋ถ
+ async isDesignatedRequest(estimationId: string): Promise<IsActivate> {
+ const estimation = await this.prisma.estimation.findUnique({
+ where: { id: estimationId },
+ include: {
+ moveInfo: {
+ include: {
+ requests: {
+ where: {
+ status: 'APPLY', //
+ },
+ },
+ },
+ },
+ },
+ });
+
+ // ์ง์ ์์ฒญ์ด๋ฉด 'Active', ์ผ๋ฐ์์ฒญ์ด๋ฉด 'Inactive'
+ return estimation?.moveInfo.requests.length > 0 ? IsActivate.Active : IsActivate.Inactive;
+ }
} | TypeScript | ์๋ง๋ javascript ๋ ๋ฒจ์ด ์๋๋ผ prisma์์ findMany ํด์ค๋ ๋จ๊ณ์์ ์ด ์์
์ ํด์ ๊ฐ์ ธ์ฌ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค. ๋์ค์ ํ ๋ฒ ์ฐพ์๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค์. |
@@ -1,4 +1,4 @@
-import { EstimationInputDTO, Estimation } from '#estimations/estimation.types.js';
+import { EstimationInputDTO, Estimation, IsActivate } from '#estimations/estimation.types.js';
import { IEstimationRepository } from '#estimations/interfaces/estimation.repository.interface.js';
import { PrismaService } from '#global/prisma.service.js';
import { FindOptions } from '#types/options.type.js';
@@ -88,4 +88,52 @@ export class EstimationRepository implements IEstimationRepository {
take: pageSize,
});
}
+
+ // confirmedForId ๊ฐ์ด null์ด ์๋๋ฉด์ ์ด์ฌํ์ ๋๋๊ฒ์ด๋ค..~!
+ async findReviewable(userId: string, moveInfoIds: string[], page: number, pageSize: number) {
+ const estimations = await this.prisma.estimation.findMany({
+ where: {
+ confirmedForId: { in: moveInfoIds }, // ์๋ฃ๋ ์ด์ฌ์ ์ฐ๊ฒฐ๋ ๊ฒฌ์ ์ ์กฐํ
+ },
+ include: {
+ driver: true, // ๊ฒฌ์ ๊ณผ ๊ด๋ จ๋ ๋๋ผ์ด๋ฒ ์ ๋ณด
+ moveInfo: true, // ๊ฒฌ์ ๊ณผ ๊ด๋ จ๋ ์ด์ฌ ์ ๋ณด
+ reviews: true, // ๊ฒฌ์ ์ ์์ฑ๋ ๋ฆฌ๋ทฐ๋ค
+ },
+ skip: (page - 1) * pageSize,
+ take: pageSize,
+ });
+
+ // ๋ฆฌ๋ทฐ๊ฐ ์๋ ๊ฒฌ์ ๋ง ํํฐ๋ง
+ return estimations.filter(estimation => !estimation.reviews.some(review => review.ownerId === userId));
+ }
+
+ // ์ ์ ์ ์ด์ฌ ์ ๋ณด ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ
+ async getUserMoveInfos(userId: string) {
+ return this.prisma.moveInfo.findMany({
+ where: { ownerId: userId }, // ๋ก๊ทธ์ธํ ์ ์ ์ ์ด์ฌ๋ง ์กฐํํ๊ธฐ
+ select: { id: true }, // ์ด์ฌ ID๋ง ์ ํ
+ });
+ }
+
+ // ์ง์ ๊ฒฌ์ ์์ฒญ ์ฌ๋ถ
+ async isDesignatedRequest(estimationId: string): Promise<IsActivate> {
+ const estimation = await this.prisma.estimation.findUnique({
+ where: { id: estimationId },
+ include: {
+ moveInfo: {
+ include: {
+ requests: {
+ where: {
+ status: 'APPLY', //
+ },
+ },
+ },
+ },
+ },
+ });
+
+ // ์ง์ ์์ฒญ์ด๋ฉด 'Active', ์ผ๋ฐ์์ฒญ์ด๋ฉด 'Inactive'
+ return estimation?.moveInfo.requests.length > 0 ? IsActivate.Active : IsActivate.Inactive;
+ }
} | TypeScript | ๋ค!! ์ฐพ์๋ณด๊ฒ ์ต๋๋ค !! ํ์๋ ์๋ ค์ฃผ์
์ ๊ฐ์ฌํฉ๋๋ค!!!! |
@@ -3,8 +3,10 @@ import { ParsedUrlQuery } from "querystring";
import { GetServerSideProps } from "next";
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
+import { getReviewStat, getReviews } from "@/lib/api/ReviewService";
import { getTrainerInfo } from "@/lib/api/trainerService";
import { getFavorite } from "@/lib/api/userService";
+import { ReviewResult } from "@/types/reviews";
import FindTrainerCard from "@/components/Cards/FindTrainerCard";
import { HorizontalLine } from "@/components/Common/Line";
import Loading from "@/components/Common/Loading";
@@ -38,6 +40,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
export default function DetailTrainer({ trainerId }: { trainerId: string | null }) {
const [currentPage, setCurrentPage] = useState<number>(1);
+ const pageSize = 3;
const { data, isLoading, isError } = useQuery(
["trainer-detail", trainerId],
() => getTrainerInfo(trainerId as string),
@@ -46,22 +49,42 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null
},
);
+ const { data: reviewStat } = useQuery(["review-stat"], () => getReviewStat(trainerId as string), {
+ enabled: !!trainerId,
+ cacheTime: 5 * 60 * 1000,
+ staleTime: 5 * 60 * 1000,
+ });
+ const {
+ data: reviewList,
+ isLoading: isReviewLoading,
+ isError: isReviewError,
+ } = useQuery<ReviewResult>(
+ ["reviews", currentPage],
+ () => getReviews(trainerId as string, { page: currentPage, limit: pageSize }),
+ {
+ enabled: !!trainerId,
+ keepPreviousData: true,
+ },
+ );
+
const {
data: favoriteInfo,
isLoading: isFavoriteLoading,
isError: isFavoriteError,
} = useQuery(["favorite"], () => getFavorite(trainerId as string), { enabled: !!trainerId });
- if (isError || isFavoriteError) return <div>error!!</div>;
+ if (isError || isReviewError || isFavoriteError) return <div>error!!</div>;
const trainerInfo = data?.profile;
+ const reviews = reviewList?.reviews || [];
+ const totalCount = reviewList?.totalCount || 0;
return (
<div
className={clsx(
"relative flex flex-col justify-between gap-16 m-auto mt-[2.4rem] mb-16 px-8",
"pc:flex-row pc:gap-0 pc:mt-[5.6rem] pc:max-w-[144rem]",
- "tablet:max-w-[74.4rem] mobile:max-w-[37.5rem]",
+ "tablet:max-w-[74.4rem] mobile:max-w-auto",
)}
>
<div className={"flex flex-col gap-[2.4rem] w-full pc:gap-16 pc:pr-[10rem]"}>
@@ -73,8 +96,12 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null
<HorizontalLine width="100%" />
<TrainerInfo profile={trainerInfo} />
{/* ๋ฆฌ๋ทฐ ๋ฆฌ์คํธ api ์ฐ๊ฒฐํด์ผํจ */}
- <TrainerReview reviewList={trainerInfo} />
- <Pagination currentPage={currentPage} totalPages={5} onPageChange={setCurrentPage} />
+ <TrainerReview reviewList={reviews} reviewStat={reviewStat} totalCount={totalCount} />
+ <Pagination
+ currentPage={currentPage}
+ totalPages={Math.ceil(totalCount / pageSize)}
+ onPageChange={setCurrentPage}
+ />
</div>
<div className="flex flex-col gap-[2.4rem] pc:gap-16">
<TrainerControl profile={trainerInfo} />
@@ -83,7 +110,7 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null
<ShareSNS label="๋๋ง ์๊ธฐ์ ์์ฌ์ด ๊ฐ์ฌ๋์ธ๊ฐ์?" trainerInfo={trainerInfo} />
</div>
</div>
- {isLoading || (isFavoriteLoading && <Loading />)}
+ {(isLoading || isReviewLoading || isFavoriteLoading) && <Loading />}
</div>
);
} | Unknown | ```js
useQuery(["review-stat", trainerId],
() => getReviewStat(trainerId as string),
{ enabled: !!trainerId,
cacheTime: 5 * 60 * 1000,
staleTime: 5 * 60 * 1000 });
```
๋ก ํด์ฃผ์๋๊ฒ ์ข์๋ณด์ฌ์. |
@@ -3,8 +3,10 @@ import { ParsedUrlQuery } from "querystring";
import { GetServerSideProps } from "next";
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
+import { getReviewStat, getReviews } from "@/lib/api/ReviewService";
import { getTrainerInfo } from "@/lib/api/trainerService";
import { getFavorite } from "@/lib/api/userService";
+import { ReviewResult } from "@/types/reviews";
import FindTrainerCard from "@/components/Cards/FindTrainerCard";
import { HorizontalLine } from "@/components/Common/Line";
import Loading from "@/components/Common/Loading";
@@ -38,6 +40,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
export default function DetailTrainer({ trainerId }: { trainerId: string | null }) {
const [currentPage, setCurrentPage] = useState<number>(1);
+ const pageSize = 3;
const { data, isLoading, isError } = useQuery(
["trainer-detail", trainerId],
() => getTrainerInfo(trainerId as string),
@@ -46,22 +49,42 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null
},
);
+ const { data: reviewStat } = useQuery(["review-stat"], () => getReviewStat(trainerId as string), {
+ enabled: !!trainerId,
+ cacheTime: 5 * 60 * 1000,
+ staleTime: 5 * 60 * 1000,
+ });
+ const {
+ data: reviewList,
+ isLoading: isReviewLoading,
+ isError: isReviewError,
+ } = useQuery<ReviewResult>(
+ ["reviews", currentPage],
+ () => getReviews(trainerId as string, { page: currentPage, limit: pageSize }),
+ {
+ enabled: !!trainerId,
+ keepPreviousData: true,
+ },
+ );
+
const {
data: favoriteInfo,
isLoading: isFavoriteLoading,
isError: isFavoriteError,
} = useQuery(["favorite"], () => getFavorite(trainerId as string), { enabled: !!trainerId });
- if (isError || isFavoriteError) return <div>error!!</div>;
+ if (isError || isReviewError || isFavoriteError) return <div>error!!</div>;
const trainerInfo = data?.profile;
+ const reviews = reviewList?.reviews || [];
+ const totalCount = reviewList?.totalCount || 0;
return (
<div
className={clsx(
"relative flex flex-col justify-between gap-16 m-auto mt-[2.4rem] mb-16 px-8",
"pc:flex-row pc:gap-0 pc:mt-[5.6rem] pc:max-w-[144rem]",
- "tablet:max-w-[74.4rem] mobile:max-w-[37.5rem]",
+ "tablet:max-w-[74.4rem] mobile:max-w-auto",
)}
>
<div className={"flex flex-col gap-[2.4rem] w-full pc:gap-16 pc:pr-[10rem]"}>
@@ -73,8 +96,12 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null
<HorizontalLine width="100%" />
<TrainerInfo profile={trainerInfo} />
{/* ๋ฆฌ๋ทฐ ๋ฆฌ์คํธ api ์ฐ๊ฒฐํด์ผํจ */}
- <TrainerReview reviewList={trainerInfo} />
- <Pagination currentPage={currentPage} totalPages={5} onPageChange={setCurrentPage} />
+ <TrainerReview reviewList={reviews} reviewStat={reviewStat} totalCount={totalCount} />
+ <Pagination
+ currentPage={currentPage}
+ totalPages={Math.ceil(totalCount / pageSize)}
+ onPageChange={setCurrentPage}
+ />
</div>
<div className="flex flex-col gap-[2.4rem] pc:gap-16">
<TrainerControl profile={trainerInfo} />
@@ -83,7 +110,7 @@ export default function DetailTrainer({ trainerId }: { trainerId: string | null
<ShareSNS label="๋๋ง ์๊ธฐ์ ์์ฌ์ด ๊ฐ์ฌ๋์ธ๊ฐ์?" trainerInfo={trainerInfo} />
</div>
</div>
- {isLoading || (isFavoriteLoading && <Loading />)}
+ {(isLoading || isReviewLoading || isFavoriteLoading) && <Loading />}
</div>
);
} | Unknown | ๋ต ์์ ํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,6 @@
+package racingcar;
+
+public interface RacingNumberGenerator {
+
+ int generate();
+} | Java | ๋ค๋ฆฌ ๋ฏธ์
์์ ์ด ์ธํฐํ์ด์ค๋ฅผ ํจ์ํ์ธํฐํ์ด์ค๋ก ๊ตฌํ์ ํ๋๋ผ๊ตฌ์.
`@FunctionalInterface` ์ด๋
ธํ
์ด์
์ ๋ถ์ฌ์
ํ๋ฒ์ฏค, ๊ณ ๋ คํด๋ณผ๋ง ํ ๊ฒ ๊ฐ์, ๊ด๋ จ ํฌ์คํ
๋งํฌ ๋จ๊ฒจ๋์ต๋๋ค!
https://codechacha.com/ko/java8-functional-interface/ |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์ ์ด๊ฑฐ ์ถฉ๊ฒฉ์ ์ธ๋ฐ์...
Comparable<Car>๋ฅผ ํตํด Car ํด๋์ค๋ฅผ ์ ์ํ์ ๊ฑฐ ์ง์ง ์์ด๋์ด ์ข๋ค์.
๋ฐฐ์ ๊ฐ๋๋ค! |
@@ -0,0 +1,48 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class Position implements Comparable<Position> {
+
+ private static final int STARTING_POSITION = 0;
+
+ private int position;
+
+ public Position(int position) {
+ this.position = position;
+ }
+
+ public static Position from(int position) {
+ return new Position(position);
+ }
+
+ public static Position init() {
+ return new Position(STARTING_POSITION);
+ }
+
+ public void increase() {
+ position++;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Position position1 = (Position) o;
+ return position == position1.position;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(position);
+ }
+
+ @Override
+ public int compareTo(Position other) {
+ return this.position - other.position;
+ }
+} | Java | Position์ ์ผ๊ธ๊ฐ์ฒดํํ ํ์๊ฐ ์๋?์ ๋ํ ์๋ฌธ์ด ๋ญ๋๋ค. Car ๊ฐ์ฒด์ int ํ๋๋ก ์ ์ธํด๋ ์ถฉ๋ถํด ๋ณด์ฌ์์.
์์ฑ์ validation์ด ํ์ํ ๊ฒ๋ ์๋๊ณ , ์ด๊ธฐ๊ฐ ์ค์ ๋ Car ๊ฐ์ฒด์์ ์ถฉ๋ถํ ๊ฐ๋ฅํ๊ณ ,
Car์์ compareTo๋ฅผ ์ค๋ฒ๋ผ์ด๋ฉํ ๋, Car์ ๋ํ getter๋ง ์ค์ ํ๋ค๋ฉด ๋ฉ์๋ ๊ตฌํ์ด ๊ฐ๋ฅํ๋ค๊ณ ์๊ฐํฉ๋๋ค.
ํน ๋ค๋ฅธ ์๋๊ฐ ์๊ฑฐ๋, ์ ์๊ฐ์ด ํ๋ ธ๋ค๋ฉด ํผ๋๋ฐฑ ๋ถํ๋๋ฆฝ๋๋ค:) |
@@ -0,0 +1,65 @@
+package racingcar.model;
+
+import racingcar.RacingNumberGenerator;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DUPLICATED_CAR_NAMES = "์๋์ฐจ๋ค์ ์ด๋ฆ์ ์๋ก ์ค๋ณต๋ ์ ์์ต๋๋ค.";
+
+ private final List<Car> cars;
+
+ private Cars(List<String> names) {
+ validate(names);
+ cars = toCars(names);
+ }
+
+ private List<Car> toCars(List<String> names) {
+ return names.stream()
+ .map(Car::from)
+ .collect(Collectors.toList());
+ }
+
+ private void validate(List<String> names) {
+ if (isDuplicated(names)) {
+ throw new IllegalArgumentException(DUPLICATED_CAR_NAMES);
+ }
+ }
+
+ private boolean isDuplicated(List<String> names) {
+ Set<String> uniqueNames = new HashSet<>(names);
+
+ return uniqueNames.size() != names.size();
+ }
+
+ public static Cars from(List<String> names) {
+ return new Cars(names);
+ }
+
+ public void race(RacingNumberGenerator numberGenerator) {
+ cars.forEach(car -> car.move(numberGenerator.generate()));
+ }
+
+ public List<CarName> findWinners() {
+ Car maxPositionCar = getCarWithMaxPosition();
+ return cars.stream()
+ .filter(maxPositionCar::isSamePosition)
+ .map(Car::getName)
+ .collect(Collectors.toList());
+ }
+
+ private Car getCarWithMaxPosition() {
+ return cars.stream()
+ .max(Car::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("์ฐจ๋ ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ต๋๋ค."));
+ }
+
+ public List<Car> get() {
+ return Collections.unmodifiableList(cars);
+ }
+} | Java | from์ด๋ผ๋ ๋ฉ์๋๊ฐ ๋ชจ๋ ํด๋์ค์์ ๋ฐ๋ณต๋๋๋ฐ,
Cars.from(names)์ด๋ฐ ์์ผ๋ก ์ฐ๊ฒฐ๋ ๊ฒฝ์ฐ ์๋ฏธ๊ฐ ๋๋ฌ๋์ง๋ง
๋ฉ์๋ ์ด๋ฆ์ด๋ ๊ฒฐ๊ตญ ๊ฐ์ฒด์๊ฒ ๋ณด๋ด๋ ๋ฉ์ธ์ง๋ผ๋ ๊ด์ ์์๋ 'from'์ด๋ผ๋ ๋ฉ์๋ ์ด๋ฆ๋ง ๋ณผ ๊ฒฝ์ฐ ์ด๋ค ์ฉ๋์ธ์ง ์๊ธฐ ํ๋ ๊ฒ ์ฌ์ค์ธ๊ฒ ๊ฐ์ต๋๋ค.
`setNewCars()`๋ฑ๋ฑ ์ ์ ํ๊ฒ ๋ฉ์๋๋ช
์ ๋ฐ๊ฟ๋ณด์๋ฉด ์ด๋จ๊น ์๊ฐํด๋ณด์์ต๋๋ค.ใ
ใ
|
@@ -0,0 +1,24 @@
+package racingcar.model;
+
+public class AttemptCount {
+
+ private static final int IS_OVER = 0;
+
+ private int count;
+
+ public AttemptCount(int count) {
+ this.count = count;
+ }
+
+ public static AttemptCount from(int count) {
+ return new AttemptCount(count);
+ }
+
+ public boolean isPlayable() {
+ return count > IS_OVER;
+ }
+
+ public void decrease() {
+ count--;
+ }
+} | Java | AttemptCount ํด๋์ค๋ ์๋ ํ์์ ๋ํ ์ผ๊ธ ๊ฐ์ฒด๋ก์์ ์ฑ๊ฒฉ๋ณด๋ค,
RacingGame์ ์ ์ฒด ์ฝ๋ ํ๋ฆ์ ์ ์ดํ๋ ์ญํ ์ ํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
isPlayable ๋ฉ์๋๋ฅผ ํตํด ๊ฒ์ ์งํ์ฌ๋ถ๋ฅผ ํ๋จํด looping์ ์ด๋๊น์ง ๋ ์ง ํ๋จํด์ฃผ๋ ๊ฒ์ด ํต์ฌ์ด๋๊น์.
`RacingGame`๋ฑ์ผ๋ก ํด๋์ค๋ช
์ ๋ฐ๊พธ๊ณ , attemptCount๋ฅผ ์ธ์คํด์ค ํ๋ํ ํด๋ณด๋ฉด ์ด๋จ๊น์?ใ
ใ
|
@@ -0,0 +1,24 @@
+package racingcar.model;
+
+public class AttemptCount {
+
+ private static final int IS_OVER = 0;
+
+ private int count;
+
+ public AttemptCount(int count) {
+ this.count = count;
+ }
+
+ public static AttemptCount from(int count) {
+ return new AttemptCount(count);
+ }
+
+ public boolean isPlayable() {
+ return count > IS_OVER;
+ }
+
+ public void decrease() {
+ count--;
+ }
+} | Java | ์ ๋ ์ฌ์ฉํ๋ฉด์ ์ฐ์ฐํ๋๋ฐ `RacingGame` ๊ฐ์ฒด๋ก ๊ฒ์์ ์งํํ๋ ์ญํ ์ ๋ถ๋ฆฌํด๋ด์ผ๊ฒ ๋ค์...!
์ข์ ์ง์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์์ ์ ์์ด๋์ด๋ ์๋๊ตฌ... **Tecoble** ์์ `getter()` ์ ๊ด๋ จ๋ ๋ด์ฉ์ ์ฝ๋ค๊ฐ ์๊ฒ ๋ ๋ด์ฉ์ ๊ทธ๋๋ก ์ ์ฉํด๋ณธ ๊ฑฐ์์... ํํ... ์ ๋ ์ฒ์ ์ด ์์ด๋์ด๋ฅผ ์ ํ๊ณ ์ถฉ๊ฒฉ์ ๋ง์ด ๋ฐ์์ต๋๋ค ใ
- [getter() ๋ฅผ ์ฌ์ฉํ๋ ๋์ ๊ฐ์ฒด์ ๋ฉ์์ง๋ฅผ ๋ณด๋ด์ง](https://tecoble.techcourse.co.kr/post/2020-04-28-ask-instead-of-getter/)
- [Comparator ์ Comparable ์ ์ดํด](https://st-lab.tistory.com/243)
๋๋ฌด ์ข์ ์๋ฃ๋ค์ด๋ผ ํ๋ฒ ์ฝ์ด๋ณด์๋ ๊ฑฐ ๊ฐ๋ ฅํ ์ถ์ฒ๋๋ฆฝ๋๋ค!! |
@@ -0,0 +1,65 @@
+package racingcar.model;
+
+import racingcar.RacingNumberGenerator;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DUPLICATED_CAR_NAMES = "์๋์ฐจ๋ค์ ์ด๋ฆ์ ์๋ก ์ค๋ณต๋ ์ ์์ต๋๋ค.";
+
+ private final List<Car> cars;
+
+ private Cars(List<String> names) {
+ validate(names);
+ cars = toCars(names);
+ }
+
+ private List<Car> toCars(List<String> names) {
+ return names.stream()
+ .map(Car::from)
+ .collect(Collectors.toList());
+ }
+
+ private void validate(List<String> names) {
+ if (isDuplicated(names)) {
+ throw new IllegalArgumentException(DUPLICATED_CAR_NAMES);
+ }
+ }
+
+ private boolean isDuplicated(List<String> names) {
+ Set<String> uniqueNames = new HashSet<>(names);
+
+ return uniqueNames.size() != names.size();
+ }
+
+ public static Cars from(List<String> names) {
+ return new Cars(names);
+ }
+
+ public void race(RacingNumberGenerator numberGenerator) {
+ cars.forEach(car -> car.move(numberGenerator.generate()));
+ }
+
+ public List<CarName> findWinners() {
+ Car maxPositionCar = getCarWithMaxPosition();
+ return cars.stream()
+ .filter(maxPositionCar::isSamePosition)
+ .map(Car::getName)
+ .collect(Collectors.toList());
+ }
+
+ private Car getCarWithMaxPosition() {
+ return cars.stream()
+ .max(Car::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("์ฐจ๋ ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ต๋๋ค."));
+ }
+
+ public List<Car> get() {
+ return Collections.unmodifiableList(cars);
+ }
+} | Java | ์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋ ๋ค์ด๋ฐ์ ์ปจ๋ฒค์
์ด ์กด์ฌํ๋ค๊ณ ์๊ณ ์์ต๋๋ค!
์ ๋ ๊ฐ์ธ์ ์ผ๋ก๋ ์ ์ ํ ์ด๋ฆ์ ์ง์ด์ฃผ๋ ๊ฒ์ด ์ข๋ค๊ณ ์๊ฐํฉ๋๋ค.
ํ์ง๋ง ์ปจ๋ฒค์
์ด๋๊น...! ์ปจ๋ฒค์
์ ์งํค๋ ๋ฐฉํฅ์ผ๋ก ์งํํ๊ณ ์์ด์...
- [์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋์ ๋ค์ด๋ฐ ๋ฐฉ์](https://velog.io/@saint6839/%EC%A0%95%EC%A0%81-%ED%8C%A9%ED%86%A0%EB%A6%AC-%EB%A9%94%EC%84%9C%EB%93%9C-%EB%84%A4%EC%9D%B4%EB%B0%8D-%EB%B0%A9%EC%8B%9D) |
@@ -0,0 +1,48 @@
+package racingcar.model;
+
+import java.util.Objects;
+
+public class Position implements Comparable<Position> {
+
+ private static final int STARTING_POSITION = 0;
+
+ private int position;
+
+ public Position(int position) {
+ this.position = position;
+ }
+
+ public static Position from(int position) {
+ return new Position(position);
+ }
+
+ public static Position init() {
+ return new Position(STARTING_POSITION);
+ }
+
+ public void increase() {
+ position++;
+ }
+
+ public int getPosition() {
+ return position;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Position position1 = (Position) o;
+ return position == position1.position;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(position);
+ }
+
+ @Override
+ public int compareTo(Position other) {
+ return this.position - other.position;
+ }
+} | Java | ๋ง์ํด์ฃผ์ ๋๋ก ํ์ฌ ๋ฏธ์
์์๋ ๊ตณ์ด ์ผ๊ธ ๊ฐ์ฒดํํ ํ์๊ฐ ์๋ค๋ ๊ฒ์ ๋์ํฉ๋๋ค.
๋์ดํด์ฃผ์ ๊ทผ๊ฑฐ๋ค๋ ๋ชจ๋ ํ๋นํ๋ค๊ณ ์๊ฐํด์..!
๋ค๋ง ๋ฏธ์
์ ์งํํ๋ฉด์ ์ต๋ํ ๊ฐ์ฒด์งํฅ ์ํ ์ฒด์กฐ ์์น๋ค์ ์งํค๋ฉด์ ๊ตฌํํด๋ณด๋ ๊ฒ์ ๋ชฉํ๋ก ํ๊ณ ์์ด์. (๊ฐ์ฒด์งํฅ ์ํ ์ฒด์กฐ ์์น์ **๋ชจ๋ ์์ ๊ฐ๊ณผ ๋ฌธ์์ด์ ํฌ์ฅํ๋ค.** ๋ผ๋ ํญ๋ชฉ์ด ์๋๋ผ๊ตฌ์!) ์ผ์ข
์ ์ฐ์ต์ด๋ผ๊ณ ๋ด์ฃผ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค! ์์ ๊ฐ์ ํฌ์ฅํ๋ ๊ฒ์ด ์ถํ์ ์๊ตฌ์ฌํญ์ด ๋ณํ์ ๋ ์ ์ง ๋ณด์์๋ ํฐ ์ด์ ์ด ์๋ค๋ ๊ฒ์ ๊ณ ๋ คํด์ ์ต์ง๋ก๋ผ๋ Wrapping ํ๋ ค๊ณ ๋
ธ๋ ฅํ๊ณ ์์ต๋๋ค...
- [์์ ํ์
์ ํฌ์ฅํด์ผ ํ๋ ์ด์ ](https://tecoble.techcourse.co.kr/post/2020-05-29-wrap-primitive-type/) |
@@ -0,0 +1,6 @@
+package racingcar;
+
+public interface RacingNumberGenerator {
+
+ int generate();
+} | Java | ์ ์ข์ ์๋ฃ ๊ฐ์ฌํฉ๋๋ค! ์ต๊ทผ์ ๋๋ค๋ฅผ ํ์ตํ๋ฉด์ ํจ์ํ ์ธํฐํ์ด์ค๋ ํจ๊ป ๊ณต๋ถํ์๋๋ฐ
์ง์ ํ์ฉํด๋ณผ ์๊ฐ์ ๋ชปํด๋ดค๋ค์. ์ง๊ธ ๋น์ฅ ๋ฆฌํฉํ ๋ง ํด๋ด์ผ๊ฒ ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ด ๋ถ๋ถ์ ๋ฉ์ธ์ง ๋ถ๋ถ์ด๋ผ view์์ ์ฒ๋ฆฌํ์ ๊ฒ ๊ฐ์๋ฐ, ์ด ๊ฒฝ์ฐ `cars`๊ฐ ์ง์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์๋ ์ง์ ๊บผ๋ด์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ธ ๊ฒ ๊ฐ์์ ์ฐจ๋ผ๋ฆฌ ๋ฉ์์ง๋ฅผ `cars`์์ ์ฒ๋ฆฌํ๋ ๊ฒ ๋ ๊ฐ์ฒด๋ฅผ ๋
๋ฆฝ์ ์ผ๋ก ๋ง๋๋ ๊ฒ ์๋๊น ํ๋ ์๊ฐ์ด ๋๋ค์! |
@@ -0,0 +1,37 @@
+package racingcar.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import racingcar.model.AttemptCount;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class InputView {
+
+ private static final String SPLIT_REGEX = ",";
+ private static final String NON_NUMERIC_INPUT_MESSAGE = "์ซ์๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.";
+ private static final String REQUEST_ATTEMPT_COUNT_MESSAGE = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+
+ public List<String> readNames() {
+ String names = Console.readLine();
+
+ return Arrays.stream(names.split(SPLIT_REGEX))
+ .collect(Collectors.toList());
+ }
+
+ public AttemptCount readAttemptCount() {
+ System.out.println(REQUEST_ATTEMPT_COUNT_MESSAGE);
+ String attemptCount = Console.readLine();
+
+ return AttemptCount.from(parseInt(attemptCount));
+ }
+
+ private int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException error) {
+ throw new IllegalArgumentException(NON_NUMERIC_INPUT_MESSAGE);
+ }
+ }
+} | Java | ์ด๋ฌํ ๋ฉ์์ง๋ค์ ์ผ์ผ์ด ์์์ฒ๋ฆฌํ๋ฉด ๋์ค์ ๊ท๋ชจ๊ฐ ์ปค์ก์ ๋, ๊ด๋ฆฌํ๊ธฐ ํ๋ค์ด์ง๋ค๊ณ ํฉ๋๋ค. ์ฌ์ฌ์ฉ๋์ง ์๋ ๋ฉ์์ง๋ค์ ์ง์ ๊ฐ์ ๋ด์๋ณด๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,24 @@
+package racingcar.model;
+
+public class AttemptCount {
+
+ private static final int IS_OVER = 0;
+
+ private int count;
+
+ public AttemptCount(int count) {
+ this.count = count;
+ }
+
+ public static AttemptCount from(int count) {
+ return new AttemptCount(count);
+ }
+
+ public boolean isPlayable() {
+ return count > IS_OVER;
+ }
+
+ public void decrease() {
+ count--;
+ }
+} | Java | ์ ๋ ๋ง์ด ๋ฐฐ์๊ฐ๋ค์ ๊ฐ์ฌํฉ๋๋ค :D |
@@ -0,0 +1,47 @@
+package racingcar.model;
+
+public class Car implements Comparable<Car> {
+ private static final int MOVABLE_LOWER_BOUND = 4;
+
+ private final CarName name;
+ private final Position position = Position.init();
+
+ private Car(String name) {
+ this.name = CarName.from(name);
+ }
+
+ public static Car from(String name) {
+ return new Car(name);
+ }
+
+ public void move(int command) {
+ if (isMovable(command)) {
+ position.increase();
+ }
+ }
+
+ private static boolean isMovable(int command) {
+ return command >= MOVABLE_LOWER_BOUND;
+ }
+
+ public boolean isAt(Position expectedPosition) {
+ return position.equals(expectedPosition);
+ }
+
+ public int getPosition() {
+ return position.getPosition();
+ }
+
+ public CarName getName() {
+ return name;
+ }
+
+ public boolean isSamePosition(Car other) {
+ return this.position.equals(other.position);
+ }
+
+ @Override
+ public int compareTo(Car other) {
+ return this.position.compareTo(other.position);
+ }
+} | Java | ์ ๋ ์ฌ๊ธฐ์ ๋๋๋๋ฐ ๋๋ถ์ ์ข์ ์ปจํ
์ธ ๋ค์ ๋ฐฐ์๊ฐ๋ค์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ์ด ๋ถ๋ถ์ `RacingNumberGenerator`๋ผ๊ณ ํจ์ํ ์ธํฐํ์ด์ค๋ก ์ด๋ฏธ ์ ์ํด ๋์
จ์ผ๋ ์ง์ ๊ตฌํํ ํ์ ์์ด ๊ฐ๋จํ๊ฒ ๋๋ค๋ก ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ```java
cars.race(() -> new Integer[]{4, 1, 4, 4, 1, 4})
```
์ด๋ฐ ๋๋์ผ๋ก์! |
@@ -0,0 +1,65 @@
+package racingcar.model;
+
+import racingcar.RacingNumberGenerator;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class Cars {
+
+ private static final String DUPLICATED_CAR_NAMES = "์๋์ฐจ๋ค์ ์ด๋ฆ์ ์๋ก ์ค๋ณต๋ ์ ์์ต๋๋ค.";
+
+ private final List<Car> cars;
+
+ private Cars(List<String> names) {
+ validate(names);
+ cars = toCars(names);
+ }
+
+ private List<Car> toCars(List<String> names) {
+ return names.stream()
+ .map(Car::from)
+ .collect(Collectors.toList());
+ }
+
+ private void validate(List<String> names) {
+ if (isDuplicated(names)) {
+ throw new IllegalArgumentException(DUPLICATED_CAR_NAMES);
+ }
+ }
+
+ private boolean isDuplicated(List<String> names) {
+ Set<String> uniqueNames = new HashSet<>(names);
+
+ return uniqueNames.size() != names.size();
+ }
+
+ public static Cars from(List<String> names) {
+ return new Cars(names);
+ }
+
+ public void race(RacingNumberGenerator numberGenerator) {
+ cars.forEach(car -> car.move(numberGenerator.generate()));
+ }
+
+ public List<CarName> findWinners() {
+ Car maxPositionCar = getCarWithMaxPosition();
+ return cars.stream()
+ .filter(maxPositionCar::isSamePosition)
+ .map(Car::getName)
+ .collect(Collectors.toList());
+ }
+
+ private Car getCarWithMaxPosition() {
+ return cars.stream()
+ .max(Car::compareTo)
+ .orElseThrow(() -> new IllegalArgumentException("์ฐจ๋ ๋ฆฌ์คํธ๊ฐ ๋น์ด์์ต๋๋ค."));
+ }
+
+ public List<Car> get() {
+ return Collections.unmodifiableList(cars);
+ }
+} | Java | ์ซ์ ์์ฑํ๋ ๋ถ๋ถ์ ์์ฑ์๊ฐ ์๋ `race`์์ ์ฃผ์
๋ฐ์ ์๋ ์๋ค์! ํ๋ ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ์ ๋ ํผ๋๋ฐฑ ๋ฐ๊ณ ๋ ๋ค ๋๋ค๋ก ํํํด๋ณด๋ ค๊ณ ํ๋๋ฐ `TestRacingNumberGenerator` ์ฒ๋ผ ๋ด๋ถ์ ์ํ๊ฐ ํ์ํ ๊ฒฝ์ฐ์ ์จ์ ํ ๋๋ค๋ก ํํํ๋๊ฒ ํ๋ค๋๋ผ๊ตฌ์!
๋ง์ฝ ๋๋ค๋ก ์ฒ๋ฆฌํ๋ ค๋ฉด
```java
cars.race(() -> newArrayList(4, 1, 4, 4, 1, 4).remove(0));
```
๊ณผ ๊ฐ์ ํํ๋ก ์ฌ์ฉํด์ผ ํ๋๋ฐ ์ด๋ ๊ฒ ๋๋ฉด ์ผ๋จ ๊ฐ๋
์ฑ์ด ๋๋ฌด ๋จ์ด์ง๋๋ผ๊ตฌ์.
๋ฌด์๋ณด๋ค๋ `race()` ๋ฅผ ์ฌ๋ฌ ๋ฒ ๋ฐ๋ณตํ๋๋ผ๋ ํญ์ *List*๊ฐ ์ด๊ธฐํ ๋๊ธฐ ๋๋ฌธ์ ํญ์ ๊ฐ์ ๊ฐ๋ง ๋ฐ๋ณตํ๊ฒ ๋๊ธฐ๋ ํ๊ตฌ์.
```java
cars.race(() -> newArrayList(4, 1, 4, 4, 1, 4).remove(0)); // 4 ๋ฐํ
cars.race(() -> newArrayList(4, 1, 4, 4, 1, 4).remove(0)); // 4 ๋ฐํ
```
[Functional Interface ๋?](https://tecoble.techcourse.co.kr/post/2020-07-17-Functional-Interface/) ์ ์ฝ์ด๋ณด๋ ๋ค์๊ณผ ๊ฐ์ ๋๋ชฉ์ด ์๋๋ผ๊ตฌ์!
```
ํจ์ํ ๊ฐ๋ฐ ๋ฐฉ์์ ํ์์ ํด๋นํ๋ ๋ถ๋ถ๋ ๊ฐ์ผ๋ก ์ทจ๊ธ์ด ๊ฐ๋ฅํด ์ก๋ค๋ ๊ฒ์ธ๋ฐ
์๋ฐ์์ ์๋ฏธํ๋ ๊ธฐ๋ณธํ์ ๋ฐ์ดํฐ(Integer ๋ String)๋ง ๊ฐ์ด ์๋๋ผ ํ์(๋ก์ง)๋
๊ฐ์ผ๋ก ์ทจ๊ธํ ์ ์๊ฒ ๋์๋ค๋ ์ด์ผ๊ธฐ์
๋๋ค.
์ด๊ฒ์ ์๋ฐ๊ฐ ์ฝ๋์ ์ฌํ์ฉ ๋จ์๊ฐ ํด๋์ค์๋ ๊ฒ์ด ํจ์ ๋จ์๋ก ์ฌ์ฌ์ฉ์ด ๊ฐ๋ฅํด ์ง๋ฉด์
์กฐ๊ธ ๋ ๊ฐ๋ฐ์ ์ ์ฐํ๊ฒ ํ ์ ์๊ฒ ๋ ์ ์ด๋ผ๊ณ ํ ์ ์์ต๋๋ค.
```
ํจ์ํ ์ธํฐํ์ด์ค์ ๋์์ด ๋๋ ๊ฒ์
ํด๋์ค์ ์ข
์ ๋์ด์๋ (์ ๊ฐ ๋๋ผ๊ธฐ์ ์ํ์ ๋ฌถ์ฌ์๋) **๋ฉ์๋**๊ฐ ์๋๋ผ
์ค๋ก์ง ํ์ ๋ง์ ๋ค๋ฃจ๊ณ ์๋ **ํจ์** ๋ฟ์ธ ๊ฒ ๊ฐ์ต๋๋ค.
(๊ทธ๋ฌ๊ณ ๋ณด๋ ์ด๋ฆ๋ **ํจ์ํ** ์ธํฐํ์ด์ค๋ค์...)
๋ถ๋ช
์ ์ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ํ์ตํ์๋๋ฐ ํจ์์ ๋ฉ์๋์ ์ฐจ์ด๋ฅผ ์๊ฐํ์ง ์๊ณ ์ฌ์ฉํ๋ ค๊ณ ํ๋ค์ ํ...
์๊ฒฝ๋ ๋๋ถ์ ์ ๋ ํจ์ํ ์ธํฐํ์ด์ค์ ๋ํด์ ์ข ๋ ๋ช
๋ฃํ๊ฒ ์ดํดํ๊ฒ ๋๋ ๊ณ๊ธฐ๊ฐ ๋์์ต๋๋ค.
์ข์ ์ง์ ๊ฐ์ฌ๋๋ ค์!!! :) |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ๋ง์ํด์ฃผ์ ๋๋ก *UI* ๋ฅผ ๋ค๋ฃจ๋ ๋ก์ง์ ๋๋ฉ์ธ ๋ด๋ถ์์ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ๋ง์์ ๊ฑธ๋ฆฌ๋๋ผ๊ตฌ์...
*UI* ์๊ตฌ์ฌํญ์ด ๋ณํ๋ฉด ๋๋ฉ์ธ ๋ด๋ถ์๋ ๋ณํ๊ฐ ๊ฐ์ ๋๋ค ๋ ๊ฒ ์คํ๋ ค ๊ฐ์ฒด ๋
๋ฆฝ์ฑ์ ํด์น๋ค๊ณ ์๊ฐํ์ต๋๋ค.
๊ทธ๋์ ๊ฐ์ฒด๋ฅผ ์ง์ ๊บผ๋ด์ค๋ ๋์ ๋ค์๊ณผ ๊ฐ์ด ๋ถ๋ณ ๊ฐ์ฒด๋ก ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋๋ก ์ฒ๋ฆฌํด์ฃผ์์ต๋๋ค.
```java
public List<Car> get() {
return Collections.unmodifiableList(cars);
}
```
๊ทธ๋๋ ์ญ์ ์ผ๊ธ ์ปฌ๋ ์
๋ด์ ์ํ๋ฅผ ์ง์ ๊บผ๋ด์์ ์ฌ์ฉํ๋ ๊ฒ ๋๋ฌด ์ฐ์ฐํ๊ธด ํ๋๋ผ๊ตฌ์...
์ ๊ฐ์ธ์ ์ธ ์๊ฐ์ ๋ ์์ธํ๊ฒ ๋ง์๋๋ฆฌ๊ฒ ์ต๋๋ค.
์ ๋ *UI* ๋ก์ง์ ๋๋ฉ์ธ์ ํตํฉํ๋ ๊ฒ ๋ณด๋ค๋ `getter()` ๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์คํ๋ ค ๋ ๋ซ๋ค๊ณ ํ๋จํ์ต๋๋ค.
๋ฏธ์
์ ์งํํ๋ฉด์ ์ ๋ `getter()`๋ฅผ ๋ฌด์กฐ๊ฑด์ ์ผ๋ก ๋ฐฐ์ ํ๋๊ฒ ๋๋ฌด ํ๋ค๋๋ผ๊ตฌ์...
๋ ๊ฐ์ฒด๋ฅผ ๋น๊ตํ๊ฑฐ๋, ์ง์ ์ฌ๋ถ๋ฅผ ํ๋จํ๋ ๋ก์ง์์๋ ๋ฉ์์ง๋ง์ผ๋ก๋ ์ถฉ๋ถํ ๊ตฌํํ ์ ์์์ง๋ง,
๋ ๊ฐ์ฒด์ ๊ฐ์ผ๋ก ์ฐ์ฐ์ ํด์ผ ํ๋ค๊ฑฐ๋, ๊ฐ์ฒด ๋ด๋ถ์ ๊ฐ์ ์ถ๋ ฅํด์ผ ํ๋ ๊ฒฝ์ฐ์ `getter()`๋ฅผ ์ฌ์ฉํ๋ ๊ฒ
์คํ๋ ค ๋ ํจ์จ์ ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค.
๊ทธ๋์ `getter()`๋ฅผ ์ต๋ํ ์ง์ํ๋ ์ด์ ์ฆ, ์บก์ํ๋ฅผ ์๋ฐํ์ง ์๋์ง, ๋ถ๋ณ์ฑ์ ํด์น์ง ์๋์ง๋ฅผ ๊ณ์ ์์ํ๋ฉด์ ์์ ํ๊ฒ `getter()`๋ฅผ ์ฌ์ฉํด๋ณด๋ ค๊ณ ๋
ธ๋ ฅํ๊ณ ์์ต๋๋ค.
ํน์ ์๊ฒฝ๋์ `getter()`๋ฅผ ์ฌ์ฉํ๋ ๊ฒ ๋ณด๋ค UI ๋ก์ง์ ๋๋ฉ์ธ ๋ด๋ถ์์ ๊ตฌํํ๋ ๊ฒ ์ด ๋ ํฉ๋ฆฌ์ ์ด๋ผ๊ณ ์๊ฐํ์
๋ ๋ค๋ฅธ ์ด์ ๊ฐ ์๋์? ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,37 @@
+package racingcar.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import racingcar.model.AttemptCount;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class InputView {
+
+ private static final String SPLIT_REGEX = ",";
+ private static final String NON_NUMERIC_INPUT_MESSAGE = "์ซ์๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.";
+ private static final String REQUEST_ATTEMPT_COUNT_MESSAGE = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+
+ public List<String> readNames() {
+ String names = Console.readLine();
+
+ return Arrays.stream(names.split(SPLIT_REGEX))
+ .collect(Collectors.toList());
+ }
+
+ public AttemptCount readAttemptCount() {
+ System.out.println(REQUEST_ATTEMPT_COUNT_MESSAGE);
+ String attemptCount = Console.readLine();
+
+ return AttemptCount.from(parseInt(attemptCount));
+ }
+
+ private int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException error) {
+ throw new IllegalArgumentException(NON_NUMERIC_INPUT_MESSAGE);
+ }
+ }
+} | Java | ์ ๊ฐ ์๊ฐํ์ ๋ ์์๋ฅผ ์ฌ์ฉํ๋ ์ด์ ์๋
- ํด๋์ค ๊ฐ, ๋ฉ์๋ ๊ฐ ๊ฐ์ ์ฌ์ฌ์ฉํ๋ ์ธก๋ฉด๋ ์์ง๋ง
- **์ธ์คํด์ค ๊ฐ์ ๊ฐ์ ๊ณต์ **ํ๋ ์ธก๋ฉด๋ ์๋ค๊ณ ์๊ณ ์์ต๋๋ค!
- (์ด๋ฆ์ ์ง์ด์ค์ผ๋ก์จ ๊ฐ๋
์ฑ์ ๊ณ ๋ คํ๋ ์ธก๋ฉด๋ ์๊ตฌ์!)
`InputView` ์์ ์ฌ์ฉ๋๋ ์๋ฌ ๋ฉ์์ง๋ค ๊ฐ์ ๊ฒฝ์ฐ ์ธ์คํด์ค์ ์ํ์ ๋ฌด๊ดํ๊ฒ ํญ์ ๊ฐ์ด ๊ณ ์ ๋์ด ์์ต๋๋ค.
์ด๋ฐ ๊ฒฝ์ฐ ํ๋์ฝ๋ฉ๋ ๊ฐ์ `static final` ๋ก ์ ์ธํด์ฃผ๋ฉด ํ๋ก๊ทธ๋จ ์คํ ๋จ๊ณ์์ ๋ฑ ํ๋ฒ๋ง ์ด๊ธฐํ๋๊ธฐ ๋๋ฌธ์
์๋ก ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ์ฌ๋ฌ ๋ฒ ํธ์ถํ๋๋ผ๋ ์ธ์คํด์ค์ ํจ๊ป ๋งค๋ฒ ์ด๊ธฐํํด ์ค ํ์๊ฐ ์์ด์ง๊ฒ ๋ฉ๋๋ค.
๊ทธ๋์ ๋ฉ๋ชจ๋ฆฌ ์ ์ผ๋ก๋ ๋ ํจ์จ์ ์ด๋ผ๊ณ ์๊ฐํด์!
(์ซ์ ์ผ๊ตฌ ํผ๋๋ฐฑ ์์์์ ํด๋์ค ๋ณ์์ ์ธ์คํด์ค ๋ณ์์ ์ฐจ์ด๋ฅผ ๋ ์ฌ๋ฆฌ์๋ฉด ๋ ๊ฒ ๊ฐ์์!!)
์ด๋ฌํ ์ด์ ์์ ์ ๋ ํ๋์ฝ๋ฉ๋ ๊ฐ์ ๋ฌด์กฐ๊ฑด ์์ ์ฒ๋ฆฌ ํด๋ฒ๋ฆฌ์! ๋ผ๋ ์ฃผ์๋ก ํ๋ก๊ทธ๋๋ฐ ํ๊ณ ์์ต๋๋ค.
์๋์ฐจ ๊ฒฝ์ฃผ ๋ฏธ์
์์๋ `InputView` ๊ฐ ์ฌ๋ฌ๋ฒ ์ด๊ธฐํ ๋ ๊ฐ๋ฅ์ฑ์ด ๊ฑฐ์ ์์ง๋ง ๊ทธ๋ ๋ค ํ๋๋ผ๋
ํ๋์ฝ๋ฉ ๋ ๊ฐ์ ์ต๋ํ ์์๋ก ์ฒ๋ฆฌํ๋ ์ต๊ด์ ๋ค์ด๋ ค๊ณ ๋
ธ๋ ฅ์ด๋ผ๊ณ ์ดํดํด์ฃผ์๋ฉด ๋ ๊ฒ ๊ฐ์์!
ํน์ฌ๋ ๊ท๋ชจ๊ฐ ์ปค์ง๋ค๋ฉด `InputView` ํด๋์ค๋ฅผ ์ธ๋ถ ํด๋์ค๋ค๋ก ๋๋๋ ๊ฒ๋ ๊ด์ฐฎ์ ๋ฐฉ๋ฒ์ผ ๊ฒ ๊ฐ์์!
๊ทธ๋ ๊ฒ ๋๋ฉด ํด๋์ค๊ฐ ๊ด๋ จ๋ ์์๋ฅผ ํจ๊ป ๊ด๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ์ ์ ์ง๋ณด์ ์ธก๋ฉด์์๋ ๋ ์ ๋ฆฌํ๋ค๊ณ ์๊ฐํฉ๋๋ค! |
@@ -0,0 +1,37 @@
+package racingcar.view;
+
+import camp.nextstep.edu.missionutils.Console;
+import racingcar.model.AttemptCount;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class InputView {
+
+ private static final String SPLIT_REGEX = ",";
+ private static final String NON_NUMERIC_INPUT_MESSAGE = "์ซ์๋ง ์
๋ ฅํ ์ ์์ต๋๋ค.";
+ private static final String REQUEST_ATTEMPT_COUNT_MESSAGE = "์๋ํ ํ์๋ ๋ชํ์ธ๊ฐ์?";
+
+ public List<String> readNames() {
+ String names = Console.readLine();
+
+ return Arrays.stream(names.split(SPLIT_REGEX))
+ .collect(Collectors.toList());
+ }
+
+ public AttemptCount readAttemptCount() {
+ System.out.println(REQUEST_ATTEMPT_COUNT_MESSAGE);
+ String attemptCount = Console.readLine();
+
+ return AttemptCount.from(parseInt(attemptCount));
+ }
+
+ private int parseInt(String input) {
+ try {
+ return Integer.parseInt(input);
+ } catch (NumberFormatException error) {
+ throw new IllegalArgumentException(NON_NUMERIC_INPUT_MESSAGE);
+ }
+ }
+} | Java | ์คํธ ์ข์ ์ต๊ด์ธ ๊ฒ ๊ฐ์์ ใ
ใ
ํ๋์ฝ๋ฉ๋ ๊ฐ์ ์ฌ์ฉํ์ง ์๋ค๊ฐ ์ฌ์ฉํ๋ ๊ฑด ์ผ์ผ์ด ์์ํ๊ธฐ ํ๋ค์ง๋ง, ์์๋ฅผ ์ฌ์ฉํ๋ ์์ผ๋ก ์ฝ๋ฉํ๋ค๊ฐ ๊ฐ์ ์ง์ ์ฐ๋ ๋ฐฉ์์ผ๋ก ์ ํํ๊ธฐ ์ฌ์ธ ํ
๋๊น์
์ด๋ฌํ ํผ๋๋ฐฑ์ ๋๋ฆฐ ์ด์ ๋ ํ๋ฆฌ์ฝ์ค ์ธ์ ์ธํดํ๋ ํ์ฌ์์ `์์ธ ๋ฉ์ธ์ง์ ์์ ๊ฐ์ ์ง์ ๋ค ๋ฐ์ ๊ฒฝ์ฐ ์์๋ฅผ ๋ฌด๋ถ๋ณํ๊ฒ ๋ง์ด ์ฌ์ฉํ๊ฒ ๋ผ์ ์ฝ๋๋ฅผ ์ฝ์ ๋ ์ ์ ๋ฌด์์ด ์ค์ํ์ง ํ๋ณํ๊ธฐ ํ๋ค์ด์ง๊ณ ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ค๋ ๋ฌธ์ ์ ์ด ๋ฐ์ํ๋ค` ๋ผ๋ ํผ๋๋ฐฑ์ ๋ฐ์๊ธฐ ๋๋ฌธ์ธ๋ฐ์
๋ง์ํ์ ๊ฒ์ฒ๋ผ ์ฐ์ต์ ๊ณผ์ ์ด๋ผ๋ฉด ์คํ๋ ค ์ข์ ์ ํธ๊ฒ ๋ค์! ๐ |
@@ -0,0 +1,44 @@
+package racingcar.model;
+
+import org.junit.jupiter.api.DisplayNameGeneration;
+import org.junit.jupiter.api.DisplayNameGenerator;
+import org.junit.jupiter.api.Test;
+import racingcar.RacingNumberGenerator;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.util.Lists.newArrayList;
+
+@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
+public class CarsTest {
+
+ @Test
+ void ์๋์ฐจ๋ค์_์ด๋ฆ์_์๋ก_์ค๋ณต๋ _์_์๋ค() {
+ assertThatIllegalArgumentException()
+ .isThrownBy(() -> Cars.from(List.of("pobi,jun,pobi")));
+ }
+
+ @Test
+ void ๊ฐ์ฅ_๋ฉ๋ฆฌ_์ด๋ํ_์ฌ๋ฆผ์ด_์ฐ์นํ๋ค() {
+ Cars cars = Cars.from(List.of("pobi", "jun", "khj"));
+ TestRacingNumberGenerator numberGenerator = new TestRacingNumberGenerator();
+
+ cars.race(numberGenerator);
+ cars.race(numberGenerator);
+
+ assertThat(cars.findWinners())
+ .contains(CarName.from("pobi"), CarName.from("khj"));
+ }
+
+ static class TestRacingNumberGenerator implements RacingNumberGenerator {
+
+ private final List<Integer> numbers = newArrayList(4, 1, 4, 4, 1, 4);
+
+ @Override
+ public int generate() {
+ return numbers.remove(0);
+ }
+ }
+} | Java | ๊ทธ๋ ๊ตฐ์! ์ฌ์ค ์ด ๋ถ๋ถ์ ์ทจํฅ์ด๋ผ ๋ ๊ฐ๋จํ๊ฒ ํํ์ด ๊ฐ๋ฅํ ๊ฒ ๊ฐ์์ ๋ง์๋๋ฆฐ ๋ถ๋ถ์ด์์ต๋๋ค ใ
ใ
์ ๊ฐ์ ๊ฒฝ์ฐ์๋ js๋ง ์ฌ์ฉํ๋ค๊ฐ ์ด๋ฒ ํ๋ฆฌ์ฝ์ค์์ ์๋ฐ๋ฅผ ์์ํ๋๋ฐ ์คํ๋ ค ๋ฉ์๋์ ์ฌ๊ณ ํ๋๊ฒ ํ๋ค๋๋ผ๊ตฌ์ ์์ด๋ ๊ฒ ์ง์ ์ ๋๋ ๋ถ๋ถ๋ค์ด ๋ง์์ง...ใ
ใ
์ฒจ๋ถํด์ฃผ์ ๋ด์ฉ์ ๋ํด ๋ง๋ถ์ด์๋ฉด ์ ์ค๋ช
์์ ๋งํ๊ณ ์๋๊ฑด ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ธ์๋ก ํจ์๋ฅผ ๋๊ธธ ์ ์๊ฒ ๋๋ค๋ ๊ฑธ ๋งํ๋ ๊ฒ ๊ฐ์ต๋๋ค. (js์ฒ๋ผ์!) ์ด๋ฐ ํน์ง์ `์ผ๊ธํจ์`๋ผ๊ณ ๋ถ๋ฅด๋๋ฐ์, ์ด ์ผ๊ธํจ์๋ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ํํ๊ธฐ ์ํ ์ต์ํ์ ์กฐ๊ฑด์ด ๋ฉ๋๋ค.
๋ฐ๋ผ์, ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ๋๊ธฐ๋ฉด ์ฌ์ค์ ์ต๋ช
ํด๋์ค์ง๋ง ํจ์๋ฅผ ๋๊ธฐ๋ ๊ฒ์ฒ๋ผ ๋ง๋ค์ด ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ด๋ด๋ ๋ฐฉ์์ ์ฝ๋๋ฅผ ์งค ์ ์๋ค! ๋ผ๋๊ฑฐ์ฃ |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ ์ฐ์ ์ ์ ๋ ๋ฉ์ธ์ง๋ฅผ ๋ง๋๋ ๋ถ๋ถ ์์ฒด๋ฅผ UI ๋ก์ง์ด๋ผ๊ณ ์๊ฐํ๊ธฐ ๋ณด๋ค๋ ๋๋ฉ์ธ์์ ํด๊ฒฐ ๊ฐ๋ฅํ ์์ญ์ด๋ผ๊ณ ์๊ฐํ์ต๋๋ค. ์๋ํ๋ฉด ์ด๋ฏธ `Car`์์ ํด๋น ์ํ์ ๋ํด ๋ชจ๋ ๊ฑธ ์๊ณ ์๋๋ฐ ์ด๋ฅผ ๊ตณ์ด ๋ฐ์ผ๋ก ๊บผ๋ธ๋ค๋ฉด ์บก์ํ๊ฐ ๊นจ์ง๋ค๊ณ ์๊ฐํ๊ฑฐ๋ ์. ๊ทธ๋ฐ๋ฐ ๋ง์ํด์ฃผ์ ๋ด์ฉ์ ์ฝ์ด๋ณด๋ ์ด๋ฅผ UI๋ผ๊ณ ์๊ฐํ๋ฉด ์ ์ด์ ๋ค๋ฅธ ์์ญ์ผ๋ก ๋์ ธ์ฃผ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๊ทธ๋๋ก ๊บผ๋ธ๋ค๊ณ ํด์ ์บก์ํ๊ฐ ๊นจ์ง๋ ๊ฑด ์๋๊ฐ? ํ๋ ์๊ฐ๋ ๋ค๊ณ ์ด๋ ต๋ค์ ใ
ใ
์ฌ์ค ์ ๋ `getter`๋ฅผ ๋ฌด์กฐ๊ฑด์ ์ผ๋ก ๋ฐฐ์ ํ๋ ๊ฒ์ ์ณ์ง ์๋ค๊ณ ์๊ฐํ๋ ์ฃผ์๊ธฐ๋ ํ๊ตฌ์! ๋ค ํ์ํ๋๊น ์ฐ๋ ๊ฒ์ธ๋ฐ ํ๋ ์๊ฐ๋ ๋ค๊ณ ์ :D ๋จ๊ฒจ์ฃผ์ ์๊ฒฌ ๋ชจ๋ ์ฆ๊ฑฐ์ด ์ ๋ค์์ต๋๋ค ใ
ใ
ใ
๋ค์ ์๊ฐํด๋ณผ ์ ์๋ ๊ณ๊ธฐ๋ ๋์๊ตฌ์ |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์์์ฒ๋ฆฌ๊ฐ ์ ๋ง ๊น๋ํ๋ค์!
ํนํ SCORE_BOARD ๋ถ๋ถ ๋ฐฐ์๊ฐ๋๋ค. |
@@ -0,0 +1,57 @@
+package racingcar.view;
+
+import racingcar.model.Car;
+import racingcar.model.CarName;
+import racingcar.model.Cars;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class OutputView {
+
+ private static final String NEW_LINE = "\n";
+ private static final String SCORE_UNIT = "-";
+ private static final String SCORE_BOARD = "%s : %s";
+ private static final String WINNER_SEPARATOR = ", ";
+ private static final String WINNERS_ARE = "์ต์ข
์ฐ์น์ : ";
+ private static final String ERROR_PREFIX = "[ERROR] ";
+ private static final String RESULT_INTRO = "์คํ ๊ฒฐ๊ณผ";
+
+ public void printResult(Cars cars) {
+ System.out.println(getScore(cars));
+ }
+
+ private String getScore(Cars cars) {
+ return cars.get().stream()
+ .map(this::toScoreBoard)
+ .collect(Collectors.joining(NEW_LINE));
+ }
+
+ private String toScoreBoard(Car car) {
+ return String.format(SCORE_BOARD, car.getName(), convertToScore(car.getPosition()));
+ }
+
+ private String convertToScore(int position) {
+ return SCORE_UNIT.repeat(position);
+ }
+
+ public void printWinners(List<CarName> winners) {
+ System.out.print(WINNERS_ARE);
+ System.out.println(getWinnerNames(winners));
+ }
+
+ private String getWinnerNames(List<CarName> winners) {
+ return winners.stream()
+ .map(CarName::get)
+ .collect(Collectors.joining(WINNER_SEPARATOR));
+ }
+
+ public void printError(IllegalArgumentException error) {
+ System.out.print(ERROR_PREFIX);
+ System.out.println(error.getMessage());
+ }
+
+ public void printRaceIntro() {
+ System.out.println(RESULT_INTRO);
+ }
+} | Java | ์ ๋ @khj1 ๋๊ณผ ๊ฐ์ ์ด์ ๋ก getter๋ฅผ ์ฌ์ฉํ๋๋ฐ์,
๊ฐ์ธ์ ์ผ๋ก๋ `์ถ๋ ฅ์ ์ํ getter๋ ์บก์ํ๋ฅผ ์๋ฐฐํ๋ ๊ฒ ์๋๋ค` ๋ผ๊ณ ์๊ฐํฉ๋๋ค.
๋ฌผ๋ก ์๋์ ๋ค๋ฅด๊ฒ ์ฌ์ฉ๋๋ ๊ฒฝ์ฐ ์บก์ํ๋ฅผ ์ ํด์ํฌ ์ ์์ต๋๋ค. ์ด ๋ถ๋ถ์ ์ด์ฉ ์ ์๋ ๋จ์ ์ด๋ผ๊ณ ์๊ฐํด์.
๊ฐ์ธ์ ์ผ๋ก ํ๋ฆฌ์ฝ์ค ๊ธฐ๊ฐ ๋์ '๊ฐ์ฒด์ ์์จ์ฑ์ ์ผ๋ง๋ ํ์ฉํด์ค์ผ ํ๋์ง' ๋ฅผ ๊ณ์ ๊ณ ๋ฏผํ๋๋ฐ, ์ ๊ฐ ๋ค๋ค๋ฅธ ๊ฒฐ๋ก ์ '๊ฐ์ฒด๋ UI ๋ก์ง ์ธ์ ๊ฒฝ์ฐ์์ ์์จ์ฑ์ ๋ณด์ฅํด์ค์ผ ํ๋ค' ์์ต๋๋ค.
๊ฐ์ฒด์งํฅ์ ์ฌ์ฉํ๋ ์ด์ ๋ ๊ฒฐ๊ตญ '์์จ์ฑ'๋ณด๋ค๋ '์ ์ฐ์ฑ'์ด๋๊น์! UI ๋ก์ง์ ๋ณ๊ฒฝ์ด ๋๋ฉ์ธ ์์ญ์ ์นจ๋ฒํ๋ ๊ฒ์ ์๋๋ค๊ณ ํ๋จํ์ต๋๋ค.
์ ๋ ์ด๋ ๊ฒ ์๊ฐํ๋๋ฐ,
@JerryK026 ๋ ๋ง์ ๋ค์ด๋ณด๋ ์ด ๋ถ๋ถ์ ์คํ์ผ์ ์ฐจ์ด์ผ ์๋ ์๊ฒ ๋ค์!
์
๋ฌด ํ๊ฒฝ์ด๋ ํ์ ์ฝ๋ ์คํ์ผ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ๊ฒ ๊ฐ๊ธฐ๋ ํฉ๋๋ค. ํ์คํ ์ด ๋ถ์ผ์ ์ ๋ต์ ์๋ ๊ฒ ๊ฐ๋ค์.. ใ
ใ
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.