본문 바로가기

자바(java)

음식 주문 시스템: 메뉴에서 음식을 선택하고 주문하는 시스템을 구축합니다.

반응형

음식 주문 시스템: 자바 구현

1. 내용 설명

음식 주문 시스템은 사용자가 메뉴에서 음식을 선택하고 주문할 수 있는 간단한 자바 기반 애플리케이션입니다. 사용자는 주어진 메뉴 목록에서 원하는 음식을 선택하고, 선택한 음식의 총 금액을 계산하여 주문을 완료할 수 있습니다. 이 시스템은 Swing을 사용하여 GUI를 제공합니다.

2. 프로그램 간 사용 함수 설명

  • createAndShowGUI(): 프로그램의 GUI를 생성하고 보여줍니다.
  • initializeMenu(): 사용 가능한 메뉴 항목을 초기화합니다.
  • addOrder(): 선택한 음식을 주문 목록에 추가합니다.
  • calculateTotal(): 주문한 음식의 총 금액을 계산합니다.

3. 코딩 내용

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;

public class FoodOrderSystem {
    private JFrame frame;
    private JComboBox<String> menuComboBox;
    private JTextArea orderList;
    private JLabel totalLabel;
    private Map<String, Integer> menuPrices;
    private int total;

    public FoodOrderSystem() {
        createAndShowGUI();
    }

    private void createAndShowGUI() {
        frame = new JFrame("Food Order System");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        menuComboBox = new JComboBox<>();
        JButton orderButton = new JButton("Add to Order");
        orderButton.addActionListener(e -> addOrder());

        orderList = new JTextArea(10, 30);
        orderList.setEditable(false);

        totalLabel = new JLabel("Total: 0");

        frame.add(menuComboBox, BorderLayout.NORTH);
        frame.add(orderButton, BorderLayout.CENTER);
        frame.add(new JScrollPane(orderList), BorderLayout.EAST);
        frame.add(totalLabel, BorderLayout.SOUTH);

        initializeMenu();
        frame.pack();
        frame.setVisible(true);
    }

    private void initializeMenu() {
        menuPrices = new HashMap<>();
        menuPrices.put("Pizza", 20);
        menuPrices.put("Burger", 15);
        menuPrices.put("Pasta", 12);
        menuPrices.forEach((food, price) -> menuComboBox.addItem(food + " - $" + price));
    }

    private void addOrder() {
        String selectedItem = (String) menuComboBox.getSelectedItem();
        if (selectedItem != null) {
            orderList.append(selectedItem + "\n");
            calculateTotal();
        }
    }

    private void calculateTotal() {
        String[] items = orderList.getText().split("\n");
        total = 0;
        for (String item : items) {
            String food = item.split(" - ")[0];
            total += menuPrices.get(food);
        }
        totalLabel.setText("Total: $" + total);
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> new FoodOrderSystem());
    }
}

 

4. 전문용어

  • JFrame: 자바 Swing의 창 프레임입니다.
  • JComboBox: 드롭다운 메뉴를 제공하는 컴포넌트입니다.
  • JTextArea: 텍스트 영역을 표시하는 컴포넌트입니다.
  • JLabel: 텍스트 레이블을 표시하는 컴포넌트입니다.
  • HashMap: 키-값 쌍을 저장하는 자바의 컬렉션 클래스입니다.
  • ActionListener: 이벤트 리스너 인터페이스입니다.

5. 라이브러리 추가 및 실행 방법

  • 본 프로그램은 자바 표준 라이브러리의 Swing 컴포넌트를 사용합니다.
반응형