From c91d1797a095569f83b679ff2a66c495667f8945 Mon Sep 17 00:00:00 2001
From: hjoon <guswns3371@gmail.com>
Date: Sun, 22 Jan 2023 16:11:41 +0900
Subject: [PATCH] =?UTF-8?q?9=EC=A3=BC=EC=B0=A8=20:=20=EC=BB=B4=ED=8F=AC?=
 =?UTF-8?q?=EC=A7=80=ED=8A=B8=20=ED=8C=A8=ED=84=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .idea/misc.xml                        |  2 +-
 src/composite/CompositePattern.java   | 61 +++++++++++++++++++++++++++
 src/composite/Waitress.java           | 16 +++++++
 src/composite/menu/Menu.java          | 52 +++++++++++++++++++++++
 src/composite/menu/MenuComponent.java | 35 +++++++++++++++
 src/composite/menu/MenuItem.java      | 45 ++++++++++++++++++++
 6 files changed, 210 insertions(+), 1 deletion(-)
 create mode 100644 src/composite/CompositePattern.java
 create mode 100644 src/composite/Waitress.java
 create mode 100644 src/composite/menu/Menu.java
 create mode 100644 src/composite/menu/MenuComponent.java
 create mode 100644 src/composite/menu/MenuItem.java

diff --git a/.idea/misc.xml b/.idea/misc.xml
index f760089..521c4f0 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -4,7 +4,7 @@
     <option name="enabled" value="true" />
     <option name="wasEnabledAtLeastOnce" value="true" />
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_18" default="true" project-jdk-name="18" project-jdk-type="JavaSDK">
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/out" />
   </component>
   <component name="ProjectType">
