Skip to content

Commit d45d52e

Browse files
committed
#21 - Java
1 parent 4440637 commit d45d52e

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* EJERCICIO:
3+
* Explora el concepto de callback en tu lenguaje creando un ejemplo
4+
* simple (a tu elección) que muestre su funcionamiento.
5+
*
6+
* DIFICULTAD EXTRA (opcional):
7+
* Crea un simulador de pedidos de un restaurante utilizando callbacks.
8+
* Estará formado por una función que procesa pedidos.
9+
* Debe aceptar el nombre del plato, una callback de confirmación, una
10+
* de listo y otra de entrega.
11+
* - Debe imprimir un confirmación cuando empiece el procesamiento.
12+
* - Debe simular un tiempo aleatorio entre 1 a 10 segundos entre
13+
* procesos.
14+
* - Debe invocar a cada callback siguiendo un orden de procesado.
15+
* - Debe notificar que el plato está listo o ha sido entregado.
16+
*/
17+
18+
public class JimsimroDev {
19+
20+
private static final int MIN = 1000;
21+
private static final int MAX = 10000;
22+
23+
interface Noficar {
24+
void notificacion(String mensaje);
25+
}
26+
27+
static void notificacionEnviada(String proceso, Noficar callback) {
28+
try {
29+
System.out.println("Iniciando proceso...");
30+
Thread.sleep(5000);
31+
callback.notificacion("Proceso completado: " + proceso);
32+
} catch (InterruptedException e) {
33+
System.out.println("Erro timeout" + e);
34+
}
35+
36+
}
37+
38+
interface Callback {
39+
void notificar(String mensaje);
40+
}
41+
42+
static int ramdon() {
43+
return (int) (Math.random() * (MAX - MIN + 1)) + MIN;
44+
}
45+
46+
static void procesarPedidos(String plato, Callback callback) {
47+
try {
48+
System.out.println("Iniciando pedido....");
49+
Thread.sleep(ramdon());
50+
callback.notificar(plato + " Confirmado");
51+
52+
System.out.println("Preparando pedido");
53+
Thread.sleep(ramdon());
54+
callback.notificar(plato + " Listo");
55+
56+
System.out.println("Entregando el plato " + plato);
57+
Thread.sleep(ramdon());
58+
callback.notificar(plato + " Entregado");
59+
60+
} catch (InterruptedException e) {
61+
System.out.println("Error timeout " + e);
62+
}
63+
}
64+
65+
static void hacerOrden(String plato) {
66+
procesarPedidos(plato, new Callback() {
67+
68+
@Override
69+
public void notificar(String mensaje) {
70+
System.out.println(mensaje);
71+
}
72+
});
73+
}
74+
75+
public static void main(String[] args) {
76+
notificacionEnviada("Enviando SMS ", new Noficar() {
77+
78+
@Override
79+
public void notificacion(String mensaje) {
80+
System.out.println(mensaje);
81+
}
82+
});
83+
84+
// EXTRA
85+
hacerOrden("Bandeja Paisa");
86+
hacerOrden("Lechona");
87+
hacerOrden("Mote De Queso");
88+
}
89+
}

0 commit comments

Comments
 (0)