Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- interface IMenu {
- void print();
- String getName();
- double getPrice();
- }
- class MenuItem implements IMenu {
- private String name;
- private double price;
- public MenuItem(String name, double price) {
- this.name = name;
- this.price = price;
- }
- @Override
- public void print() {
- System.out.println(String.format("%s, $%.1f", getName(), getPrice()));
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- return price;
- }
- }
- abstract class MenuItemDecorator implements IMenu {
- protected IMenu menuItem;
- public MenuItemDecorator(IMenu menuItem) {
- this.menuItem = menuItem;
- }
- @Override
- public abstract void print();
- @Override
- public abstract String getName();
- @Override
- public abstract double getPrice();
- }
- class SpicyDecorator extends MenuItemDecorator {
- public SpicyDecorator(IMenu menuItem) {
- super(menuItem);
- }
- @Override
- public void print() {
- menuItem.print(); // Here is the idea of using decorator
- System.out.println("-- This item is spicy (+ $2)");
- }
- @Override
- public String getName() {
- return menuItem.getName();
- }
- @Override
- public double getPrice() {
- return menuItem.getPrice() + 2;
- }
- }
- class VegetarianDecorator extends MenuItemDecorator {
- public VegetarianDecorator(IMenu menuItem) {
- super(menuItem);
- }
- @Override
- public void print() {
- menuItem.print();
- System.out.println("-- This item is vegetarian (+ $4)");
- }
- @Override
- public String getName() {
- return menuItem.getName();
- }
- @Override
- public double getPrice() {
- return menuItem.getPrice() + 4;
- }
- }
- class CompositeMenu implements IMenu {
- private String name;
- private List<IMenu> menuItems;
- public CompositeMenu(String name) {
- this.name = name;
- this.menuItems = new ArrayList<>();
- }
- public void addMenuItem(IMenu menuItem) {
- menuItems.add(menuItem);
- }
- public void removeMenuItem(IMenu menuItem) {
- menuItems.remove(menuItem);
- }
- @Override
- public void print() {
- System.out.println(String.format("%s [ $%.1f ]", getName(), getPrice()));
- System.out.println("------------------------");
- for (IMenu menuItem : menuItems) {
- menuItem.print();
- System.out.println();
- }
- System.out.println();
- }
- @Override
- public String getName() {
- return name;
- }
- @Override
- public double getPrice() {
- double price = 0;
- for (IMenu menuItem : menuItems) {
- price += menuItem.getPrice();
- }
- return price;
- }
- }
- class Main {
- public static void main(String[] args) {
- // Create the menus
- CompositeMenu mainMenu = new CompositeMenu("Main Menu");
- CompositeMenu appetizerMenu = new CompositeMenu("Appetizer Menu");
- CompositeMenu dessertMenu = new CompositeMenu("Dessert Menu");
- // Create the menu items
- MenuItem garlicBread = new MenuItem("Garlic bread", 5.5);
- MenuItem chickenWings = new MenuItem("Chicken wings", 12.5);
- MenuItem tomatoSoup = new MenuItem("Tomato soup", 10.5);
- MenuItem pie = new MenuItem("Pie", 4.5);
- MenuItem iceCream = new MenuItem("Ice cream", 3.6);
- // Add the menu items to the menus
- appetizerMenu.addMenuItem(new VegetarianDecorator(garlicBread));
- appetizerMenu.addMenuItem(new SpicyDecorator(chickenWings));
- appetizerMenu.addMenuItem(new VegetarianDecorator(new SpicyDecorator(tomatoSoup)));
- dessertMenu.addMenuItem(pie);
- dessertMenu.addMenuItem(pie);
- dessertMenu.addMenuItem(new SpicyDecorator(iceCream));
- mainMenu.addMenuItem(appetizerMenu);
- mainMenu.addMenuItem(dessertMenu);
- // Print the menus
- mainMenu.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement