mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(ch 7): listeners ex added - LluviaListener
This commit is contained in:
@@ -0,0 +1,115 @@
|
|||||||
|
package tutorialJava.capitulo6_ColeccionesDeDatos.ejercicios.bloque02_Hashmap;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Principal {
|
||||||
|
|
||||||
|
|
||||||
|
private static HashMap<Integer, Articulo> hm = new HashMap<Integer, Articulo> ();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
int opcion;
|
||||||
|
Articulo art = null;
|
||||||
|
|
||||||
|
do {
|
||||||
|
opcion = menu(); // Muestro menú y recojo selección del usuario
|
||||||
|
|
||||||
|
// Trato la opción elegida
|
||||||
|
switch (opcion) {
|
||||||
|
case 0:
|
||||||
|
System.exit(0);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1: // Ver artículos
|
||||||
|
mostrarArticulos();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2: // Insertar artículo
|
||||||
|
insertarArticulo();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3: // Eliminar artículo
|
||||||
|
System.out.println("Introduzca el codigo de barras deel artículo a eliminar: ");
|
||||||
|
int codigoAEliminar = sc.nextInt();
|
||||||
|
hm.remove(codigoAEliminar);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4: // Modificar artículo
|
||||||
|
modificarArticulo();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
} while (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static int menu() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Elige lo que desea hacer:");
|
||||||
|
System.out.println("0.-Salir");
|
||||||
|
System.out.println("1.-Ver los objeatos del almecen");
|
||||||
|
System.out.println("2.-Insertar nuevo artículo");
|
||||||
|
System.out.println("3.-Eliminar un articulo");
|
||||||
|
System.out.println("4.-Actualizar un articulo");
|
||||||
|
return sc.nextInt();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void mostrarArticulos() {
|
||||||
|
Object claves[] = hm.keySet().toArray();
|
||||||
|
for (int i = 0; i < claves.length; i++) {
|
||||||
|
System.out.println("Codigo de barras: " + claves[i] +
|
||||||
|
"\nDatos del " + hm.get(claves[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void insertarArticulo() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
System.out.println("Introduzca codigo de barras: ");
|
||||||
|
int cB = sc.nextInt();
|
||||||
|
System.out.println("Introduzca nombre: ");
|
||||||
|
String nombre = sc.next();
|
||||||
|
System.out.println("Introduzca número de estanteria: ");
|
||||||
|
int estanteria = sc.nextInt();
|
||||||
|
System.out.println("Introduzca stock: ");
|
||||||
|
int cantidad = sc.nextInt();
|
||||||
|
|
||||||
|
hm.put(cB, new Articulo(nombre, cB, estanteria, cantidad));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void modificarArticulo() {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
Articulo art = new Articulo();
|
||||||
|
System.out.println("Introzuca codigo de barras: ");
|
||||||
|
art.setCodigoDeBarras(sc.nextInt());
|
||||||
|
System.out.println("Introduzca nombre: ");
|
||||||
|
art.setNombre(sc.next());
|
||||||
|
System.out.println("Introduzca número de estanteria: ");
|
||||||
|
art.setNumeroDeEstante(sc.nextInt());
|
||||||
|
System.out.println("Introduzca stock: ");
|
||||||
|
art.setStock(sc.nextInt());
|
||||||
|
|
||||||
|
hm.replace(art.getCodigoDeBarras(), art);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,8 +30,8 @@ public class IntroduccionNumeros {
|
|||||||
*/
|
*/
|
||||||
private static void fireNumeroImparListener(int numeroImparIntroducido) {
|
private static void fireNumeroImparListener(int numeroImparIntroducido) {
|
||||||
NumeroImparEvent event = new NumeroImparEvent(numeroImparIntroducido);
|
NumeroImparEvent event = new NumeroImparEvent(numeroImparIntroducido);
|
||||||
for (int i = listNumeroImparListeners.size() - 1; i >= 0; i--) {
|
for (NumeroImparListener listener : listNumeroImparListeners) {
|
||||||
listNumeroImparListeners.get(i).numeroImparIntroducido(event);
|
listener.numeroImparIntroducido(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ public class Perro implements NumeroImparListener {
|
|||||||
public void numeroImparIntroducido(NumeroImparEvent e) {
|
public void numeroImparIntroducido(NumeroImparEvent e) {
|
||||||
System.out.println("Soy un perro y me han notificado la introducción de un número "
|
System.out.println("Soy un perro y me han notificado la introducción de un número "
|
||||||
+ "impar: " + e.getNumeroIntroducido());
|
+ "impar: " + e.getNumeroIntroducido());
|
||||||
IntroduccionNumeros.removeNumeroImparListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
||||||
|
|
||||||
|
public interface InteresadoLLuviaListener {
|
||||||
|
|
||||||
|
public void estaLloviendo(InteresadoLluviaEvent event);
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
||||||
|
|
||||||
|
public class InteresadoLluviaEvent {
|
||||||
|
|
||||||
|
private int litrosPorMetro;
|
||||||
|
|
||||||
|
public InteresadoLluviaEvent() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLitrosPorMetro() {
|
||||||
|
return litrosPorMetro;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLitrosPorMetro(int litrosPorMetro) {
|
||||||
|
this.litrosPorMetro = litrosPorMetro;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
||||||
|
|
||||||
|
public class ProgramaRadio implements InteresadoLLuviaListener {
|
||||||
|
|
||||||
|
public ProgramaRadio () {
|
||||||
|
Aemet.addInteresadoLluviaListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void estaLloviendo(InteresadoLluviaEvent event) {
|
||||||
|
System.out.println("Soy un programa de radio y me notifican que llueve");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
||||||
|
|
||||||
|
public class Telediario implements InteresadoLLuviaListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public Telediario() {
|
||||||
|
Aemet.addInteresadoLluviaListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void estaLloviendo(InteresadoLluviaEvent event) {
|
||||||
|
System.out.println("Soy un telediario y me dicen que llueve "
|
||||||
|
+ event.getLitrosPorMetro() + " litros por metro");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user