장바구니 기능을 고도화하여 다음과 같은 핵심 기능들을 추가 구현하였습니다.
사용자 편의성 개선
상품 추가 전 확인 절차 기능 추가
다중 상품 담기 기능 구현
상품별 수량 지정 기능 추가
주문 프로세스 강화
장바구니 내 총 주문 금액 계산 기능 추가
최종 주문 프로세스 구현 및 주문시 주문 받은 리스트를 초기화
try-catch를 활용한 예외 처리를 구현하면서 예외 처리 메커니즘에 대한 이해도를 높일 수 있었습니다.
package com.example.kiosk6;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Kiosk {
private final List<Cart> carts;
private final List<Menu> menus;
private final Scanner scanner;
public Kiosk(List<Menu> menus) {
this.carts = new ArrayList<>();
this.menus = menus;
this.scanner = new Scanner(System.in);
}
public void start() {
boolean running = true;
while (running) {
try {
showMainMenu();
int choice = scanner.nextInt();
if (choice == 0) {
running = false;
continue;
}
if (choice < 0 || choice > menus.size()) {
throw new IllegalArgumentException("잘못된 메뉴 번호입니다.");
}
if (choice >= 1 && choice <= menus.size()) {
Menu selectedMenu = menus.get(choice - 1);
selectedMenu.showMenu();
int menuChoice = scanner.nextInt();
if (menuChoice == 0) continue;
if (menuChoice < 0 || menuChoice > selectedMenu.getMenuItems().size()) {
throw new IllegalArgumentException("잘못된 메뉴 항목 번호입니다.");
}
MenuItem selectedItem = selectedMenu.getMenuItems().get(menuChoice - 1);
System.out.printf("선택한 메뉴: %-15s | W%-4.1f | %s%n", selectedItem.getName(), selectedItem.getPrice(), selectedItem.getDescribe());
System.out.println("위 메뉴를 장바구니에 추가하시겠습니까?");
System.out.println("1. 확인 2. 취소");
int inCart = scanner.nextInt();
if (inCart == 2)
continue;
else if (inCart != 1)
throw new IllegalArgumentException("잘못된 입력입니다.");
System.out.println("수량을 입력하세요: ");
int quantity = scanner.nextInt();
if (quantity <= 0) {
throw new IllegalArgumentException("수량은 1개 이상이어야 합니다.");
}
addToCart(selectedItem, quantity);
System.out.println(selectedItem.getName() + " " + quantity + "개가 장바구니에 추가되었습니다.");
if (!carts.isEmpty()) {
System.out.println("[ ORDER MENU ]");
System.out.println("4. Orders");
System.out.println("5. Cancel");
int orderDecision = scanner.nextInt();
if (orderDecision == 4) {
System.out.println("아래와 같이 주문하시겠습니까?");
System.out.println();
showCart();
System.out.println();
totalPrice();
System.out.println();
System.out.println("1. 주문 2. 메뉴판");
int finalDecision = scanner.nextInt();
if (finalDecision == 1) {
System.out.println();
double totalPrice = 0;
for (Cart item : carts) {
MenuItem menuItem = item.getMenuItem();
totalPrice += item.getTotalPrice();
}
System.out.println("주문이 완료되었습니다. 금액은 W " + totalPrice + " 입니다.");
System.out.println();
System.out.println();
carts.clear();
continue;
} else if (finalDecision == 2) {
continue;
} else
throw new IllegalArgumentException("잘못된 입력입니다.");
} else if (orderDecision == 5) {
System.out.println("주문을 취소합니다.");
continue;
} else
throw new IllegalArgumentException("잘못된 입력입니다.");
}
}
} catch (IllegalArgumentException e) {
System.out.println("오류: " + e.getMessage());
scanner.nextLine(); // 입력 버퍼 비우기
} catch (Exception e) {
System.out.println("잘못된 입력입니다. 다시 시도해주세요.");
scanner.nextLine(); // 입력 버퍼 비우기
}
}
}
private void showMainMenu() {
System.out.println("[ MAIN MENU ]");
for (int i = 0; i < menus.size(); i++) {
System.out.printf("%-2d. %-10s%n", (i + 1), menus.get(i).getName());
}
System.out.printf("%-2d. %-10s | %s%n", 0, "종료", "종료");
}
private void addToCart(MenuItem item, int quantity) {
for (Cart cartItem : carts) {
if (cartItem.getMenuItem().getName().equals(item.getName())) {
cartItem.addQuantity(quantity);
return;
}
}
carts.add(new Cart(item, quantity));
}
private void showCart() {
System.out.println("\n[ 장바구니 ]");
for (Cart item : carts) {
MenuItem menuItem = item.getMenuItem();
System.out.printf("%-15s | W%-4.1f | %s - %d개%n", menuItem.getName(), menuItem.getPrice(), menuItem.getDescribe(), item.getQuantity());
}
}
private void totalPrice() {
System.out.println("[ TOTAL ]");
double totalPrice = 0;
for (Cart item : carts) {
MenuItem menuItem = item.getMenuItem();
totalPrice += item.getTotalPrice();
}
System.out.printf("W%.1f%n", totalPrice);
System.out.println();
}
}
package com.example.kiosk6;
import java.util.ArrayList;
import java.util.List;
public class Menu {
private final String name;
private final List<MenuItem> menuItems;
private final List<Cart> carts;
public Menu(String name) {
this.name = name;
this.menuItems = new ArrayList<>();
this.carts = new ArrayList<>();
}
public void addMenuItem(MenuItem item) {
menuItems.add(item);
}
public String getName() {
return name;
}
public List<MenuItem> getMenuItems() {
return menuItems;
}
public List<Cart> getCarts() {
return carts;
}
public void showMenu() {
System.out.println("[ " + name + " MENU ]");
for (int i = 0; i < menuItems.size(); i++) {
MenuItem item = menuItems.get(i);
System.out.printf("%-2d. %-15s | W%-4.1f | %s%n",
(i + 1),
item.getName(),
item.getPrice(),
item.getDescribe());
}
System.out.printf("%-2d. %-10s | %s%n", 0, "종료", "종료");
}
}
package com.example.kiosk6;
public class MenuItem {
private final String name;
private final double price;
private final String describe;
public MenuItem(String name, double price, String describe) {
this.name = name;
this.price = price;
this.describe = describe;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public String getDescribe() {
return describe;
}
}
package com.example.kiosk6;
public class Cart {
private final MenuItem menuItem;
private int quantity;
public Cart(MenuItem menuItem, int quantity) {
this.menuItem = menuItem;
this.quantity = quantity;
}
public MenuItem getMenuItem() {
return menuItem;
}
public int getQuantity() {
return quantity;
}
public void addQuantity(int quantity) {
this.quantity += quantity;
}
public double getTotalPrice() {
return menuItem.getPrice() * quantity;
}
}
'TIL' 카테고리의 다른 글
오늘 배운 것 (0) | 2025.01.21 |
---|---|
[키오스크 만들기] 키오스크 만들기 4일차 (0) | 2025.01.20 |
[키오스크 만들기] 키오스크 만들기 2일차 (3) | 2025.01.16 |
[스타크래프트 게임 출력] 2일차 (1) | 2025.01.15 |
[스타크래프트 게임 출력] 1일차 (0) | 2025.01.14 |