Feat: Decorator implemented

This commit is contained in:
Namu
2025-10-22 23:15:15 +02:00
commit 349cdb484e
13 changed files with 301 additions and 0 deletions

31
.gitignore vendored Normal file
View File

@@ -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

6
src/Main.java Normal file
View File

@@ -0,0 +1,6 @@
import application.Application;
public static void main(String[] args) {
var app = new Application();
app.run();
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

27
src/entities/Addon.java Normal file
View File

@@ -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;
}
}

42
src/entities/Command.java Normal file
View File

@@ -0,0 +1,42 @@
package entities;
import java.util.ArrayList;
import java.util.List;
public class Command {
private List<Kebab> kebabs;
private CommandState state;
public Command() {
this.kebabs = new ArrayList<>();
this.state = CommandState.CREATED;
}
public List<Kebab> getKebabs() {
return kebabs;
}
public void addKebab(Kebab kebab) {
kebabs.add(kebab);
}
public void setKebabs(List<Kebab> 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 +
'}';
}
}

View File

@@ -0,0 +1,9 @@
package entities;
public enum CommandState {
CREATED,
RECEIVED,
COOKED,
DELIVERED,
FAILED
}

50
src/entities/Kebab.java Normal file
View File

@@ -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<Addon> 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() +
'}';
}
}

View File

@@ -0,0 +1,7 @@
package entities;
public class TendersAddon extends Addon {
public TendersAddon() {
super("Tenders", 2.0);
}
}

View File

@@ -0,0 +1,7 @@
package entities;
public class TomatoAddon extends Addon {
public TomatoAddon() {
super("Tomato", 0.5);
}
}

View File

@@ -0,0 +1,31 @@
package managers;
import entities.Command;
import java.util.ArrayList;
import java.util.List;
public class CommandManager {
private List<Command> 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);
}
}
}