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 ๋‹˜ ๋ง์”€ ๋“ค์–ด๋ณด๋‹ˆ ์ด ๋ถ€๋ถ„์€ ์Šคํƒ€์ผ์˜ ์ฐจ์ด์ผ ์ˆ˜๋„ ์žˆ๊ฒ ๋„ค์š”! ์—…๋ฌด ํ™˜๊ฒฝ์ด๋‚˜ ํŒ€์˜ ์ฝ”๋“œ ์Šคํƒ€์ผ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์งˆ ๊ฒƒ ๊ฐ™๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค. ํ™•์‹คํžˆ ์ด ๋ถ„์•ผ์— ์ •๋‹ต์€ ์—†๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”.. ใ…Žใ…Ž