-
Notifications
You must be signed in to change notification settings - Fork 1
feat : 결제 기능 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: jbm
Are you sure you want to change the base?
feat : 결제 기능 #15
Changes from 2 commits
243d011
def2667
cd63784
cf1e207
315dc17
d171c80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import domain.Menu; | ||
| //import jdk.internal.util.xml.impl.Input; | ||
| import domain.PayType; | ||
| import repository.MenuRepository; | ||
| import domain.Table; | ||
| import repository.OrderRepository; | ||
|
|
@@ -9,6 +10,8 @@ | |
| import view.OutputView; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.NoSuchElementException; | ||
|
|
||
| public class Application { | ||
| // TODO 구현 진행 | ||
|
|
@@ -28,6 +31,9 @@ public static void main(String[] args) { | |
| { | ||
| payOrders(tables, cafeOrderService); | ||
| } | ||
| else if(func>3 || func<0){ | ||
| throw new NoSuchElementException("해당 기능은 존재하지 않습니다."); | ||
| } | ||
| OutputView.printMain(); | ||
| func = InputView.inputFunction(); | ||
| } | ||
|
|
@@ -36,27 +42,37 @@ public static void main(String[] args) { | |
| private static void payOrders(List<Table> tables, CafeOrderService cafeOrderService) { | ||
| OutputView.printTables(tables, cafeOrderService); | ||
| int tableNum = InputView.inputPayTableNumber(); | ||
| OutputView.printOrders(cafeOrderService, tableNum); | ||
| if(cafeOrderService.isOrderedTable(tableNum)==false){ | ||
| OutputView.printNoOrder(); | ||
| return; | ||
| }; | ||
| Map<Menu, Long> bill = cafeOrderService.getBillByTable(tableNum); | ||
| OutputView.printOrders(bill); | ||
| OutputView.printPayMessage(tableNum); | ||
| int payTypeNumber = InputView.inputPayType(); | ||
| PayType payType = PayType.findByNumber(payTypeNumber); | ||
| long amountOfPayment = cafeOrderService.getAmountOfPayment(tableNum, payType, bill); | ||
| OutputView.printAmountOfPayment(amountOfPayment); | ||
| } | ||
|
|
||
| private static void orderMenu(List<Table> tables, CafeOrderService cafeOrderService) | ||
| { | ||
| OutputView.printTables(tables, cafeOrderService); | ||
| int tableNum = InputView.inputTableNumber(); | ||
| boolean isValidTblNum = cafeOrderService.isValidTableNum(tableNum); | ||
| boolean isValidTblNum = cafeOrderService.isValidTableNumber(tableNum); | ||
| if(isValidTblNum==false){ | ||
| orderMenu(tables, cafeOrderService); | ||
| } | ||
| final List<Menu> menus = MenuRepository.menus(); | ||
| OutputView.printMenus(menus); | ||
| int menuNum = InputView.inputMenu(); | ||
| boolean isValidMenuNum = cafeOrderService.checkMenuNum(menuNum); | ||
| boolean isValidMenuNum = cafeOrderService.isValidMenuNumber(menuNum); | ||
| if(isValidMenuNum==false){ | ||
| OutputView.printMenuAlert(); | ||
| orderMenu(tables, cafeOrderService); | ||
| } | ||
| int menuCount = InputView.inputCount(); | ||
| boolean isValidMenuCount = cafeOrderService.canOrderMenu(tableNum, menuNum, menuCount); | ||
| boolean isValidMenuCount = cafeOrderService.checkMaximumCount(tableNum, menuNum, menuCount); | ||
| if(isValidMenuCount==false){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위와 마찬가지인 부분! |
||
| OutputView.printMaxAlert(); | ||
| return; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,27 @@ | ||
| package domain; | ||
|
|
||
| public class Order { | ||
| private final int menuNum; | ||
| private final int tableNum; | ||
| private final int menuNumber; | ||
| private final int tableNumber; | ||
|
|
||
| public Order(int menuNum, int tableNum) { | ||
| this.menuNum = menuNum; | ||
| this.tableNum = tableNum; | ||
| this.menuNumber = menuNum; | ||
| this.tableNumber = tableNum; | ||
| } | ||
|
|
||
| public int getMenuNum() { | ||
| return this.menuNum; | ||
| public int getMenuNumber() { | ||
| return this.menuNumber; | ||
| } | ||
|
|
||
| public int getTableNum() { | ||
| return tableNum; | ||
| public int getTableNumber() { | ||
| return tableNumber; | ||
| } | ||
|
|
||
| public boolean isEqualTable(int tableNum){ | ||
| return this.tableNum == tableNum; | ||
| public boolean isEqualTable(int tableNumber){ | ||
| return this.tableNumber == tableNumber; | ||
| } | ||
|
|
||
| public boolean isEqualMenuNumber(int menuNumber){ | ||
| return this.menuNumber==menuNumber; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package domain; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| public enum PayType { | ||
| CARD(1, 0), CASH(2,10); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 만약 이렇게 구현해놓은 이후에 요구사항이 생겨 할인 수단이 다양해진다면 어떻게 구성하실걸까요?! 추후에 충분히 카드에만 해당하는 할인수단, 현금에만 해당하는 할인수단이 다르게 적용될 수도 있을 거 같은데, 확장성을 좀 더 고려해보셔도 좋을 거 같습니다! |
||
|
|
||
| private final int number; | ||
| private final int discountRate; | ||
|
|
||
| PayType(int number, int discountRate){ | ||
| this.number = number; | ||
| this.discountRate = discountRate; | ||
| } | ||
|
|
||
| public static PayType findByNumber(int number){ | ||
| return Arrays.stream(values()) | ||
| .filter(payType -> isEqualPayType(payType, number)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException("결제 수단은 카드(1)와 현금(2)만 존재합니다.")); | ||
| } | ||
|
|
||
| private static boolean isEqualPayType(PayType payType, int number){ | ||
| return payType.number==number; | ||
| } | ||
|
|
||
| public int getDiscountRate(){ | ||
| return this.discountRate; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,8 @@ public int getNumber() { | |
| return number; | ||
| } | ||
|
|
||
| public boolean isEqualNumber(int tableNumber){ return this.number==number;} | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 입력받은 파라미터가 사용되지 않고있어요! 이렇게 되면 항상 true를 리턴할 거 같아요! |
||
| @Override | ||
| public String toString() { | ||
| return Integer.toString(number); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,92 +5,59 @@ | |
|
|
||
| import java.util.*; | ||
| import java.util.stream.Collectors; | ||
| import java.util.stream.Stream; | ||
|
|
||
| import static java.util.stream.Collectors.*; | ||
|
|
||
| //주문 정보들을 저장하는 클래스 | ||
| public class OrderRepository { | ||
| private static final int MAX_NUMBER_PER_MENU = 30; | ||
| private final List<Order> orders = new ArrayList<>(); | ||
|
|
||
| //주문을 받는다. | ||
| public void addOrder(final Order order) | ||
| { | ||
| orders.add(order); | ||
| } | ||
|
|
||
| public int countOrders(){ | ||
| return orders.size(); | ||
| } | ||
|
|
||
| public int countMenusPerTable(int tableNum) | ||
| { | ||
| return (int)orders.stream() | ||
| .filter(order-> order.isEqualTable(tableNum)) | ||
| .count(); | ||
| } | ||
|
|
||
| public int countMenusNumber(int tableNum, int menuNum){ | ||
| //해당 테이블에서 주문한 해당 메뉴의 수량을 return한다. | ||
| public int countMenusNumber(int tableNumber, int menuNumber){ | ||
| int count= (int)orders.stream() | ||
| .filter(order->order.isEqualTable(tableNum)) | ||
| .filter(order->menuNum==order.getMenuNum()) | ||
| .filter(order->order.isEqualTable(tableNumber)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 스트림은 인라인으로 빼서 바로 리턴해도 될 거 같습니다! |
||
| .filter(order->order.isEqualMenuNumber(menuNumber)) | ||
| .count(); | ||
| return count; | ||
| } | ||
|
|
||
| //가장 수량이 많은 메뉴의 수량을 체크하는 메서드 | ||
| //need to edit | ||
| public int countNumberPerMenu(int tableNum) | ||
| { | ||
| List<Order> orders = findByTableNumber(tableNum); | ||
| if(orders.isEmpty()){ | ||
| return 0; | ||
| } | ||
| Map<Menu, Long> bill = orders.stream() | ||
| .map(order -> MenuRepository.findByNumber(order.getMenuNum())) | ||
| .collect(Collectors.groupingBy(order->order,HashMap::new, counting())); | ||
| List<Map.Entry<Menu, Long>> entryList = new LinkedList(bill.entrySet()); | ||
| entryList.sort(Map.Entry.comparingByValue()); | ||
|
|
||
| System.out.println(entryList.get(0).getValue().intValue()); | ||
| return entryList.get(0).getValue().intValue(); | ||
| } | ||
|
|
||
| public boolean canOrder(int tableNum) | ||
| { | ||
| if(countNumberPerMenu(tableNum)<=MAX_NUMBER_PER_MENU){ | ||
| return true; | ||
| }else{ | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| public boolean canOrder(int tableNum, int menuNum, int menuCount){ | ||
| int existedMenusNum = countMenusNumber(tableNum, menuNum); | ||
| if(existedMenusNum+menuCount<=MAX_NUMBER_PER_MENU){ | ||
| //해당 테이블에서 주문할 수 있는 한 메뉴의 최대 수량을 넘는지 아닌지 검사한다. | ||
| public boolean checkMaximumPerMenu(int tableNumber, int menuNumber, int menuCount){ | ||
| int existedMenusCount = countMenusNumber(tableNumber, menuNumber); | ||
| if(existedMenusCount+menuCount<=MAX_NUMBER_PER_MENU){ | ||
| return true; | ||
| }else{ | ||
| return false; | ||
| } | ||
| return false; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return existedMenusCount + menuCount <= MAX_NUMBER_PER_MENU 로 바로 리턴하는건 어떨까요? |
||
| } | ||
|
|
||
| public List<Order> findByTableNumber(int tableNum) | ||
| //해당 테이블에서 주문한 것들을 모두 return한다. | ||
| public List<Order> findByTableNumber(int tableNumber) | ||
| { | ||
| return | ||
| orders.stream() | ||
| .filter(order-> order.isEqualTable(tableNum)) | ||
| .collect(toList()); | ||
| .filter(order-> order.isEqualTable(tableNumber)) | ||
| .collect(toList()); | ||
| } | ||
|
|
||
| //해당 테이블에서 해당 메뉴의 수량을 출력하는 메소드 | ||
| public Map<Menu, Long> getBill(int tableNum){ | ||
| List<Order> orders = findByTableNumber(tableNum); | ||
| //해당 테이블에서 주문한 메뉴와 수량을 return한다. | ||
| public Map<Menu, Long> getBill(int tableNumber){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Map으로 들고 있는 것보단 객체로 빼서 책임을 할당하는건 어떨까요? 아래 코드에도 bill을 의미하는 맵을 가지고 로직을 처리하는 것보단, 가독성이나 추후 확장성에도 좋아보입니다. 개인적인 견해이니, 참고만 하시면 좋을 거 같습니다! |
||
| List<Order> orders = findByTableNumber(tableNumber); | ||
| Map<Menu, Long> bill = orders.stream() | ||
| .map(order -> MenuRepository.findByNumber(order.getMenuNum())) | ||
| .map(order -> MenuRepository.findByNumber(order.getMenuNumber())) | ||
| .collect(Collectors.groupingBy(order->order,HashMap::new, counting())); | ||
|
|
||
| return bill; | ||
| } | ||
|
|
||
|
|
||
| //지불이 완료되면 해당 테이블의 주문을 모두 삭제한다. | ||
| public void finishedPayment(int tableNumber){ | ||
| orders.removeIf(order->order.isEqualTable(tableNumber)); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isValidMenuNum 이 이미 boolean 이니 false와 비교하지 않고 바로 if(!isValidMenuNum) 으로 사용하셔도 좋을 것 같아요!