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์€ ์–ด๋””์„œ ํ™œ์šฉ๋˜๋‚˜์š”?