Feat: Decorator implemented
This commit is contained in:
31
.gitignore
vendored
Normal file
31
.gitignore
vendored
Normal 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
6
src/Main.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
import application.Application;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var app = new Application();
|
||||||
|
app.run();
|
||||||
|
}
|
||||||
24
src/application/Application.java
Normal file
24
src/application/Application.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/decorator/KebabDecorator.java
Normal file
27
src/decorator/KebabDecorator.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/decorator/TendersKebabDecorator.java
Normal file
20
src/decorator/TendersKebabDecorator.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/decorator/TomatoKebabDecorator.java
Normal file
20
src/decorator/TomatoKebabDecorator.java
Normal 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
27
src/entities/Addon.java
Normal 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
42
src/entities/Command.java
Normal 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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/entities/CommandState.java
Normal file
9
src/entities/CommandState.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package entities;
|
||||||
|
|
||||||
|
public enum CommandState {
|
||||||
|
CREATED,
|
||||||
|
RECEIVED,
|
||||||
|
COOKED,
|
||||||
|
DELIVERED,
|
||||||
|
FAILED
|
||||||
|
}
|
||||||
50
src/entities/Kebab.java
Normal file
50
src/entities/Kebab.java
Normal 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() +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/entities/TendersAddon.java
Normal file
7
src/entities/TendersAddon.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package entities;
|
||||||
|
|
||||||
|
public class TendersAddon extends Addon {
|
||||||
|
public TendersAddon() {
|
||||||
|
super("Tenders", 2.0);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/entities/TomatoAddon.java
Normal file
7
src/entities/TomatoAddon.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package entities;
|
||||||
|
|
||||||
|
public class TomatoAddon extends Addon {
|
||||||
|
public TomatoAddon() {
|
||||||
|
super("Tomato", 0.5);
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/managers/CommandManager.java
Normal file
31
src/managers/CommandManager.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user