diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..c3f502a --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 디폴트 무시된 파일 +/shelf/ +/workspace.xml +# 에디터 기반 HTTP 클라이언트 요청 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/jpa-buddy.xml b/.idea/jpa-buddy.xml new file mode 100644 index 0000000..966d5f5 --- /dev/null +++ b/.idea/jpa-buddy.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8210f62 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,9 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ece9f84 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..a49763d --- /dev/null +++ b/src/Main.java @@ -0,0 +1,63 @@ +import domain.PaymentMethod; +import domain.Product; +import domain.UserType; +import service.*; +import util.message.SystemMessage; + +import static domain.Temperature.*; +import static domain.UserType.*; + +public class Main { + private static ProductService productService; + private static SystemMessage systemMessage; + private static UserService userService; + private static AdminService adminService; + + public static void main(String[] args) { + UserType user = USER; + initialize(); + systemMessage.welcome(); + String input; + input = systemMessage.inputMessage(user); + if (input.equals("admin")) { + user = ADMIN; + + return; + } + systemMessage.printTemperatureMenu(); + userService.selectTemperature(systemMessage.inputMessage(user)); + Product product = userService.selectProduct(systemMessage.inputMessage(user)); + + systemMessage.printPaymentMethodMenu(); + + PaymentMethod paymentMethod = userService.selectPaymentMethod(systemMessage.inputMessage(user), product); + if (paymentMethod.equals(PaymentMethod.CASH)) { + int insertedCash = userService.insertCash(product); + systemMessage.cashResult(product, insertedCash); + } else if (paymentMethod.equals(PaymentMethod.CARD)) { + systemMessage.cardResult(product); + } else { + System.out.println("잘못된 입력값"); + } + } + + public static void initialize() { + productService = new ProductServiceImpl(); + systemMessage = new SystemMessage(); + userService = new UserServiceImpl(productService, systemMessage); + adminService = new AdminServiceImpl(productService, systemMessage); + + initData(); + } + + public static void initData() { + Product product1 = new Product(COLD, "스프라이트", 1500); + productService.addProduct(product1); + Product product2 = new Product(COLD, "코카콜라", 1500); + productService.addProduct(product2); + Product product3 = new Product(COLD, "솔의 눈", 1500); + productService.addProduct(product3); + Product product4 = new Product(HOT, "팥죽", 1500); + productService.addProduct(product4); + } +} \ No newline at end of file diff --git a/src/domain/MoneyType.java b/src/domain/MoneyType.java new file mode 100644 index 0000000..f9fa9b8 --- /dev/null +++ b/src/domain/MoneyType.java @@ -0,0 +1,23 @@ +package domain; + +import java.util.List; + +public enum MoneyType { + PAPER_50000("5만원권"), + PAPER_10000("1만원권"), + PAPER_5000("5천원권"), + PAPER_1000("1천원권"), + COIN_500("5백원 동전"), + COIN_100("1백원 동전"), + ; + + private String money; + + MoneyType(String money) { + this.money = money; + } + + public String getMoney() { + return money; + } +} diff --git a/src/domain/PaymentMethod.java b/src/domain/PaymentMethod.java new file mode 100644 index 0000000..b236faa --- /dev/null +++ b/src/domain/PaymentMethod.java @@ -0,0 +1,17 @@ +package domain; + +public enum PaymentMethod { + CASH("현금"), + CARD("카드"), + ; + + private String paymentMethod; + + PaymentMethod(String paymentMethod) { + this.paymentMethod = paymentMethod; + } + + public String getPaymentMethod() { + return paymentMethod; + } +} diff --git a/src/domain/Product.java b/src/domain/Product.java new file mode 100644 index 0000000..88c2843 --- /dev/null +++ b/src/domain/Product.java @@ -0,0 +1,39 @@ +package domain; + +public class Product { + private Long productId; + private Temperature temperature; + private String productName; + private Integer cost; + + public Product(Temperature temperature, String productName, Integer cost) { + this.temperature = temperature; + this.productName = productName; + this.cost = cost; + } + + public Long getProductId() { + return productId; + } + + public void setProductId(Long productId) { + this.productId = productId; + } + + public Temperature getTemperture() { + return temperature; + } + + public String getProductName() { + return productName; + } + + public Integer getCost() { + return cost; + } + + @Override + public String toString() { + return String.format("[%d] %s : %d\n", productId, productName, cost); + } +} diff --git a/src/domain/Temperature.java b/src/domain/Temperature.java new file mode 100644 index 0000000..37120f7 --- /dev/null +++ b/src/domain/Temperature.java @@ -0,0 +1,15 @@ +package domain; + +public enum Temperature { + COLD("차가운 음료"), HOT("따뜻한 음료"); + + private String temperature; + + Temperature(String temperature) { + this.temperature = temperature; + } + + public String getTemperature() { + return temperature; + } +} diff --git a/src/domain/UserType.java b/src/domain/UserType.java new file mode 100644 index 0000000..dba97b5 --- /dev/null +++ b/src/domain/UserType.java @@ -0,0 +1,5 @@ +package domain; + +public enum UserType { + USER, ADMIN; +} diff --git a/src/repository/ProductRepository.java b/src/repository/ProductRepository.java new file mode 100644 index 0000000..f6ddb46 --- /dev/null +++ b/src/repository/ProductRepository.java @@ -0,0 +1,15 @@ +package repository; + +import domain.Product; + +import java.util.List; + +public interface ProductRepository { + void add(Product product); + + void delete(Long productId); + + Product findByProductId(Long productId); + + List findAllProducts(); +} diff --git a/src/repository/ProductRepositoryImpl.java b/src/repository/ProductRepositoryImpl.java new file mode 100644 index 0000000..452a689 --- /dev/null +++ b/src/repository/ProductRepositoryImpl.java @@ -0,0 +1,32 @@ +package repository; + +import domain.Product; +import domain.Temperature; + +import java.util.*; + +public class ProductRepositoryImpl implements ProductRepository { + private Map products = new HashMap<>(); + private Long productId = 1L; + + @Override + public void add(Product product) { + product.setProductId(productId); + products.put(productId++, product); + } + + @Override + public void delete(Long productId) { + products.remove(productId); + } + + @Override + public Product findByProductId(Long productId) { + return products.get(productId); + } + + @Override + public List findAllProducts() { + return new ArrayList<>(products.values()); + } +} diff --git a/src/service/AdminService.java b/src/service/AdminService.java new file mode 100644 index 0000000..83b9b97 --- /dev/null +++ b/src/service/AdminService.java @@ -0,0 +1,5 @@ +package service; + +public interface AdminService { + +} diff --git a/src/service/AdminServiceImpl.java b/src/service/AdminServiceImpl.java new file mode 100644 index 0000000..6a7467b --- /dev/null +++ b/src/service/AdminServiceImpl.java @@ -0,0 +1,13 @@ +package service; + +import util.message.SystemMessage; + +public class AdminServiceImpl implements AdminService { + private ProductService productService; + private SystemMessage systemMessage; + + public AdminServiceImpl(ProductService productService, SystemMessage systemMessage) { + this.productService = productService; + this.systemMessage = systemMessage; + } +} diff --git a/src/service/ProductService.java b/src/service/ProductService.java new file mode 100644 index 0000000..02bbbb9 --- /dev/null +++ b/src/service/ProductService.java @@ -0,0 +1,16 @@ +package service; + +import domain.Product; +import domain.Temperature; + +import java.util.List; + +public interface ProductService { + void addProduct(Product product); + + Product getProductByProductId(Long productId); + + void deleteByProductId(Long productId); + + List getProductsByTemperature(Temperature temperature); +} diff --git a/src/service/ProductServiceImpl.java b/src/service/ProductServiceImpl.java new file mode 100644 index 0000000..1172282 --- /dev/null +++ b/src/service/ProductServiceImpl.java @@ -0,0 +1,44 @@ +package service; + +import domain.Product; +import domain.Temperature; +import repository.ProductRepository; +import repository.ProductRepositoryImpl; + +import java.util.ArrayList; +import java.util.List; + +public class ProductServiceImpl implements ProductService{ + private ProductRepository productRepository; + + public ProductServiceImpl() { + this.productRepository = new ProductRepositoryImpl(); + } + + @Override + public void addProduct(Product product) { + productRepository.add(product); + } + + @Override + public Product getProductByProductId(Long productId) { + return productRepository.findByProductId(productId); + } + + @Override + public void deleteByProductId(Long productId) { + productRepository.delete(productId); + } + + @Override + public List getProductsByTemperature(Temperature temperature) { + List productList = productRepository.findAllProducts(); + List filteredProductList = new ArrayList<>(); + for (Product product : productList) { + if (product.getTemperture() == temperature) { + filteredProductList.add(product); + } + } + return filteredProductList; + } +} diff --git a/src/service/UserService.java b/src/service/UserService.java new file mode 100644 index 0000000..f817a43 --- /dev/null +++ b/src/service/UserService.java @@ -0,0 +1,16 @@ +package service; + +import domain.PaymentMethod; +import domain.Product; + +public interface UserService { + void selectTemperature(String input); + + Product selectProduct(String input); + + PaymentMethod selectPaymentMethod(String input, Product product); + + int insertCash(Product product); + + int selectedCash(String input); +} diff --git a/src/service/UserServiceImpl.java b/src/service/UserServiceImpl.java new file mode 100644 index 0000000..c22f84f --- /dev/null +++ b/src/service/UserServiceImpl.java @@ -0,0 +1,84 @@ +package service; + +import domain.PaymentMethod; +import domain.Product; +import domain.UserType; +import util.message.SystemMessage; + +import java.io.IOException; + +import static domain.Temperature.COLD; +import static domain.Temperature.HOT; + +public class UserServiceImpl implements UserService { + private final ProductService productService; + private final SystemMessage systemMessage; + private final UserType user; + + public UserServiceImpl(ProductService productService, SystemMessage systemMessage) { + this.productService = productService; + this.systemMessage = systemMessage; + this.user = UserType.USER; + } + + @Override + public void selectTemperature(String input) { + if (input.equals("1")) { + systemMessage.printProductMenu(productService.getProductsByTemperature(COLD), COLD); + } else if (input.equals("2")) { + systemMessage.printProductMenu(productService.getProductsByTemperature(HOT), HOT); + } else { + System.out.println("올바른 값을 입력 하십시오."); + } + } + + @Override + public Product selectProduct(String input) { + return productService.getProductByProductId(Long.parseLong(input)); + } + + @Override + public PaymentMethod selectPaymentMethod(String input, Product product) { + if (input.equals("1")) { + return PaymentMethod.CASH; + } else if (input.equals("2")) { + return PaymentMethod.CARD; + } + return null; + } + + @Override + public int insertCash(Product product) { + String input; + int insertedCash = 0; + while (insertedCash <= product.getCost()) { + systemMessage.printCashMenu(insertedCash); + input = systemMessage.inputMessage(user); + if (input.equals("0")) { + insertedCash = 0; + } + insertedCash += selectedCash(input); + } + return insertedCash; + } + + @Override + public int selectedCash(String input) { + if (input.equals("1")) { + return 50000; + } else if (input.equals("2")) { + return 10000; + } else if (input.equals("3")) { + return 5000; + } else if (input.equals("4")) { + return 1000; + } else if (input.equals("5")) { + return 500; + } else if (input.equals("6")) { + return 100; + } else { + System.out.println("잘못된 입력값"); + } + return 0; + } +} diff --git a/src/util/message/SystemMessage.java b/src/util/message/SystemMessage.java new file mode 100644 index 0000000..ebe11d7 --- /dev/null +++ b/src/util/message/SystemMessage.java @@ -0,0 +1,137 @@ +package util.message; + +import domain.MoneyType; +import domain.Product; +import domain.Temperature; +import domain.UserType; + +import java.util.List; +import java.util.Scanner; + +import static domain.PaymentMethod.*; +import static domain.UserType.*; + +public class SystemMessage { + public String inputMessage(UserType userType) { + Scanner sc = new Scanner(System.in); + if (userType.equals(USER)) { + System.out.print("사용자 입력 > "); + } else if (userType.equals(ADMIN)) { + System.out.print("admin 입력 > "); + } + return sc.next(); + } + + public void welcome() { + System.out.println(""" + [어서와요! GDSC 음료 자판기] + + 계속 하려면 아무키나 입력하세요 ... + """); + } + + public void printTemperatureMenu() { + divideLine(); + System.out.println(""" + 음료를 선택 해주세요 ! + + [1] 차가운 음료 + [2] 따뜻한 음료 + """); + } + + public void printPaymentMethodMenu() { + divideLine(); + System.out.printf(""" + [1] %s + [2] %s (부가세 10%% 적용) + + """, CASH.getPaymentMethod(), CARD.getPaymentMethod()); + } + + public void printProductMenu(List products, Temperature temperature) { + divideLine(); + System.out.println("[" + temperature.getTemperature() + "]\n"); + for (Product product : products) { + System.out.printf("[%d] %s : %d원%n", + product.getProductId(), + product.getProductName(), + product.getCost()); + } + System.out.println(); + } + + public void printCashMenu(int insertedCash) { + divideLine(); + System.out.printf("[현금 투입 : %d원]\n\n", insertedCash); + + System.out.println(""" + [1] 5만원권 + [2] 1만원권 + [3] 5천원권 + [4] 1천원권 + [5] 5백원 동전 + [6] 1백원 동전 + [0] 반환 + """); + } + + public void printResult(Product product) { + divideLine(); + System.out.printf(""" + 이용해주셔서 감사합니다. + + [주문 음료] + %s + + """, product.getProductName()); + } + + public void cashResult(Product product, int insertedCash) { + printResult(product); + + System.out.printf(""" + [투입 금액] + %d원 + + [잔돈] + %s + """, insertedCash, returnChange(product, insertedCash)); + } + + public String returnChange(Product product, int insertedCash) { + StringBuilder sb = new StringBuilder(); + int change = insertedCash - product.getCost(); + + int[] moneyTypes = {50000, 10000, 5000, 1000, 500, 100}; // 화폐 단위 + String[] moneyNames = {"50000원권", "10000원권", "5000원권", "1000원권", "500원 동전", "100원 동전"}; // 화폐 이름 + int[] moneyCounts = new int[moneyTypes.length]; // 각 화폐 단위별 개수 + + for (int i = 0; i < moneyTypes.length; i++) { + moneyCounts[i] = change / moneyTypes[i]; // 거스름돈을 해당 화폐 단위로 나눈 몫 + change %= moneyTypes[i]; // 거스름돈을 해당 화폐 단위로 나눈 나머지 + } + + // 결과 출력 + for (int i = 0; i < moneyNames.length; i++) { + if (moneyCounts[i] > 0) { + sb.append(moneyNames[i]).append(" : ").append(moneyCounts[i]).append("개\n"); + } + } + return sb.toString(); + } + + public void cardResult(Product product) { + printResult(product); + System.out.printf(""" + [결제 금액] + %d원 + """, product.getCost()); + } + + public void divideLine() { + System.out.print(""" + ============================= + """); + } +} diff --git a/vending-machine.iml b/vending-machine.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/vending-machine.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/vending-machine/.idea/vcs.xml b/vending-machine/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/vending-machine/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/vending-machine/.idea/workspace.xml b/vending-machine/.idea/workspace.xml new file mode 100644 index 0000000..0feae5f --- /dev/null +++ b/vending-machine/.idea/workspace.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + { + "associatedIndex": 5 +} + + + + { + "keyToString": { + "RunOnceActivity.OpenProjectViewOnStart": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "WebServerToolWindowFactoryState": "false", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" + } +} + + + + + 1700719892270 + + + + + + \ No newline at end of file