diff --git a/src/composite/CompositePattern.java b/src/composite/CompositePattern.java
new file mode 100644
index 0000000..a5e5168
--- /dev/null
+++ b/src/composite/CompositePattern.java
@@ -0,0 +1,61 @@
+package composite;
+
+import composite.menu.Menu;
+import composite.menu.MenuComponent;
+import composite.menu.MenuItem;
+
+public class CompositePattern {
+    public static void main(String[] args) {
+        MenuComponent pancakeHouseMenu = new Menu("pancake house menu", "아침 메뉴");
+        MenuComponent dinerMenu = new Menu("diner menu", "점심 메뉴");
+        MenuComponent cafeMenu = new Menu("cafe menu", "저녁 메뉴");
+        MenuComponent dessertMenu = new Menu("dessert menu", "디저트 메뉴");
+        MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");
+
+        MenuComponent allMenu = new Menu("all menu", "전체 메뉴");
+
+        allMenu.add(pancakeHouseMenu);
+        allMenu.add(dinerMenu);
+        allMenu.add(cafeMenu);
+        pancakeHouseMenu.add(new MenuItem(
+                "K&B's Pancake Breakfast",
+                "Pancakes with scrambled eggs and toast",
+                true,
+                2.99));
+
+
+        dinerMenu.add(new MenuItem(
+                "Vegetarian BLT",
+                "(Fakin') Bacon with lettuce & tomato on whole wheat",
+                true,
+                2.99));
+
+        allMenu.add(dessertMenu);
+
+        dessertMenu.add(new MenuItem(
+                "Apple Pie",
+                "Apple pie with a flakey crust, topped with vanilla icecream",
+                true,
+                1.59));
+
+        cafeMenu.add(new MenuItem(
+                "Veggie Burger and Air Fries",
+                "Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
+                true,
+                3.99));
+
+        cafeMenu.add(coffeeMenu); // cafe 메뉴 하위에 coffee 메뉴
+        coffeeMenu.add(new MenuItem(
+                "Bagel",
+                "Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
+                false,
+                0.69));
+        coffeeMenu.add(new MenuItem(
+                "Biscotti",
+                "Three almond or hazelnut biscotti cookies",
+                true,
+                0.89));
+        Waitress waitress = new Waitress(allMenu);
+        waitress.printMenu();
+    }
+}
diff --git a/src/composite/Waitress.java b/src/composite/Waitress.java
new file mode 100644
index 0000000..bc862b3
--- /dev/null
+++ b/src/composite/Waitress.java
@@ -0,0 +1,16 @@
+package composite;
+
+
+import composite.menu.MenuComponent;
+
+public class Waitress {
+    private final MenuComponent allMenus; // 트리의 최상위 노드
+
+    public Waitress(MenuComponent allMenus) {
+        this.allMenus = allMenus;
+    }
+
+    public void printMenu() {
+        allMenus.print();
+    }
+}
diff --git a/src/composite/menu/Menu.java b/src/composite/menu/Menu.java
new file mode 100644
index 0000000..f7bced6
--- /dev/null
+++ b/src/composite/menu/Menu.java
@@ -0,0 +1,52 @@
+package composite.menu;
+
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Menu extends MenuComponent {
+    private final List<MenuComponent> menuComponents = new ArrayList<>();
+    private final String name;
+    private final String description;
+
+    public Menu(String name, String description) {
+        this.name = name;
+        this.description = description;
+    }
+
+    @Override
+    public void add(MenuComponent menuComponent) {
+        menuComponents.add(menuComponent);
+    }
+
+    @Override
+    public void remove(MenuComponent menuComponent) {
+        menuComponents.remove(menuComponent);
+    }
+
+    @Override
+    public MenuComponent getChild(int i) {
+        return menuComponents.get(i);
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public String getDescription() {
+        return description;
+    }
+
+    @Override
+    public void print() {
+        System.out.print("\n" + getName());
+        System.out.println(", " + getDescription());
+        System.out.println("-------------------");
+
+        for (MenuComponent menuComponent : menuComponents) {
+            menuComponent.print();
+        }
+    }
+}
diff --git a/src/composite/menu/MenuComponent.java b/src/composite/menu/MenuComponent.java
new file mode 100644
index 0000000..efe3b7b
--- /dev/null
+++ b/src/composite/menu/MenuComponent.java
@@ -0,0 +1,35 @@
+package composite.menu;
+
+public abstract class MenuComponent {
+    public void add(MenuComponent menuComponent) {
+        throw new UnsupportedOperationException();
+    }
+
+    public void remove(MenuComponent menuComponent) {
+        throw new UnsupportedOperationException();
+    }
+
+    public MenuComponent getChild(int i) {
+        throw new UnsupportedOperationException();
+    }
+
+    public String getName() {
+        throw new UnsupportedOperationException();
+    }
+
+    public String getDescription() {
+        throw new UnsupportedOperationException();
+    }
+
+    public double getPrice() {
+        throw new UnsupportedOperationException();
+    }
+
+    public boolean isVegetarian() {
+        throw new UnsupportedOperationException();
+    }
+
+    public void print() {
+        throw new UnsupportedOperationException();
+    }
+}
diff --git a/src/composite/menu/MenuItem.java b/src/composite/menu/MenuItem.java
new file mode 100644
index 0000000..5aa54be
--- /dev/null
+++ b/src/composite/menu/MenuItem.java
@@ -0,0 +1,45 @@
+package composite.menu;
+
+public class MenuItem extends MenuComponent {
+    private final String name;
+    private final String description;
+    private final boolean vegetarian;
+    private final double price;
+
+    public MenuItem(String name, String description, boolean vegetarian, double price) {
+        this.name = name;
+        this.description = description;
+        this.vegetarian = vegetarian;
+        this.price = price;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    @Override
+    public String getDescription() {
+        return description;
+    }
+
+    @Override
+    public boolean isVegetarian() {
+        return vegetarian;
+    }
+
+    @Override
+    public double getPrice() {
+        return price;
+    }
+
+    @Override
+    public void print() {
+        System.out.print(" " + getName());
+        if (isVegetarian()) {
+            System.out.print("(v)");
+        }
+        System.out.println(", " + getPrice());
+        System.out.println("    ---" + getDescription());
+    }
+}
