-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat : 결제 기능 #15
base: jbm
Are you sure you want to change the base?
feat : 결제 기능 #15
Changes from all 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,66 +1,70 @@ | ||
import domain.Menu; | ||
//import jdk.internal.util.xml.impl.Input; | ||
import repository.MenuRepository; | ||
import domain.Table; | ||
import repository.OrderRepository; | ||
import repository.TableRepository; | ||
import java.util.NoSuchElementException; | ||
|
||
import domain.Bill; | ||
import domain.PayType; | ||
import service.CafeOrderService; | ||
import view.InputView; | ||
import view.OutputView; | ||
|
||
import java.util.List; | ||
|
||
public class Application { | ||
// TODO 구현 진행 | ||
public static void main(String[] args) { | ||
OutputView.printMain(); | ||
final CafeOrderService cafeOrderService = new CafeOrderService(new OrderRepository()); | ||
final List<Table> tables = TableRepository.tables(); | ||
int func = InputView.inputFunction(); | ||
final static int ORDER = 1; | ||
final static int PAY = 2; | ||
final static int STOP = 3; | ||
|
||
// TODO 구현 진행 | ||
public static void main(String[] args) { | ||
OutputView.printMain(); | ||
final CafeOrderService cafeOrderService = new CafeOrderService(); | ||
int selectedFunction = InputView.inputFunction(); | ||
|
||
while(func!=3) | ||
{ | ||
if(func == 1) | ||
{ | ||
orderMenu(tables, cafeOrderService); | ||
} | ||
if(func == 2) | ||
{ | ||
payOrders(tables, cafeOrderService); | ||
} | ||
OutputView.printMain(); | ||
func = InputView.inputFunction(); | ||
} | ||
} | ||
while (selectedFunction != STOP) { | ||
if (selectedFunction == ORDER) { | ||
orderMenu(cafeOrderService); | ||
} | ||
if (selectedFunction == PAY) { | ||
payOrders(cafeOrderService); | ||
} | ||
if (!cafeOrderService.isValidFunction(selectedFunction, STOP, ORDER)) { | ||
throw new NoSuchElementException("해당 기능은 존재하지 않습니다."); | ||
} | ||
OutputView.printMain(); | ||
selectedFunction = InputView.inputFunction(); | ||
} | ||
} | ||
|
||
private static void payOrders(List<Table> tables, CafeOrderService cafeOrderService) { | ||
OutputView.printTables(tables, cafeOrderService); | ||
int tableNum = InputView.inputPayTableNumber(); | ||
OutputView.printOrders(cafeOrderService, tableNum); | ||
} | ||
private static void payOrders(CafeOrderService cafeOrderService) { | ||
OutputView.printTables(cafeOrderService); | ||
int tableNumber = InputView.inputPayTableNumber(); | ||
if (!cafeOrderService.checkOrderedTable(tableNumber)) { | ||
OutputView.printNoOrder(); | ||
return; | ||
} | ||
Bill bill = cafeOrderService.getBillByTable(tableNumber); | ||
OutputView.printBill(bill); | ||
OutputView.printPayMessage(tableNumber); | ||
int payTypeNumber = InputView.inputPayType(); | ||
PayType payType = cafeOrderService.findPayType(payTypeNumber); | ||
long amountOfPayment = cafeOrderService.getAmountOfPayment(bill, payType); | ||
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); | ||
if(isValidTblNum==false){ | ||
orderMenu(tables, cafeOrderService); | ||
} | ||
final List<Menu> menus = MenuRepository.menus(); | ||
OutputView.printMenus(menus); | ||
int menuNum = InputView.inputMenu(); | ||
boolean isValidMenuNum = cafeOrderService.checkMenuNum(menuNum); | ||
if(isValidMenuNum==false){ | ||
OutputView.printMenuAlert(); | ||
orderMenu(tables, cafeOrderService); | ||
} | ||
int menuCount = InputView.inputCount(); | ||
boolean isValidMenuCount = cafeOrderService.canOrderMenu(tableNum, menuNum, menuCount); | ||
if(isValidMenuCount==false){ | ||
OutputView.printMaxAlert(); | ||
return; | ||
} | ||
cafeOrderService.orderMenu(menuNum, menuCount, tableNum); | ||
} | ||
private static void orderMenu(CafeOrderService cafeOrderService) { | ||
OutputView.printTables(cafeOrderService); | ||
int tableNumber = InputView.inputTableNumber(); | ||
if (!cafeOrderService.isValidTableNumber(tableNumber)) { | ||
orderMenu(cafeOrderService); | ||
} | ||
OutputView.printMenus(); | ||
int menuNumber = InputView.inputMenu(); | ||
if (!cafeOrderService.isValidMenuNumber(menuNumber)) { | ||
OutputView.printMenuAlert(); | ||
orderMenu(cafeOrderService); | ||
} | ||
int menuCount = InputView.inputCount(); | ||
if (!cafeOrderService.checkMaximumCount(tableNumber, menuNumber, menuCount)) { | ||
OutputView.printMaxAlert(); | ||
return; | ||
} | ||
cafeOrderService.orderMenu(tableNumber, menuNumber, menuCount); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package domain; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import repository.MenuRepository; | ||
import repository.OrderRepository; | ||
|
||
//주문 메뉴와 수량을 알고있는 객체. 계산과 관련된 것을 책임진다. | ||
public class Bill { | ||
private static final int DISCOUNT_FOR_CAKES = 3; | ||
private static final int DISCOUNT_MONEY_PER_NUM_OF_CAKES = 3000; | ||
private static final int MAX_NUMBER_PER_MENU = 30; | ||
|
||
private final int tableNumber; | ||
private Map<Menu, Integer> orderedMenus; | ||
|
||
public Bill(int tableNumber) { | ||
this.tableNumber = tableNumber; | ||
this.orderedMenus = Collections.emptyMap(); | ||
} | ||
|
||
//생성 | ||
public Bill(OrderRepository orderRepository, 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. Bill 객체가 orderRepository를 인자로 받아서 생성하지 않고 Bill 객체를 생성하는 주체가 orderRepository에서 order List를 조회해서 넘겨주는 방식은 어떻게 생각하시나요??? |
||
List<Order> orders = orderRepository.findByTableNumber(tableNumber); | ||
Map<Menu, Integer> orderedMenus = orders.stream() | ||
.filter(order -> order.getTableNumber() == tableNumber) | ||
.collect(Collectors.toMap(order -> MenuRepository.findByNumber(order.getMenuNumber()), | ||
order -> order.getMenuCount(), (c1, c2) -> c1 + c2)); | ||
|
||
this.tableNumber = tableNumber; | ||
this.orderedMenus = orderedMenus; | ||
} | ||
|
||
public int getTableNumber() { | ||
return tableNumber; | ||
} | ||
|
||
public Map<Menu, Integer> getOrderedMenus() { | ||
return this.orderedMenus; | ||
} | ||
|
||
//최대수량 검사 | ||
public boolean checkMaximumPerMenu(int menuNumber, int menuCount) { | ||
int existedMenusCount = this.countMenusNumber(menuNumber); | ||
return existedMenusCount + menuCount <= MAX_NUMBER_PER_MENU; | ||
} | ||
|
||
//해당 테이블에서 주문한 해당 메뉴의 수량을 return한다. | ||
public int countMenusNumber(int menuNumber) { | ||
int menuCount = 0; | ||
for (Menu key : orderedMenus.keySet()) { | ||
if (key.getNumber() != menuNumber) | ||
continue; | ||
menuCount = orderedMenus.get(key); | ||
break; | ||
} | ||
Comment on lines
+54
to
+59
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. stream의 findFirst를 사용해서 찾아보는건 어떨까요??? |
||
return menuCount; | ||
} | ||
|
||
//해당 테이블의 bill을 통해 주문한 케익의 갯수를 계산한다. | ||
public int getNumberOfCakes() { | ||
int numberOfCakes = 0; | ||
for (Map.Entry<Menu, Integer> entry : this.orderedMenus.entrySet()) { | ||
numberOfCakes += entry.getKey().isThisCake() ? entry.getValue() : 0; | ||
} | ||
return numberOfCakes; | ||
} | ||
|
||
//해당 테이블의 bill을 통해 주문한 메뉴들 가격의 총 합을 계산한다. | ||
public long getSumOfPayment() { | ||
long totalSumOfPayment = 0; | ||
for (Map.Entry<Menu, Integer> entry : this.orderedMenus.entrySet()) { | ||
totalSumOfPayment += entry.getKey().getPrice() * entry.getValue(); | ||
} | ||
return totalSumOfPayment; | ||
} | ||
|
||
//할인을 적용한 최종 금액을 계산한다. | ||
public long getDiscountedPayment(int discountRate) { | ||
long totalSumOfPayment = this.getSumOfPayment(); | ||
int numberOfCakes = this.getNumberOfCakes(); | ||
if (numberOfCakes >= DISCOUNT_FOR_CAKES) { | ||
totalSumOfPayment -= DISCOUNT_MONEY_PER_NUM_OF_CAKES * (numberOfCakes / 3); | ||
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. 요부분 만들어주신 상수 |
||
} | ||
totalSumOfPayment = (long)(totalSumOfPayment - totalSumOfPayment * ((float)discountRate / 100)); | ||
return totalSumOfPayment; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package domain; | ||
|
||
public enum Category { | ||
CAKE("케이크"), | ||
BEVERAGE("음료"); | ||
CAKE("케이크"), | ||
BEVERAGE("음료"); | ||
|
||
private final String name; | ||
private final String name; | ||
|
||
Category(final String name) { | ||
this.name = name; | ||
} | ||
Category(final String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "[" + name + "]"; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[" + name + "]"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,44 @@ | ||
package domain; | ||
|
||
public class Menu { | ||
private final int number; | ||
private final String name; | ||
private final Category category; | ||
private final int price; | ||
|
||
public Menu(final int number, final String name, final Category category, final int price) { | ||
this.number = number; | ||
this.name = name; | ||
this.category = category; | ||
this.price = price; | ||
} | ||
|
||
public int getNumber() { | ||
return number; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Category getCategory() { | ||
return category; | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return category + " " + number + " - " + name + " : " + price + "원"; | ||
} | ||
private final int number; | ||
private final String name; | ||
private final Category category; | ||
private final int price; | ||
|
||
public Menu(final int number, final String name, final Category category, final int price) { | ||
this.number = number; | ||
this.name = name; | ||
this.category = category; | ||
this.price = price; | ||
} | ||
|
||
public int getNumber() { | ||
return number; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Category getCategory() { | ||
return category; | ||
} | ||
|
||
public int getPrice() { | ||
return price; | ||
} | ||
|
||
public boolean isEqualNumber(int number) { | ||
return this.number == number; | ||
} | ||
|
||
public boolean isThisCake() { | ||
return this.category == Category.CAKE; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return category + " " + number + " - " + name + " : " + price + "원"; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,33 @@ | ||
package domain; | ||
|
||
public class Order { | ||
private final int menuNum; | ||
private final int tableNum; | ||
private final int tableNumber; | ||
private final int menuNumber; | ||
private final int menuCount; | ||
|
||
public Order(int menuNum, int tableNum) { | ||
this.menuNum = menuNum; | ||
this.tableNum = tableNum; | ||
} | ||
public Order(int tableNumber, int menuNumber, int menuCount) { | ||
this.tableNumber = tableNumber; | ||
this.menuNumber = menuNumber; | ||
this.menuCount = menuCount; | ||
} | ||
|
||
public int getMenuNum() { | ||
return this.menuNum; | ||
} | ||
public int getMenuNumber() { | ||
return this.menuNumber; | ||
} | ||
|
||
public int getTableNum() { | ||
return tableNum; | ||
} | ||
public int getTableNumber() { | ||
return this.tableNumber; | ||
} | ||
|
||
public boolean isEqualTable(int tableNum){ | ||
return this.tableNum == tableNum; | ||
} | ||
public int getMenuCount() { | ||
return this.menuCount; | ||
} | ||
|
||
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,21 @@ | ||
package domain; | ||
|
||
//지불 수단 | ||
public class PayType { | ||
private final int number; | ||
private final int discountRate; | ||
|
||
public PayType(int number, int discountRate) { | ||
this.number = number; | ||
this.discountRate = discountRate; | ||
} | ||
|
||
public boolean isEqualPayType(int number) { | ||
return this.number == number; | ||
} | ||
|
||
public int getDiscountRate() { | ||
return this.discountRate; | ||
} | ||
|
||
} |
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.
상수 👍