From 349cdb484e5bb36a7d499540dbf1b713d6493434 Mon Sep 17 00:00:00 2001 From: Namu Date: Wed, 22 Oct 2025 23:15:15 +0200 Subject: [PATCH] Feat: Decorator implemented --- .gitignore | 31 +++++++++++++++ src/Main.java | 6 +++ src/application/Application.java | 24 ++++++++++++ src/decorator/KebabDecorator.java | 27 +++++++++++++ src/decorator/TendersKebabDecorator.java | 20 ++++++++++ src/decorator/TomatoKebabDecorator.java | 20 ++++++++++ src/entities/Addon.java | 27 +++++++++++++ src/entities/Command.java | 42 ++++++++++++++++++++ src/entities/CommandState.java | 9 +++++ src/entities/Kebab.java | 50 ++++++++++++++++++++++++ src/entities/TendersAddon.java | 7 ++++ src/entities/TomatoAddon.java | 7 ++++ src/managers/CommandManager.java | 31 +++++++++++++++ 13 files changed, 301 insertions(+) create mode 100644 .gitignore create mode 100644 src/Main.java create mode 100644 src/application/Application.java create mode 100644 src/decorator/KebabDecorator.java create mode 100644 src/decorator/TendersKebabDecorator.java create mode 100644 src/decorator/TomatoKebabDecorator.java create mode 100644 src/entities/Addon.java create mode 100644 src/entities/Command.java create mode 100644 src/entities/CommandState.java create mode 100644 src/entities/Kebab.java create mode 100644 src/entities/TendersAddon.java create mode 100644 src/entities/TomatoAddon.java create mode 100644 src/managers/CommandManager.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1de677d --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ +.kotlin +.idea/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..5c42cc1 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,6 @@ +import application.Application; + +public static void main(String[] args) { + var app = new Application(); + app.run(); +} diff --git a/src/application/Application.java b/src/application/Application.java new file mode 100644 index 0000000..01652ae --- /dev/null +++ b/src/application/Application.java @@ -0,0 +1,24 @@ +package application; + +import decorator.TendersKebabDecorator; +import decorator.TomatoKebabDecorator; +import entities.Command; +import entities.Kebab; +import managers.CommandManager; + +public class Application { + public void run() { + var commandManager = CommandManager.getInstance(); + + Kebab kebab = new Kebab("3 viandes", 10.0); + + Kebab kebab1 = new TomatoKebabDecorator(new TendersKebabDecorator(new TomatoKebabDecorator(new TomatoKebabDecorator(new Kebab("3 viandes", 10.0))))).getKebab(); + + Command command = new Command(); + command.addKebab(kebab); + command.addKebab(kebab1); + + commandManager.add(command); + commandManager.list(); + } +} diff --git a/src/decorator/KebabDecorator.java b/src/decorator/KebabDecorator.java new file mode 100644 index 0000000..fb409e7 --- /dev/null +++ b/src/decorator/KebabDecorator.java @@ -0,0 +1,27 @@ +package decorator; + +import entities.Kebab; + +public abstract class KebabDecorator { + private Kebab kebab; + + public KebabDecorator(Kebab kebab) { + this.kebab = kebab; + } + + public KebabDecorator(KebabDecorator kebabDecorator) { + this.kebab = kebabDecorator.getKebab(); + } + + public Kebab getKebab() { + return kebab; + } + + public String getName() { + return kebab.getName(); + } + + public double getPrice() { + return kebab.getPrice(); + } +} diff --git a/src/decorator/TendersKebabDecorator.java b/src/decorator/TendersKebabDecorator.java new file mode 100644 index 0000000..1bb2fe7 --- /dev/null +++ b/src/decorator/TendersKebabDecorator.java @@ -0,0 +1,20 @@ +package decorator; + +import entities.Kebab; +import entities.TendersAddon; + +public class TendersKebabDecorator extends KebabDecorator { + public TendersKebabDecorator(Kebab kebab) { + super(kebab); + + TendersAddon tenders = new TendersAddon(); + kebab.addIngredient(tenders); + } + + public TendersKebabDecorator(KebabDecorator kebabDecorator) { + super(kebabDecorator); + + TendersAddon tenders = new TendersAddon(); + kebabDecorator.getKebab().addIngredient(tenders); + } +} diff --git a/src/decorator/TomatoKebabDecorator.java b/src/decorator/TomatoKebabDecorator.java new file mode 100644 index 0000000..fb64364 --- /dev/null +++ b/src/decorator/TomatoKebabDecorator.java @@ -0,0 +1,20 @@ +package decorator; + +import entities.Kebab; +import entities.TomatoAddon; + +public class TomatoKebabDecorator extends KebabDecorator { + public TomatoKebabDecorator(Kebab kebab) { + super(kebab); + + TomatoAddon tomato = new TomatoAddon(); + kebab.addIngredient(tomato); + } + + public TomatoKebabDecorator(KebabDecorator kebabDecorator) { + super(kebabDecorator); + + TomatoAddon tomato = new TomatoAddon(); + kebabDecorator.getKebab().addIngredient(tomato); + } +} diff --git a/src/entities/Addon.java b/src/entities/Addon.java new file mode 100644 index 0000000..382bb37 --- /dev/null +++ b/src/entities/Addon.java @@ -0,0 +1,27 @@ +package entities; + +public abstract class Addon { + private String name; + private double price; + + public Addon(String name, double price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } +} diff --git a/src/entities/Command.java b/src/entities/Command.java new file mode 100644 index 0000000..b674e77 --- /dev/null +++ b/src/entities/Command.java @@ -0,0 +1,42 @@ +package entities; + +import java.util.ArrayList; +import java.util.List; + +public class Command { + private List kebabs; + private CommandState state; + + public Command() { + this.kebabs = new ArrayList<>(); + this.state = CommandState.CREATED; + } + + public List getKebabs() { + return kebabs; + } + + public void addKebab(Kebab kebab) { + kebabs.add(kebab); + } + + public void setKebabs(List kebabs) { + this.kebabs = kebabs; + } + + public CommandState getState() { + return state; + } + + public void setState(CommandState state) { + this.state = state; + } + + @Override + public String toString() { + return "Command{" + + "kebabs=" + kebabs + + ", state=" + state + + '}'; + } +} diff --git a/src/entities/CommandState.java b/src/entities/CommandState.java new file mode 100644 index 0000000..7233f1a --- /dev/null +++ b/src/entities/CommandState.java @@ -0,0 +1,9 @@ +package entities; + +public enum CommandState { + CREATED, + RECEIVED, + COOKED, + DELIVERED, + FAILED +} diff --git a/src/entities/Kebab.java b/src/entities/Kebab.java new file mode 100644 index 0000000..13ab744 --- /dev/null +++ b/src/entities/Kebab.java @@ -0,0 +1,50 @@ +package entities; + +import java.util.ArrayList; +import java.util.List; + +public class Kebab { + private String name; + private double price; + private List addons; + + public Kebab(String name, double price) { + this.name = name; + this.price = price; + this.addons = new ArrayList<>(); + } + + public String getName() { + StringBuilder name = new StringBuilder(); + + name.append(this.name); + + for (Addon addon : addons) { + name.append(", "); + name.append(addon.getName()); + } + + return name.toString(); + } + + public double getPrice() { + double price = 0.0d; + + for (Addon addon : addons) { + price += addon.getPrice(); + } + return price + this.price; + } + + public void addIngredient(Addon addon) { + addons.add(addon); + } + + @Override + public String toString() { + return "Kebab{" + + "name='" + getName() + '\'' + + ", price=" + getPrice() + + '}'; + } +} diff --git a/src/entities/TendersAddon.java b/src/entities/TendersAddon.java new file mode 100644 index 0000000..7a325f1 --- /dev/null +++ b/src/entities/TendersAddon.java @@ -0,0 +1,7 @@ +package entities; + +public class TendersAddon extends Addon { + public TendersAddon() { + super("Tenders", 2.0); + } +} diff --git a/src/entities/TomatoAddon.java b/src/entities/TomatoAddon.java new file mode 100644 index 0000000..1255171 --- /dev/null +++ b/src/entities/TomatoAddon.java @@ -0,0 +1,7 @@ +package entities; + +public class TomatoAddon extends Addon { + public TomatoAddon() { + super("Tomato", 0.5); + } +} diff --git a/src/managers/CommandManager.java b/src/managers/CommandManager.java new file mode 100644 index 0000000..f5b4318 --- /dev/null +++ b/src/managers/CommandManager.java @@ -0,0 +1,31 @@ +package managers; + +import entities.Command; + +import java.util.ArrayList; +import java.util.List; + +public class CommandManager { + private List commands; + private static CommandManager INSTANCE; + + private CommandManager() { + commands = new ArrayList<>(); + } + + public static CommandManager getInstance() { + if (INSTANCE == null) + INSTANCE = new CommandManager(); + return INSTANCE; + } + + public void add(Command command) { + commands.add(command); + } + + public void list() { + for (Command command : commands) { + System.out.println(command); + } + } +}