Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions _1_basics/src/main/java/code/_3_in_class/Salut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package code._3_in_class;

public class Salut {

public static void main(String[] args) {
System.out.println("Salut ");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clean.code.design_patterns.requirements.composition;

public class CompositionTest {

//composition problem to imitate a pizza restaurant

public static void main(String[] args) {
Kitchen kitchen = new Kitchen();
Thread cook1 = new Cook("Cook1",kitchen); cook1.start();
Thread cook2 = new Cook("Cook2",kitchen); cook2.start();
Thread cook3 = new Cook("Cook3",kitchen); cook3.start();
Thread waiter1 = new Waiter("Waiter1",kitchen); waiter1.start();
Thread waiter2 = new Waiter("Waiter2",kitchen); waiter2.start();
Thread waiter3 = new Waiter("Waiter3",kitchen); waiter3.start();
Thread waiter4 = new Waiter("Waiter4",kitchen); waiter4.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package clean.code.design_patterns.requirements.composition;


import java.util.*;

public class Cook extends Thread {
private String name;
private Kitchen kitchen;
private static int id;

//constructor
public Cook(String name, Kitchen kitchen) {
this.name = name;
this.kitchen = kitchen;
}


//add a pizza to the kitchen class
@Override
public void run() {
Random rand = new Random();
while(true) {
int noOfIngredients =rand.nextInt(4) + 3;
int id1 = ++id;
Pizza pizza = new Pizza(id1,noOfIngredients);
kitchen.add(pizza);
System.out.println("Pizza with id " + pizza.getId() + " having " + pizza.getNumberOfIngredients() + " ingredients was cooked");
try {
sleep((5 + noOfIngredients) * 2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package clean.code.design_patterns.requirements.composition;

import java.util.LinkedList;
import java.util.Queue;

//kitchen class
public class Kitchen {

private final Queue<Pizza> queue = new LinkedList<>();
private static final int MAX_SIZE = 5; //the size of the queue

public synchronized void add(Pizza pizza){
//block a thread from adding a pizza if the queue has more than 5 elements
while(queue.size() > MAX_SIZE)
try{
System.out.println("Blocked add");
wait();
} catch (InterruptedException e){
e.printStackTrace();
}
//add the pizza and wake the next thread
queue.add(pizza);
notify();
}


public synchronized Pizza remove(){
//if the queue is empty make the thread wait for an element to be added
while (queue.isEmpty())
try{
System.out.println("Blocked remove");
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
//remove the pizza and notify the next thread
notify();
return queue.remove();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package clean.code.design_patterns.requirements.composition;

//pizza class
public class Pizza {
private int id; //separate id for every pizza baked
private int numberOfIngredients; //the number of ingredients which is going to be random for every pizza(3 through 7)

public int getId() {
return id;
}

public int getNumberOfIngredients() {
return numberOfIngredients;
}

public Pizza(int id, int numberOfIngredients) {
this.id = id;
this.numberOfIngredients = numberOfIngredients;
}

@Override
public String toString() {
return "Pizza number " + id +
", having " + numberOfIngredients + " ingredients";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package clean.code.design_patterns.requirements.composition;

//waiter class
public class Waiter extends Thread {
private String name;
private Kitchen kitchen;

public Waiter(String name, Kitchen kitchen){
super();
this.name = name;
this.kitchen = kitchen;
}

//take a pizza from the kitchen class
@Override
public void run() {
while(true){
Pizza pizza = kitchen.remove();
System.out.println(pizza.toString() + " has been taken for delivery.");
try{
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package clean.code.design_patterns.requirements.decorator;

public class Appetizer implements Food {

@Override
public String prepareFood(){
return "Appetizer";
}

@Override
public double foodPrice(){
return 25.0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package clean.code.design_patterns.requirements.decorator;

public class Decorator implements Food {
private Food newFood;

public Decorator(Food newFood) {
this.newFood = newFood;
}

@Override
public String prepareFood() {
return newFood.prepareFood();
}

@Override
public double foodPrice() {
return newFood.foodPrice();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package clean.code.design_patterns.requirements.decorator;

import java.io.IOException;
import java.util.Scanner;

public class DecoratorTest {

public static void main(String[] args) throws IOException {
printMenu();
}


public static void printMenu() throws NumberFormatException, IOException {
Scanner s = new Scanner(System.in);
boolean check = true;
while(check){
System.out.println("==========Food Menu==========");
System.out.println("\t1.Appetizer");
System.out.println("\t2.ItalianFood");
System.out.println("\t3.SpanishFood");
System.out.println("\t4.Exit");
System.out.println("Enter your choice: ");
int choice = s.nextInt();
switch (choice){
case 1: {
Appetizer appetizer = new Appetizer();
System.out.println(appetizer.prepareFood());
System.out.println(appetizer.foodPrice());
break;
}
case 2: {
Food food = new ItalianFood(new Appetizer());
System.out.println(food.prepareFood());
System.out.println(food.foodPrice());
break;
}
case 3: {
Food food1 = new SpanishFood(new Appetizer());
System.out.println(food1.prepareFood());
System.out.println(food1.foodPrice());
break;
}
case 4: {
check = false;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package clean.code.design_patterns.requirements.decorator;

public interface Food {
public String prepareFood();
public double foodPrice();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clean.code.design_patterns.requirements.decorator;

public class ItalianFood extends Decorator {
public ItalianFood(Food newFood) {
super(newFood);
}

@Override
public String prepareFood() {
return super.prepareFood()+ " With Cannoli and Lasagna";
}

@Override
public double foodPrice() {
return super.foodPrice()+70.50;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package clean.code.design_patterns.requirements.decorator;

public class SpanishFood extends Decorator {
public SpanishFood(Food newFood) {
super(newFood);
}

@Override
public String prepareFood() {
return super.prepareFood() + " With Gazpacho and Paella";
}

@Override
public double foodPrice() {
return super.foodPrice()+ 82.75;
}
}