code stringlengths 41 34.3k | lang stringclasses 8 values | review stringlengths 1 4.74k |
|---|---|---|
@@ -0,0 +1,58 @@
+package store.product;
+
+import store.product.promotion.PromotionState;
+import store.utils.ExceptionFactory;
+import store.utils.ExceptionType;
+import store.utils.Validator;
+
+public class PurchaseRequest {
+ private String productName;
+ private int countPurchased;
+ private PromotionState promotionState;
+ public PurchaseRequest(){
+
+ }
+
+ public PurchaseRequest(String productName, int countPurchased, PromotionState promotionState){
+ this.countPurchased = countPurchased;
+ this.productName = productName;
+ this.promotionState = promotionState;
+ }
+
+ public String getProductName(){
+ return this.productName;
+ }
+ public int getCountPurchased(){
+ return this.countPurchased;
+ }
+
+ public PromotionState getPromotionState(){
+ return this.promotionState;
+ }
+
+ public void increaseCount(int value){
+ Validator.validatePositiveNumber(value);
+ this.countPurchased += value;
+ }
+
+ public PurchaseRequest tryToIncrease(boolean isIncrease){
+ int step = 1;
+ if(isIncrease){
+ this.increaseCount(step);
+ return this;
+ }
+ return this;
+ }
+
+ public void decreaseCount(int value){
+ if(value > this.countPurchased){
+ ExceptionFactory.throwIllegalStateException(ExceptionType.INTERNAL_ERROR);
+ }
+ this.countPurchased -=value;
+ }
+
+ public boolean isCountMet(){
+ int zero = 0;
+ return this.countPurchased == zero;
+ }
+} | Java | PurchaseRequest๋ DTO ์ญํ ๊ณผ ๊ตฌ๋งค ์ฃผ๋ฌธ ๋ฐ์ดํฐ ์ ์ฅ ์ญํ ์ ํ๋ ํด๋์ค์ธ๋ฐ์.
DTO ์ญํ ์ ์ํด์ ๋ค๋ฅธ ํด๋์ค์์๋ ์ฝ๊ฒ ์์ฑํ ์ ์๋๋ก, public์ ์ฌ์ฉํ์์ต๋๋ค. |
@@ -0,0 +1,71 @@
+package store.product.promotion;
+
+
+import store.utils.ExceptionFactory;
+import store.utils.ExceptionType;
+
+public class PromotionResult {
+
+ private int appliedItemCount = 0;
+ private int omittedItemCount = 0;
+ private int totalItemCount = 0;
+ private PromotionState state;
+ private String productName;
+
+ public PromotionResult(){
+
+ }
+
+ public PromotionResult(PromotionState state, int totalItemCount,int appliedItemCount, int omittedItemCount, String productName){
+ this.state = state;
+ this.totalItemCount = totalItemCount;
+ this.appliedItemCount = appliedItemCount;
+ this.omittedItemCount = omittedItemCount;
+ this.productName = productName;
+
+ }
+
+// public void setAppliedItemCount(int appliedItemCount) {
+// this.appliedItemCount = appliedItemCount;
+// }
+//
+// public void setNeededItemCount(int enableItemCount) {
+// this.omittedItemCount = enableItemCount;
+// }
+
+ public int getAppliedItemCount() {
+ return appliedItemCount;
+ }
+
+ public int getOmittedItemCount() {
+ return omittedItemCount;
+ }
+
+ public int getTotalItemCount() { return totalItemCount;}
+
+ public int getNonPromotedCount() {
+ int fullPromotedCount = totalItemCount + omittedItemCount;
+ int appliedCountWhenFullPromoted = appliedItemCount + 1;
+ int zero = 0;
+ if(fullPromotedCount% appliedCountWhenFullPromoted != zero){
+ ExceptionFactory.throwIllegalStateException(ExceptionType.INTERNAL_ERROR);
+ }
+ int promotionUnit = fullPromotedCount/appliedCountWhenFullPromoted;
+ return totalItemCount % promotionUnit;
+ }
+
+ public PromotionState getState() { return state; }
+
+ public String getProductName() {return productName;}
+
+ public PromotionResult transitState(PromotionState newState){
+ return new PromotionResult(newState,totalItemCount,appliedItemCount,omittedItemCount,productName);
+ }
+
+ static public PromotionResult createNoPromotion(String productName,int totalItemCount){
+ final int zero = 0;
+ return new PromotionResult(PromotionState.NO_PROMOTION,totalItemCount,zero,zero,productName);
+ }
+
+
+} | Java | ์ ๋ ๋์ผํ ์๊ฒฌ์ธ๋ฐ์.
์ธํ
๋ฆฌ์ ์ด์ fomatter ์ค์ ์์ ์ด ํ์ํ๊ฒ ๋ค์. |
@@ -0,0 +1,80 @@
+package store;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import store.io.ProductFile;
+import store.io.PromotionFile;
+import store.product.Product;
+import store.utils.ExceptionFactory;
+import store.utils.ExceptionType;
+import store.utils.Transformer;
+import store.utils.Validator;
+
+public class LoadModel {
+ public final static String PRODUCT_LIST_FORMAT = "name,price,quantity,promotion";
+ public final static String PROMOTION_LIST_FROMAT= "name,buy,get,start_date,end_date";
+ public final static String DELIMITER = "\n";
+ final static String RESOURCE_PATH = "./src/main/resources/";
+ final static String PRODUCT_LIST = "products.md";
+ final static String PROMOTION_LIST = "promotions.md";
+ public LoadModel(){
+ }
+
+ public String loadProductList(){
+ String result = readFileAsString(RESOURCE_PATH + PRODUCT_LIST);
+ validateProductList(result);
+ return result;
+ }
+
+ public String loadPromotionList(){
+ String result = readFileAsString(RESOURCE_PATH + PROMOTION_LIST);
+ validatePromotionList(result);
+ return result;
+ }
+
+ public List<String> readFile(String path){
+ Validator.validateFileSize(path);
+ try {
+ return Files.readAllLines(Paths.get(path));
+ } catch (IOException exception){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.READ_FILE_FAIL,exception);
+ throw new RuntimeException();
+ }
+ }
+
+ public String readFileAsString(String path){
+
+ List<String> contents = readFile(path);
+ if(contents.isEmpty()) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.EMPTY_FILE);
+ }
+ return Transformer.concatenateList(contents,DELIMITER);
+ }
+
+ public static void validateProductList(String products){
+ if(products.isBlank()) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.EMPTY_FILE);
+ }
+ List<String> productList = Arrays.stream(products.split(DELIMITER)).toList();
+ if(!productList.getFirst().equals(PRODUCT_LIST_FORMAT)){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_FILE_FORMAT);
+ }
+ Validator.validateEachLine(productList, ProductFile.COLUMN_DELIMITER,ProductFile.values().length);
+ }
+
+ public static void validatePromotionList(String promotions){
+ if(promotions.isBlank()) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.EMPTY_FILE);
+ }
+ List<String> promotionList = Arrays.stream(promotions.split(DELIMITER)).toList();
+ if(!promotionList.getFirst().equals(PROMOTION_LIST_FROMAT)){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_FILE_FORMAT);
+ }
+ Validator.validateEachLine(promotionList,PromotionFile.COLUMN_DELIMITER,PromotionFile.values().length);
+ }
+
+
+} | Java | ์์ ์๋ ๋ฉ์๋์์ `throw`๋ฅผ ํ๋๋ฐ, `throw`๋ฅผ ํ ๋ฒ ๋ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,47 @@
+package store.utils;
+
+public enum ExceptionType {
+ LIST_OVER_MAX_LENGTH("list is over mat length."),
+ INVALID_NUMERIC_STRING("String is not numeric."),
+ OUT_OF_RANGE_INT("number is out of int type rage"),
+ NON_POSITIVE_NUMBER("number is not positive number"),
+ NON_DIVISIBLE("values are not divisible relation"),
+ OUT_OF_SPECIFIC_RANGE("out of specific range"),
+ NEGATIVE_NUMBER("number is negative"),
+
+ //Number list related
+ NOT_PROPER_SIZE("list size is not proper"),
+ DUPLICATED_ELEMENTS("list includes duplicated elements"),
+
+ //STRING related
+ EMPTY_STRING("string is empty"),
+ DISABLE_ENCODED_TO_UTF8("string is not able to be encoded by utf 8"),
+ INVALID_INPUT_STRING_FORMAT("input string format is invalid"),
+ INVALID_NAME_FORMAT("name should be composed by english, korean and number"),
+
+ //Model
+ INTERNAL_ERROR("Internal logic error"),
+
+ //Promotion
+ INVALID_RETURN_PROMOTION("return should be 1"),
+
+ //File or I/O
+ READ_FILE_FAIL("can't read the file"),
+ TOO_LARGE_FILE("File is too large to process"),
+ EMPTY_FILE("File is empty"),
+ INVALID_FILE_FORMAT("File format is invalid"),
+
+ //LocalDate
+ INVALID_DATE_STRING("String is not able to pared to LocalDate/LocalDateTime");
+
+ private final String message;
+
+ private ExceptionType(String message){
+ this.message = message;
+ }
+
+ public String getMessage(){
+ return this.message;
+ }
+
+} | Java | ๋ค์ํ ์์ธ๋ฅผ ์ฒ๋ฆฌํ์ ๋ถ๋ถ์ด ์ธ์์ ์
๋๋ค! |
@@ -0,0 +1,80 @@
+package store;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import store.io.ProductFile;
+import store.io.PromotionFile;
+import store.product.Product;
+import store.utils.ExceptionFactory;
+import store.utils.ExceptionType;
+import store.utils.Transformer;
+import store.utils.Validator;
+
+public class LoadModel {
+ public final static String PRODUCT_LIST_FORMAT = "name,price,quantity,promotion";
+ public final static String PROMOTION_LIST_FROMAT= "name,buy,get,start_date,end_date";
+ public final static String DELIMITER = "\n";
+ final static String RESOURCE_PATH = "./src/main/resources/";
+ final static String PRODUCT_LIST = "products.md";
+ final static String PROMOTION_LIST = "promotions.md";
+ public LoadModel(){
+ }
+
+ public String loadProductList(){
+ String result = readFileAsString(RESOURCE_PATH + PRODUCT_LIST);
+ validateProductList(result);
+ return result;
+ }
+
+ public String loadPromotionList(){
+ String result = readFileAsString(RESOURCE_PATH + PROMOTION_LIST);
+ validatePromotionList(result);
+ return result;
+ }
+
+ public List<String> readFile(String path){
+ Validator.validateFileSize(path);
+ try {
+ return Files.readAllLines(Paths.get(path));
+ } catch (IOException exception){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.READ_FILE_FAIL,exception);
+ throw new RuntimeException();
+ }
+ }
+
+ public String readFileAsString(String path){
+
+ List<String> contents = readFile(path);
+ if(contents.isEmpty()) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.EMPTY_FILE);
+ }
+ return Transformer.concatenateList(contents,DELIMITER);
+ }
+
+ public static void validateProductList(String products){
+ if(products.isBlank()) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.EMPTY_FILE);
+ }
+ List<String> productList = Arrays.stream(products.split(DELIMITER)).toList();
+ if(!productList.getFirst().equals(PRODUCT_LIST_FORMAT)){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_FILE_FORMAT);
+ }
+ Validator.validateEachLine(productList, ProductFile.COLUMN_DELIMITER,ProductFile.values().length);
+ }
+
+ public static void validatePromotionList(String promotions){
+ if(promotions.isBlank()) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.EMPTY_FILE);
+ }
+ List<String> promotionList = Arrays.stream(promotions.split(DELIMITER)).toList();
+ if(!promotionList.getFirst().equals(PROMOTION_LIST_FROMAT)){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_FILE_FORMAT);
+ }
+ Validator.validateEachLine(promotionList,PromotionFile.COLUMN_DELIMITER,PromotionFile.values().length);
+ }
+
+
+} | Java | ์ฌ์ํ ๋ถ๋ถ์ด์ง๋ง, ์คํ๊ฐ ์๋ ๊ฑฐ ๊ฐ์์!
`PROMOTION_LIST_FROMAT`์ `PROMOTION_LIST_FORMAT`์ผ๋ก ๋ฐ๊พธ์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์! |
@@ -0,0 +1,196 @@
+package store;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import camp.nextstep.edu.missionutils.Console;
+import store.product.PurchaseRequest;
+import store.product.promotion.PromotionResult;
+import store.product.promotion.PromotionState;
+import store.utils.ExceptionFactory;
+import store.utils.ExceptionType;
+import store.utils.Transformer;
+import store.utils.Validator;
+
+public class StoreInputView {
+ static final Pattern PURCHASE_PATTERN = Pattern.compile(StoreViewMessage.PURCHASE_PATTERN);
+
+ public List<PurchaseRequest> readPurchaseProduct(){
+ StoreViewMessage.PURCHASE_GUIDE.printMessage();
+ String purchaseList = Console.readLine();
+ try {
+ return createPurchaseRequests(purchaseList);
+ }catch (IllegalArgumentException exception){
+ StoreViewMessage.ERROR_INVALID_FORMAT.printMessage();
+ return readPurchaseProduct();
+ }
+ }
+
+ public List<PurchaseRequest> retryReadPurchaseProduct(List<StoreViewMessage> errorMessages){
+ for(StoreViewMessage message : errorMessages){
+ message.printMessage();
+ }
+ return readPurchaseProduct();
+ }
+
+ public List<PurchaseRequest> readAnswerPurchaseChange(List<PromotionResult> promotionResults){
+ List<PurchaseRequest> newPurchaseRequest = new ArrayList<>();
+ for(PromotionResult promotionResult : promotionResults){
+ newPurchaseRequest.add(handlePromotionResults(promotionResult));
+ }
+
+ return newPurchaseRequest;
+ }
+
+ public PurchaseRequest handlePromotionResults(PromotionResult promotionResult){
+ if(promotionResult.getState().equals(PromotionState.OMISSION)) {
+ return readPromotionEnableProduct(promotionResult);
+ }
+ if(promotionResult.getState().equals(PromotionState.INSUFFICIENT)){
+ return readPromotionInsufficient(promotionResult);
+ }
+ return new PurchaseRequest(promotionResult.getProductName(),promotionResult.getTotalItemCount(),promotionResult.getState());
+ }
+
+ public PurchaseRequest readPromotionInsufficient(PromotionResult promotionResult){
+ PurchaseRequest newRequest = null;
+ String answer =readAnswerPromotionInSufficient(promotionResult);
+
+ if(answer.equals(StoreViewMessage.ANSWER_NO)){
+ newRequest = new PurchaseRequest(promotionResult.getProductName(),promotionResult.getTotalItemCount(),promotionResult.getState());
+ newRequest.decreaseCount(promotionResult.getNonPromotedCount());
+ return newRequest;
+ }
+
+ return new PurchaseRequest(promotionResult.getProductName(),promotionResult.getTotalItemCount(),PromotionState.APPLIED);
+ }
+
+ public boolean readAnswerContinuePurchase(){
+ StoreViewMessage.RETRY_PURCHASE.printMessage();
+ String answer = Console.readLine();
+ try{
+ validateAnswer(answer);
+ return answer.equals(StoreViewMessage.ANSWER_YES);
+ }catch (IllegalArgumentException exception){
+ StoreViewMessage.ERROR_INVALID_FORMAT.printMessage();
+ return readAnswerContinuePurchase();
+ }
+ }
+
+ public boolean readAnswerDiscountApply(){
+ StoreViewMessage.MEMBERSHIP_GUIDE.printMessage();
+ String answer = Console.readLine();
+ try{
+ validateAnswer(answer);
+ return answer.equals(StoreViewMessage.ANSWER_YES);
+ }catch (IllegalArgumentException exception){
+ StoreViewMessage.ERROR_INVALID_FORMAT.printMessage();
+ return readAnswerDiscountApply();
+ }
+ }
+
+
+ public PurchaseRequest readPromotionEnableProduct(PromotionResult promotionResult){
+ PurchaseRequest newRequest = new PurchaseRequest(promotionResult.getProductName(),promotionResult.getTotalItemCount(),PromotionState.APPLIED);
+ for(int tryCount = 0; tryCount < promotionResult.getOmittedItemCount(); tryCount++){
+ String answer = readAnswerPromotionEnable(promotionResult);
+ newRequest.tryToIncrease(answer.equals(StoreViewMessage.ANSWER_YES));
+ }
+
+ return newRequest;
+ }
+
+ public String readAnswerPromotionInSufficient(PromotionResult promotionResult){
+ StoreViewMessage.printPromotionWarning(promotionResult.getProductName(),promotionResult.getNonPromotedCount());
+ String answer = Console.readLine();
+ try{
+ validateAnswer(answer);
+ return answer;
+ }catch (IllegalArgumentException exception){
+ StoreViewMessage.ERROR_INVALID_FORMAT.printMessage();
+ return readAnswerPromotionInSufficient(promotionResult);
+ }
+ }
+
+ public String readAnswerPromotionEnable(PromotionResult promotionResult){
+ StoreViewMessage.printPromotionReturnAvailable(promotionResult.getProductName());
+ String answer = Console.readLine();
+ try{
+ validateAnswer(answer);
+ return answer;
+ }catch (IllegalArgumentException exception){
+ StoreViewMessage.ERROR_INVALID_FORMAT.printMessage();
+ return readAnswerPromotionEnable(promotionResult);
+ }
+ }
+
+ public List<PurchaseRequest> createPurchaseRequests(String purchaseList){
+ validatePurchaseList(purchaseList);
+ List<String> purchases = List.of(purchaseList.split(StoreViewMessage.PURCHASE_LIST_DELIMITER));
+ return purchases.stream().map((purchase)->{
+ List<String> elements = extractElementsFromPurchase(purchase);
+ int nameIdx = 0;
+ int countIdx = 1;
+ return new PurchaseRequest(elements.get(nameIdx), Transformer.parsePositiveInt(elements.get(countIdx)),PromotionState.NONE);
+ }).toList();
+ }
+
+
+ static public void validatePurchaseList(String purchaseList){
+ Validator.validateBlankString(purchaseList);
+ List<String> purchases = List.of(purchaseList.split(StoreViewMessage.PURCHASE_LIST_DELIMITER));
+
+ for(String purchase : purchases){
+ validatePurchase(purchase);
+ }
+
+ }
+
+ static public void validatePurchase(String purchase){
+
+ if(!purchase.matches(StoreViewMessage.PURCHASE_PATTERN)){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_INPUT_STRING_FORMAT);
+ }
+ try {
+ validatePurchaseElement(purchase);
+ }catch (Exception e){
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_INPUT_STRING_FORMAT);
+ }
+
+ }
+
+ static public void validatePurchaseElement(String purchase){
+ Matcher matcher = PURCHASE_PATTERN.matcher(purchase);
+ if(matcher.matches()) {
+ int productNameGroup = 1;
+ int countGroup = 2;
+ Validator.validateBlankString(matcher.group(productNameGroup));
+ Validator.validatePositiveNumericString(matcher.group(countGroup));
+ return;
+ }
+ ExceptionFactory.throwIllegalStateException(ExceptionType.INVALID_INPUT_STRING_FORMAT);
+
+
+ }
+
+ static public List<String> extractElementsFromPurchase(String purchase){
+ Matcher matcher = PURCHASE_PATTERN.matcher(purchase);
+ if(matcher.matches()){
+ int nameGroup = 1;
+ int countGroup = 2;
+ return List.of(matcher.group(nameGroup), matcher.group(countGroup));
+ }
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_INPUT_STRING_FORMAT);
+ throw new RuntimeException();
+ }
+
+ static public void validateAnswer(String answer){
+ Validator.validateBlankString(answer);
+ if(!(answer.equals(StoreViewMessage.ANSWER_NO)
+ || answer.equals(StoreViewMessage.ANSWER_YES))) {
+ ExceptionFactory.throwIllegalArgumentException(ExceptionType.INVALID_INPUT_STRING_FORMAT);
+ }
+ }
+
+} | Java | ์ด ๋ถ๋ถ์ `validatePurchaseElement`์์๋ ํ์ธํ๋ ๋ถ๋ถ ๊ฐ์์! |
@@ -0,0 +1,26 @@
+package store.utils;
+
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class TransformerTest {
+
+
+ @Test
+ void testJoinToString(){
+ assertThat(Transformer.joinToString(List.of(1,2,3,4,5),", ")).isEqualTo("1, 2, 3, 4, 5");
+ assertThat(Transformer.joinToString(List.of(1,2,3,4,5),"-")).isEqualTo("1-2-3-4-5");
+ }
+
+ @Test
+ void testParsePositiveInt(){
+ assertThat(Transformer.parsePositiveInt("1000")).isEqualTo(1000);
+ assertThatThrownBy(()->Transformer.parsePositiveInt("?")).isInstanceOf(IllegalArgumentException.class);
+ assertThatThrownBy(()->Transformer.parsePositiveInt("")).isInstanceOf(IllegalArgumentException.class);
+ }
+
+} | Java | ๊ผผ๊ผผํ ํ
์คํธ๊ฐ ์ธ์์ ์
๋๋ค!
๋ค๋ฅธ ๋ถ๋ถ์์๋ `@ParameterizedTest`๋ฅผ ์ ์ฌ์ฉํ์
จ๋๋ฐ, ์ด ๋ถ๋ถ์ ์ฌ์ฉํ์ง ์์ผ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค!
ํน์๋ `List`๊ฐ์ ๋ถ๋ถ์ `@ValueSource`๋ก ๋ชป ๋ฐ์์ ๊ทธ๋ฌ์
จ๋ค๋ฉด, `MethodSource`์ ๋ํด ์์๋ณด์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์! |
@@ -0,0 +1,58 @@
+package store.product;
+
+import store.product.promotion.PromotionState;
+import store.utils.ExceptionFactory;
+import store.utils.ExceptionType;
+import store.utils.Validator;
+
+public class PurchaseRequest {
+ private String productName;
+ private int countPurchased;
+ private PromotionState promotionState;
+ public PurchaseRequest(){
+
+ }
+
+ public PurchaseRequest(String productName, int countPurchased, PromotionState promotionState){
+ this.countPurchased = countPurchased;
+ this.productName = productName;
+ this.promotionState = promotionState;
+ }
+
+ public String getProductName(){
+ return this.productName;
+ }
+ public int getCountPurchased(){
+ return this.countPurchased;
+ }
+
+ public PromotionState getPromotionState(){
+ return this.promotionState;
+ }
+
+ public void increaseCount(int value){
+ Validator.validatePositiveNumber(value);
+ this.countPurchased += value;
+ }
+
+ public PurchaseRequest tryToIncrease(boolean isIncrease){
+ int step = 1;
+ if(isIncrease){
+ this.increaseCount(step);
+ return this;
+ }
+ return this;
+ }
+
+ public void decreaseCount(int value){
+ if(value > this.countPurchased){
+ ExceptionFactory.throwIllegalStateException(ExceptionType.INTERNAL_ERROR);
+ }
+ this.countPurchased -=value;
+ }
+
+ public boolean isCountMet(){
+ int zero = 0;
+ return this.countPurchased == zero;
+ }
+} | Java | ์๋ ๋ถ๋ถ์์ `return this;`๋ฅผ ํด์ ์ด ๋ถ๋ถ์ ์์ด๋ ๋ ๊ฑฐ ๊ฐ์์! |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ๋ฐฉ์ด์ ๋ณต์ฌ๊ฐ ํ์ํด ๋ณด์
๋๋ค ๐ค |
@@ -0,0 +1,35 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class Coaches {
+ private static final int MINIMUM_COACH_COUNT = 2;
+ private static final int MAXIMUM_COACH_COUNT = 5;
+
+ private List<Coach> coaches = new ArrayList<>();
+
+ public Coaches(List<String> coachNames) {
+ validateCoachesCount(coachNames);
+ this.coaches = getCoaches(coachNames);
+ }
+
+ private void validateCoachesCount(List<String> coachNames) {
+ if(!(coachNames.size() >= MINIMUM_COACH_COUNT && coachNames.size() <= MAXIMUM_COACH_COUNT)) {
+ throw new IllegalArgumentException(INVALID_COACHES_COUNT.getMessage());
+ }
+ }
+
+ private List<Coach> getCoaches(List<String> coachNames) {
+ return coachNames.stream()
+ .map(Coach::new)
+ .collect(Collectors.toList());
+ }
+
+ public String getCoachName(int index) {
+ return coaches.get(index).getCoachName();
+ }
+} | Java | ์ ์ฒด์ ์ผ๋ก ์งง์ ์๊ฐ์์ ๊ฒ์ฆ ๋ก์ง์ ์ ์์ฑํ์ ๊ฒ ๊ฐ์์ ๐ |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | ๋ฌธ์ ์์๋ ๋์๊ฐ 1๋ถํฐ 5์๋ ๊ฒ ๊ฐ์๋ฐ ์ด๋ ๊ฒ ์ฌ์ฉํด๋ ๋ฌธ์ ๊ฐ ์์๋์? |
@@ -0,0 +1,22 @@
+package menu.exception;
+
+public enum ErrorMessage {
+ INVALID_COACH_NAME_LENGTH("์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ฌ์ผ ํฉ๋๋ค."),
+ INVALID_COACHES_COUNT("์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
์ฌ์ผ ํฉ๋๋ค."),
+ INVALID_MENU_COUNT("๋ชป ๋จน๋ ๋ฉ๋ด๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ฌ์ผ ํฉ๋๋ค."),
+ NOT_EXIST_MENU("์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค."),
+ ;
+
+ private static final String PREFIX = "[ERROR] ";
+ private static final String POSTFIX = " ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ private final String message;
+
+ ErrorMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage(Object... args) {
+ return String.format(PREFIX + message + POSTFIX, args);
+ }
+} | Java | ์๋ฌ ๋ฉ์ธ์ง๋ ๊น๋ํ๊ฒ ๋ถ๋ฆฌํ์
จ๋ค์ ๐ฎ |
@@ -34,27 +34,35 @@ BUILD SUCCESSFUL in 0s
ํ ์ฃผ์ ์ ์ฌ ๋ฉ๋ด๋ฅผ ์ถ์ฒํด ์ฃผ๋ ์๋น์ค๋ค.
-- ์ฝ์น๋ค์ ์, ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ์ ์ฌ ์์ฌ๋ฅผ ๊ฐ์ด ํ๋ค.
-- ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ ๊ณผ์ ์ ์๋์ ๊ฐ์ด ์ด๋ค์ง๋ค.
- 1. ์์์ผ์ ์ถ์ฒํ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋ฌด์์๋ก ์ ํ๋ค.
- 2. ๊ฐ ์ฝ์น๊ฐ ์์์ผ์ ๋จน์ ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ค.
- 3. ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ๋ํด i, ii ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
-- ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ด๋ค.
-- ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง ์์ฌ๋ฅผ ํจ๊ป ํ๋ค.
-- ๊ฐ ์ฝ์น๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ ๋ชป ๋จน๋ ๋ฉ๋ด๊ฐ ์๋ค. (`,` ๋ก ๊ตฌ๋ถํด์ ์
๋ ฅํ๋ค.)
- - ๋จน์ง ๋ชปํ๋ ๋ฉ๋ด๊ฐ ์์ผ๋ฉด ๋น ๊ฐ์ ์
๋ ฅํ๋ค.
- - ์ถ์ฒ์ ๋ชปํ๋ ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์์ผ๋ ๊ณ ๋ คํ์ง ์์๋ ๋๋ค.
-- ํ ์ฃผ์ ๊ฐ์ ์นดํ
๊ณ ๋ฆฌ๋ ์ต๋ 2ํ๊น์ง๋ง ๊ณ ๋ฅผ ์ ์๋ค.
-- ๊ฐ ์ฝ์น์๊ฒ ํ ์ฃผ์ ์ค๋ณต๋์ง ์๋ ๋ฉ๋ด๋ฅผ ์ถ์ฒํด์ผ ํ๋ค.
+- [x] ์ฝ์น๋ค์ ์, ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ์ ์ฌ ์์ฌ๋ฅผ ๊ฐ์ด ํ๋ค.
+- [x] ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ ๊ณผ์ ์ ์๋์ ๊ฐ์ด ์ด๋ค์ง๋ค.
+ - [x] ์์์ผ์ ์ถ์ฒํ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋ฌด์์๋ก ์ ํ๋ค.
+ - [x] ๊ฐ ์ฝ์น๊ฐ ์์์ผ์ ๋จน์ ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ค.
+ - [x] ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ๋ํด i, ii ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
+- [x] ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ด๋ค.
+- [x] ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง ์์ฌ๋ฅผ ํจ๊ป ํ๋ค.
+- [x] ๊ฐ ์ฝ์น๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ ๋ชป ๋จน๋ ๋ฉ๋ด๊ฐ ์๋ค. (`,` ๋ก ๊ตฌ๋ถํด์ ์
๋ ฅํ๋ค.)
+ - [x] ๋จน์ง ๋ชปํ๋ ๋ฉ๋ด๊ฐ ์์ผ๋ฉด ๋น ๊ฐ์ ์
๋ ฅํ๋ค.
+ - [x] ์ถ์ฒ์ ๋ชปํ๋ ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์์ผ๋ ๊ณ ๋ คํ์ง ์์๋ ๋๋ค.
+- [x] ํ ์ฃผ์ ๊ฐ์ ์นดํ
๊ณ ๋ฆฌ๋ ์ต๋ 2ํ๊น์ง๋ง ๊ณ ๋ฅผ ์ ์๋ค.
+- [x] ๊ฐ ์ฝ์น์๊ฒ ํ ์ฃผ์ ์ค๋ณต๋์ง ์๋ ๋ฉ๋ด๋ฅผ ์ถ์ฒํด์ผ ํ๋ค.
- ์์)
- - ๊ตฌ๊ตฌ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์๋ฐฅ, ๊ท๋, ์ฐ๋ โ ํ์์ 3ํ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
- - ํ ๋ฏธ: ๋น๋น๋ฐฅ, ๋น๋น๋ฐฅ, ๊ท๋, ์ฐ๋, ๋ณถ์๋ฉด โ ํ ์ฝ์น๊ฐ ๊ฐ์ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
- - ์ ์์ค: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ๋งค์ผ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
- - ํฌ์ฝ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ์ ์์ค์ ๋ฉ๋ด๊ฐ ๊ฐ์ง๋ง, ํฌ์ฝ๋ ๋งค๋ฒ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
-- ๋ฉ๋ด ์ถ์ฒ์ ์๋ฃํ๋ฉด ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋๋ค.
-- ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ๊ทธ ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์
+ - [x] ๊ตฌ๊ตฌ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์๋ฐฅ, ๊ท๋, ์ฐ๋ โ ํ์์ 3ํ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
+ - [x] ํ ๋ฏธ: ๋น๋น๋ฐฅ, ๋น๋น๋ฐฅ, ๊ท๋, ์ฐ๋, ๋ณถ์๋ฉด โ ํ ์ฝ์น๊ฐ ๊ฐ์ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
+ - [x] ์ ์์ค: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ๋งค์ผ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
+ - [x] ํฌ์ฝ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ์ ์์ค์ ๋ฉ๋ด๊ฐ ๊ฐ์ง๋ง, ํฌ์ฝ๋ ๋งค๋ฒ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
+- [x] ๋ฉ๋ด ์ถ์ฒ์ ์๋ฃํ๋ฉด ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋๋ค.
+- [x] ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ๊ทธ ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์
๋ฐ๋๋ค.
- - `Exception`์ด ์๋ `IllegalArgumentException`, `IllegalStateException` ๋ฑ๊ณผ ๊ฐ์ ๋ช
ํํ ์ ํ์ ์ฒ๋ฆฌํ๋ค.
+ - [x] `Exception`์ด ์๋ `IllegalArgumentException`, `IllegalStateException` ๋ฑ๊ณผ ๊ฐ์ ๋ช
ํํ ์ ํ์ ์ฒ๋ฆฌํ๋ค.
+
+๊ณผ์ ์์ ์๊ฐ: 14:30 <br>
+TDD ์ข
๋ฃ : 17:17 (2์๊ฐ 47๋ถ) <br>
+MVP ์์ฑ : 19:13 (TDD + 1์๊ฐ 56๋ถ) <br>
+๋ฆฌํฉํ ๋ง ์ข
๋ฃ : 19:29 (MVP ์์ฑ + 16๋ถ) <br>
+์ด 4์๊ฐ 59๋ถ <br>
+
+์์ฌ์ด ์ : ํจ์ํ ์ธํฐํ์ด์ค ์ ์ฉ, Service ํด๋์ค ๋์
, ๋ฆฌํฉํ ๋ง
### ์
์ถ๋ ฅ ์๊ตฌ ์ฌํญ
| Unknown | PR ์ค๋ช
์ ์ฝ์ด๋ณด๋ TDD ๋ง์กฑ๋๊ฐ ๋์๋ณด์ด๋๋ฐ, ์ง๋์ฃผ์ ๋นํด ๊ณผ์ ์ด๋ ๋๋์ด ๋ฌ๋ผ์ง ๊ฒ ์์๋์?? |
@@ -0,0 +1,75 @@
+package menu.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import menu.domain.CantEatingMenus;
+import menu.domain.Category;
+import menu.domain.Coaches;
+import menu.domain.Menu;
+import menu.util.GenerateCategory;
+import menu.util.GenerateMenus;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+public class RecommendController {
+ private Coaches coaches;
+ private CantEatingMenus cantEatingMenus;
+
+ public void start() {
+ OutputView.start();
+ execute();
+ OutputView.finish();
+ }
+
+ private void execute() {
+ List<String> inputCoachNames = getCoachNames();
+ getCantEatingMenus(inputCoachNames);
+ getResult(inputCoachNames);
+ }
+
+ private List<String> getCoachNames() {
+ List<String> inputCoachNames = new ArrayList<>();
+
+ while (true) {
+ try {
+ OutputView.inputCoachNames();
+ inputCoachNames = InputView.inputCoachNames();
+ coaches = new Coaches(inputCoachNames);
+ break;
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ }
+ }
+ return inputCoachNames;
+ }
+
+ private void getCantEatingMenus(List<String> inputCoachNames) {
+ for (int index = 0; index < inputCoachNames.size(); index++) {
+ while (true) {
+ try {
+ OutputView.inputCantEatingMenus(coaches.getCoachName(index));
+ List<String> inputCantEatingMenus = InputView.inputCantEatingMenus();
+ cantEatingMenus = new CantEatingMenus(inputCantEatingMenus);
+ break;
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ }
+ }
+ }
+ }
+
+ private static List<Category> getCategories() {
+ List<Category> categories = GenerateCategory.generate();
+ OutputView.printDayAndCategory(categories);
+ return categories;
+ }
+
+ private void getResult(List<String> inputCoachNames) {
+ List<Category> categories = getCategories();
+ for (int index = 0; index < inputCoachNames.size(); index++) {
+ List<Menu> menus = GenerateMenus.generate(categories, cantEatingMenus);
+ OutputView.printResult(coaches.getCoachName(index), menus);
+ }
+ }
+} | Java | for๋ฌธ์ ์ฌ์ฉํ์ง ์๋ ๋ฐฉํฅ์ ์์์์ง ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ํ๋, ๋ณ์, ๋ฉ์๋๋ช
์ด ์ ๋ถ ๊ฒน์น๋ ์ด ํด๋์ค์ ๋ก์ง์ ์ดํดํ๊ธฐ๊ฐ ํ๋ ๊ฒ ๊ฐ์์ ๐ฅฒ |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | @seongm1n ๋ด๋ถ ๋ฉ์๋์์ `List<String>`์ `List<Menu>`๋ก ๋ณํํ๊ณ ์๋๋ฐ ๋ฐฉ์ด์ ๋ณต์ฌ๊ฐ ํ์ํ๋ค๊ณ ๋ณด์๋์!? |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ์์ฒญ๋ค์ด์จ ๋ฉ๋ด๋ช
์ด ์ค์ ๋ฉ๋ด์ค์ ์กด์ฌํ๋์ง ํ์ธํ๋ ๋ฉ์๋์ธ ๊ฒ ๊ฐ์์.
์ด๊ฑด ์ด ํด๋์ค์ ๊ด์ฌ์ฌ๊ฐ ์๋ ์๋ ์๊ฒ ๋ค๋ ์๊ฐ์ด ๋๋๋ฐ ์ด๋ป๊ฒ ์๊ฐํ์๋์!? |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | ์ด๋ฐ ๋ฐฉ์์ผ๋ก enum์ ๊ด๋ฆฌํ๋ ๊ฒ๋ ์ ๋ง ์ข์ ๊ฒ ๊ฐ๋ค์! ๐ฏ |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | ๋๋ค๋ฝ๊ธฐ๊ฐ ์ด ํด๋์ค์ ๊ด์ฌ์ฌ์ธ์ง ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | ๋ฉ์๋๋ช
์์ ๋๋ค ๋ฝ๊ธฐ์์ด ๋๋ฌ๋์ง ์์ ๊ฒ ๊ฐ์์! ์ผ๋ถ๋ฌ ์จ๊ธฐ์ ๊ฑด๊ฐ์?? |
@@ -0,0 +1,22 @@
+package menu.exception;
+
+public enum ErrorMessage {
+ INVALID_COACH_NAME_LENGTH("์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ฌ์ผ ํฉ๋๋ค."),
+ INVALID_COACHES_COUNT("์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
์ฌ์ผ ํฉ๋๋ค."),
+ INVALID_MENU_COUNT("๋ชป ๋จน๋ ๋ฉ๋ด๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ฌ์ผ ํฉ๋๋ค."),
+ NOT_EXIST_MENU("์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค."),
+ ;
+
+ private static final String PREFIX = "[ERROR] ";
+ private static final String POSTFIX = " ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ private final String message;
+
+ ErrorMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage(Object... args) {
+ return String.format(PREFIX + message + POSTFIX, args);
+ }
+} | Java | ์์ธ๋ฉ์์ง ๋ด์ ์์๋ฅผ ์ธ๋ถ์์ ๊ฐ์ ธ์ค๋ฉด ์ข์ ๊ฒ ๊ฐ์์! |
@@ -0,0 +1,27 @@
+package menu.util;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import menu.domain.Category;
+
+public class GenerateCategory {
+ public static List<Category> generate() {
+ List<Category> categories = new ArrayList<>();
+ Map<String, Integer> categoryCount = new HashMap<>();
+
+ while (categories.size() < 5) {
+ Category randomCategory = Category.randomCategory();
+ String categoryName = randomCategory.getCategoryName();
+
+ int count = categoryCount.getOrDefault(categoryName, 0);
+ if (count < 2) {
+ categories.add(randomCategory);
+ categoryCount.put(categoryName, count + 1);
+ }
+ }
+ return categories;
+ }
+} | Java | ์กฐ๊ธ ๋ ๋ฆฌํฉํ ๋งํด๋ณผ ์ ์์ ๊ฒ ๊ฐ์์..! |
@@ -0,0 +1,33 @@
+package menu.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import menu.domain.CantEatingMenus;
+import menu.domain.Category;
+import menu.domain.Menu;
+
+public class GenerateMenus {
+ public static List<Menu> generate(List<Category> categories, CantEatingMenus cantEatingMenus) {
+ List<Menu> recommendMenus = new ArrayList<>();
+ do {
+ for (Category category : categories) {
+ getRecommendMenus(cantEatingMenus, category, recommendMenus);
+ }
+ } while (recommendMenus.size() > 5);
+ return recommendMenus;
+ }
+
+ private static void getRecommendMenus(CantEatingMenus cantEatingMenus, Category category, List<Menu> recommendMenus) {
+ if(!isIncludeNotEatingMenus(recommendMenus, cantEatingMenus)) {
+ List<String> menusByCategory = Category.getMenusByCategory(category);
+ String menu = Category.getMenu(menusByCategory);
+ recommendMenus.add(new Menu(menu));
+ }
+ }
+
+ private static boolean isIncludeNotEatingMenus(List<Menu> recommendMenus, CantEatingMenus cantEatingMenus) {
+ return cantEatingMenus.getCantEatingMenus().stream()
+ .anyMatch(recommendMenus::contains);
+ }
+} | Java | ์ด ํด๋์ค์ ๋ก์ง์ด ์กฐ๊ธ ๋ํดํ ๊ฐ์ด ์์ด ๋ณด์
๋๋ค.
๋ค๋ฅธ ํด๋์ค๋ฅผ ๋ ์ ๋ขฐํ๊ณ ์์
์ ๋งก๊ธธ ์ ์๋ค๋ฉด ๋ ์์ ์ฝ๋๊ฐ ๋์ฌ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,22 @@
+package menu.view;
+
+public enum OutputMessage {
+ START("์ ์ฌ ๋ฉ๋ด ์ถ์ฒ์ ์์ํฉ๋๋ค."),
+ INPUT_COACH_NAMES("\n์ฝ์น์ ์ด๋ฆ์ ์
๋ ฅํด ์ฃผ์ธ์. (, ๋ก ๊ตฌ๋ถ)"),
+ INPUT_CANT_EATING_MENU("\n%s(์ด)๊ฐ ๋ชป ๋จน๋ ๋ฉ๋ด๋ฅผ ์
๋ ฅํด ์ฃผ์ธ์.\n"),
+ RESULT_MESSAGE_START("\n๋ฉ๋ด ์ถ์ฒ ๊ฒฐ๊ณผ์
๋๋ค."),
+ DAY("[ ๊ตฌ๋ถ | ์์์ผ | ํ์์ผ | ์์์ผ | ๋ชฉ์์ผ | ๊ธ์์ผ ]"),
+ CATEGORY_RESULT("[ ์นดํ
๊ณ ๋ฆฌ"),
+ FINISH("\n\n์ถ์ฒ์ ์๋ฃํ์ต๋๋ค."),
+ ;
+
+ private final String message;
+
+ OutputMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage(Object... args) {
+ return String.format(message, args);
+ }
+} | Java | String.format์ ์ฌ์ฉ์ค์ธ๋ฐ %n์ ์ฌ์ฉํ์ง ์์ ์ด์ ๊ฐ ์๋์!? |
@@ -0,0 +1,48 @@
+package menu.view;
+
+import static menu.view.OutputMessage.*;
+
+import java.util.List;
+
+import menu.domain.Category;
+import menu.domain.Menu;
+
+public class OutputView {
+ public static void start() {
+ System.out.println(START.getMessage());
+ }
+
+ public static void inputCoachNames() {
+ System.out.println(INPUT_COACH_NAMES.getMessage());
+ }
+
+ public static void inputCantEatingMenus(String coachName) {
+ System.out.printf(INPUT_CANT_EATING_MENU.getMessage(coachName));
+ }
+
+ public static void printDayAndCategory(List<Category> categories) {
+ System.out.println(RESULT_MESSAGE_START.getMessage());
+ System.out.println(DAY.getMessage());
+ System.out.print(CATEGORY_RESULT.getMessage());
+ for(Category category : categories) {
+ System.out.printf(" | %s", category.getCategoryName());
+ }
+ System.out.print(" ]");
+ }
+
+ public static void printResult(String coachName, List<Menu> menus) {
+ System.out.printf("\n[ %s", coachName);
+ for(Menu menu : menus) {
+ System.out.printf(" | %s", menu.getMenuName());
+ }
+ System.out.print(" ]");
+ }
+
+ public static void finish() {
+ System.out.print(FINISH.getMessage());
+ }
+
+ public static void printErrorMessage(IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+} | Java | printf์ println์ ํผ์ฉํ์ ์ด์ ๊ฐ ์์๊น์?!
์์๋ฌธ์๋ getMessage() ์์์ ์ ๋ถ ๋์ฒด๋์ง ์๋์?
+) print์ println์ ํผ์ฉํ์ง ์์ผ๋ฉด ๋ ์ผ๊ด์ ์ธ ์ฝ๋๊ฐ ๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,25 @@
+package menu.domain;
+
+import static org.assertj.core.api.AssertionsForClassTypes.*;
+
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+public class CantEatingMenusTest {
+ @Test
+ void ๊ฐ_์ฝ์น์_๋ชป๋จน๋_๋ฉ๋ด๊ฐ_0๊ฐ_์ด์_2๊ฐ_์ดํ๊ฐ_์๋๋ฉด_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
+ List<String> menus = List.of("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ");
+
+ assertThatThrownBy(() -> new CantEatingMenus(menus))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @Test
+ void ๊ฐ_์ฝ์น๊ฐ_๋ชป๋จน๋_๋ฉ๋ด๊ฐ_์๋_๋ฉ๋ด์ด๋ฉด_์์ธ๊ฐ_๋ฐ์ํ๋ค() {
+ List<String> menus = List.of("๊น์น์ฐ", "๋ญ๋ณถ์ํ");
+
+ assertThatThrownBy(() -> new CantEatingMenus(menus))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+} | Java | ๊ณผ์ ๊ตฌํ์ ์ํด ํ์ํ ์ต์ํ์ ํ
์คํธ๋ฅผ ์ ์์ฑํด์ฃผ์ ๊ฒ ๊ฐ์์! ๐ |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ```getCantEatingMenus()``` ๋ฉ์๋์์ cantEatinMenus๋ฅผ ๋ฐํํด์ ํ์ํ๋ค๊ณ ์๊ฐํ์ต๋๋ค!
์ด๋ฐ ๊ฒฝ์ฐ๋ ์๊ด ์๋๊ฑด๊ฐ์? ๐ซค |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ๋ง์ํด์ฃผ์ ๋ฐฉ์ด์ ๋ณต์ฌ๋ ์์ฑ์๊ฐ ์๋๋ผ getter์์ ์ด๋ฃจ์ด์ง๋ ๊ฒ ๋ ์์ฐ์ค๋ฌ์ธ ๊ฒ ๊ฐ์์.
์์ฑ์์์ ๋ฐฉ์ด์ ๋ณต์ฌ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด getter๋ ๋ฐฉ์ด์ ๋ณต์ฌ๋ฅผ ํ์ง ์์๋ ๋๋ค๊ณ ์๊ฐํ์๋์? |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | String ํ์
์ ๋ฆฌ์คํธ๋ฅผ Menu ํ์
์ ๋ฆฌ์คํธ๋ก ๋ณํํ๊ณ ์์ด ์์ฑ์์์์ ๋ฐฉ์ด์ ๋ณต์ฌ๋ ํ์ํ์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค.
๋ค๋ง ๋ง์ํด์ฃผ์ ๋ถ๋ถ๋ค์ฒ๋ผ getCantEatingMenus()๋ ๋ด๋ถ ๋ฆฌ์คํธ๋ฅผ ๊ทธ๋๋ก ๋ฐํํ๊ณ ์์ด์ ๋ฐฉ์ด์ ๋ณต์ฌ๋ฅผ ์ ์ฉํ๋ ๊ฒ์ด ๋ ์ ์ ํ๊ฒ ๋ค์:) ์์ผ๋ก ๋ ๊ผผ๊ผผํ ์๊ฐํด๋ด์ผ๊ฒ ์ด์ ํผ๋๋ฐฑ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | README์์ ์๋์ ๊ฐ์ ์ฃผ์์ ํ์ธํ ํ, ๋ณ๊ฒฝํ์ต๋๋ค! ํน์ ์ ๊ฐ ์๋ชป ์ดํดํ๊ฑธ๊น์?!
```md
// ์์ ์ฝ๋. ์ฌ์ฉํ๋ ์๋ฃ ๊ตฌ์กฐ์ ๋ฐ๋ผ ๋์๋ฅผ ์ ์ ํ๊ฒ ๊ฐ๊ณตํด๋ ๋๋ค.
String category = categories.get(Randoms.pickNumberInRange(1, 5));
``` |
@@ -34,27 +34,35 @@ BUILD SUCCESSFUL in 0s
ํ ์ฃผ์ ์ ์ฌ ๋ฉ๋ด๋ฅผ ์ถ์ฒํด ์ฃผ๋ ์๋น์ค๋ค.
-- ์ฝ์น๋ค์ ์, ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ์ ์ฌ ์์ฌ๋ฅผ ๊ฐ์ด ํ๋ค.
-- ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ ๊ณผ์ ์ ์๋์ ๊ฐ์ด ์ด๋ค์ง๋ค.
- 1. ์์์ผ์ ์ถ์ฒํ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋ฌด์์๋ก ์ ํ๋ค.
- 2. ๊ฐ ์ฝ์น๊ฐ ์์์ผ์ ๋จน์ ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ค.
- 3. ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ๋ํด i, ii ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
-- ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ด๋ค.
-- ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง ์์ฌ๋ฅผ ํจ๊ป ํ๋ค.
-- ๊ฐ ์ฝ์น๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ ๋ชป ๋จน๋ ๋ฉ๋ด๊ฐ ์๋ค. (`,` ๋ก ๊ตฌ๋ถํด์ ์
๋ ฅํ๋ค.)
- - ๋จน์ง ๋ชปํ๋ ๋ฉ๋ด๊ฐ ์์ผ๋ฉด ๋น ๊ฐ์ ์
๋ ฅํ๋ค.
- - ์ถ์ฒ์ ๋ชปํ๋ ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์์ผ๋ ๊ณ ๋ คํ์ง ์์๋ ๋๋ค.
-- ํ ์ฃผ์ ๊ฐ์ ์นดํ
๊ณ ๋ฆฌ๋ ์ต๋ 2ํ๊น์ง๋ง ๊ณ ๋ฅผ ์ ์๋ค.
-- ๊ฐ ์ฝ์น์๊ฒ ํ ์ฃผ์ ์ค๋ณต๋์ง ์๋ ๋ฉ๋ด๋ฅผ ์ถ์ฒํด์ผ ํ๋ค.
+- [x] ์ฝ์น๋ค์ ์, ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ์ ์ฌ ์์ฌ๋ฅผ ๊ฐ์ด ํ๋ค.
+- [x] ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ ๊ณผ์ ์ ์๋์ ๊ฐ์ด ์ด๋ค์ง๋ค.
+ - [x] ์์์ผ์ ์ถ์ฒํ ์นดํ
๊ณ ๋ฆฌ๋ฅผ ๋ฌด์์๋ก ์ ํ๋ค.
+ - [x] ๊ฐ ์ฝ์น๊ฐ ์์์ผ์ ๋จน์ ๋ฉ๋ด๋ฅผ ์ถ์ฒํ๋ค.
+ - [x] ํ, ์, ๋ชฉ, ๊ธ์์ผ์ ๋ํด i, ii ๊ณผ์ ์ ๋ฐ๋ณตํ๋ค.
+- [x] ์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ด๋ค.
+- [x] ์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
๊น์ง ์์ฌ๋ฅผ ํจ๊ป ํ๋ค.
+- [x] ๊ฐ ์ฝ์น๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ ๋ชป ๋จน๋ ๋ฉ๋ด๊ฐ ์๋ค. (`,` ๋ก ๊ตฌ๋ถํด์ ์
๋ ฅํ๋ค.)
+ - [x] ๋จน์ง ๋ชปํ๋ ๋ฉ๋ด๊ฐ ์์ผ๋ฉด ๋น ๊ฐ์ ์
๋ ฅํ๋ค.
+ - [x] ์ถ์ฒ์ ๋ชปํ๋ ๊ฒฝ์ฐ๋ ๋ฐ์ํ์ง ์์ผ๋ ๊ณ ๋ คํ์ง ์์๋ ๋๋ค.
+- [x] ํ ์ฃผ์ ๊ฐ์ ์นดํ
๊ณ ๋ฆฌ๋ ์ต๋ 2ํ๊น์ง๋ง ๊ณ ๋ฅผ ์ ์๋ค.
+- [x] ๊ฐ ์ฝ์น์๊ฒ ํ ์ฃผ์ ์ค๋ณต๋์ง ์๋ ๋ฉ๋ด๋ฅผ ์ถ์ฒํด์ผ ํ๋ค.
- ์์)
- - ๊ตฌ๊ตฌ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์๋ฐฅ, ๊ท๋, ์ฐ๋ โ ํ์์ 3ํ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
- - ํ ๋ฏธ: ๋น๋น๋ฐฅ, ๋น๋น๋ฐฅ, ๊ท๋, ์ฐ๋, ๋ณถ์๋ฉด โ ํ ์ฝ์น๊ฐ ๊ฐ์ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
- - ์ ์์ค: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ๋งค์ผ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
- - ํฌ์ฝ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ์ ์์ค์ ๋ฉ๋ด๊ฐ ๊ฐ์ง๋ง, ํฌ์ฝ๋ ๋งค๋ฒ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
-- ๋ฉ๋ด ์ถ์ฒ์ ์๋ฃํ๋ฉด ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋๋ค.
-- ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ๊ทธ ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์
+ - [x] ๊ตฌ๊ตฌ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์๋ฐฅ, ๊ท๋, ์ฐ๋ โ ํ์์ 3ํ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
+ - [x] ํ ๋ฏธ: ๋น๋น๋ฐฅ, ๋น๋น๋ฐฅ, ๊ท๋, ์ฐ๋, ๋ณถ์๋ฉด โ ํ ์ฝ์น๊ฐ ๊ฐ์ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๋ถ๊ฐ๋ฅ
+ - [x] ์ ์์ค: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ๋งค์ผ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
+ - [x] ํฌ์ฝ: ๋น๋น๋ฐฅ, ๊น์น์ฐ๊ฐ, ์ค์, ๊ฐ์ธ ๋, ์ง์ฅ๋ฉด โ ์ ์์ค์ ๋ฉ๋ด๊ฐ ๊ฐ์ง๋ง, ํฌ์ฝ๋ ๋งค๋ฒ ๋ค๋ฅธ ๋ฉ๋ด๋ฅผ ๋จน์ผ๋ฏ๋ก ๊ฐ๋ฅ
+- [x] ๋ฉ๋ด ์ถ์ฒ์ ์๋ฃํ๋ฉด ํ๋ก๊ทธ๋จ์ด ์ข
๋ฃ๋๋ค.
+- [x] ์ฌ์ฉ์๊ฐ ์๋ชป๋ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ `IllegalArgumentException`๋ฅผ ๋ฐ์์ํค๊ณ , "[ERROR]"๋ก ์์ํ๋ ์๋ฌ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅ ํ ๊ทธ ๋ถ๋ถ๋ถํฐ ์
๋ ฅ์ ๋ค์
๋ฐ๋๋ค.
- - `Exception`์ด ์๋ `IllegalArgumentException`, `IllegalStateException` ๋ฑ๊ณผ ๊ฐ์ ๋ช
ํํ ์ ํ์ ์ฒ๋ฆฌํ๋ค.
+ - [x] `Exception`์ด ์๋ `IllegalArgumentException`, `IllegalStateException` ๋ฑ๊ณผ ๊ฐ์ ๋ช
ํํ ์ ํ์ ์ฒ๋ฆฌํ๋ค.
+
+๊ณผ์ ์์ ์๊ฐ: 14:30 <br>
+TDD ์ข
๋ฃ : 17:17 (2์๊ฐ 47๋ถ) <br>
+MVP ์์ฑ : 19:13 (TDD + 1์๊ฐ 56๋ถ) <br>
+๋ฆฌํฉํ ๋ง ์ข
๋ฃ : 19:29 (MVP ์์ฑ + 16๋ถ) <br>
+์ด 4์๊ฐ 59๋ถ <br>
+
+์์ฌ์ด ์ : ํจ์ํ ์ธํฐํ์ด์ค ์ ์ฉ, Service ํด๋์ค ๋์
, ๋ฆฌํฉํ ๋ง
### ์
์ถ๋ ฅ ์๊ตฌ ์ฌํญ
| Unknown | ์ด์ ์๋ TDD ๋ฐ Controller, VIew ๋ฑ์ ํด๋์ค๋ฅผ ๋์์ ์์ฑํ๋ฉด์ ๊ตฌํํ๊ธฐ์ ์ฃผ์ ๋ก์ง์ ๋ํ ์ดํด๋๊ฐ ์ ์๋ ๊ฒ ๊ฐ์ต๋๋ค.
ํ์ดํ๋ก๊ทธ๋๋ฐ์์ ๊ฒช์๋ ๊ณผ์ ์ ๊ธฐ๋ฐ์ผ๋ก ์ฃผ์ ๋ก์ง์ ๋ํ ํ
์คํธ์ฝ๋๋ฅผ ๋จผ์ ์์ฑํ๊ณ ๋๋, ๋ก์ง์ ๋ํ ์ดํด๋๊ฐ ๋์์ง๋ ๊ฒ์ ๋ฌผ๋ก ์ด๋ฏธ ๊ตฌํ์ด ์๋ฃ๋ '์์ ๊ตฌ์ญ'์ด ํ๋ณด๋ ๋๋์ ๋ฐ์ ํจ์ฌ ์์ ์ ์ด์์ต๋๋ค! |
@@ -0,0 +1,75 @@
+package menu.controller;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import menu.domain.CantEatingMenus;
+import menu.domain.Category;
+import menu.domain.Coaches;
+import menu.domain.Menu;
+import menu.util.GenerateCategory;
+import menu.util.GenerateMenus;
+import menu.view.InputView;
+import menu.view.OutputView;
+
+public class RecommendController {
+ private Coaches coaches;
+ private CantEatingMenus cantEatingMenus;
+
+ public void start() {
+ OutputView.start();
+ execute();
+ OutputView.finish();
+ }
+
+ private void execute() {
+ List<String> inputCoachNames = getCoachNames();
+ getCantEatingMenus(inputCoachNames);
+ getResult(inputCoachNames);
+ }
+
+ private List<String> getCoachNames() {
+ List<String> inputCoachNames = new ArrayList<>();
+
+ while (true) {
+ try {
+ OutputView.inputCoachNames();
+ inputCoachNames = InputView.inputCoachNames();
+ coaches = new Coaches(inputCoachNames);
+ break;
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ }
+ }
+ return inputCoachNames;
+ }
+
+ private void getCantEatingMenus(List<String> inputCoachNames) {
+ for (int index = 0; index < inputCoachNames.size(); index++) {
+ while (true) {
+ try {
+ OutputView.inputCantEatingMenus(coaches.getCoachName(index));
+ List<String> inputCantEatingMenus = InputView.inputCantEatingMenus();
+ cantEatingMenus = new CantEatingMenus(inputCantEatingMenus);
+ break;
+ } catch (IllegalArgumentException e) {
+ OutputView.printErrorMessage(e);
+ }
+ }
+ }
+ }
+
+ private static List<Category> getCategories() {
+ List<Category> categories = GenerateCategory.generate();
+ OutputView.printDayAndCategory(categories);
+ return categories;
+ }
+
+ private void getResult(List<String> inputCoachNames) {
+ List<Category> categories = getCategories();
+ for (int index = 0; index < inputCoachNames.size(); index++) {
+ List<Menu> menus = GenerateMenus.generate(categories, cantEatingMenus);
+ OutputView.printResult(coaches.getCoachName(index), menus);
+ }
+ }
+} | Java | depth๊ฐ 3์ด๋ผ์ ํ์คํ ๊ณ ๋ฏผํด์ผ๋ด์ผํ ๋ถ๋ถ์ธ ๊ฒ ๊ฐ๋ค์๐ฅฒ |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | setUp ๋ฑ์ ๋ฉ์๋๋ช
๊ณผ input ๋ฑ์ ๋ณ์๋ช
์ผ๋ก ์์ ํ๋๊ฒ ๋ ์ข๊ฒ ๋ค์! ํผ๋๋ฐฑ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๋ชป ๋จน๋ ๋ฉ๋ด๊ฐ ํ์ฌ ๋ณด๊ดํ๊ณ ์๋ ๋ฉ๋ด ์ ๋ณด์ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ๋ฅผ ๊ณ ๋ คํ๊ณ ์ถ์์ต๋๋ค! ์ ๊ถ๋์ ๋ง์๋๋ก๋ผ๋ฉด Menu ํด๋์ค์์ ์ด๋ฅผ ๊ด๋ฆฌํ๋๊ฒ ๋ ์ ์ ํด๋ณด์ด์๋์? |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | ๋ง์ํ์ ๋ถ๋ถ์ ๋ํด ์๊ฐํด๋ณด๋ GenerateCategory ํด๋์ค์์ ์ด๋ฅผ ๊ด๋ฆฌํด๋ ์ข์์ ๊ฒ ๊ฐ๋ค์ ํผ๋๋ฐฑ ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,50 @@
+package menu.domain;
+
+import static menu.exception.ErrorMessage.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
+
+public class CantEatingMenus {
+ private static final int MAXIMUM_MENUS_LENGTH = 2;
+
+ private List<Menu> cantEatingMenus = new ArrayList<>();
+
+ public CantEatingMenus(List<String> cantEatingMenus) {
+ validateMenusLength(cantEatingMenus);
+ validateIsMenuExist(cantEatingMenus);
+ this.cantEatingMenus = cantEatingMenus(cantEatingMenus);
+ }
+
+ private void validateMenusLength(List<String> cantEatingMenus) {
+ if(!(cantEatingMenus.size() <= MAXIMUM_MENUS_LENGTH)) {
+ throw new IllegalArgumentException(INVALID_MENU_COUNT.getMessage());
+ }
+ }
+
+ private void validateIsMenuExist(List<String> cantEatingMenus) {
+ if(!isMenuExist(cantEatingMenus)) {
+ throw new IllegalArgumentException(NOT_EXIST_MENU.getMessage());
+ }
+ }
+
+ public List<Menu> getCantEatingMenus() {
+ return cantEatingMenus;
+ }
+
+ private List<Menu> cantEatingMenus(List<String> cantEatingMenus) {
+ return cantEatingMenus.stream()
+ .map(Menu::new)
+ .collect(Collectors.toList());
+ }
+
+ private boolean isMenuExist(List<String> cantEatingMenus) {
+ List<String> allMenus = Arrays.stream(Category.values())
+ .flatMap(category -> category.getMenus().stream())
+ .collect(Collectors.toList());
+
+ return cantEatingMenus.stream().anyMatch(allMenus::contains);
+ }
+} | Java | ๋ต ์ ๋ ๋ฉ๋ด์ ์กด์ฌ ์ ๋ฌด๋ ๋ฉ๋ด์์ ๊ฒ์ฆํ๋ ๊ฒ์ด ์ ์ ํ๋ค๊ณ ์๊ฐํด์!
์ ์ ํฉํ ๋ฆฌ ๋ฉ์๋๋ฅผ ํ์ฉํ๋ฉด ์์ฐ์ค๋ฌ์ด ํ๋ฆ์ด ์ง์ฌ์ง ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -0,0 +1,43 @@
+package menu.domain;
+
+import java.util.Arrays;
+import java.util.List;
+
+import camp.nextstep.edu.missionutils.Randoms;
+
+public enum Category {
+ JAPANESE("์ผ์", Arrays.asList("๊ท๋", "์ฐ๋", "๋ฏธ์์๋ฃจ", "์ค์", "๊ฐ์ธ ๋", "์ค๋๊ธฐ๋ฆฌ", "ํ์ด๋ผ์ด์ค", "๋ผ๋ฉ", "์ค์ฝ๋
ธ๋ฏธ์ผ๋ผ")),
+ KOREAN("ํ์", Arrays.asList("๊น๋ฐฅ", "๊น์น์ฐ๊ฐ", "์๋ฐฅ", "๋์ฅ์ฐ๊ฐ", "๋น๋น๋ฐฅ", "์นผ๊ตญ์", "๋ถ๊ณ ๊ธฐ", "๋ก๋ณถ์ด", "์ ์ก๋ณถ์")),
+ CHINESE("์ค์", Arrays.asList("๊นํ๊ธฐ", "๋ณถ์๋ฉด", "๋ํ์ก", "์ง์ฅ๋ฉด", "์งฌ๋ฝ", "๋งํ๋๋ถ", "ํ์์ก", "ํ ๋งํ ๋ฌ๊ฑ๋ณถ์", "๊ณ ์ถ์ก์ฑ")),
+ ASIAN("์์์", Arrays.asList("ํํ์ด", "์นด์ค ํ", "๋์๊ณ ๋ ", "ํ์ธ์ ํ ๋ณถ์๋ฐฅ", "์๊ตญ์", "๋ ์๊ฟ", "๋ฐ๋ฏธ", "์๋จ์", "๋ถ์ง")),
+ WESTERN("์์", Arrays.asList("๋ผ์๋", "๊ทธ๋ผํฑ", "๋จ๋ผ", "๋ผ์", "ํ๋ ์น ํ ์คํธ", "๋ฐ๊ฒํธ", "์คํ๊ฒํฐ", "ํผ์", "ํ๋๋"));;
+
+ private final String categoryName;
+ private final List<String> menus;
+
+ Category(String categoryName, List<String> menus) {
+ this.categoryName = categoryName;
+ this.menus = menus;
+ }
+
+ public String getCategoryName() {
+ return categoryName;
+ }
+
+ public List<String> getMenus() {
+ return menus;
+ }
+
+ public static Category randomCategory() {
+ int randomIndex = Randoms.pickNumberInRange(0, 4);
+ return Category.values()[randomIndex];
+ }
+
+ public static List<String> getMenusByCategory(Category category) {
+ return category.menus;
+ }
+
+ public static String getMenu(List<String> menus) {
+ return Randoms.shuffle(menus).get(0);
+ }
+} | Java | ์ 41๋ผ์ธ์ ๋ชฉ์ ์ ๊ฒฐ๊ตญ ๋ฉ๋ด๋ฅผ ๋ฝ๊ธฐ ์ํจ์ด๋ผ ์๊ฐํด์ getMenu()๋ก ๋ค์ด๋ฐ ํ์ต๋๋ค.
๋๋ค์ผ๋ก ๋ฉ๋ด๋ฅผ ์ ํํ๋ค๋ ์ฌ์ค์ ์ง๊ด์ ์ผ๋ก ํํํจ์ด ๊ฐ๋
์ฑ ๋ฐ ์ ์ง๋ณด์์ฑ ์ธก๋ฉด์์ ๋ ์ข์ ๊ฒ ๊ฐ๋ค์!:) |
@@ -0,0 +1,22 @@
+package menu.exception;
+
+public enum ErrorMessage {
+ INVALID_COACH_NAME_LENGTH("์ฝ์น์ ์ด๋ฆ์ ์ต์ 2๊ธ์, ์ต๋ 4๊ธ์์ฌ์ผ ํฉ๋๋ค."),
+ INVALID_COACHES_COUNT("์ฝ์น๋ ์ต์ 2๋ช
, ์ต๋ 5๋ช
์ฌ์ผ ํฉ๋๋ค."),
+ INVALID_MENU_COUNT("๋ชป ๋จน๋ ๋ฉ๋ด๋ ์ต์ 0๊ฐ, ์ต๋ 2๊ฐ์ฌ์ผ ํฉ๋๋ค."),
+ NOT_EXIST_MENU("์กด์ฌํ์ง ์๋ ๋ฉ๋ด์
๋๋ค."),
+ ;
+
+ private static final String PREFIX = "[ERROR] ";
+ private static final String POSTFIX = " ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ private final String message;
+
+ ErrorMessage(String message) {
+ this.message = message;
+ }
+
+ public String getMessage(Object... args) {
+ return String.format(PREFIX + message + POSTFIX, args);
+ }
+} | Java | ์๊ฐ์ด ๋ถ์กฑํด ๋ฆฌํฉํ ๋งํ์ง ๋ชปํ ์ ์ด ์์ฝ๋ค์ใ
ํผ๋๋ฐฑ ๊ฐ์ฌํฉ๋๋ค! |
@@ -22,6 +22,7 @@ public ResponseEntity<Void> highlight(
@Valid @RequestBody HighlightsRequest request,
@ReviewGroupSession ReviewGroup reviewGroup
) {
+ // todo: session ์์ ๋ฆฌ๋ทฐ ๊ทธ๋ฃน ์ง์ ๊บผ๋ด๋ ๊ฒ์ด ์๋, request ์ ์๋ reviewGroupId ๋ฅผ ํ์ฉํ๋๋ก ์์ ํด์ผ ํ๋ค.
highlightService.editHighlight(request, reviewGroup);
return ResponseEntity.ok().build();
} | Java | ์ง๊ธ ๋๋ถ๋ถ์ ์๋น์ค ์ฝ๋๋ค์ด ReviewGroupSession ์ผ๋ก ReviewGroup ์์ฒด๋ฅผ ๋ฐ์์ค๊ณ ์์ด์๐
์ด๊ฑธ ๋ฐ๊พธ๋๊ฒ ๋๊ณต์ฌ๊ฐ ๋ ๊ฒ ๊ฐ๋ค์... |
@@ -16,7 +16,7 @@
import reviewme.reviewgroup.service.dto.ReviewGroupCreationRequest;
import reviewme.reviewgroup.service.dto.ReviewGroupCreationResponse;
import reviewme.reviewgroup.service.dto.ReviewGroupPageResponse;
-import reviewme.reviewgroup.service.dto.ReviewGroupResponse;
+import reviewme.reviewgroup.service.dto.ReviewGroupSummaryResponse;
@RestController
@RequiredArgsConstructor
@@ -26,8 +26,8 @@ public class ReviewGroupController {
private final ReviewGroupLookupService reviewGroupLookupService;
@GetMapping("/v2/groups/summary")
- public ResponseEntity<ReviewGroupResponse> getReviewGroupSummary(@RequestParam String reviewRequestCode) {
- ReviewGroupResponse response = reviewGroupLookupService.getReviewGroupSummary(reviewRequestCode);
+ public ResponseEntity<ReviewGroupSummaryResponse> getReviewGroupSummary(@RequestParam String reviewRequestCode) {
+ ReviewGroupSummaryResponse response = reviewGroupLookupService.getReviewGroupSummary(reviewRequestCode);
return ResponseEntity.ok(response);
}
| Java | ํ์์์
- ์ด api ๋ reviewGroupId ๋ก ํ๋ณํ ๊น?๐ค
- ๋ฆฌ๋ฆฌ์ฝ๋ก ๋ฆฌ๋ทฐ ๊ทธ๋ฃน ์ ๋ณด๋ฅผ ์๋ณํด์ค๋๊ฒ ํต์ผ๋ ์๋ ๊ฒ ๊ฐ๊ณ ์ ๋งคํ๊ธด ํ๋ฐ,..
ํธํ์ฑ์ ๋ฌธ์ ๋ ์๊ณ ๋ณต์กํ๋ ์ผ๋จ api ๋ ๊ทธ๋๋ก ๋๊ณ response ์ด๋ฆ๋ง ๋ณ๊ฒฝํ์! ๐
๋ผ๊ณ ํ์ด์ ๋ฐ์ํ์ต๋๋ค~ |
@@ -4,8 +4,6 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
-import reviewme.reviewgroup.controller.ReviewGroupSession;
-import reviewme.reviewgroup.domain.ReviewGroup;
import reviewme.template.service.TemplateService;
import reviewme.template.service.dto.response.SectionNamesResponse;
@@ -16,10 +14,8 @@ public class SectionController {
private final TemplateService templateService;
@GetMapping("/v2/sections")
- public ResponseEntity<SectionNamesResponse> getSectionNames(
- @ReviewGroupSession ReviewGroup reviewGroup
- ) {
- SectionNamesResponse sectionNames = templateService.getSectionNames(reviewGroup);
+ public ResponseEntity<SectionNamesResponse> getSectionNames() {
+ SectionNamesResponse sectionNames = templateService.getSectionNames();
return ResponseEntity.ok(sectionNames);
}
} | Java | ์ด ๋ถ๋ถ์ ์ฌ์ค ํ์์์ `/templates/{templateId}/sections`๋ก ํ์๊ณ ํ์๋๋ฐ,
์ ํ
๋๊ฐ ๊ธฐ์กด api ๊ทธ๋๋ก ๋๊ณ ๊ณ ์ ์๋ต์ ํ์๊ณ ํ๋์ง ์ฝ๋๋ฅผ ๋ณด๋ฉด์ ๊นจ๋ฌ์์ด์ ใ
templateId ๋ฅผ ๋ฐ๊ฒ ํด๋ดค์, templateId ๋ฅผ ์ง์ ํด์ ์์ฑํ๋ ๊ณณ๋ ์๊ณ , ์๋ต์ผ๋ก ๋ด๋ ค์ฃผ๋ ๊ณณ๋ ์๊ณ ...
์ง๊ธ ๋ณ๊ฒฝ์ ํ๋๋ผ๋ ๋งฅ๋ฝ์ ์๋ง๋ ๋ณ๊ฒฝ ๊ฐ๋๋ผ๊ณ ์๐
๊ทธ๋์ ๊ธฐ์กด ๋ด์ฉ๋๋ก ๊ทธ๋๋ก ๋์ต๋๋ค!
๋์ defaultTemplate ๊ด๋ จ๋ ๋ด์ฉ์ด ์์ง๋์๊ฒ ์กด์ฌํ ์ ์๋๋ก
DefaultTemplateService ๋ฅผ ๋ง๋ค์ด๋ดค์ด์. |
@@ -22,6 +22,7 @@
public class AdminController {
private final AdminService adminService;
+ // ์์์ ๋ํ ๋ณต์ํ ํ๊ธฐ
@PostMapping("/artist")
public ResponseEntity<ArtistDTO> createArtist(@Validated @RequestBody NewArtistDTO newArtistDTO) {
return ResponseEntity.ok(adminService.createArtist(newArtistDTO)); | Java | ๋ค๋ฅธ ๊ณณ์ url ์์ ์์(ex. artist)์ ๋ณต์ํ์ผ๋ก ํ๊ธฐํ๋๋ฐ ์๋ ๊ณณ๋ ์๋ค์
ํต์ผ์ฑ์ ๊ฐ์ง๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์~ |
@@ -84,6 +84,7 @@ public ResponseEntity<PageResponse<MusicContentDTO>> searchMusic(
return ResponseEntity.ok(musicService.searchMusic(query, mcType, pageRequest));
}
+ // ๋จ์ด ๊ตฌ๋ถ ํต์ผ์ํค๊ธฐ auto-complete
@GetMapping("/search/autocomplete")
public ResponseEntity<List<String>> autoCompleteSearchKeyword(
@RequestParam(name = "q") String query, | Java | ๋ฉ์๋๋ช
์ autoComplete ์ธ๋ฐ, url ๋ช
์ autocomplete ์ด๋ค์
๋จ์ด๋ฅผ ๊ตฌ๋ถ์ง์ด์ auto-complete ์ผ๋ก ํต์ผ์ฑ์๊ฒ ๋ค์ด๋ฐ ํ์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์. |
@@ -4,6 +4,7 @@
import com.anarchyadventure.music_dabang_api.entity.user.User;
import lombok.Data;
+// RequestDto, ResponseDto ๊ตฌ๋ถํด์ฃผ๊ธฐ (ํจํค์ง๋ช
or ํด๋์ค๋ช
)
@Data
public class NewPlaylistDTO {
private String name; | Java | ์์ฒญ dto ์ธ์ง ์๋ต dto ์ธ์ง๋ฅผ ํจํค์ง๋ก ๋ถ๋ฆฌํด์ฃผ๊ฑฐ๋, ํด๋์ค๋ช
์ผ๋ก ๊ตฌ์ฒด์ ์ผ๋ก ๋ช
์ํด์ฃผ๋ฉด ๋ค๋ฅธ ์ฌ๋์ด ๋ดค์ ๋ ์ข ๋ ํธํ๊ฒ ๊ตฌ๋ถํ ์ ์์ ๊ฑฐ ๊ฐ์์ |
@@ -8,6 +8,8 @@
@Data
public class KakaoUserDTO {
private Long id;
+
+ // ์ธ๋ถ API ํ๋๋ช
์ ์์กดํ์ง ์๋๋ก @JsonProperty ์ฌ์ฉ
private KakaoUserInfo kakao_account;
@Data
@@ -20,6 +22,7 @@ public static class KakaoUserInfo {
private Profile profile;
public LocalDate getBirth() {
+ // StringUtils ์ฌ์ฉ
if (birthday == null || birthyear == null) {
return null;
}
@@ -34,6 +37,7 @@ public LocalDate getBirth() {
}
public Gender getGender() {
+ // null ๋ณด๋ค๋ UNKNOWN ๊ณ ๋ ค
if (gender == null) {
return null;
} | Java | ์ธ๋ถ API ์๋ต์ด snake case ๋ก ๋ด๋ ค์จ๋ค๊ณ ํด์ ํ๋๋ช
์ ๊ทธ๋๋ก ๋ง์ถ ํ์ ์์ ๊ฑฐ ๊ฐ์์. ์ธ๋ถ API ์๋ต์ snake case ๋ก ๋ฐ๊ณ java ์ฝ๋ ๋ด์์๋ camel case ๋ฅผ ์ฌ์ฉํ๋๋ก @JsonProperty, @JsonNaming ๋ฑ์ ํ์ฉํด๋ณด๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์
https://lvolz.tistory.com/77 |
@@ -8,6 +8,8 @@
@Data
public class KakaoUserDTO {
private Long id;
+
+ // ์ธ๋ถ API ํ๋๋ช
์ ์์กดํ์ง ์๋๋ก @JsonProperty ์ฌ์ฉ
private KakaoUserInfo kakao_account;
@Data
@@ -20,6 +22,7 @@ public static class KakaoUserInfo {
private Profile profile;
public LocalDate getBirth() {
+ // StringUtils ์ฌ์ฉ
if (birthday == null || birthyear == null) {
return null;
}
@@ -34,6 +37,7 @@ public LocalDate getBirth() {
}
public Gender getGender() {
+ // null ๋ณด๋ค๋ UNKNOWN ๊ณ ๋ ค
if (gender == null) {
return null;
} | Java | StringUtils ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด null ์ฒดํฌ์ blank ์ฒดํฌ๋ฅผ ํ๋ฒ์ ํ์ค ์ ์์ด์
ex)
```java
if (StringUtils.isBlank(birthday) || StringUtils.isBlank(birthyear)) {
return null;
}
``` |
@@ -8,6 +8,8 @@
@Data
public class KakaoUserDTO {
private Long id;
+
+ // ์ธ๋ถ API ํ๋๋ช
์ ์์กดํ์ง ์๋๋ก @JsonProperty ์ฌ์ฉ
private KakaoUserInfo kakao_account;
@Data
@@ -20,6 +22,7 @@ public static class KakaoUserInfo {
private Profile profile;
public LocalDate getBirth() {
+ // StringUtils ์ฌ์ฉ
if (birthday == null || birthyear == null) {
return null;
}
@@ -34,6 +37,7 @@ public LocalDate getBirth() {
}
public Gender getGender() {
+ // null ๋ณด๋ค๋ UNKNOWN ๊ณ ๋ ค
if (gender == null) {
return null;
} | Java | ์ ๋ enum ๊ฐ์ null ๋ก ๋ฐํํ๋ ๊ฒ์ ์ง์ํ๋ ํธ์ธ๋ฐ์. null ์ npe ๋ฑ์ ์ด์๊ฐ ์์ผ๋ '์์์์(ex. UNKNOWN)'์ ๋ํ๋ด๋ enum ์ ์ ์ํ์๋ ๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค |
@@ -38,6 +38,7 @@ public class MusicContent extends BaseEntity {
private MusicContentType musicContentType = MusicContentType.MUSIC;
public MusicContent(String thumbnailUrl, String musicContentUrl, String videoContentUrl, String title, Artist artist) {
+ // ํ์๊ฐ, ๊ธธ์ด ์ ํ ๋ฑ์ ๋ํ validation (+ dto ๋ณด๋ค๋ ์์ฑ์์ ๊ธฐ๋ก)
this.thumbnailUrl = thumbnailUrl;
this.musicContentUrl = musicContentUrl;
this.videoContentUrl = videoContentUrl; | Java | ํ์๊ฐ์ด๋ ๊ธธ์ด ์ ํ ๋ฑ์ ๋ํด ์์ฑ์์ validation ์ฝ๋๋ฅผ ๋ฃ์ด๋ ์ข์ ๊ฑฐ ๊ฐ์์ |
@@ -8,6 +8,7 @@ public enum MusicContentType {
@JsonCreator
public static MusicContentType from(String value) {
+ // java 8 stream ์ฌ์ฉํ๊ธฐ
for (MusicContentType type : MusicContentType.values()) {
if (type.name().equalsIgnoreCase(value)) {
return type; | Java | java 8 ์ stream ์ ์ฌ์ฉํ์๋ ๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค. ๊ฐ๋
์ฑ์ ์ํด ์ต๋ํ ๋ค์ฌ์ฐ๊ธฐ depth ๊ฐ 2๊ฐ ๋์ด๊ฐ์ง ์๊ฒ ์ฝ๋ฉํด๋ณด์๋ ์ฐ์ต์ ํด๋ณด์๋ ๊ฒ ์ข์ต๋๋ค.
ex)
```java
public static MusicContentType from(String value) {
return Arrays.stream(MusicContentType.values())
.filter(type -> type.name().equalsIgnoreCase(value))
.findFirst()
.orElse(null);
}
``` |
@@ -70,6 +70,8 @@ public AbstractAuthenticationToken convert(Jwt jwt) {
try {
Long userId = Long.parseLong(jwt.getSubject());
Optional<User> userOpt = userRepository.findById(userId);
+
+ // early return ๊ณ ๋ ค
if (userOpt.isPresent()) {
principalDetails = new PrincipalDetails(userOpt.get());
if (tokenType.equals(ACCESS_TOKEN_NAME)) { | Java | depth ๊ฐ ๊น์ด์ง๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๊ฒ ๋์ด์์ด์. early return ํ ์ ์๋ ๋ถ๋ถ์ early return ํ๋ ๊ฒ๋ ๋ฐฉ๋ฒ์ด์์. (์ ๋์ ์ธ ๋ฐฉ๋ฒ์ ์๋๋๋ค!)
https://woonys.tistory.com/209 |
@@ -29,6 +29,7 @@ public static User getUser() {
return null;
}
+ // service ์์ ๋งค๋ฒ ํด๋น ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋๋ผ, ArgumentResolver ๋ก ํธํ๊ฒ ์ฒ๋ฆฌํ๊ธฐ
public static User getUserAuth(UserRole... userRoles) {
User user = getUser();
if (user == null) throw new UnauthorizedException(); | Java | service ์์ ๋งค๋ฒ `SecurityHandler.getUserAuth()` ๋ฅผ ํธ์ถํ๋๋ฐ User ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํจ์ผ๋ก ๋ณด์ฌ์. ๋์ค์ ArgumentResolver ๋ฅผ ์ฌ์ฉํด์ ์ฒ๋ฆฌํด๋ณด๋ ๊ฒ๋ ์ข์ ๊ฑฐ ๊ฐ์์
https://velog.io/@uiurihappy/Spring-Argument-Resolver-%EC%A0%81%EC%9A%A9%ED%95%98%EC%97%AC-%EC%9C%A0%EC%A0%80-%EC%A0%95%EB%B3%B4-%EB%B6%88%EB%9F%AC%EC%98%A4%EA%B8%B0 |
@@ -68,6 +68,7 @@ public MusicContentDTO editMusic(Long musicId, NewMusicContentDTO newMusicConten
newMusicContentDTO.getVideoContentUrl(),
newMusicContentDTO.getTitle()
);
+ // jpa ๋ํฐ ์ฒดํน์ ์ด์ฉํ์ฌ update
musicContentRepository.save(musicContent);
return MusicContentDTO.from(musicContent);
} | Java | jpa ๋ฅผ ์ฌ์ฉํ๊ณ ๊ณ์ ๋ฐ ์์์ฑ ์ปจํ
์คํธ์์ ๊ด๋ฆฌํ๊ฒ๋ @Transactional ์ ๋ถ์ฌ์ฃผ๋ฉด save ๋ฉ์๋๋ ๋ณ๋๋ก ํธ์ถํ ํ์๊ฐ ์๊ฒ ๋ค์ |
@@ -67,7 +67,11 @@ public PlaylistItemDTO addMyMusicListItem(Long musicId) {
User user = SecurityHandler.getUserAuth();
PlaylistItem item = new PlaylistItem(user, music, null);
Long lastOrderingNum = playlistItemRepository.lastOrderingNumInMyMusicList(user.getId());
+ // addPlaylistItem ๊ณผ ์ค๋ณต ๋ก์ง
if (lastOrderingNum != null) {
+ // ๋งค์ง ๋๋ฒ๋ณด๋ค๋ ์์๋ฅผ ์ฌ์ฉ
+ // ์ฒ ๋จ์๋ ์ธ๋๋ฐ ์ฌ์ฉํ๋ ์ต๊ด
+ // setter ์ฌ์ฉ์ ์ง์. ์์ฑ์๋ก ์ฒ๋ฆฌํ ์ ์์ ๊ฑฐ ๊ฐ์๋ฐ ์๋ํด๋ณด๊ธฐ.
item.setOrderingNum(lastOrderingNum + 1000L);
}
playlistItemRepository.save(item); | Java | `addPlaylistItem` ์์ ์ค๋ณต ๋ก์ง์ด ์ข ๋ณด์ด๋๋ฐ ๋ณ๋ ๋ฉ์๋๋ก ๋นผ๋ ๊ฑธ ๊ณ ๋ คํด๋ณด์
๋ ์ข์ ๊ฑฐ ๊ฐ์์ |
@@ -67,7 +67,11 @@ public PlaylistItemDTO addMyMusicListItem(Long musicId) {
User user = SecurityHandler.getUserAuth();
PlaylistItem item = new PlaylistItem(user, music, null);
Long lastOrderingNum = playlistItemRepository.lastOrderingNumInMyMusicList(user.getId());
+ // addPlaylistItem ๊ณผ ์ค๋ณต ๋ก์ง
if (lastOrderingNum != null) {
+ // ๋งค์ง ๋๋ฒ๋ณด๋ค๋ ์์๋ฅผ ์ฌ์ฉ
+ // ์ฒ ๋จ์๋ ์ธ๋๋ฐ ์ฌ์ฉํ๋ ์ต๊ด
+ // setter ์ฌ์ฉ์ ์ง์. ์์ฑ์๋ก ์ฒ๋ฆฌํ ์ ์์ ๊ฑฐ ๊ฐ์๋ฐ ์๋ํด๋ณด๊ธฐ.
item.setOrderingNum(lastOrderingNum + 1000L);
}
playlistItemRepository.save(item); | Java | 1. 1000L ์ด ์๋ฏธํ๋ ๋ถ๋ถ์ ๋ณ๋ ์์๋ก ์ ์ํ๋๊ฒ ๊ฐ๋
์ฑ์ ์ํด ์ข์ ๊ฑฐ ๊ฐ์์.
2. ์ฒ ๋จ์๋ ์ธ๋๋ฐ๋ฅผ ์ฌ์ฉํด ๊ตฌ๋ถํ๋ ์ต๊ด์ ๊ฐ์ง๋ฉด ์ข์์
ex) `private static final DEFAULT_ADDITIONAL_ORDERING_NUM = 1_000L;`
3. setter ์ฌ์ฉ์ ์ต๋ํ ์ง์ํ๋๊ฒ ์ข์์. item ์ ์์ฑ์๋ฅผ ๋ง๋ค์ด ํ๋ผ๋ฏธํฐ๋ฅผ ๋๊ฒจ ์ฒ๋ฆฌ ๊ฐ๋ฅํด๋ณด์ด๋๋ฐ ์๋ํด๋ณด๋ฉด ์ด๋จ๊น์? |
@@ -37,6 +37,7 @@ public TokenDTO loginWithKakao(String accessToken) {
user = new User(
OAuthProvider.KAKAO,
kakaoUser.getId().toString(),
+ // ๋๋ฏธํฐ ๋ฒ์น ๊ณ ๋ ค
kakaoUser.getKakao_account().getProfile().getNickname(),
kakaoUser.getKakao_account().getProfile().getProfile_image_url(),
kakaoUser.getKakao_account().getPhone_number(), | Java | ๊ฐ์ฒด์ getter ๋ฅผ ์ค์ค์ด ํธ์ถํ๋ ๊ตฌ์กฐ๋ค์. ๋๋ฏธํฐ ๋ฒ์น์ ๋ํด ์์๋ณด์
๋ ์ข์ ๊ฑฐ ๊ฐ์์.
https://mangkyu.tistory.com/147 |
@@ -0,0 +1,32 @@
+package racingcar.controller.Validation
+
+class RaceCountValidation(
+ private val raceCount: String,
+) {
+ fun validateRaceCount() {
+ isNotEmpty()
+ isLong()
+ isInteger()
+ over1()
+ }
+
+ private fun isNotEmpty() {
+ require(raceCount.isNotEmpty()) { RaceCountErrorType.EMPTY_INPUT.errorMessage }
+ }
+
+ private fun isLong() {
+ require(raceCount.toLongOrNull() != null) { RaceCountErrorType.NO_LONG.errorMessage }
+ }
+
+ private fun isInteger() {
+ require(raceCount.toIntOrNull() != null) { RaceCountErrorType.INTEGER.errorMessage }
+ }
+
+ private fun over1() {
+ require(raceCount.toInt() >= RACE_COUNT_OK) { RaceCountErrorType.UP_1.errorMessage }
+ }
+
+ companion object {
+ private const val RACE_COUNT_OK = 1
+ }
+}
\ No newline at end of file | Kotlin | Intํ ๊ฒ์ฌ๋ฅผ ํ๊ธฐ ์ ์ Long Type ์ผ๋ก ๊ฒ์ฌํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค ใ
ใ
Int๋ก ํํ ๊ฐ๋ฅํ ๋ฒ์๋ฅผ ์ด๊ณผํ ๊ฒฝ์ฐ๋ฅผ ์ํด์ ํ์ ๊ฑธ๊น์ฉ? |
@@ -0,0 +1,19 @@
+package racingcar.model
+
+import racingcar.util.RandomNumber
+import javax.swing.text.Position
+
+data class Car(
+ val name: String,
+ private var _position: Int = 0,
+) {
+ val position: Int
+ get() = _position
+
+ fun move() {
+ val randomNumber = RandomNumber().getRandomNumber()
+ if (randomNumber >= 4) {
+ _position++
+ }
+ }
+} | Kotlin | ์ธ๋ถ์์ Position์ ๋ณ๊ฒฝํ ์ ์๋๋ก ํ์ ๊ฒ ์ข๋ค์ ! |
@@ -0,0 +1,27 @@
+package racingcar.view
+
+import racingcar.model.Car
+
+class OutputView {
+ fun showStartMessage() {
+ println("๊ฒฝ์ฃผํ ์๋์ฐจ ์ด๋ฆ์ ์
๋ ฅํ์ธ์.(์ด๋ฆ์ ์ผํ(,) ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ)")
+ }
+
+ fun showRaceCount() {
+ println("์๋ํ ํ์๋ ๋ช ํ์ธ๊ฐ์?")
+ }
+
+ fun showPlayStart() {
+ println("์คํ๊ฒฐ๊ณผ")
+ }
+
+ fun showEachRoundResult(cars: List<Car>) {
+ for (car in cars) {
+ println("${car.name} : ${"-".repeat(car.position)}")
+ }
+ }
+
+ fun showWinner(winners: List<String>) {
+ println("์ต์ข
์ฐ์น์ : ${winners.joinToString(", ")}")
+ }
+}
\ No newline at end of file | Kotlin | ์ ๋ ์ฌ์ฉ์์๊ฒ ๋ฉ์์ง๋ฅผ ๋ณด์ฌ์ค ๋ฉ์์ง๋ฅผ ๋ง๋๋ ๊ฒ๋
ํ๋์ ๋น์ฆ๋์ค ๋ก์ง์ด๋ผ๊ณ ์๊ฐํด์ ๋ฉ์์ง๋ฅผ ๊ฐ๊ณตํ OutputView์ ์ ๋ฌํ๋๋ก ๊ตฌํํ ํฐ๋ผ
์ฑ์ฑ๋๋ OutputView๋ ์ถ๋ ฅ์ ๋ํ ์ญํ ๋ง ํ๋๋ก ํ๋๊ฑด ์ด๋จ๊น ์ถ์ด์ ! |
@@ -0,0 +1,10 @@
+package racingcar.view
+
+import camp.nextstep.edu.missionutils.Console
+
+class InputView {
+ fun getInput(): String {
+ val userInput = Console.readLine().trim()
+ return userInput
+ }
+}
\ No newline at end of file | Kotlin | ์ค! ์ฌ๊ธฐ์ trim() ์ ์จ๋ ๊น๋ํ๊ฒ ๊ตฐ์ ๐ |
@@ -0,0 +1,27 @@
+package racingcar.view
+
+import racingcar.model.Car
+
+class OutputView {
+ fun showStartMessage() {
+ println("๊ฒฝ์ฃผํ ์๋์ฐจ ์ด๋ฆ์ ์
๋ ฅํ์ธ์.(์ด๋ฆ์ ์ผํ(,) ๊ธฐ์ค์ผ๋ก ๊ตฌ๋ถ)")
+ }
+
+ fun showRaceCount() {
+ println("์๋ํ ํ์๋ ๋ช ํ์ธ๊ฐ์?")
+ }
+
+ fun showPlayStart() {
+ println("์คํ๊ฒฐ๊ณผ")
+ }
+
+ fun showEachRoundResult(cars: List<Car>) {
+ for (car in cars) {
+ println("${car.name} : ${"-".repeat(car.position)}")
+ }
+ }
+
+ fun showWinner(winners: List<String>) {
+ println("์ต์ข
์ฐ์น์ : ${winners.joinToString(", ")}")
+ }
+}
\ No newline at end of file | Kotlin | ๋ง์์! ์ฐฌํธ๋ ์ฝ๋ ๋ณด๋ฉด์ ๊ทธ ๋ถ๋ถ์ด ๋๋ฌด ์ข์์ด์! outputview์ ์ถ๋ ฅ๋ง ํ๋ ๊ฒ ์ข์์ต๋๋ค! ์ ๋ ์ฐฌํธ๋ ์ฝ๋ ๋ค์๋ณด๋ฉด์ ์ ์ฝ๋์ ์ด๋ป๊ฒ ์ ์ฉํ ์ ์๋ ์ง ๊ณ ๋ฏผํด๋ด์ผ๊ฒ ์ด์! ์๊ฒฌ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,32 @@
+package racingcar.controller.Validation
+
+class RaceCountValidation(
+ private val raceCount: String,
+) {
+ fun validateRaceCount() {
+ isNotEmpty()
+ isLong()
+ isInteger()
+ over1()
+ }
+
+ private fun isNotEmpty() {
+ require(raceCount.isNotEmpty()) { RaceCountErrorType.EMPTY_INPUT.errorMessage }
+ }
+
+ private fun isLong() {
+ require(raceCount.toLongOrNull() != null) { RaceCountErrorType.NO_LONG.errorMessage }
+ }
+
+ private fun isInteger() {
+ require(raceCount.toIntOrNull() != null) { RaceCountErrorType.INTEGER.errorMessage }
+ }
+
+ private fun over1() {
+ require(raceCount.toInt() >= RACE_COUNT_OK) { RaceCountErrorType.UP_1.errorMessage }
+ }
+
+ companion object {
+ private const val RACE_COUNT_OK = 1
+ }
+}
\ No newline at end of file | Kotlin | ๋ค ๋ง์์!!! long ํ์
์ซ์๋ ์๋ฌ๋ก ๋ด์ผํ ๊ฑฐ ๊ฐ์์! long ํ์
๋ ํ์ฉํ๋ฉด ๊ณ ๋ คํ ํ์
์ด 2๊ฐ๋ ์๊ฒจ์ ์ต๋ํ ๊ณ ๋ คํ ์์๋ฅผ ์ค์ด์๋ผ๋ ๊ฒ ์ํ์ ์ํ๋ ์ ์ ์์ธ์
๋๋ค... ํํํ ์ค์ ๋ก ํ๋ก๋ํธ ๋ง๋ค ๋๋ ์ด๋ฌ์ง ๋ง์..... ๋ ์์ ์... |
@@ -0,0 +1,86 @@
+package racingcar.controller
+
+import racingcar.controller.Validation.CarsNameValidation
+import racingcar.controller.Validation.RaceCountValidation
+import racingcar.controller.domain.UserInteractionController
+import racingcar.model.Car
+
+class RacingCar(
+ private val userInteractionController: UserInteractionController = UserInteractionController()
+) {
+ fun run() {
+ val cars = startCarsName()
+ val playCount = startRaceCount()
+ val doneCars = startGame(playCount, cars)
+ val winnerNames = getWinner(doneCars)
+ showWinners(winnerNames)
+ }
+
+ private fun getCarsName(): String {
+ val carsName = userInteractionController.handleCarsName()
+ return carsName
+ }
+
+ private fun getRaceCount(): String {
+ val raceCount = userInteractionController.handleRaceCount()
+ return raceCount
+ }
+
+ private fun validateCarName(carsName: String) {
+ val carsNameValidation = CarsNameValidation(carsName)
+ carsNameValidation.validateCarsName()
+ }
+
+ private fun validateRaceCount(raceCount: String) {
+ val raceCountValidation = RaceCountValidation(raceCount)
+ raceCountValidation.validateRaceCount()
+ }
+
+ private fun startCarsName(): List<Car> {
+ val carsName = getCarsName()
+ validateCarName(carsName)
+ val cars = adapterCarsName(carsName)
+ return cars
+ }
+
+ private fun startRaceCount(): Int {
+ val raceCount = getRaceCount()
+ validateRaceCount(raceCount)
+ val playCount = adapterRaceCount(raceCount)
+ return playCount
+ }
+
+ private fun adapterCarsName(carsName: String): List<Car> {
+ val cars = carsName.split(COMMA).map { Car(it.trim()) }
+ return cars
+ }
+
+ private fun adapterRaceCount(raceCount: String): Int {
+ return raceCount.toInt()
+ }
+
+ private fun startGame(playCount: Int, cars: List<Car>): List<Car> {
+ userInteractionController.handlePlayStart()
+ for (count in START_GAME..playCount) {
+ cars.forEach { it.move() }
+ userInteractionController.handleEachRoundResult(cars)
+ }
+ return cars
+ }
+
+ private fun getWinner(cars: List<Car>): List<String> {
+ val winnerPosition = cars.map { it.position }.max()
+ val winner = cars.filter { winnerPosition == it.position }
+ val winnerName = winner.map { it.name }
+ return winnerName
+ }
+
+ private fun showWinners(winnersName: List<String>) {
+ userInteractionController.handleWinners(winnersName)
+ }
+
+ companion object {
+ private const val COMMA = ","
+ private const val START_GAME = 1
+ }
+}
\ No newline at end of file | Kotlin | controller๋ฅผ ์ด๋ ๊ฒ ์ฐ๋ ๊ฑฐ๊ตฐ์! ํจ์์ด๋ฆ๋ค์ด ๊น๋ํ๊ณ ์ ๋ ฌ๋ ์๋์ด์์ด์ ํ๋์ ์์๋ณด๊ธฐ ์ฝ๋ค์ :) |
@@ -0,0 +1,44 @@
+package racingcar.controller.Validation
+
+class CarsNameValidation(
+ private val carsName: String,
+) {
+ fun validateCarsName() {
+ isNotEmpty()
+ checkCarsCount()
+ checkCarNameEmpty()
+ checkCarName5()
+ checkSameName()
+ }
+
+ private fun isNotEmpty() {
+ require(carsName.isNotEmpty()) { CarsNameErrorType.EMPTY_INPUT.errorMessage }
+ }
+
+ private fun checkCarsCount() {
+ val carsCount = carsName.split(COMMA).size
+ require(carsCount >= RACING_OK_COUNT) { CarsNameErrorType.UP_1.errorMessage }
+ }
+
+ private fun checkCarNameEmpty() {
+ val carsName = carsName.split(COMMA)
+ require(carsName.all { it.isNotEmpty() }) { CarsNameErrorType.EMPTY_INPUT.errorMessage }
+ }
+
+ private fun checkCarName5() {
+ val carsName = carsName.split(COMMA)
+ require(carsName.all { it.length <= CAR_NAME_OK_LENGTH }) { CarsNameErrorType.UNDER_5.errorMessage }
+ }
+
+ private fun checkSameName() {
+ val carsName = carsName.split(COMMA)
+ require(carsName.size == carsName.toSet().size) { CarsNameErrorType.SAME_NAME.errorMessage }
+ }
+
+ companion object {
+ private const val COMMA = ","
+ private const val RACING_OK_COUNT = 2
+ private const val CAR_NAME_OK_LENGTH = 5
+
+ }
+}
\ No newline at end of file | Kotlin | ์ฝค๋ง๋ฅผ ์์ํ ํ์ ๊ฒ ์ ๋ง ์ข๋ค์!! |
@@ -0,0 +1,19 @@
+package racingcar.model
+
+import racingcar.util.RandomNumber
+import javax.swing.text.Position
+
+data class Car(
+ val name: String,
+ private var _position: Int = 0,
+) {
+ val position: Int
+ get() = _position
+
+ fun move() {
+ val randomNumber = RandomNumber().getRandomNumber()
+ if (randomNumber >= 4) {
+ _position++
+ }
+ }
+} | Kotlin | 4๋ ์์ํ๋ฅผ ํด์ ๋ณ๊ฒฝ์ด ์ฝ๊ฒ ํ๋ฉด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค |
@@ -0,0 +1,15 @@
+package racingcar.controller.Validation
+
+enum class RaceCountErrorType(private val _errorMessage: String) {
+ EMPTY_INPUT("๊ฒฝ์ฃผ ํ์์ ๋น ๋ฌธ์๋ฅผ ์
๋ ฅํ์ต๋๋ค."),
+ NO_LONG("๊ฒฝ์ฃผ ํ์์๋ 21์ต๋ณด๋ค ํฐ ์๋ ์
๋ ฅํ์ง ๋ชปํฉ๋๋ค."),
+ INTEGER("๊ฒฝ์ฃผ ํ์์๋ ์ซ์๋ง ์
๋ ฅ์ด ๊ฐ๋ฅํฉ๋๋ค."),
+ UP_1("๊ฒฝ์ฃผ ํ์๋ 1๋ณด๋ค ํฐ ์๋ง ์
๋ ฅ์ด ๊ฐ๋ฅํฉ๋๋ค.");
+
+ val errorMessage: String
+ get() = ERROR_MESSAGE + _errorMessage
+
+ companion object {
+ private const val ERROR_MESSAGE = "[ERROR] "
+ }
+}
\ No newline at end of file | Kotlin | enum๋ controller ํด๋์์ ๋ค์ด๊ฐ๋๊ตฐ์ ๋ฐฐ์๊ฐ๋๋ค |
@@ -0,0 +1,19 @@
+package racingcar.model
+
+import racingcar.util.RandomNumber
+import javax.swing.text.Position
+
+data class Car(
+ val name: String,
+ private var _position: Int = 0,
+) {
+ val position: Int
+ get() = _position
+
+ fun move() {
+ val randomNumber = RandomNumber().getRandomNumber()
+ if (randomNumber >= 4) {
+ _position++
+ }
+ }
+} | Kotlin | ๋ท! ์๊ฒ ์ต๋๋ค!! |
@@ -1 +1,44 @@
-# java-convenience-store-precourse
+์ฐ์ํํ
ํฌ์ฝ์ค 7๊ธฐ ํ๋ฆฌ์ฝ์ค 4์ฃผ์ฐจ ๊ณผ์ : ํธ์์ (java-convenience-store-precourse)
+===============================================================================
+
+๊ธฐ๋ฅ ๋ชฉ๋ก
+---------
+* AppConfig: Controller, Service, Repository, View ๊ฐ์ฒด ๊ตฌํ์ฒด๋ฅผ ์ค์ ํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
+* InputView: ์
๋ ฅ๊ณผ ๊ด๋ จ๋ ์๋ด ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ณ ์
๋ ฅ์ ๋ฐ์์ StoreController์ ์ ๋ฌํ๋ค.
+ * ์
๋ ฅ๊ฐ์ด Y ๋๋ N์ด์ด์ผ ํ๋ ๊ฒฝ์ฐ ์
๋ ฅ๋ฐ์ ๋ฌธ์์ด์ ๊ฒ์ฆํด์ ์๋ชป๋ ํ์์ด๋ฉด IllegalArgumentException๋ฅผ ํธ์ถํ๊ณ , ์์ธ ๋ฉ์์ง๋ฅผ ์ถ๋ ฅํ๊ณ ๋ค์ ์
๋ ฅ์ ๋ฐ๋๋ค.
+* OutputView: StoreController๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ ๊ฒฐ๊ณผ๊ฐ์ ์ถ๋ ฅํ๋ค.
+* StoreController: ํ๋ก๊ทธ๋จ์ ๋ฉ์ธ ํ๋ฆ์ ์ ์ดํ๊ณ , View์ Service ๊ณ์ธต์ ๋งค๊ฐํ๋ ์ญํ ์ ํ๋ค.
+ * InputView๋ก๋ถํฐ ๋ชจ๋ธ ์์ฑ์ ํ์ํ ๊ฐ์ ์ ๋ฌ๋ฐ์์ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , StoreServcie์ ์ ๋ฌํ๋ค.
+ * StoreService์์ ์์ฑํ ๊ฒฐ๊ณผ ๊ฐ์ ์์ฒญํด์ ์ ๋ฌ๋ฐ๊ณ ์ด๋ฅผ OutputView์ ์ ๋ฌํ๋ค.
+* StoreService: controller์์ ์ ๋ฌ ๋ฐ์ ๋ชจ๋ธ์ ๊ฐ์ง๊ณ ์ฃผ์ ๋น์ฆ๋์ค ๋ก์ง์ ์คํํ๋ฉฐ, ๊ฒฐ๊ณผ๊ฐ์ Repository์ ์ ์ฅํ๊ฑฐ๋, Controller์ ๋ฐํํ๋ค.
+* Repository: ์ฃผ์ ๋ชจ๋ธ๋ฅผ ์ ์ฅํ๋ค.
+* Inventory: ํธ์์ ์ํ ์ฌ๊ณ ์ํฉ์ ๋ณด๊ดํ๋ค.
+* Stock: ์ํ ์ฌ๊ณ ์ํฉ(ํ๋ก๋ชจ์
์ฌ๊ณ ์ ์ผ๋ฐ์ฌ๊ณ )์ ์ ์ฅํ๋ค.
+* Product: ํธ์์ ์์ ํ๋ ์ํ ์ ๋ณด(์ด๋ฆ, ๊ฐ๊ฒฉ, ์ ์ฉ๋๋ ํ๋ก๋ชจ์
์ ๋ณด)๋ฅผ ๋ด๋๋ค.
+* Promotion: ํ๋ก๋ชจ์
ํ ์ธ ์ ๋ณด(์ด๋ฆ, ๋ณด๋์ค ์๋, ํ์ฌ ์์์ผ, ์ข
๋ฃ์ผ)๋ฅผ ๋ด๋๋ค.
+* Promotions: Promotion ๊ฐ์ฒด๋ฅผ ์ปฌ๋ ์
์ผ๋ก ๋ณด๊ดํ๋ค.
+* OrderDetails: ์ฃผ๋ฌธ ์ ๋ณด(์ฃผ๋ฌธ ์ํ, ์๋)๋ฅผ ์ ์ฅํ๋ค.
+* OrderDetailsFactory: ์ฃผ๋ฌธ ์ ๋ณด ๊ฐ์ฒด(OrderDetails)๋ฅผ ๋ง๋๋ ํฉํ ๋ฆฌ ํด๋์ค์ด๋ค. ์ฃผ๋ฌธ ์ ๋ณด ๋ฌธ์์ด์ ๋ฐ์์ ๊ฒ์ฆํ๋ฉฐ ํ์์ด ๋ง์ง ์์ผ๋ฉด IllegalException๋ฅผ ํธ์ถํ๋ค.
+* OrderQuantityOption: ์ฃผ๋ฌธํ ์ํ ํ๋ก๋ชจ์
ํ ์ธ๊ณผ ๊ด๋ จ๋ ์๋ ์กฐ์ ์ ํ์ง๋ฅผ ๋ด๋๋ค. ์ต์
์ข
๋ฅ(OptionCase)๋ ์ฃผ๋ฌธํ ์ํ์ด ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ์๋ ๊ฒฝ์ฐ, ํ๋ก๋ชจ์
์ ์ฉ ๋ณด๋์ค ์ํ์ ์ถ๊ฐ๋ก ๋ฐ์ ์ ์๋ ์ฃผ๋ฌธ์ธ ๊ฒฝ์ฐ ๋ ๊ฐ์ง์ด๋ค.
+ * ์ํ๋ณ ๊ฒฝ์ฐ์, ์ด๋ฅผ ์๋ดํ ๋ฉ์์ง, ๊ณ ๊ฐ์ ์ ํ์ ์ ์ฅํ๋ค.
+* OrderQuantityOptions: OrderQuantityOption ๊ฐ์ฒด๋ฅผ ์ปฌ๋ ์
์ผ๋ก ๋ณด๊ดํ๋ค.
+* QuantityOptionsFactory: ์ฃผ๋ฌธ ์ ๋ณด(OrderDetails)์ ์ฌ๊ณ ํํฉ(Inventory)๊ณผ ์ฃผ๋ฌธ์๊ฐ์ ๋ฐํ์ผ๋ก ๋น๊ตํด์ ์ฃผ๋ฌธ ์ค์ ์๋์ ์กฐ์ ํด์ผ ํ๋ ๊ฒฝ์ฐ(OptionCase)๊ฐ ์๋์ง ํ์ธํ๊ณ OrderQuantityOptions ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
+ * ๋จผ์ ํ๋ก๋ชจ์
์ ์ ์ฉํ ์ ์๋์ง ํ์ธํ๋ค.
+ * ํ๋ก๋ชจ์
์ ์ ์ฉํ ์ ์๋ค๋ฉด ๋์ด๊ฐ๋ค.
+ * ํ๋ก๋ชจ์
์ ์ ์ฉํ ์ ์๋ค๋ฉด ์ฃผ๋ฌธํ ์๋์ด ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ด๊ณผํ๋์ง ํ์ธํ๋ค.
+ * ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ด๊ณผํ๋ฉด ์ผ๋ฐ์ฌ๊ณ ๋ก ์ฃผ๋ฌธํ ์ ํ์ง๋ฅผ ์ ์ฅํ๋ค.
+ * ์ฌ๊ณ ๋ฅผ ๋์ด๊ฐ์ง ์๋๋ค๋ฉด ์ฃผ๋ฌธํ ์๋์ด ๋ชจ๋ ์ฆ์ ํ์ ํฌํจํ๋์ง ํ์ธํ๋ค.
+ * ๋ชจ๋ ์ฆ์ ํ์ ํฌํจํ์ง ์๋๋ค๋ฉด ์ฆ์ ํ์ ํฌํจํ ์๋๋งํผ ์ฌ๊ณ ๊ฐ ์๋์ง ํ์ธํ๊ณ ์ฌ๊ณ ๊ฐ ์์ผ๋ฉด ์ฃผ๋ฌธ ์๋ ์ถ๊ฐ ์ ํ์ง๋ฅผ ์ ์ฅํ๋ค.
+ * ๋ง์ฝ ์ฌ๊ณ ๊ฐ ์๋ค๋ฉด ๋์ด๊ฐ๋ค.
+* OrderProductQuantity: ์ฃผ๋ฌธํ ์ํ ์๋์ ์ ์ฅํ๋ค. ์๋ ์ ๋ณด๋ ์ด ๊ตฌ๋งค์๋, ํ๋ก๋ชจ์
์ฌ๊ณ ์ฐจ๊ฐ ์๋, ์ผ๋ฐ ์ฌ๊ณ ์ฐจ๊ฐ ์๋์ผ๋ก ์ด๋ฃจ์ด์ง๋ค.
+* OrderQuantityAdjuster: ์ฃผ๋ฌธํ ์ํ ์๋ ์กฐ์ ์ ํ์ง(OrderQuantityOptions)์ ๋ํ ์ฃผ๋ฌธ์์ ์ ํ์ ๋ฐํ์ผ๋ก ์๋์ ์กฐ์ ํ๋ฉฐ, ์ฃผ๋ฌธ ์๋(OrderProductQuantity)๊ณผ ์ฌ๊ณ ํํฉ(Inventory)๋ฅผ ๋ฐํ์ผ๋ก ์ฃผ๋ฌธ ์๋์์ ํ๋ก๋ชจ์
์ฌ๊ณ ์ฐจ๊ฐ๋ถ๊ณผ ์ผ๋ฐ ์ฌ๊ณ ์ฐจ๊ฐ๋ถ์ ๊ฐฑ์ ํ๋ค.
+* PromotionDiscountDetail: ์ฃผ๋ฌธ ์ํ ๋ณ ํ๋ก๋ชจ์
ํ ์ธ ์ ์ฉ ์ ๋ณด(ํ๋ก๋ชจ์
์ด๋ฆ, ์ฆ์ ํ ์๋, ์ฆ์ ํ ๊ฐ๊ฒฉ ํฉ)๋ฅผ ์ ์ฅํ๋ค.
+* PromotionDiscountDetails: PromotionDiscountDetail๋ฅผ ์ปฌ๋ ์
์ผ๋ก ๋ณด๊ดํ๋ค. ์ฃผ๋ฌธ ์ ๋ณด, ํธ์์ ์ฌ๊ณ ํํฉ, ์ฃผ๋ฌธ ์๊ฐ์ ๊ฐ์ง๊ณ ์ํ ๋ณ ํ๋ก๋ชจ์
ํ ์ธ ์ ๋ณด๋ฅผ ๋ง๋ค์ด์ ๋ณด๊ดํ๋ค.
+* MembershipDiscountDetail: ์ฃผ๋ฌธ์ ๋ํ ๋ฉค๋ฒ์ญํ ์ธ ๊ธ์ก๋ฅผ ์ ์ฅํ๋ค.
+* Receipt: ๊ณ ๊ฐ์๊ฒ ๋ณด์ฌ์ค ์์์ฆ์ ๋ด๋นํ๋ ํด๋์ค, ์ฃผ๋ฌธ ์ ๋ณด์ ํ ์ธ ๋ด์ญ์ ํ์ํ๋ค. ์ฃผ๋ฌธ ์ ๋ณด, ํ๋ก๋ชจ์
ํ ์ธ ์ ๋ณด, ๋ฉค๋ฒ์ญ ํ ์ธ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์์์ฆ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
+* InventoryInitializer: ์ฌ๊ณ ํ์ผ์์ ์ฌ๊ณ ํํฉ์ ์ฝ์ด์ ์ด๊ธฐํํ๋ค.
+* PromotionInitializer: ํ๋ก๋ชจ์
ํ์ผ์์ ์ํ๋ณ ํ๋ก๋ชจ์
์ ๋ณด๋ฅผ ์ฝ์ด์ ์ด๊ธฐํํ๋ค.
+* StringValidator: ์ฌ๋ฌ ํด๋์ค์์ ๊ณตํต์ผ๋ก ์ฐ๋ ๋ฌธ์์ด ๊ฒ์ฆ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ค.
+* Constants: ์ฌ๋ฌ ํด๋์ค์์ ๊ณตํต์ผ๋ก ์ฐ๋ ์์๋ฅผ ์ ์ฅํ๋ค.
+* ExceptionMessage: ์์ธ ๋ฉ์์ง๋ฅผ ์ ์ฅํ๋ค.
+* KoreanStringFormatter: ์๋ฆฌ์์ ๋ง์ถ์ด ์ ๋ ฌ๋ ๋ฌธ์์ด์ ๋ง๋ ๋ค.
\ No newline at end of file | Unknown | ํด๋์ค๋ณ ์ญํ ์ ์ ๋ฆฌํ๋ ๊ฒ๋ณด๋ค, ์ ์ฒด์ ์ธ ํ๋ฆ๊ณผ ์ญํ ๋ณ ๊ธฐ๋ฅ ๋ชฉ๋ก์ ์ ๋ฆฌํ๋๊ฒ ์ด๋จ๊น์? |
@@ -0,0 +1,34 @@
+package store.config;
+
+import store.controller.StoreController;
+import store.repository.MemoryStoreRepository;
+import store.repository.StoreRepository;
+import store.service.StoreService;
+import store.service.StoreServiceImpl;
+import store.view.InputView;
+import store.view.OutputView;
+
+/** Controller, Service, Repository, View ๊ฐ์ฒด ๊ตฌํ์ฒด๋ฅผ ์ค์ ํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํด๋์ค */
+public class AppConfig {
+
+ private StoreRepository getRepository() {
+ return new MemoryStoreRepository();
+ }
+
+ private InputView getInputView() {
+ return new InputView();
+ }
+
+ private OutputView getOutputView() {
+ return new OutputView();
+ }
+
+ private StoreService getService() {
+ return new StoreServiceImpl(getRepository());
+ }
+
+ public StoreController getStoreController() {
+ return new StoreController(getInputView(), getOutputView(), getService());
+ }
+
+} | Java | ํด๋์ค๋ช
์ผ๋ก ์ถ์ธก ๊ฐ๋ฅํ ๋ด์ฉ์ด๋ผ ์ฃผ์์ ์์ด๋ ๋ ๊ฒ ๊ฐ์ต๋๋ค! |
@@ -0,0 +1,34 @@
+package store.config;
+
+import store.controller.StoreController;
+import store.repository.MemoryStoreRepository;
+import store.repository.StoreRepository;
+import store.service.StoreService;
+import store.service.StoreServiceImpl;
+import store.view.InputView;
+import store.view.OutputView;
+
+/** Controller, Service, Repository, View ๊ฐ์ฒด ๊ตฌํ์ฒด๋ฅผ ์ค์ ํ์ฌ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ํด๋์ค */
+public class AppConfig {
+
+ private StoreRepository getRepository() {
+ return new MemoryStoreRepository();
+ }
+
+ private InputView getInputView() {
+ return new InputView();
+ }
+
+ private OutputView getOutputView() {
+ return new OutputView();
+ }
+
+ private StoreService getService() {
+ return new StoreServiceImpl(getRepository());
+ }
+
+ public StoreController getStoreController() {
+ return new StoreController(getInputView(), getOutputView(), getService());
+ }
+
+} | Java | ์ฝ๋ ์ปจ๋ฒค์
์ค public์ด private๋ณด๋ค ์์ ์์ด์ผ ํ๋ค๋ ๋ด์ฉ์ด ์์ต๋๋ค. |
@@ -0,0 +1,101 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+
+import store.model.inventory.Inventory;
+import store.model.order.OrderDetails;
+import store.model.order.OrderDetailsFactory;
+import store.model.orderquantity.OrderQuantityOption;
+import store.model.orderquantity.OrderQuantityOptions;
+import store.model.order.Receipt;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.time.LocalDateTime;
+
+/**
+ * ํ๋ก๊ทธ๋จ์ ๋ฉ์ธ ํ๋ฆ์ ์ ์ดํ๊ณ , View์ Service ๊ณ์ธต์ ๋งค๊ฐํ๋ ์ญํ ์ ํ๋ ํด๋์ค
+ * InputView๋ก๋ถํฐ ๋ชจ๋ธ ์์ฑ์ ํ์ํ ๊ฐ์ ์ ๋ฌ๋ฐ์์ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , StoreServcie์ ์ ๋ฌํ๋ค.
+ * StoreService์์ ์์ฑํ ๊ฒฐ๊ณผ ๊ฐ์ ์์ฒญํด์ ์ ๋ฌ๋ฐ๊ณ ์ด๋ฅผ OutputView์ ์ ๋ฌํ๋ค.
+ */
+public class StoreController {
+
+ private static final String YES = "Y";
+
+ private final InputView inputView;
+
+ private final OutputView outputView;
+
+ private final StoreService service;
+
+ public StoreController(InputView inputView, OutputView outputView, StoreService service) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.service = service;
+ }
+
+ public void run() {
+ service.initPromotionsAndInventory();
+ boolean keepOpen = true;
+ while (keepOpen) {
+ keepOpen = storeOpen(DateTimes.now());
+ }
+ }
+
+ private boolean storeOpen(LocalDateTime orderTime) {
+ getOrderAndSave();
+ OrderQuantityOptions options = service.getOrderQuantityOptions(orderTime);
+ setQuantityOptionDecisions(options);
+ service.adjustQuantity(options, orderTime);
+ discountAndPrintReceipt(orderTime);
+ service.deductInventoryBySale();
+ return doesKeepOrder();
+ }
+
+ private void getOrderAndSave() {
+ OrderDetailsFactory factory = new OrderDetailsFactory();
+ Inventory inventory = service.getInventory();
+ OrderDetails orderDetails = getOrderDetails(factory, inventory);
+ service.saveOrderDetails(orderDetails);
+ }
+
+ private OrderDetails getOrderDetails(OrderDetailsFactory factory, Inventory inventory) {
+ while (true) {
+ try {
+ String orderLine = inputView.getOrder(inventory);
+ return factory.create(orderLine, inventory);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ private void setQuantityOptionDecisions(OrderQuantityOptions options) {
+ for (OrderQuantityOption option : options) {
+ String decisionLine = inputView.getOptionDecision(option);
+ if (decisionLine.equals(YES)) {
+ option.setDecisionTrue();
+ }
+ }
+ }
+
+ private void discountAndPrintReceipt(LocalDateTime orderTime) {
+ service.discountPromotion(orderTime);
+ String membershipDiscountChoice = inputView.getMemberShipDiscountDecision();
+ boolean wantMembershipDiscount = doesWantMembershipDiscount(membershipDiscountChoice);
+ service.discountMembership(wantMembershipDiscount);
+ Receipt receipt = service.getReceipt();
+ outputView.printReceipt(receipt);
+ }
+
+ private boolean doesWantMembershipDiscount(String membershipDiscountChoice) {
+ return membershipDiscountChoice.equals(YES);
+ }
+
+ private boolean doesKeepOrder() {
+ String keepOpenLine = inputView.getKeepOpenLine();
+ return keepOpenLine.equals(YES);
+ }
+
+} | Java | ๊ฐ์ธ์ ์ธ ๊ถ๊ธ์ฆ์ธ๋ฐ, orderTime์ ์์ํ ๋ ๋ฐ๋๋ค๋ฉด ํ๋ก๊ทธ๋จ ์งํ ์ค ๊ฒฐ์ ์ ๋ค๋ค๋์ ๋ ์ด๋ฏธ ํ๋ก๋ชจ์
๋ ์ง๋ฅผ ์ง๋ ๊ฒฝ์ฐ๋ ์ด๋ป๊ฒ ์ฒ๋ฆฌ๋๋์? |
@@ -0,0 +1,101 @@
+package store.controller;
+
+import camp.nextstep.edu.missionutils.DateTimes;
+
+import store.model.inventory.Inventory;
+import store.model.order.OrderDetails;
+import store.model.order.OrderDetailsFactory;
+import store.model.orderquantity.OrderQuantityOption;
+import store.model.orderquantity.OrderQuantityOptions;
+import store.model.order.Receipt;
+import store.service.StoreService;
+import store.view.InputView;
+import store.view.OutputView;
+
+import java.time.LocalDateTime;
+
+/**
+ * ํ๋ก๊ทธ๋จ์ ๋ฉ์ธ ํ๋ฆ์ ์ ์ดํ๊ณ , View์ Service ๊ณ์ธต์ ๋งค๊ฐํ๋ ์ญํ ์ ํ๋ ํด๋์ค
+ * InputView๋ก๋ถํฐ ๋ชจ๋ธ ์์ฑ์ ํ์ํ ๊ฐ์ ์ ๋ฌ๋ฐ์์ ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , StoreServcie์ ์ ๋ฌํ๋ค.
+ * StoreService์์ ์์ฑํ ๊ฒฐ๊ณผ ๊ฐ์ ์์ฒญํด์ ์ ๋ฌ๋ฐ๊ณ ์ด๋ฅผ OutputView์ ์ ๋ฌํ๋ค.
+ */
+public class StoreController {
+
+ private static final String YES = "Y";
+
+ private final InputView inputView;
+
+ private final OutputView outputView;
+
+ private final StoreService service;
+
+ public StoreController(InputView inputView, OutputView outputView, StoreService service) {
+ this.inputView = inputView;
+ this.outputView = outputView;
+ this.service = service;
+ }
+
+ public void run() {
+ service.initPromotionsAndInventory();
+ boolean keepOpen = true;
+ while (keepOpen) {
+ keepOpen = storeOpen(DateTimes.now());
+ }
+ }
+
+ private boolean storeOpen(LocalDateTime orderTime) {
+ getOrderAndSave();
+ OrderQuantityOptions options = service.getOrderQuantityOptions(orderTime);
+ setQuantityOptionDecisions(options);
+ service.adjustQuantity(options, orderTime);
+ discountAndPrintReceipt(orderTime);
+ service.deductInventoryBySale();
+ return doesKeepOrder();
+ }
+
+ private void getOrderAndSave() {
+ OrderDetailsFactory factory = new OrderDetailsFactory();
+ Inventory inventory = service.getInventory();
+ OrderDetails orderDetails = getOrderDetails(factory, inventory);
+ service.saveOrderDetails(orderDetails);
+ }
+
+ private OrderDetails getOrderDetails(OrderDetailsFactory factory, Inventory inventory) {
+ while (true) {
+ try {
+ String orderLine = inputView.getOrder(inventory);
+ return factory.create(orderLine, inventory);
+ } catch (IllegalArgumentException e) {
+ System.out.println(e.getMessage());
+ }
+ }
+ }
+
+ private void setQuantityOptionDecisions(OrderQuantityOptions options) {
+ for (OrderQuantityOption option : options) {
+ String decisionLine = inputView.getOptionDecision(option);
+ if (decisionLine.equals(YES)) {
+ option.setDecisionTrue();
+ }
+ }
+ }
+
+ private void discountAndPrintReceipt(LocalDateTime orderTime) {
+ service.discountPromotion(orderTime);
+ String membershipDiscountChoice = inputView.getMemberShipDiscountDecision();
+ boolean wantMembershipDiscount = doesWantMembershipDiscount(membershipDiscountChoice);
+ service.discountMembership(wantMembershipDiscount);
+ Receipt receipt = service.getReceipt();
+ outputView.printReceipt(receipt);
+ }
+
+ private boolean doesWantMembershipDiscount(String membershipDiscountChoice) {
+ return membershipDiscountChoice.equals(YES);
+ }
+
+ private boolean doesKeepOrder() {
+ String keepOpenLine = inputView.getKeepOpenLine();
+ return keepOpenLine.equals(YES);
+ }
+
+} | Java | doesKeep๋ณด๋ค keeps๋ก ์ค์ด๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,57 @@
+package store.model.discount;
+
+import java.time.LocalDate;
+import java.util.Objects;
+
+public class Promotion {
+
+ private final String name;
+
+ private final LocalDate startDate;
+
+ private final LocalDate endDate;
+
+ private final int condition;
+
+ public Promotion(String name, LocalDate startDate,
+ LocalDate endDate, int condition) {
+
+ this.name = name;
+ this.startDate = startDate;
+ this.endDate = endDate;
+ this.condition = condition;
+ }
+
+ public int getPromotionCondition() {
+ return this.condition;
+ }
+
+ public LocalDate getStartDate() {
+ return this.startDate;
+ }
+
+ public LocalDate getEndDate() {
+ return endDate;
+ }
+
+ @Override
+ public boolean equals(Object object) {
+ if (this == object) return true;
+ if (object == null || getClass() != object.getClass()) return false;
+ Promotion promotion = (Promotion) object;
+ return condition == promotion.condition && Objects.equals(name, promotion.name)
+ && Objects.equals(startDate, promotion.startDate)
+ && Objects.equals(endDate, promotion.endDate);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, startDate, endDate, condition);
+ }
+
+ @Override
+ public String toString() {
+ return this.name;
+ }
+
+} | Java | ํ๋ก๋ชจ์
์ด ์ค์ค๋ก ์ฒ๋ฆฌํ ์ ์๋ ๊ธฐ๋ฅ๋ค์ด ๋ ์์ ๊ฒ ๊ฐ์ต๋๋ค. getter๋ง ๊ฐ์ง๊ณ ์๋ ์ ์ด ์กฐ๊ธ ์์ฝ๋ค์. |
@@ -0,0 +1,113 @@
+package store.model.inventory;
+
+import store.model.order.OrderDetails;
+import store.model.orderquantity.OrderProductQuantity;
+
+import java.util.Map;
+
+/** ํธ์์ ์ํ ์ฌ๊ณ ๋ฅผ ๋ด๋นํ๋ ํด๋์ค */
+public class Inventory {
+
+ private final Map<Product, Stock> inventory;
+
+ public Inventory(Map<Product, Stock> inventory) {
+ this.inventory = inventory;
+ }
+
+ public boolean doNotContainsProduct(String name) {
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ String productName = entry.getKey().getName();
+ if (productName.equals(name)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public boolean exceedStockOfProductNameAndQuantity(String orderProductName, int orderQuantity) {
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ Product product = entry.getKey();
+ Stock stock = entry.getValue();
+ if (product.equalsName(orderProductName) && orderQuantity > stock.getTotal()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public Product getProduct(String name) {
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ Product product = entry.getKey();
+ String productName = product.getName();
+ if (productName.equals(name)) {
+ return product;
+ }
+ }
+ return new Product("", 0, null);
+ }
+
+ public int getPromotionStock(Product product) {
+ Stock stock = inventory.get(product);
+ return stock.getPromotion();
+ }
+
+ public int getPromotionStockShortage(Product product, int totalQuantity) {
+ int promotionStock = this.getPromotionStock(product);
+ int promotionStockShortage = totalQuantity - promotionStock;
+ return Math.max(0, promotionStockShortage);
+ }
+
+ public void setInventoryDeductionByOrder(OrderDetails details) {
+ for (Map.Entry<Product, OrderProductQuantity> entry : details.getEntrySet()) {
+ Product product = entry.getKey();
+ OrderProductQuantity saleQuantity = entry.getValue();
+ Stock stock = inventory.get(product);
+ stock.setBySale(saleQuantity);
+ }
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ Product product = entry.getKey();
+ Stock stock = entry.getValue();
+ addStringInventoryDetails(stringBuilder, product, stock);
+ }
+ return stringBuilder.toString();
+ }
+
+ private void addStringInventoryDetails(StringBuilder stringBuilder,
+ Product product, Stock stock) {
+ if (product.containsPromotion()) {
+ addStringOfProductAndBothStock(stringBuilder, product, stock);
+ }
+ if (!product.containsPromotion()) {
+ addStringOfProductAndStock(stringBuilder, product, stock);
+ }
+ }
+
+ private void addStringOfProductAndBothStock(StringBuilder stringBuilder,
+ Product product, Stock stock) {
+ stringBuilder.append(getProductAndUnitPrice(product.getName(), product.getUnitPrice()))
+ .append(stock.getPromotionString())
+ .append(" ")
+ .append(product.getPromotionName())
+ .append("\n")
+ .append(getProductAndUnitPrice(product.getName(), product.getUnitPrice()))
+ .append(stock.getRegularString())
+ .append("\n");
+ }
+
+ private void addStringOfProductAndStock(StringBuilder stringBuilder,
+ Product product, Stock stock) {
+ stringBuilder.append(getProductAndUnitPrice(product.getName(), product.getUnitPrice()))
+ .append(stock.getRegularString())
+ .append("\n");
+ }
+
+ private String getProductAndUnitPrice(String productName, int unitPrice) {
+ return String.format("- %s %,d์ ", productName, unitPrice);
+ }
+
+} | Java | ํฌ๋งทํ
ํ ๊ฒฐ๊ณผ๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ด ์ข์๋ณด์
๋๋ค bb |
@@ -0,0 +1,113 @@
+package store.model.inventory;
+
+import store.model.order.OrderDetails;
+import store.model.orderquantity.OrderProductQuantity;
+
+import java.util.Map;
+
+/** ํธ์์ ์ํ ์ฌ๊ณ ๋ฅผ ๋ด๋นํ๋ ํด๋์ค */
+public class Inventory {
+
+ private final Map<Product, Stock> inventory;
+
+ public Inventory(Map<Product, Stock> inventory) {
+ this.inventory = inventory;
+ }
+
+ public boolean doNotContainsProduct(String name) {
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ String productName = entry.getKey().getName();
+ if (productName.equals(name)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public boolean exceedStockOfProductNameAndQuantity(String orderProductName, int orderQuantity) {
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ Product product = entry.getKey();
+ Stock stock = entry.getValue();
+ if (product.equalsName(orderProductName) && orderQuantity > stock.getTotal()) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public Product getProduct(String name) {
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ Product product = entry.getKey();
+ String productName = product.getName();
+ if (productName.equals(name)) {
+ return product;
+ }
+ }
+ return new Product("", 0, null);
+ }
+
+ public int getPromotionStock(Product product) {
+ Stock stock = inventory.get(product);
+ return stock.getPromotion();
+ }
+
+ public int getPromotionStockShortage(Product product, int totalQuantity) {
+ int promotionStock = this.getPromotionStock(product);
+ int promotionStockShortage = totalQuantity - promotionStock;
+ return Math.max(0, promotionStockShortage);
+ }
+
+ public void setInventoryDeductionByOrder(OrderDetails details) {
+ for (Map.Entry<Product, OrderProductQuantity> entry : details.getEntrySet()) {
+ Product product = entry.getKey();
+ OrderProductQuantity saleQuantity = entry.getValue();
+ Stock stock = inventory.get(product);
+ stock.setBySale(saleQuantity);
+ }
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder stringBuilder = new StringBuilder();
+ for (Map.Entry<Product, Stock> entry : inventory.entrySet()) {
+ Product product = entry.getKey();
+ Stock stock = entry.getValue();
+ addStringInventoryDetails(stringBuilder, product, stock);
+ }
+ return stringBuilder.toString();
+ }
+
+ private void addStringInventoryDetails(StringBuilder stringBuilder,
+ Product product, Stock stock) {
+ if (product.containsPromotion()) {
+ addStringOfProductAndBothStock(stringBuilder, product, stock);
+ }
+ if (!product.containsPromotion()) {
+ addStringOfProductAndStock(stringBuilder, product, stock);
+ }
+ }
+
+ private void addStringOfProductAndBothStock(StringBuilder stringBuilder,
+ Product product, Stock stock) {
+ stringBuilder.append(getProductAndUnitPrice(product.getName(), product.getUnitPrice()))
+ .append(stock.getPromotionString())
+ .append(" ")
+ .append(product.getPromotionName())
+ .append("\n")
+ .append(getProductAndUnitPrice(product.getName(), product.getUnitPrice()))
+ .append(stock.getRegularString())
+ .append("\n");
+ }
+
+ private void addStringOfProductAndStock(StringBuilder stringBuilder,
+ Product product, Stock stock) {
+ stringBuilder.append(getProductAndUnitPrice(product.getName(), product.getUnitPrice()))
+ .append(stock.getRegularString())
+ .append("\n");
+ }
+
+ private String getProductAndUnitPrice(String productName, int unitPrice) {
+ return String.format("- %s %,d์ ", productName, unitPrice);
+ }
+
+} | Java | \n์ ์ง์ํ๋ผ๋ ์๊ฒฌ์ด ์์ด์ ๋จ๊ฒจ๋ด
๋๋ค.
https://sm-studymemo.tistory.com/94 |
@@ -0,0 +1,148 @@
+package store.model.order;
+
+import store.model.discount.MembershipDiscountDetail;
+import store.model.inventory.Product;
+import store.model.orderquantity.OrderProductQuantity;
+import store.model.discount.PromotionDiscountDetail;
+import store.model.discount.PromotionDiscountDetails;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+import static store.util.KoreanStringFormatter.CONVERT_LEFT;
+import static store.util.KoreanStringFormatter.CONVERT_RIGHT;
+import static store.util.KoreanStringFormatter.CONVERT_RIGHT_WITH_MINUS;
+
+public class Receipt {
+
+ private final List<String> receipt;
+
+ public Receipt(OrderDetails orderDetails, PromotionDiscountDetails promotionDiscountDetails,
+ MembershipDiscountDetail membershipDiscountDetail) {
+ List<String> receipt = new ArrayList<>();
+ addOrderDetails(receipt, orderDetails);
+ addPromotionDiscount(receipt, promotionDiscountDetails);
+ addSummaryPayAndPromotionDiscount(receipt, orderDetails, promotionDiscountDetails);
+ addMembershipDiscount(receipt, membershipDiscountDetail);
+ String moneyToPayLine =
+ getMoneyToPayLine(orderDetails, promotionDiscountDetails, membershipDiscountDetail);
+ receipt.add(moneyToPayLine);
+ this.receipt = receipt;
+ }
+
+ private void addOrderDetails(List<String> receipt, OrderDetails orderDetails) {
+ receipt.add(Message.HEADER);
+ receipt.add(Message.ORDER_DETAILS_HEADER);
+ for (Map.Entry<Product, OrderProductQuantity> entry : orderDetails.getEntrySet()) {
+ Product product = entry.getKey();
+ OrderProductQuantity quantity = entry.getValue();
+ int totalAmount = product.getUnitPrice() * quantity.getTotal();
+ String orderDetailLine =
+ Message.GET_ORDER_DETAILS(product.getName(), quantity.getTotal(), totalAmount);
+ receipt.add(orderDetailLine);
+ }
+ }
+
+ private String getMoneyToPayLine(OrderDetails orderDetails,
+ PromotionDiscountDetails promotionDiscountDetails,
+ MembershipDiscountDetail membershipDiscountDetail) {
+ int moneyBeforeDiscount = orderDetails.getTotalAmount();
+ int totalAmountPromotion = promotionDiscountDetails.getTotalAmount();
+ int totalAmountMembership = membershipDiscountDetail.getAmount();
+ int moneyToPay = moneyBeforeDiscount - totalAmountPromotion - totalAmountMembership;
+ return Message.GET_MONEY_TO_PAY(moneyToPay);
+ }
+
+ private void addMembershipDiscount(List<String> receipt,
+ MembershipDiscountDetail membershipDiscountDetail) {
+ int discountAmount = membershipDiscountDetail.getAmount();
+ String membershipDiscountLine = Message.GET_MEMBERSHIP_DISCOUNT_LINE(discountAmount);
+ receipt.add(membershipDiscountLine);
+ }
+
+ private void addSummaryPayAndPromotionDiscount(List<String> receipt,
+ OrderDetails orderDetails,
+ PromotionDiscountDetails discountDetails) {
+ int totalAmount = orderDetails.getTotalAmount();
+ int totalQuantity = orderDetails.getTotalQuantity();
+ int promotionAmount = discountDetails.getTotalAmount();
+ receipt.add(Message.PAY_AND_DISCOUNT_HEADER);
+ String totalAmountLine = Message.GET_TOTAL_AMOUNT(totalQuantity, totalAmount);
+ String promotionDiscountSummary = Message.GET_PROMOTION_SUMMARY(promotionAmount);
+ receipt.add(totalAmountLine);
+ receipt.add(promotionDiscountSummary);
+ }
+
+ private void addPromotionDiscount(List<String> receipt, PromotionDiscountDetails discountDetails) {
+ receipt.add(Message.PROMOTION_DETAILS_HEADER);
+ if (discountDetails.isEmpty()) {
+ receipt.add(Message.NOT_CONTAINS_PROMOTION);
+ return;
+ }
+ for (PromotionDiscountDetail detail : discountDetails) {
+ String detailLine = Message.GET_PROMOTION_DETAIL(detail.getProductName(),
+ detail.getBonusCount());
+ receipt.add(detailLine);
+ }
+ }
+
+ @Override
+ public String toString() {
+ return String.join("\n", receipt);
+ }
+
+ static final class Message {
+
+ static final int LEFT_BLANK_SIZE = 22;
+
+ static final int MIDDLE_BLANK_SIZE = 6;
+
+ static final int RIGHT_BLANK_SIZE = 10;
+
+ static final String HEADER = "==============W ํธ์์ ================";
+
+ static final String ORDER_DETAILS_HEADER = CONVERT_LEFT("์ํ๋ช
", LEFT_BLANK_SIZE)
+ + CONVERT_LEFT("์๋", MIDDLE_BLANK_SIZE)
+ + CONVERT_RIGHT("๊ธ์ก", RIGHT_BLANK_SIZE);
+
+ static final String PROMOTION_DETAILS_HEADER = "==============์ฆ ์ ===============";
+
+ static final String NOT_CONTAINS_PROMOTION = "์ฆ์ ํ ์์";
+
+ static final String PAY_AND_DISCOUNT_HEADER = "======================================";
+
+ public static String GET_ORDER_DETAILS(String name, int totalQuantity, int totalAmount) {
+ return CONVERT_LEFT(name, LEFT_BLANK_SIZE)
+ + CONVERT_LEFT(totalQuantity, MIDDLE_BLANK_SIZE)
+ + CONVERT_RIGHT(totalAmount, RIGHT_BLANK_SIZE);
+ }
+
+ public static String GET_PROMOTION_DETAIL(String productName, int bonusCount) {
+ return CONVERT_LEFT(productName, LEFT_BLANK_SIZE) + bonusCount;
+ }
+
+ public static String GET_MONEY_TO_PAY(int moneyToPay) {
+ return CONVERT_LEFT("๋ด์ค๋", LEFT_BLANK_SIZE + MIDDLE_BLANK_SIZE)
+ + CONVERT_RIGHT(moneyToPay, RIGHT_BLANK_SIZE);
+ }
+
+ public static String GET_MEMBERSHIP_DISCOUNT_LINE(int discountAmount) {
+ return CONVERT_LEFT("๋ฉค๋ฒ์ญํ ์ธ", LEFT_BLANK_SIZE + MIDDLE_BLANK_SIZE)
+ + CONVERT_RIGHT_WITH_MINUS(discountAmount, RIGHT_BLANK_SIZE);
+ }
+
+ public static String GET_TOTAL_AMOUNT(int totalQuantity, int totalAmount) {
+ return CONVERT_LEFT("์ด๊ตฌ๋งค์ก", LEFT_BLANK_SIZE)
+ + CONVERT_LEFT(totalQuantity, MIDDLE_BLANK_SIZE)
+ + CONVERT_RIGHT(totalAmount, RIGHT_BLANK_SIZE);
+ }
+
+ public static String GET_PROMOTION_SUMMARY(int promotionAmount) {
+ return CONVERT_LEFT("ํ์ฌํ ์ธ", LEFT_BLANK_SIZE + MIDDLE_BLANK_SIZE)
+ + CONVERT_RIGHT_WITH_MINUS(promotionAmount, RIGHT_BLANK_SIZE);
+ }
+
+ }
+
+}
\ No newline at end of file | Java | ๊ฐ์ธ์ ์ผ๋ก ํด๋์ค ๋ด๋ถ์ ํด๋์ค๊ฐ ์กด์ฌํ๋ ๊ฑด ๊ฐ๋
์ฑ์ด ์ข์ง ์์ ๊ฒ ๊ฐ์ต๋๋ค.
๋, Message๊ฐ ๋ฐ์ ์๋ค๋ณด๋ ํ์
ํ๋๋ฐ ์กฐ๊ธ ์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ๊ฒ ๊ฐ์ต๋๋ค. |
@@ -1 +1,78 @@
# java-convenience-store-precourse
+
+## ๊ตฌํํญ๋ชฉ
+
+### ์
์ถ๋ ฅ
+
+- ์์์ฆ
+ - ๊ตฌ๋งค ์ํ ๋ด์ญ : ๊ตฌ๋งคํ ์ํ๋ช
, ์๋, ๊ฐ๊ฒฉ
+ - ์ฆ์ ์ํ ๋ด์ญ : ํ๋ก๋ชจ์
์ ๋ฐ๋ผ ๋ฌด๋ฃ๋ก ์ ๊ณต๋ ์ฆ์ ์ํ์ ๋ชฉ๋ก
+ - ๊ธ์ก ์ ๋ณด
+ - ์ด๊ตฌ๋งค์ก: ๊ตฌ๋งคํ ์ํ์ ์ด ๊ธ์ก
+ - ์ด๊ตฌ๋งค์๋ : ๊ตฌ๋งคํ ์ํ์ ์ด ์๋
+ - ํ์ฌํ ์ธ: ํ๋ก๋ชจ์
์ ์ํด ํ ์ธ๋ ๊ธ์ก
+ - ๋ฉค๋ฒ์ญํ ์ธ: ๋ฉค๋ฒ์ญ์ ์ํด ์ถ๊ฐ๋ก ํ ์ธ๋ ๊ธ์ก(์ต๋ 8000์)
+ - ๋ด์ค๋: ์ต์ข
๊ฒฐ์ ๊ธ์ก(์ด๊ตฌ๋งค์ก์์ ํ ์ธ ๊ฐ๊ฒฉ์ ๋บธ ๊ธ์ก)
+
+ * ์์์ฆ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌ
+ * ์๋ ์์ ์ "์ฌ๊ณ ์์" ์ถ๋ ฅ
+ * ํ๋ก๋ชจ์
์ํ์ด ์๋ ๊ฒฝ์ฐ ๋์ผํ ์ ํ๋ช
์ ์ผ๋ฐ ์ํ ์ฌ๊ณ ๊ฐ ์์ผ๋ฉด ์ผ๋ฐ์ํ์๋ "์ฌ๊ณ ์์" ์ถ๋ ฅ
+- ์ํ์
๋ ฅ
+ - ์ํ ์
๋ ฅ ํ์์ด [์ ํ๋ช
:๊ฐฏ์] ๋ก ๋ค์ด์๋์ง ํ์ธ
+
+### ์ํ
+
+* ์ํ promotion์ด "null" string์ผ๋ก ๋ค์ด์ฌ ๊ฒฝ์ฐ ๋น ๊ฐ์ผ๋ก ์ฒ๋ฆฌ
+
+### ์ฌ๊ณ ๊ด๋ฆฌ
+
+* ์ฌ๊ณ ์๋ ํ์
ํ์ฌ ๊ฒฐ์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
+* ์ํ ๊ตฌ๋งค ์ ์ฌ๊ณ ์ฐจ๊ฐ
+
+### ํ๋ก๋ชจ์
(n๊ฐ ๊ตฌ๋งค ์ 1๊ฐ ๋ฌด๋ฃ ์ฆ์ )
+
+* ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ์ด๋ด์ธ์ง ํ์ธ
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ์๋์ง ํ์ธ(์ฌ๊ณ ์์ ์ ํ๋ก๋ชจ์
์ ์ฉ ๋ถ๊ฐ)
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฐจ๊ฐ
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ผ๋ ํ๋ก๋ชจ์
์กฐ๊ฑด ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ๊ฒฝ์ฐ
+ ์ถ๊ฐ ํ์์๋ ์๋ด
+ * Y ์
๋ ฅ ์ ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ ์ถ๊ฐ
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ์ผ๋ก ์ผ๋ถ์๋์ ํํ ์์ด ๊ฒฐ์ ์ ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ ์๋ด
+ * ex)ํ์ฌ {์ํ๋ช
} {์๋}๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+ * N ์
๋ ฅ ์ ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผํ๋ ์๋๋งํผ ์ ์ธ ํ ๊ฒฐ์ ์งํ
+* ํ๋ก๋ชจ์
์ํ์ ๊ตฌ๋งค๋์ ํ์
ํ์ฌ ์๊ฐ์ ธ์จ ๋ฌด๋ฃ์๋์ ๊ณ์ฐํ๋ ํจ์ ex) 2+1์ผ๋ 5๊ฐ ์ฌ๋ฉด 1๊ฐ ๋ฌด๋ฃ๊ตฌ๋งค ๊ฐ๋ฅ
+
+### ๋ฉค๋ฒ์ญ
+
+* ๋ฉค๋ฒ์ญ ํ์์ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30% ํ ์ธ
+* ํ๋ก๋ชจ์
์ ์ฉ ํ ํ๋ก๋ชจ์
์ ์ฉ์ด ๋์ง์์ ์ ํ ๊ฒฐ์ ์ก์ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ต๋ ํ๋๋ 8000์
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ฌ๋ถ ๋ฌผ์ด๋ณด๊ณ Y ์ธ ๊ฒฝ์ฐ์๋ง ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ
+
+### ๊ธฐํ
+
+* mdํ์ผ ์ฝ์ด์ค๋ ๊ธฐ๋ฅ(์ํ ๋ชฉ๋ก, ํ์ฌ ๋ชฉ๋ก)
+* ์คํจํ ๊ฒฝ์ฐ ์คํจ๋ฅผ ํ ํด๋น ์ง๋ฌธ๋ถํฐ ์ฌ์์ํ๋ ๊ธฐ๋ฅ
+* ์ํ ๊ตฌ๋งค๋ถํฐ ์์์ฆ ์ถ๋ ฅ๊น์ง(ํ๋ก๊ทธ๋จ 1์ฌ์ดํด) ์ํ ํ ์ฌ๊ตฌ๋งค์ํ ์ ์ ํ์ถ๋ ฅ๋ถํฐ ์ฌ์์ํ๋ ๊ธฐ๋ฅ
+
+### ์์ธ์ฒ๋ฆฌ
+
+* ํ์ : ์๋ฌ๋ [ERROR]๋ก ์์
+* ์ํ ์
๋ ฅ ํ์์ด [์ ํ๋ช
:๊ฐฏ์] ํ์์ผ๋ก ๋ค์ด์ค์ง ์๋ ๊ฒฝ์ฐ
+* ์ฌ๋ฌ๊ฐ์ผ ๊ฒฝ์ฐ ,๋ก ๊ตฌ๋ถ๋์ด ๋ค์ด์ค์ง ์๋ ๊ฒฝ์ฐ
+* ๊ฐฏ์๊ฐ ์์ฐ์๊ฐ์ด ์๋ ๊ฒฝ์ฐ
+* ์ํ๋ช
์ด ์ค๋ณต์ผ๋ก ๋ค์ด์ค๋ ๊ฒฝ์ฐ
+* ์
๋ ฅํ ์ํ๋ช
์ด ์ค์ ์ํ์ ์๋ ์ด๋ฆ์ผ ๊ฒฝ์ฐ
+* ๊ฐฏ์๊ฐ int ๋ฒ์ ์ด๋ด๊ฐ ์๋ ๊ฒฝ์ฐ
+* Y/N์ผ๋ก ์
๋ ฅ๋ฐ๋ ์ง๋ฌธ์ ๊ฒฝ์ฐ Y/N์ด ์๋ ๊ฒฝ์ฐ
+* ์ ํ์ ์ฌ๊ณ ์๋๋ณด๋ค ๊ตฌ์
์๋์ผ๋ก ๋ ํฐ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ
+
+## ํ๋ก๊ทธ๋จ ์๊ตฌ์ฌํญ
+
+* else ์ฌ์ฉ ๊ธ์ง
+* enum ์ ์ฉ
+* ๋จ์ ํ
์คํธ ์์ฑ
+* ํจ์ ๊ธธ์ด 10๋ผ์ธ ์ดํ
+* ์
์ถ๋ ฅ ๋ณ๋ ํด๋์ค ๋ถ๋ฆฌ (InputView, OutputView)
+* ํ์ฌ ๋ ์ง์ ์๊ฐ -> DateTimes.now() ํ์ฉ
+* ์ฌ์ฉ์ ์
๋ ฅ -> Console.readLine() ํ์ฉ
\ No newline at end of file | Unknown | ๊ผผ๊ผผํ ์์ธ ์ฒ๋ฆฌ ์ ๋ฆฌ๊ฐ ์ธ์์ ์
๋๋ค!
README๋ฅผ ์ ์ ๋ฆฌํ์ ๊ฑฐ ๊ฐ์์! |
@@ -0,0 +1,21 @@
+package util;
+
+import exception.Exception;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class ProductValidator {
+
+ private static final String PRODUCT_PATTERN = "^\\[([a-zA-Z๊ฐ-ํฃ]+)-([1-9]\\d*)\\]$";
+ public static final String INVALID_FORMAT = "์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ public static boolean isValidProductFormat(String input) {
+ Pattern pattern = Pattern.compile(PRODUCT_PATTERN);
+ Matcher matcher = pattern.matcher(input);
+ boolean result = matcher.matches();
+ if (!result) {
+ Exception.throwException(INVALID_FORMAT);
+ }
+ return result;
+ }
+} | Java | ์ฌ๋ฐ๋ฅด์ง ์์ ๊ฒฝ์ฐ์๋ ์์ธ๋ฅผ ๋ฐ์์ํค๋๋ฐ, ๋ฐํ๊ฐ์ boolean์ผ๋ก ์ค์ ํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | ์ด ๋ถ๋ถ์ ๊ตฌ๋งค ์๋์ด ์ซ์๊ฐ ์๋ ๋ ์์ธ๋ฅผ ๋ฐ์์ํค๋ ๋ถ๋ถ ๊ฐ์์!
์ด ๋ถ๋ถ์์๋ ์ฌ๊ณ ์๋ ์ด๊ณผ์๋ ๊ด๊ณ๊ฐ ์๋๋ฐ, ์์ธ ๋ฉ์์ง `์ฌ๊ณ ์๋์ ์ด๊ณผํ์ฌ ๊ตฌ๋งคํ ์ ์์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.`๋ฅผ ์ฌ์ฉํ์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | ํ์ผ ์ฝ์ด์ค๋ ๋ถ๋ถ์์ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ์ ๋ถ๋ถ์ด ์ธ์์ ์ด๋ค์!
๋์ค์ ์ ์ง๋ณด์์ ์ข์ ๊ฑฐ ๊ฐ์์!
๊ฐ์ธ์ ์ผ๋ก, `InputView`๋ ์ฌ์ฉ์ ์
๋ ฅ์์๋ง ์ฌ์ฉํ๋ ํธ์ธ๋ฐ
ํ์ผ ์ฝ์ด์ค๋ ๋ถ๋ถ์ `InputView`์ ๋ง๋์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค! |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | stream์ ์ ํ์ฉํ์ ๋ถ๋ถ์ด ์ธ์์ ์ด๋ค์! |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | ์ ๋ `LocalDate.parse`์ ๋ ๋ฒ์งธ ์ธ์๋ก ํฌ๋งทํฐ๊น์ง ๋ฃ์ด ์ฌ์ฉํ๋๋ฐ
๊ธฐ๋ณธ์ ์ผ๋ก ์๊ตฌ ์ฌํญ์ ๋ ์ง ํ์๊ณผ ๊ฐ์์ ํฌ๋งทํฐ๋ฅผ ๋ฃ์ ํ์๊ฐ ์์๊ตฐ์!
ํจ์ฌ ๊น๋ํ๊ฒ ์์ฑํ์
จ๋ค์! |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ์ ๋ง `map`์ ์ ์ฌ์ฉํ์ ๊ฑฐ ๊ฐ์์! |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ์ฌ์ํ ๋ถ๋ถ์ด์ง๋ง, `makePrintProduct`๋ ์๋ฏธ๋ฅผ ํ์คํ ๋ํ๋ด๊ธฐ ์ด๋ ค์ด ๊ฑฐ ๊ฐ์์!
`makeProductOnlyInPromotion`๊ฐ์ ๋ฉ์๋๋ช
์ ์ด๋จ๊น์? |
@@ -0,0 +1,71 @@
+package store;
+
+import view.OutputView;
+
+public class Product {
+
+ private static final String NO_STACK = "์ฌ๊ณ ์์";
+ private static final String NUMBER = "๊ฐ";
+ private static final String WON = "์ ";
+
+ private final String name;
+ private int price;
+ private int quantity;
+ private String promotion;
+
+ public Product(String name, int quantity) {
+ this.name = name;
+ this.quantity = quantity;
+ }
+
+ public Product(String name, int price, int quantity) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ }
+
+ public Product(String name, int price, int quantity, String promotion) {
+ this.name = name;
+ this.price = price;
+ this.quantity = quantity;
+ this.promotion = promotion;
+ }
+
+ public void print() {
+ String product = "- " + name + " " + String.format("%,d", price) + WON;
+ String count = quantity + NUMBER;
+ if (quantity == 0) {
+ count = NO_STACK;
+ }
+ String promotion = this.promotion;
+ if (promotion == null) {
+ promotion = "";
+ }
+ OutputView.printMessage(product + count + " " + promotion);
+ }
+
+ public void addQuantity(int quantity) {
+ this.quantity += quantity;
+ }
+
+ public void subtractQuantity(int quantity) {
+ this.quantity -= quantity;
+ }
+
+
+ public String getName() {
+ return name;
+ }
+
+ public String getPromotion() {
+ return promotion;
+ }
+
+ public int getPrice() {
+ return price;
+ }
+
+ public int getQuantity() {
+ return quantity;
+ }
+} | Java | ์ ๋ MVCํจํด์ ๋ํด์ ์ ๋ชจ๋ฅด์ง๋ง, ์ถ๋ ฅ ๋ก์ง์ด ๋ชจ๋ธ์ ํฌํจ๋์ด ์๋ ๊ฑฐ ๊ฐ์์!
์ถ๋ ฅ ๋ก์ง์ OutputView์์ ๊ด๋ฆฌํ์๋ ๊ฒ ์ด๋จ๊น์? |
@@ -0,0 +1,142 @@
+package store;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+class BuyerTest {
+
+ private Buyer buyer;
+
+ @BeforeEach
+ public void setUp() {
+ buyer = new Buyer();
+ buyer.initializationReceiptMap();
+ }
+
+ @Test
+ @DisplayName("ํ๋ก๋ชจ์
_๋ด์ฉ์ด_์๋_์ ํ์_๋ฌด์กฐ๊ฑด_products๊ฐ_๊ฐ์ง๊ณ ์๋_๋ฆฌ์คํธ์_์ฌ์ด์ฆ๊ฐ_2๋ค")
+ void ํ๋ก๋ชจ์
_๋ด์ฉ์ด_์๋_์ ํ์_๋ฌด์กฐ๊ฑด_products๊ฐ_๊ฐ์ง๊ณ ์๋_๋ฆฌ์คํธ์_์ฌ์ด์ฆ๊ฐ_2๋ค() {
+ LinkedHashMap<String, List<Product>> products = buyer.getProducts();
+ for (String category : products.keySet()) {
+ List<Product> productList = products.get(category);
+
+ if (productList.getFirst().getPromotion() != null) {
+ assertThat(productList.size()).isEqualTo(2);
+ }
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"์ปฌ๋ผ", "์ธ์ด๋ค", "๋ด์ง๋ผ๋ฉด", "ํ๋ถ"})
+ @DisplayName("์ ํ๋ชฉ๋ก์ ์กด์ฌํ์ง ์๋ ์ ํ๋ช
์ ์
๋ ฅ ์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.")
+ void ์ ํ๋ชฉ๋ก์_์กด์ฌํ์ง_์๋_์ ํ๋ช
์_์
๋ ฅ_์_์๋ฌ๊ฐ_๋ฐ์ํ๋ค(String productName) {
+ List<Product> wantBuyProducts = new ArrayList<>();
+ wantBuyProducts.add(new Product(productName, 10));
+
+ assertThatThrownBy(() -> buyer.productNameCheck(wantBuyProducts))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {1, 2, 3, 4, 5, 6, 7})
+ @DisplayName("๊ตฌ๋งคํ_๊ฐฏ์๋งํผ_์ค์ _products_๊ฐ์ฒด์์_์๋_๊ฐ์ํ๋์ง_ํ์ธ")
+ void ๊ตฌ๋งคํ_๊ฐฏ์๋งํผ_์ค์ _products_๊ฐ์ฒด์์_์๋_๊ฐ์ํ๋์ง_ํ์ธ(int buyCount) {
+ //ํ์ฌ ์ด๊ธฐ์ ์ค์ ๋ ์ฝ๋ผ ๊ฐฏ์๋ 10๊ฐ
+ int canPromotionCokeSize = buyer.getProducts().get("์ฝ๋ผ").getFirst().getQuantity();
+ List<Product> wantBuyProducts = new ArrayList<>();
+ wantBuyProducts.add(new Product("์ฝ๋ผ", buyCount));
+
+ buyer.realBuyProducts(wantBuyProducts);
+
+ assertThat(canPromotionCokeSize - buyCount).isEqualTo(
+ buyer.getProducts().get("์ฝ๋ผ").getFirst().getQuantity());
+
+ }
+
+ @Test
+ @DisplayName("๊ตฌ๋งค๋ฅผ_์ํ๋_๊ฐฏ์๊ฐ_๊ตฌ๋งค๊ฐ๋ฅ๊ฐฏ์์ดํ๋ฉด_true_๋์ผ๋ฉด_false๋ฐํ_ํ์ธ")
+ void ๊ตฌ๋งค๋ฅผ_์ํ๋_๊ฐฏ์๊ฐ_๊ตฌ๋งค๊ฐ๋ฅ๊ฐฏ์์ดํ๋ฉด_true_๋์ผ๋ฉด_false๋ฐํ_ํ์ธ() {
+ int wantBuyQuantity = 10;
+ Product product = new Product("์ฝ๋ผ", 9);
+ Product exceedProduct = new Product("์ฝ๋ผ", 11);
+
+ assertThat(buyer.withinQuantity(product, wantBuyQuantity)).isEqualTo(false);
+ assertThat(buyer.withinQuantity(exceedProduct, wantBuyQuantity)).isEqualTo(true);
+ }
+
+ @Test
+ void ๊ตฌ๋งคํ_์ ํ์_์ด์๋์_๊ตฌํ๋ค() {
+ List<Product> wantBuyProducts = new ArrayList<>();
+ int cokeCount = 10;
+ int spriteCount = 7;
+ wantBuyProducts.add(new Product("์ฝ๋ผ", cokeCount));
+ wantBuyProducts.add(new Product("์ฌ์ด๋ค", spriteCount));
+
+ assertThat(buyer.calculateTotalBuyCount(wantBuyProducts)).isEqualTo(cokeCount + spriteCount);
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = {21, 100, 22222222})
+ @DisplayName("์ค์ _๊ตฌ๋งค_๊ฐ๋ฅํ_๊ฐฏ์๊ฐ_์๋๋ฉด_์๋ฌ_๋ฐ์์ฌ๋ถ_ํ์ธ")
+ void ์ค์ _๊ตฌ๋งค_๊ฐ๋ฅํ_๊ฐฏ์๊ฐ_์๋๋ฉด_์๋ฌ_๋ฐ์์ฌ๋ถ_ํ์ธ(int buyCount) {
+ //์ฝ๋ผ ์ด ๊ฐฏ์๋ 20๊ฐ
+ List<Product> wantBuyProducts = new ArrayList<>();
+ wantBuyProducts.add(new Product("์ฝ๋ผ", buyCount));
+
+ assertThatThrownBy(() -> buyer.checkCanBuyQuantity(wantBuyProducts))
+ .isInstanceOf(IllegalArgumentException.class);
+ }
+
+
+ @Test
+ @DisplayName("ํ๋ก๋ชจ์
_์ ํ์ด_์ฌ๊ณ ๊ฐ_๋ถ์กฑํ ๋_๋ช๊ฐ_๋ถ์กฑํ์ง_์ ์์ ์ผ๋ก_๋ฐํํ๋์ง_ํ์ธ")
+ void ํ๋ก๋ชจ์
_์ ํ์ด_์ฌ๊ณ ๊ฐ_๋ถ์กฑํ ๋_๋ช๊ฐ_๋ถ์กฑํ์ง_์ ์์ ์ผ๋ก_๋ฐํํ๋์ง_ํ์ธ() {
+ int cokeCount = 12; //์ฝ๋ผ๋ 2+1 ํ์ฌ์ค / ํ์ฌ์ ํ ๊ตฌ๋งค๊ฐ๋ฅ ๊ฐ์ = 9
+ int instantCupRamenCount = 2; //์ปต๋ผ๋ฉด์ 1+1 ํ์ฌ์ค / ํ์ฌ์ ํ ๊ตฌ๋งค๊ฐ๋ฅ ๊ฐ์ = 1
+
+ assertThat(buyer.calculateNotPromotionCount(new Product("์ฝ๋ผ", cokeCount))).isEqualTo(3);
+ assertThat(buyer.calculateNotPromotionCount(new Product("์ปต๋ผ๋ฉด", instantCupRamenCount))).isEqualTo(2);
+ }
+
+ @Test
+ @DisplayName("ํ๋ก๋ชจ์
์ผ๋ก_๋ฌด๋ฃ๊ตฌ๋งค๋๋_์ ํ์_๊ฐฏ์๋ฅผ_๊ตฌํ๋ค / 2+1์ 3์ธํธ ๊ตฌ๋งคํ๋ฉด ๋ฌด๋ฃ๋ก ๋ฐ๋๊ฑด 3๊ฐ")
+ void ํ๋ก๋ชจ์
์ผ๋ก_๋ฌด๋ฃ๊ตฌ๋งค๋๋_์ ํ์_๊ฐฏ์๋ฅผ_๊ตฌํ๋ค() {
+ int cokeCount = 10; //์ฝ๋ผ๋ 2+1 ํ์ฌ์ค / ํ์ฌ์ ํ ๊ตฌ๋งค๊ฐ๋ฅ ๊ฐ์ = 9 -> ์ฆ 3์ธํธ์ด๋ฏ๋ก 3๊ฐ ๋ฐํ
+ int instantCupRamenCount = 10; //์ปต๋ผ๋ฉด์ 1+1 ํ์ฌ์ค / ํ์ฌ์ ํ ๊ตฌ๋งค๊ฐ๋ฅ ๊ฐ์ = 1 ์ฆ 1+1๋ ์๋๋ค. 0๊ฐ ๋ฐํ
+
+ assertThat(buyer.calculatePromotionCount(new Product("์ฝ๋ผ", cokeCount))).isEqualTo(3);
+ assertThat(buyer.calculatePromotionCount(new Product("์ปต๋ผ๋ฉด", instantCupRamenCount))).isEqualTo(0);
+ }
+
+ @Test
+ @DisplayName("ํ๋ก๋ชจ์
_ํ์ธํธ(ํ๋ฌถ์)์_๋ค์ด๊ฐ๋_์ ํ๊ฐฏ์๋ฅผ_๊ตฌํ๋ค / 2+1์ด๋ฉด ํ ์ธํธ์ 3 / 1+1์ด๋ฉด ํ ์ธํธ์ 2")
+ void ํ๋ก๋ชจ์
_์ธํธ์_๊ฐฏ์๋ฅผ_๊ตฌํ๋ค() {
+ String product1 = "์ฝ๋ผ"; // 2+1 ํ์ฌ์ค
+ String product2 = "์ฌ์ด๋ค"; // 2+1 ํ์ฌ์ค
+ String product3 = "์ปต๋ผ๋ฉด"; // 1+1 ํ์ฌ์ค
+
+ assertThat(buyer.getPromotionSetSize(product1)).isEqualTo(3);
+ assertThat(buyer.getPromotionSetSize(product2)).isEqualTo(3);
+ assertThat(buyer.getPromotionSetSize(product3)).isEqualTo(2);
+ }
+
+ @Test
+ @DisplayName("์๊ฐ์ ธ์จ_ํ๋ก๋ชจ์
_์ ํ์_๊ฐฏ์๋ฅผ_๊ตฌํ๋ค / 2+1์ผ๋ 5๊ฐ๋ฅผ ์ฌ๋ฉด 1๊ฐ๋ฅผ ๋ฌด๋ฃ๋ก ๊ฐ์ ธ์ฌ ์ ์์")
+ void ์๊ฐ์ ธ์จ_ํ๋ก๋ชจ์
_์ ํ์_๊ฐฏ์๋ฅผ_๊ตฌํ๋ค() {
+ assertThat(buyer.calculateBonus(2, 1, 3)).isEqualTo(0);
+ assertThat(buyer.calculateBonus(2, 1, 5)).isEqualTo(1);
+ assertThat(buyer.calculateBonus(1, 1, 3)).isEqualTo(1);
+ assertThat(buyer.calculateBonus(2, 1, 2)).isEqualTo(1);
+ assertThat(buyer.calculateBonus(1, 1, 4)).isEqualTo(0);
+ }
+
+}
\ No newline at end of file | Java | ๊ผผ๊ผผํ ํ
์คํธ๊ฐ ์ธ์์ ์ด๋ค์!
์ฒ์ assert๊ฐ ์คํจํ๋ฉด, ์๋์ ์๋ assert๋ ํ์ธ์ ๋ชป ํ ๊ฑฐ ๊ฐ์์!
`assertAll`ํจ์์ ๋ํด์๋ ์์๋ณด์๋ฉด ์ข์ ๊ฑฐ ๊ฐ์์! |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | while-true์ ์ฌ๊ท๋ฅผ ๊ฐ์ด ์ฌ์ฉํ์ ์ด์ ๊ฐ ์์๊น์?
ํ๋๋ง ์์ด๋ ๋ ๊ฑฐ ๊ฐ์์! |
@@ -1 +1,78 @@
# java-convenience-store-precourse
+
+## ๊ตฌํํญ๋ชฉ
+
+### ์
์ถ๋ ฅ
+
+- ์์์ฆ
+ - ๊ตฌ๋งค ์ํ ๋ด์ญ : ๊ตฌ๋งคํ ์ํ๋ช
, ์๋, ๊ฐ๊ฒฉ
+ - ์ฆ์ ์ํ ๋ด์ญ : ํ๋ก๋ชจ์
์ ๋ฐ๋ผ ๋ฌด๋ฃ๋ก ์ ๊ณต๋ ์ฆ์ ์ํ์ ๋ชฉ๋ก
+ - ๊ธ์ก ์ ๋ณด
+ - ์ด๊ตฌ๋งค์ก: ๊ตฌ๋งคํ ์ํ์ ์ด ๊ธ์ก
+ - ์ด๊ตฌ๋งค์๋ : ๊ตฌ๋งคํ ์ํ์ ์ด ์๋
+ - ํ์ฌํ ์ธ: ํ๋ก๋ชจ์
์ ์ํด ํ ์ธ๋ ๊ธ์ก
+ - ๋ฉค๋ฒ์ญํ ์ธ: ๋ฉค๋ฒ์ญ์ ์ํด ์ถ๊ฐ๋ก ํ ์ธ๋ ๊ธ์ก(์ต๋ 8000์)
+ - ๋ด์ค๋: ์ต์ข
๊ฒฐ์ ๊ธ์ก(์ด๊ตฌ๋งค์ก์์ ํ ์ธ ๊ฐ๊ฒฉ์ ๋บธ ๊ธ์ก)
+
+ * ์์์ฆ์ ๊ตฌ์ฑ ์์๋ฅผ ๋ณด๊ธฐ ์ข๊ฒ ์ ๋ ฌ
+ * ์๋ ์์ ์ "์ฌ๊ณ ์์" ์ถ๋ ฅ
+ * ํ๋ก๋ชจ์
์ํ์ด ์๋ ๊ฒฝ์ฐ ๋์ผํ ์ ํ๋ช
์ ์ผ๋ฐ ์ํ ์ฌ๊ณ ๊ฐ ์์ผ๋ฉด ์ผ๋ฐ์ํ์๋ "์ฌ๊ณ ์์" ์ถ๋ ฅ
+- ์ํ์
๋ ฅ
+ - ์ํ ์
๋ ฅ ํ์์ด [์ ํ๋ช
:๊ฐฏ์] ๋ก ๋ค์ด์๋์ง ํ์ธ
+
+### ์ํ
+
+* ์ํ promotion์ด "null" string์ผ๋ก ๋ค์ด์ฌ ๊ฒฝ์ฐ ๋น ๊ฐ์ผ๋ก ์ฒ๋ฆฌ
+
+### ์ฌ๊ณ ๊ด๋ฆฌ
+
+* ์ฌ๊ณ ์๋ ํ์
ํ์ฌ ๊ฒฐ์ ๊ฐ๋ฅ ์ฌ๋ถ ํ์ธ
+* ์ํ ๊ตฌ๋งค ์ ์ฌ๊ณ ์ฐจ๊ฐ
+
+### ํ๋ก๋ชจ์
(n๊ฐ ๊ตฌ๋งค ์ 1๊ฐ ๋ฌด๋ฃ ์ฆ์ )
+
+* ์ค๋ ๋ ์ง๊ฐ ํ๋ก๋ชจ์
๊ธฐ๊ฐ ์ด๋ด์ธ์ง ํ์ธ
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๊ฐ ์๋์ง ํ์ธ(์ฌ๊ณ ์์ ์ ํ๋ก๋ชจ์
์ ์ฉ ๋ถ๊ฐ)
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๋ฅผ ์ฐ์ ์ ์ผ๋ก ์ฐจ๊ฐ
+* ํ๋ก๋ชจ์
์ ์ฉ์ด ๊ฐ๋ฅํ ์ํ์ผ๋ ํ๋ก๋ชจ์
์กฐ๊ฑด ์๋๋ณด๋ค ์ ๊ฒ ๊ฐ์ ธ์จ๊ฒฝ์ฐ
+ ์ถ๊ฐ ํ์์๋ ์๋ด
+ * Y ์
๋ ฅ ์ ์ฆ์ ๋ฐ์ ์ ์๋ ์ํ ์ถ๊ฐ
+* ํ๋ก๋ชจ์
์ฌ๊ณ ๋ถ์กฑ์ผ๋ก ์ผ๋ถ์๋์ ํํ ์์ด ๊ฒฐ์ ์ ์ ๊ฐ๋ก ๊ฒฐ์ ํ ์ง ์ฌ๋ถ ์๋ด
+ * ex)ํ์ฌ {์ํ๋ช
} {์๋}๊ฐ๋ ํ๋ก๋ชจ์
ํ ์ธ์ด ์ ์ฉ๋์ง ์์ต๋๋ค. ๊ทธ๋๋ ๊ตฌ๋งคํ์๊ฒ ์ต๋๊น? (Y/N)
+ * N ์
๋ ฅ ์ ์ ๊ฐ๋ก ๊ฒฐ์ ํด์ผํ๋ ์๋๋งํผ ์ ์ธ ํ ๊ฒฐ์ ์งํ
+* ํ๋ก๋ชจ์
์ํ์ ๊ตฌ๋งค๋์ ํ์
ํ์ฌ ์๊ฐ์ ธ์จ ๋ฌด๋ฃ์๋์ ๊ณ์ฐํ๋ ํจ์ ex) 2+1์ผ๋ 5๊ฐ ์ฌ๋ฉด 1๊ฐ ๋ฌด๋ฃ๊ตฌ๋งค ๊ฐ๋ฅ
+
+### ๋ฉค๋ฒ์ญ
+
+* ๋ฉค๋ฒ์ญ ํ์์ ํ๋ก๋ชจ์
๋ฏธ์ ์ฉ ๊ธ์ก์ 30% ํ ์ธ
+* ํ๋ก๋ชจ์
์ ์ฉ ํ ํ๋ก๋ชจ์
์ ์ฉ์ด ๋์ง์์ ์ ํ ๊ฒฐ์ ์ก์ ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ต๋ ํ๋๋ 8000์
+* ๋ฉค๋ฒ์ญ ํ ์ธ ์ฌ๋ถ ๋ฌผ์ด๋ณด๊ณ Y ์ธ ๊ฒฝ์ฐ์๋ง ๋ฉค๋ฒ์ญ ํ ์ธ ์ ์ฉ
+
+### ๊ธฐํ
+
+* mdํ์ผ ์ฝ์ด์ค๋ ๊ธฐ๋ฅ(์ํ ๋ชฉ๋ก, ํ์ฌ ๋ชฉ๋ก)
+* ์คํจํ ๊ฒฝ์ฐ ์คํจ๋ฅผ ํ ํด๋น ์ง๋ฌธ๋ถํฐ ์ฌ์์ํ๋ ๊ธฐ๋ฅ
+* ์ํ ๊ตฌ๋งค๋ถํฐ ์์์ฆ ์ถ๋ ฅ๊น์ง(ํ๋ก๊ทธ๋จ 1์ฌ์ดํด) ์ํ ํ ์ฌ๊ตฌ๋งค์ํ ์ ์ ํ์ถ๋ ฅ๋ถํฐ ์ฌ์์ํ๋ ๊ธฐ๋ฅ
+
+### ์์ธ์ฒ๋ฆฌ
+
+* ํ์ : ์๋ฌ๋ [ERROR]๋ก ์์
+* ์ํ ์
๋ ฅ ํ์์ด [์ ํ๋ช
:๊ฐฏ์] ํ์์ผ๋ก ๋ค์ด์ค์ง ์๋ ๊ฒฝ์ฐ
+* ์ฌ๋ฌ๊ฐ์ผ ๊ฒฝ์ฐ ,๋ก ๊ตฌ๋ถ๋์ด ๋ค์ด์ค์ง ์๋ ๊ฒฝ์ฐ
+* ๊ฐฏ์๊ฐ ์์ฐ์๊ฐ์ด ์๋ ๊ฒฝ์ฐ
+* ์ํ๋ช
์ด ์ค๋ณต์ผ๋ก ๋ค์ด์ค๋ ๊ฒฝ์ฐ
+* ์
๋ ฅํ ์ํ๋ช
์ด ์ค์ ์ํ์ ์๋ ์ด๋ฆ์ผ ๊ฒฝ์ฐ
+* ๊ฐฏ์๊ฐ int ๋ฒ์ ์ด๋ด๊ฐ ์๋ ๊ฒฝ์ฐ
+* Y/N์ผ๋ก ์
๋ ฅ๋ฐ๋ ์ง๋ฌธ์ ๊ฒฝ์ฐ Y/N์ด ์๋ ๊ฒฝ์ฐ
+* ์ ํ์ ์ฌ๊ณ ์๋๋ณด๋ค ๊ตฌ์
์๋์ผ๋ก ๋ ํฐ ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ
+
+## ํ๋ก๊ทธ๋จ ์๊ตฌ์ฌํญ
+
+* else ์ฌ์ฉ ๊ธ์ง
+* enum ์ ์ฉ
+* ๋จ์ ํ
์คํธ ์์ฑ
+* ํจ์ ๊ธธ์ด 10๋ผ์ธ ์ดํ
+* ์
์ถ๋ ฅ ๋ณ๋ ํด๋์ค ๋ถ๋ฆฌ (InputView, OutputView)
+* ํ์ฌ ๋ ์ง์ ์๊ฐ -> DateTimes.now() ํ์ฉ
+* ์ฌ์ฉ์ ์
๋ ฅ -> Console.readLine() ํ์ฉ
\ No newline at end of file | Unknown | ๊ฐ์ฌํฉ๋๋ค^&^ |
@@ -0,0 +1,21 @@
+package util;
+
+import exception.Exception;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class ProductValidator {
+
+ private static final String PRODUCT_PATTERN = "^\\[([a-zA-Z๊ฐ-ํฃ]+)-([1-9]\\d*)\\]$";
+ public static final String INVALID_FORMAT = "์ฌ๋ฐ๋ฅด์ง ์์ ํ์์ผ๋ก ์
๋ ฅํ์ต๋๋ค. ๋ค์ ์
๋ ฅํด ์ฃผ์ธ์.";
+
+ public static boolean isValidProductFormat(String input) {
+ Pattern pattern = Pattern.compile(PRODUCT_PATTERN);
+ Matcher matcher = pattern.matcher(input);
+ boolean result = matcher.matches();
+ if (!result) {
+ Exception.throwException(INVALID_FORMAT);
+ }
+ return result;
+ }
+} | Java | ์ค ์ด๋ถ๋ถ์ ์ฌ์ค ์์ธ๊ฐ ๋ฐ์ํ๋ฉด return๋ฌธ์ด ํ์์์ง๋ง
์์ธ๊ฐ ๋ฐ์ํ์ง ์์ผ๋ฉด ๋ฐํํด์ฃผ๋๊ฒ์ด ํ์ํ ๊ฒ ๊ฐ์์ ๋ฃ์ด๋์์ต๋๋ค.
์์ฒญ ์๋ฆฌํ์๋ค์...ใ
ใ
์๊ฐ์ ํด๋ณด๋ ๋ฐํ๊ฐ์ ์์ ๋ ๋ฐฉํฅ์ด ๋ ์ข์๋ณด์
๋๋ค!!
๋ง์ ๊ฐ์ฌํฉ๋๋ค |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | ์ ๊ฐ ์๊ฐํ๋๊ฒ์
๋ก์ง์ ์ ๋ถ๋ถ์ ๋ค์ด๊ฐ๊ฒ์ ์ซ์๋ง ๊ฐ๋ฅํ๋ฐ(์์์ ์ ๊ท์์ผ๋ก ๊ฑฐ๋ฅด๊ณ ์์)
Integer.parseInt์์ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค๋ฉด ์ซ์์ด๊ธฐ๋ ์ซ์์ธ๋ฐ
int๊ฐ์ ๋ฒ์ด๋๋ ์ซ์๋ผ๊ณ ์๊ฐํ๊ธฐ ๋๋ฌธ์
๋๋ค!!
์์ธ์ฒ๋ฆฌ๊ฐ ์ ํํ์ง ๋ชปํ๋ ๊ฒ ๊ฐ๋ค์!!
๋ง์ ๊ฐ์ฌํฉ๋๋ค! |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | ๋ฌด์์ธ๊ฐ ์ฝ์ด์ค๊ฑฐ๋ ์
๋ ฅ์ ๋ฐ๊ฑฐ๋ ํ๋ ๋ถ๋ถ์
๋ค inputView์์ ์ฒ๋ฆฌํ๋๊ฒ ์ข๋ค๊ณ ์๊ฐํ์ต๋๋ค!!
์ง๊ธ ์๊ฐํด๋ณด๋ ๋ฐ๋ก ๋ถ๋ฆฌํด๋ ๊ด์ฐฎ๊ฒ ๋ค์!
๋ง์ ๊ฐ์ฌํฉ๋๋ค!! |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | ใ
ใ
๊ฐ์ฌํฉ๋๋ค!! ํญํ๊ฒ์์ ํ์ .. |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ๊ฐ์ฌํฉ๋๋ค!! ์ด๊ฒ๋ ํญํ๊ฒ์์ ํ.... |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ์ข์ ๋ง์ ๊ฐ์ฌํฉ๋๋ค.
ํด๋น ์ด๋ฆ์ด ๋ ์ง๊ด์ ์ด๊ณ ํจ์ ๋ ์์๋ณด๊ธฐ ์ฌ์ด๊ฒ ๊ฐ์ต๋๋ค!
์์ผ๋ก๋ ๋ ์ ๊ฒฝ์จ๋ณด๋๋ก ํ๊ฒ ์ต๋๋ค! |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ๋ฉ์๋ ๋ช
์ ๋์ฌ๋ ์ ์น์ฌ๋ก ์์ํ๋ ๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค! |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ๊ฐ์ธ์ ์ผ๋ก buyer๋ผ๋ ํด๋์ค์ ๋๋ฌด ๋ง์ ์ญํ ์ ๋ด๊ณ ์๋ ๊ฒ ๊ฐ์ต๋๋ค!
buyer๋ ๊ตฌ๋งค์๋ผ๋ ์๋ฏธ ์ฆ, ์ฌ์ฉ์ ์
์ฅ์์ ์์ฑ๋ ํด๋์ค๋ผ ์๊ฐ์ด ๋๋๋ฐ, ์ฌ์ฉ์๊ฐ ์์์ฆ์ ์ถ๋ ฅํ๋ ๊ฒ๋ณด๋ค Order๊ฐ์ ํด๋์ค๋ฅผ ์๋ก ๋ง๋ค์ด ์ญํ ์ ๋ถ๋ฆฌํ๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,139 @@
+package view;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.WRONG_INPUT;
+
+import camp.nextstep.edu.missionutils.Console;
+import dto.Status;
+import exception.Exception;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.time.LocalDate;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+import store.Application;
+import store.Product;
+import store.Promotion;
+import util.ProductValidator;
+
+public class InputView {
+
+ private static final String WANT_BUY_PRODUCT = "\n๊ตฌ๋งคํ์ค ์ํ๋ช
๊ณผ ์๋์ ์
๋ ฅํด ์ฃผ์ธ์. (์: [์ฌ์ด๋ค-2],[๊ฐ์์นฉ-1])";
+
+ public List<Product> readItem() {
+ return handleRetryOnError(() -> {
+ String[] items = preWorkBuyProduct();
+ List<Product> buyProducts = new ArrayList<>();
+ for (String item : items) {
+ ProductValidator.isValidProductFormat(item);
+ addBuyProduct(item, buyProducts);
+ }
+ return buyProducts;
+ });
+ }
+
+ public String[] preWorkBuyProduct() {
+ System.out.println(WANT_BUY_PRODUCT);
+ String input = Console.readLine().trim();
+ return input.split(",");
+ }
+
+ public void addBuyProduct(String item, List<Product> buyProducts) {
+ String cleanInput = item.replace("[", "").replace("]", "");
+ String[] nameAndQuantity = cleanInput.split("-");
+ String name = nameAndQuantity[0];
+ int quantity = 0;
+ try {
+ quantity = Integer.parseInt(nameAndQuantity[1]);
+ } catch (java.lang.Exception e) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ buyProducts.add(new Product(name, quantity));
+ }
+
+ public <T> List<T> loadItems(String fileName, Function<String, T> mapper) {
+ try (BufferedReader reader = new BufferedReader(
+ new InputStreamReader(Application.class.getClassLoader().getResourceAsStream(fileName)))) {
+ return reader.lines()
+ .skip(1) // ์ฒซ ๋ฒ์งธ ์ค ๊ฑด๋๋ฐ๊ธฐ
+ .map(mapper)
+ .collect(Collectors.toList());
+ } catch (IOException e) {
+ throw new RuntimeException("[ERROR] reading file: " + fileName, e);
+ }
+ }
+
+ public List<Product> loadProducts(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] productInfo = line.split(",");
+ int price = Integer.parseInt(productInfo[1]);
+ int quantity = Integer.parseInt(productInfo[2]);
+ String promotion = productInfo[3];
+ if ("null".equals(promotion)) {
+ promotion = null;
+ }
+ return new Product(productInfo[0], price, quantity, promotion);
+ });
+ }
+
+ public List<Promotion> loadPromotions(String fileName) {
+ return loadItems(fileName, line -> {
+ String[] promotionInfo = line.split(",");
+ String name = promotionInfo[0];
+ int buy = Integer.parseInt(promotionInfo[1]);
+ int get = Integer.parseInt(promotionInfo[2]);
+ LocalDate startDate = LocalDate.parse(promotionInfo[3]);
+ LocalDate endDate = LocalDate.parse(promotionInfo[4]);
+ return new Promotion(name, buy, get, startDate, endDate);
+ });
+ }
+
+ public void addPromotion(Product wantBuyProduct, int notBringBonus) {
+ String input = checkInputYorN();
+ if (Status.Y == Status.checkStatusInput(input)) {
+ wantBuyProduct.addQuantity(notBringBonus);
+ }
+ }
+
+ public void promotionApply(Product wantBuyProduct, int notPromotionCount) {
+ String input = checkInputYorN();
+ if (Status.N == Status.checkStatusInput(input)) {
+ wantBuyProduct.subtractQuantity(notPromotionCount);
+ }
+ }
+
+ public Status memberShipApply() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public String checkInputYorN() {
+ return handleRetryOnError(() -> {
+ String input = Console.readLine().trim();
+ if (!"Y".equalsIgnoreCase(input) && !"N".equalsIgnoreCase(input)) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ return input;
+ });
+ }
+
+ public Status wantContinue() {
+ String input = checkInputYorN();
+ return Status.checkStatusInput(input);
+ }
+
+ public static <T> T handleRetryOnError(Supplier<T> method) {
+ try {
+ return method.get();
+ } catch (IllegalArgumentException e) {
+ OutputView.printMessage(e.getMessage());
+ return handleRetryOnError(method);
+ }
+ }
+
+
+}
\ No newline at end of file | Java | '[' ๋ ']' ๋ฅผ ์์๋ก ๋นผ๋ ๊ฑด ์ด๋จ๊น์? |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ํน์ ์ด ๋ถ๋ถ์ ๋ฉ์๋๋ก ๋นผ์ ์ด์ ๊ฐ ์์๊น์?? |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ๊ฐ์ธ์ ์ผ๋ก ์ด ๋ถ๋ถ์ด ๊ฐ๋
์ฑ์ด ์กฐ๊ธ์ ์ข์ง ์์ ๊ฑฐ ๊ฐ์ต๋๋ค! ๋ฉ์๋๋ฅผ ๋ถ๋ฆฌํ๋ ๋ฐฉ๋ฒ๊ณผ ๋ณ์๋ฅผ ์ค์ ํ๋ ๊ฑธ ์ถ์ฒ๋๋ ค๋ด
๋๋ค! |
@@ -0,0 +1,357 @@
+package store;
+
+import static exception.Exception.EXCEED_QUANTITY;
+import static exception.Exception.NON_EXIST_PRODUCT;
+import static exception.Exception.WRONG_INPUT;
+import static exception.Exception.throwException;
+import static util.ProductValidator.INVALID_FORMAT;
+import static view.InputView.handleRetryOnError;
+import static view.OutputView.MEMBERSHIP_BUY;
+import static view.OutputView.NOW;
+import static view.OutputView.NO_PROMOTION_BUY;
+import static view.OutputView.printMessage;
+
+import dto.Status;
+import exception.Exception;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import view.InputView;
+import view.OutputView;
+
+public class Buyer {
+
+ private static final String PROMOTIONAL_SEPARATOR = "P_";
+ private static final String PRODUCTS_FILE_NAME = "products.md";
+ private static final String PROMOTION_FILE_NAME = "promotions.md";
+ private static final String THANK_YOU_MORE_BUY = "\n๊ฐ์ฌํฉ๋๋ค. ๊ตฌ๋งคํ๊ณ ์ถ์ ๋ค๋ฅธ ์ํ์ด ์๋์? (Y/N)";
+ private static final String BLANK_SEPARATOR = "====================================";
+ private static final String PROMOTION_SEPARATOR = "=============์ฆ\t\t์ ===============";
+ private static final String DUPL_NAME = "[์ค๋ณต๋ ์ ํ๋ช
] : ";
+ private static final int MEMBERSHIP_MAX_DISCOUNT = 8000;
+ private final InputView inputView;
+ private final OutputView outputView;
+ private final LinkedHashMap<String, List<Product>> products;
+ private final Map<String, Promotion> promotions;
+ private Map<String, Integer> receiptMap;
+ private Status memberShip;
+
+ public Buyer() {
+ this.inputView = new InputView();
+ this.outputView = new OutputView();
+ this.products = makeProducts(inputView.loadProducts(PRODUCTS_FILE_NAME));
+ this.promotions = makePromotions(inputView.loadPromotions(PROMOTION_FILE_NAME));
+ outputView.printProducts(products);
+ }
+
+ public void makePrintProduct(LinkedHashMap<String, List<Product>> products) {
+ for (String key : products.keySet()) {
+ List<Product> productList = products.get(key);
+ if (productList.size() == 1 && productList.getFirst().getPromotion() != null) {
+ Product product = productList.getFirst();
+ productList.add(new Product(product.getName(), product.getPrice(), 0));
+ }
+ }
+ }
+
+
+ public LinkedHashMap<String, List<Product>> makeProducts(List<Product> products) {
+ LinkedHashMap<String, List<Product>> map = new LinkedHashMap<>();
+ for (Product product : products) {
+ map.computeIfAbsent(product.getName(), k -> new ArrayList<>())
+ .add(product);
+ }
+ makePrintProduct(map);
+ return map;
+ }
+
+ public Map<String, Promotion> makePromotions(List<Promotion> promotions) {
+ HashMap<String, Promotion> map = new HashMap<>();
+ for (Promotion promotion : promotions) {
+ map.put(promotion.getName(), promotion);
+ }
+ return map;
+ }
+
+ public void buyProducts() {
+ initializationReceiptMap();
+ List<Product> wantBuyProducts = inputBuyProduct();
+ checkNotPromotionApply(wantBuyProducts);
+ applyMemberShip();
+ printReceipt(wantBuyProducts);
+ realBuyProducts(wantBuyProducts);
+ }
+
+ public void initializationReceiptMap() {
+ receiptMap = new HashMap<>();
+ }
+
+ public List<Product> inputBuyProduct() {
+ return handleRetryOnError(() -> {
+ List<Product> wantBuyProducts = inputView.readItem();
+ checkDuplProductName(wantBuyProducts);
+ productNameCheck(wantBuyProducts);
+ checkPromotionApply(wantBuyProducts);
+ checkCanBuyQuantity(wantBuyProducts);
+ return wantBuyProducts;
+ });
+ }
+
+ public void checkDuplProductName(List<Product> wantBuyProducts) {
+ Set<String> uniqueNames = new HashSet<>();
+ for (Product product : wantBuyProducts) {
+ String productName = product.getName();
+ if (!uniqueNames.add(productName)) {
+ throwException(INVALID_FORMAT + DUPL_NAME + productName);
+ }
+ }
+ }
+
+ public void productNameCheck(List<Product> wantBuyProducts) {
+ if (wantBuyProducts.isEmpty()) {
+ Exception.throwException(WRONG_INPUT);
+ }
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()) == null) {
+ Exception.throwException(NON_EXIST_PRODUCT);
+ }
+ }
+ }
+
+ public void realBuyProducts(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int quantity = wantBuyProduct.getQuantity();
+ Product firstProduct = products.get(wantBuyProduct.getName()).getFirst();
+ if (withinQuantity(firstProduct, quantity)) {
+ continue;
+ }
+ int restQuantity = quantity - firstProduct.getQuantity();
+ firstProduct.subtractQuantity(firstProduct.getQuantity());
+ products.get(wantBuyProduct.getName()).get(1).subtractQuantity(restQuantity);
+ }
+ }
+
+ public boolean withinQuantity(Product firstProduct, int quantity) {
+ boolean exceed = false;
+ if (firstProduct.getQuantity() >= quantity) {
+ firstProduct.subtractQuantity(quantity);
+ return true;
+ }
+ return exceed;
+ }
+
+ public void wantContinue() {
+ while (true) {
+ printMessage(THANK_YOU_MORE_BUY);
+ Status status = inputView.wantContinue();
+ if (status == Status.Y) {
+ convenienceContinue();
+ }
+ break;
+ }
+ }
+
+ public void convenienceContinue() {
+ printMessage(null);
+ outputView.printProducts(products);
+ buyProducts();
+ wantContinue();
+ }
+
+ public void printReceipt(List<Product> wantBuyProducts) {
+ int totalBuyCount = printFirstReceipt(wantBuyProducts);
+ printPresentReceipt(wantBuyProducts);
+ printFinalReceipt(totalBuyCount);
+ }
+
+
+ public void printFinalReceipt(int totalBuyCount) {
+ printMessage(BLANK_SEPARATOR);
+ int totalMoney = totalMoney();
+ int promotionDiscount = promotionDiscount();
+ int memberShipDiscount = memberShipDiscount();
+ outputView.printFinalReceipt(totalBuyCount, totalMoney, promotionDiscount, memberShipDiscount);
+ }
+
+
+ public int memberShipDiscount() {
+ int memberShipMoney = 0;
+ if (memberShip != Status.Y) {
+ return memberShipMoney;
+ }
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ memberShipMoney += (int) (receiptMap.get(key) * 0.3);
+ }
+ }
+ return Math.min(memberShipMoney, MEMBERSHIP_MAX_DISCOUNT);
+ }
+
+ public int promotionDiscount() {
+ int promotionDiscount = 0;
+ ArrayList<String> removes = new ArrayList<>();
+ for (String key : receiptMap.keySet()) {
+ if (key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ promotionDiscount += receiptMap.get(key);
+ removes.add(key.substring(key.indexOf(PROMOTIONAL_SEPARATOR) + PROMOTIONAL_SEPARATOR.length()));
+ }
+ }
+ removeReceiptMap(removes);
+ return promotionDiscount;
+ }
+
+ public void removeReceiptMap(List<String> removes) {
+ for (String remove : removes) {
+ receiptMap.remove(remove);
+ }
+ }
+
+ public int totalMoney() {
+ int totalMoney = 0;
+ for (String key : receiptMap.keySet()) {
+ if (!key.startsWith(PROMOTIONAL_SEPARATOR)) {
+ totalMoney += receiptMap.get(key);
+ }
+ }
+ return totalMoney;
+ }
+
+ public void printPresentReceipt(List<Product> wantBuyProducts) {
+ printMessage(PROMOTION_SEPARATOR);
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (products.get(wantBuyProduct.getName()).size() == 1 || !promotion.checkPromotionDate()) {
+ continue;
+ }
+ printProvenPresent(wantBuyProduct);
+ }
+ }
+
+ public void printProvenPresent(Product wantBuyProduct) {
+ int promotionCount = calculatePromotionCount(wantBuyProduct);
+ if (promotionCount > 0) {
+ outputView.printPresentReceipt(wantBuyProduct, promotionCount);
+ receiptMap.put(PROMOTIONAL_SEPARATOR + wantBuyProduct.getName(),
+ promotionCount * products.get(wantBuyProduct.getName()).getFirst().getPrice());
+ }
+ }
+
+ public int printFirstReceipt(List<Product> wantBuyProducts) {
+ outputView.printFirstReceipt();
+ int totalBuyCount = calculateTotalBuyCount(wantBuyProducts);
+ return totalBuyCount;
+ }
+
+ public int calculateTotalBuyCount(List<Product> wantBuyProducts) {
+ int totalBuyCount = 0;
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Product findProduct = products.get(wantBuyProduct.getName()).getFirst();
+ int totalPrice = outputView.printPurchaseReceipt(findProduct, wantBuyProduct);
+ receiptMap.put(findProduct.getName(), totalPrice);
+ totalBuyCount += wantBuyProduct.getQuantity();
+ }
+ return totalBuyCount;
+ }
+
+
+ public void applyMemberShip() {
+ OutputView.printMessage(MEMBERSHIP_BUY);
+ this.memberShip = inputView.memberShipApply();
+ }
+
+ public void checkCanBuyQuantity(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ int count = 0;
+ List<Product> getProducts = products.get(wantBuyProduct.getName());
+ for (Product p : getProducts) {
+ count += p.getQuantity();
+ }
+ if (wantBuyProduct.getQuantity() > count) {
+ Exception.throwException(EXCEED_QUANTITY);
+ }
+ }
+ }
+
+ public void checkNotPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ if (products.get(wantBuyProduct.getName()).size() == 1) {
+ continue;
+ }
+ int notPromotionCount = calculateNotPromotionCount(wantBuyProduct);
+ questionPromotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public void questionPromotionApply(Product wantBuyProduct, int notPromotionCount) {
+ if (notPromotionCount > 0) {
+ printMessage(NOW + wantBuyProduct.getName() + " " + notPromotionCount + NO_PROMOTION_BUY);
+ inputView.promotionApply(wantBuyProduct, notPromotionCount);
+ }
+ }
+
+ public int calculateNotPromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ int canBuyPromotionCount = canBuyPromotionProductCount(wantBuyProduct.getName());
+ int promotionCount = (canBuyPromotionCount / promotionSetSize) * promotionSetSize;
+ return wantBuyProduct.getQuantity() - promotionCount;
+ }
+
+ public int calculatePromotionCount(Product wantBuyProduct) {
+ int promotionSetSize = getPromotionSetSize(wantBuyProduct.getName());
+ return Math.min(products.get(wantBuyProduct.getName()).getFirst().getQuantity() / promotionSetSize,
+ wantBuyProduct.getQuantity() / promotionSetSize);
+ }
+
+ public int canBuyPromotionProductCount(String productName) {
+ return products.get(productName).getFirst().getQuantity();
+ }
+
+ public int getPromotionSetSize(String productName) { // 2+1์ผ ๊ฒฝ์ฐ ํ set size๋ 3 / 1+1์ผ๋๋ 2
+ String promotionName = products.get(productName).getFirst().getPromotion();
+ int setSize = 0;
+ Promotion promotion = promotions.get(promotionName);
+ setSize = promotion.getBuy() + promotion.getGet();
+ return setSize;
+ }
+
+
+ public void checkPromotionApply(List<Product> wantBuyProducts) {
+ for (Product wantBuyProduct : wantBuyProducts) {
+ Promotion promotion = promotions.get(products.get(wantBuyProduct.getName()).getFirst().getPromotion());
+ if (promotion != null && promotion.checkPromotionDate()) {
+ int notBringBonus = calculateBonus(promotion.getBuy(), promotion.getGet(),
+ wantBuyProduct.getQuantity());
+ bringBonus(wantBuyProduct, notBringBonus);
+ }
+ }
+ }
+
+ public int calculateBonus(int buy, int get, int purchasedCount) {
+ int bonusCount = 0;
+ if (buy == purchasedCount) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ if (purchasedCount > buy && purchasedCount % (buy + get) == buy) {
+ bonusCount = get;
+ return bonusCount;
+ }
+ return bonusCount;
+ }
+
+ public void bringBonus(Product wantBuyProduct, int notBringBonus) {
+ if (notBringBonus > 0) {
+ outputView.printBringPromotion(wantBuyProduct.getName(), notBringBonus);
+ inputView.addPromotion(wantBuyProduct, notBringBonus);
+ }
+ }
+
+ public LinkedHashMap<String, List<Product>> getProducts() {
+ LinkedHashMap<String, List<Product>> copiedProdcuts = new LinkedHashMap<>(this.products);
+ return copiedProdcuts;
+ }
+} | Java | ํ ํด๋์ค๊ฐ ๋๋ฌด๋ ๋ง์ ์ญํ ์ ํ๊ณ ์๋ ๊ฑฐ ๊ฐ์์!! ๊ตฌ๋งค์๊ฐ ํ๋ ์ญํ , ํธ์์ ์์ ํ๋ก๋ชจ์
์ ์ฉ๊ณผ ๊ฐ์ ์ญํ ๋ฑ ํด๋์ค ๋ถ๋ฆฌํด ์์ฑํ๋ ๊ฑธ ์ถ์ฒ๋๋ฆฝ๋๋ค! |
@@ -0,0 +1,25 @@
+package christmas.config;
+
+public enum Configuration {
+ ;
+
+ private final Object value;
+
+ Configuration(Object value) {
+ this.value = value;
+ }
+
+ public int getIntValue() {
+ if (value instanceof Integer) {
+ return (int) value;
+ }
+ throw new IllegalStateException("Value is not an integer: " + value);
+ }
+
+ public String getStringValue() {
+ if (value instanceof String) {
+ return (String) value;
+ }
+ throw new IllegalStateException("Value is not a string: " + value);
+ }
+} | Java | Configuration์ ์ด๋์ ํ์ฉ๋๋์? |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.