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
Compare commits
4 Commits
38fe9675d3
...
bdbffee001
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdbffee001 | ||
|
|
6fcfc243a5 | ||
|
|
ccd2fc934d | ||
|
|
6d5b6d56d5 |
@@ -53,6 +53,30 @@ public class Utils {
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pide un número entero al usuario
|
||||
* @param mensaje String que va a mostrarse en consola para pedir el número
|
||||
* @return Número entero introducido por el usuario
|
||||
*/
|
||||
public static float pideNumeroFloat (String mensaje) {
|
||||
float n = 0;
|
||||
boolean esNumeroCorrecto;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
do {
|
||||
esNumeroCorrecto = false;
|
||||
try {
|
||||
System.out.println(mensaje);
|
||||
n = Float.parseFloat(sc.nextLine());
|
||||
esNumeroCorrecto = true;
|
||||
}
|
||||
catch (Exception e) {
|
||||
System.out.println("No ha introducido un número");
|
||||
}
|
||||
} while (esNumeroCorrecto == false);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,6 @@ public class Ejercicio02_findAndReplace {
|
||||
* @return
|
||||
*/
|
||||
public static void findAndReplace (int b[], int buscado, int reemplazo) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < b.length; i++) {
|
||||
if (b[i] == buscado) {
|
||||
|
||||
@@ -4,13 +4,14 @@ public class Ejercicio04_MatrizConBordeYCeroAlAzar {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// Declaración de variables
|
||||
char matriz[][] = new char[20][10];
|
||||
char matriz[][] = new char[10][20];
|
||||
int posicionAzarFila, posicionAzarColumna;
|
||||
|
||||
// La relleno de espacios en blanco
|
||||
for (int i = 0; i < matriz.length; i++) {
|
||||
for (int j = 0; j < matriz[i].length; j++) {
|
||||
if (i == 0 || i == (matriz.length - 1) || j == 0 || j == (matriz[0].length - 1)) {
|
||||
if (i == 0 || i == (matriz.length - 1) ||
|
||||
j == 0 || j == (matriz[0].length - 1)) {
|
||||
matriz[i][j] = '*';
|
||||
}
|
||||
else {
|
||||
|
||||
60
src/tutorialJava/capitulo5/ej01_cromosBaloncesto/Cromo.java
Normal file
60
src/tutorialJava/capitulo5/ej01_cromosBaloncesto/Cromo.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package tutorialJava.capitulo5.ej01_cromosBaloncesto;
|
||||
|
||||
public class Cromo {
|
||||
// Propiedades
|
||||
protected String nombreJugador;
|
||||
protected int puntuacion;
|
||||
protected String equipo;
|
||||
|
||||
public Cromo() {
|
||||
this.nombreJugador = "Jugador por defecto";
|
||||
this.puntuacion = 0;
|
||||
this.equipo = "Sin equipo";
|
||||
}
|
||||
|
||||
public Cromo(String nombreJugador, int puntuacion, String equipo) {
|
||||
this.nombreJugador = nombreJugador;
|
||||
this.puntuacion = puntuacion;
|
||||
this.equipo = equipo;
|
||||
}
|
||||
|
||||
|
||||
public void agregaMinimaPuntuacion() {
|
||||
this.puntuacion++;
|
||||
}
|
||||
|
||||
|
||||
public String getNombreJugador() {
|
||||
return nombreJugador;
|
||||
}
|
||||
|
||||
public void setNombreJugador(String nombreJugador) {
|
||||
this.nombreJugador = nombreJugador;
|
||||
}
|
||||
|
||||
public int getPuntuacion() {
|
||||
return puntuacion;
|
||||
}
|
||||
|
||||
public void setPuntuacion(int puntuacion) {
|
||||
this.puntuacion = puntuacion;
|
||||
}
|
||||
|
||||
public String getEquipo() {
|
||||
return equipo;
|
||||
}
|
||||
|
||||
public void setEquipo(String equipo) {
|
||||
this.equipo = equipo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Cromo [nombreJugador=" + nombreJugador + ", puntuacion=" + puntuacion + ", equipo=" + equipo + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package tutorialJava.capitulo5.ej01_cromosBaloncesto;
|
||||
|
||||
public class CromoBaloncesto extends Cromo {
|
||||
|
||||
private int altura;
|
||||
|
||||
|
||||
public CromoBaloncesto() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CromoBaloncesto(String nombreJugador, int puntuacion, String equipo) {
|
||||
super(nombreJugador, puntuacion, equipo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void agregaMinimaPuntuacion() {
|
||||
this.setPuntuacion(this.getPuntuacion() + 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CromoBaloncesto [getNombreJugador()=" + getNombreJugador() + ", getPuntuacion()=" + getPuntuacion()
|
||||
+ ", getEquipo()=" + getEquipo() + ", altura=" + altura + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package tutorialJava.capitulo5.ej01_cromosBaloncesto;
|
||||
|
||||
public class CromoFutbol extends Cromo {
|
||||
|
||||
public CromoFutbol() {
|
||||
}
|
||||
|
||||
public CromoFutbol(String nombreJugador, int puntuacion, String equipo) {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package tutorialJava.capitulo5.ej01_cromosBaloncesto;
|
||||
|
||||
public class Principal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Cromo cromoSimple = new Cromo("Iniesta", 1000, "España");
|
||||
System.out.println("Id del cromo: " + cromoSimple.hashCode());
|
||||
System.out.println(cromoSimple.toString());
|
||||
|
||||
|
||||
CromoBaloncesto cromo1 = new CromoBaloncesto("Ricky", 100, "Barcelona");
|
||||
System.out.println("Id del cromo: " + cromo1.hashCode());
|
||||
System.out.println(cromo1.toString());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
package tutorialJava.capitulo5.ej02_independenceDay;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
package tutorialJava.capitulo5.ej02_independenceDay;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
package tutorialJava.capitulo5.ej02_independenceDay;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
package tutorialJava.capitulo5.ej02_independenceDay;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
@@ -0,0 +1,161 @@
|
||||
package tutorialJava.capitulo5.ej03_independenceDayConHerencia;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class CampoBatalla {
|
||||
|
||||
private String nombre;
|
||||
private Humano humanos[] = new Humano[20];
|
||||
private Malvado malvados[] = new Malvado[20];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param nombre
|
||||
*/
|
||||
public CampoBatalla (String nombre) {
|
||||
this.nombre = nombre;
|
||||
|
||||
for (int i = 0; i < humanos.length; i++) {
|
||||
humanos[i] = new Humano("H-" + i);
|
||||
}
|
||||
// humanos[humanos.length - 1].setPuntosVida(
|
||||
// humanos[humanos.length - 1].getPuntosVida() * 2);
|
||||
|
||||
Humano ultimoHumano = humanos[humanos.length - 1];
|
||||
ultimoHumano.setPuntosVida(ultimoHumano.getPuntosVida() * 2);
|
||||
|
||||
for (int i = 0; i < malvados.length; i++) {
|
||||
malvados[i] = new Malvado("M-" + i);
|
||||
}
|
||||
// Establezco el doble de puntos de vida al último malvado
|
||||
Malvado ultimoMalvado = malvados[malvados.length - 1];
|
||||
ultimoMalvado.setPuntosVida(ultimoMalvado.getPuntosVida() * 2);
|
||||
}
|
||||
|
||||
|
||||
public void mezclaPersonajes (Personaje a[]) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
int ind1 = Utils.obtenerNumeroAzar(0, a.length - 1);
|
||||
int ind2 = Utils.obtenerNumeroAzar(0, a.length - 1);
|
||||
Personaje aux = a[ind1];
|
||||
a[ind1] = a[ind2];
|
||||
a[ind2] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void batalla () {
|
||||
do {
|
||||
// Disparo sobre el primer malvado vivo
|
||||
Malvado m = (Malvado) getPrimerPersonajeVivo(malvados);
|
||||
m.setPuntosVida(m.getPuntosVida() -
|
||||
Utils.obtenerNumeroAzar(5, 25));
|
||||
if (m.getPuntosVida() <= 0) {
|
||||
m.setVivo(false);
|
||||
m.setPuntosVida(0);
|
||||
}
|
||||
|
||||
// Sólo si queda algún malvado vivo, disparo sobre humano
|
||||
if (getPrimerPersonajeVivo(malvados) != null) {
|
||||
// Disparo sobre el primer humano vivo
|
||||
Humano h = (Humano) getPrimerPersonajeVivo(humanos);
|
||||
h.setPuntosVida(h.getPuntosVida() -
|
||||
Utils.obtenerNumeroAzar(5, 25));
|
||||
if (h.getPuntosVida() <= 0) {
|
||||
h.setVivo(false);
|
||||
h.setPuntosVida(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Imprimo campo de batalla
|
||||
System.out.println(toString());
|
||||
System.out.println();
|
||||
|
||||
} while(getPrimerPersonajeVivo(humanos) != null &&
|
||||
getPrimerPersonajeVivo(malvados) != null);
|
||||
|
||||
// Si llego hasta aquí, un bando no tiene soldados vivos
|
||||
if (getPrimerPersonajeVivo(humanos) != null) {
|
||||
System.out.println("Han ganado los humanos");
|
||||
}
|
||||
else {
|
||||
System.out.println("Han ganado los malvados");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Personaje getPrimerPersonajeVivo(Personaje a[]) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
if (a[i].isVivo()) {
|
||||
return a[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public Humano[] getHumanos() {
|
||||
return humanos;
|
||||
}
|
||||
|
||||
public void setHumanos(Humano[] humanos) {
|
||||
this.humanos = humanos;
|
||||
}
|
||||
|
||||
public Malvado[] getMalvados() {
|
||||
return malvados;
|
||||
}
|
||||
|
||||
public void setMalvados(Malvado[] malvados) {
|
||||
this.malvados = malvados;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer sb = new StringBuffer("Campo: " + this.nombre);
|
||||
sb.append("\nHumanos: ");
|
||||
for (int i = 0; i < humanos.length; i++) {
|
||||
if (humanos[i].isVivo()) {
|
||||
sb.append(humanos[i].getNombre() + ":" +
|
||||
humanos[i].getPuntosVida() + ":" +
|
||||
humanos[i].isVivo() + " ");
|
||||
}
|
||||
}
|
||||
|
||||
sb.append("\nMalvados: ");
|
||||
for (int i = 0; i < malvados.length; i++) {
|
||||
if (malvados[i].isVivo()) {
|
||||
sb.append(malvados[i].getNombre() + ":" +
|
||||
malvados[i].getPuntosVida() + ":" +
|
||||
malvados[i].isVivo() + " ");
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package tutorialJava.capitulo5.ej03_independenceDayConHerencia;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Humano extends Personaje {
|
||||
|
||||
public int altura;
|
||||
|
||||
public Humano() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Humano(String nombre) {
|
||||
super(nombre);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package tutorialJava.capitulo5.ej03_independenceDayConHerencia;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Malvado extends Personaje {
|
||||
|
||||
public Malvado() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Malvado(String nombre) {
|
||||
super(nombre);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package tutorialJava.capitulo5.ej03_independenceDayConHerencia;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Personaje {
|
||||
|
||||
private int puntosVida;
|
||||
private String nombre;
|
||||
private boolean vivo;
|
||||
|
||||
public Personaje() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Personaje(String nombre) {
|
||||
super();
|
||||
this.puntosVida = Utils.obtenerNumeroAzar(50, 100);
|
||||
this.nombre = nombre;
|
||||
this.vivo = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Personaje(int puntosVida, String nombre, boolean vivo) {
|
||||
super();
|
||||
this.puntosVida = puntosVida;
|
||||
this.nombre = nombre;
|
||||
this.vivo = vivo;
|
||||
}
|
||||
|
||||
public int getPuntosVida() {
|
||||
return puntosVida;
|
||||
}
|
||||
|
||||
public void setPuntosVida(int puntosVida) {
|
||||
this.puntosVida = puntosVida;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public boolean isVivo() {
|
||||
return vivo;
|
||||
}
|
||||
|
||||
public void setVivo(boolean vivo) {
|
||||
this.vivo = vivo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Personaje [puntosVida=" + puntosVida + ", nombre=" + nombre + ", vivo=" + vivo + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package tutorialJava.capitulo5.ej03_independenceDayConHerencia;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Principal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CampoBatalla campo = new CampoBatalla("Rute");
|
||||
|
||||
campo.mezclaPersonajes(campo.getHumanos());
|
||||
campo.mezclaPersonajes(campo.getMalvados());
|
||||
System.out.println(campo.toString());
|
||||
|
||||
campo.batalla();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque01_ArrayPersonas;
|
||||
|
||||
public class Persona {
|
||||
|
||||
private String nombre;
|
||||
private String apellidos;
|
||||
private String dni;
|
||||
private String direccion;
|
||||
private String telefono;
|
||||
|
||||
|
||||
public Persona() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Persona(String nombre, String apellidos, String dni, String direccion, String telefono) {
|
||||
super();
|
||||
this.nombre = nombre;
|
||||
this.apellidos = apellidos;
|
||||
this.dni = dni;
|
||||
this.direccion = direccion;
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
|
||||
public String getApellidos() {
|
||||
return apellidos;
|
||||
}
|
||||
|
||||
|
||||
public void setApellidos(String apellidos) {
|
||||
this.apellidos = apellidos;
|
||||
}
|
||||
|
||||
|
||||
public String getDni() {
|
||||
return dni;
|
||||
}
|
||||
|
||||
|
||||
public void setDni(String dni) {
|
||||
this.dni = dni;
|
||||
}
|
||||
|
||||
|
||||
public String getDireccion() {
|
||||
return direccion;
|
||||
}
|
||||
|
||||
|
||||
public void setDireccion(String direccion) {
|
||||
this.direccion = direccion;
|
||||
}
|
||||
|
||||
|
||||
public String getTelefono() {
|
||||
return telefono;
|
||||
}
|
||||
|
||||
|
||||
public void setTelefono(String telefono) {
|
||||
this.telefono = telefono;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Persona [nombre=" + nombre + ", apellidos=" + apellidos + ", dni=" + dni + ", direccion=" + direccion
|
||||
+ ", telefono=" + telefono + "]";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque01_ArrayPersonas;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Principal {
|
||||
|
||||
private static Persona personas[] = new Persona[3];
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
int opcion = 0;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
do {
|
||||
opcion = muestraMenu();
|
||||
|
||||
switch (opcion) {
|
||||
case 1:
|
||||
for (int i = 0; i < personas.length; i++) {
|
||||
personas[i] = new Persona();
|
||||
System.out.println("Introduzca nombre de persona");
|
||||
personas[i].setNombre(sc.nextLine());
|
||||
System.out.println("Introduzca apellidos de persona");
|
||||
personas[i].setApellidos(sc.nextLine());
|
||||
System.out.println("Introduzca dni de persona");
|
||||
personas[i].setDni(sc.nextLine());
|
||||
System.out.println("Introduzca dirección de persona");
|
||||
personas[i].setDireccion(sc.nextLine());
|
||||
System.out.println("Introduzca telefono de persona");
|
||||
personas[i].setTelefono(sc.nextLine());
|
||||
}
|
||||
|
||||
break;
|
||||
case 2:
|
||||
for (int i = 0; i < personas.length; i++) {
|
||||
System.out.println(personas[i].toString());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
} while (opcion != 0);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
*/
|
||||
public static int muestraMenu() {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("\n\nMenú" +
|
||||
"\n0.- Salir" +
|
||||
"\n1.- Introducir datos" +
|
||||
"\n2.- Visualizar datos" +
|
||||
"\n\tIntroduzca su opción: ");
|
||||
return Integer.parseInt(sc.nextLine());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque02_ColeccionAntiguedades;
|
||||
|
||||
public class Antiguedad {
|
||||
|
||||
private int anio;
|
||||
private String origen;
|
||||
private float precio;
|
||||
|
||||
public Antiguedad() {
|
||||
}
|
||||
|
||||
public Antiguedad(int anio, String origen, float precio) {
|
||||
super();
|
||||
this.anio = anio;
|
||||
this.origen = origen;
|
||||
this.precio = precio;
|
||||
}
|
||||
|
||||
public int getAnio() {
|
||||
return anio;
|
||||
}
|
||||
|
||||
public void setAnio(int anio) {
|
||||
this.anio = anio;
|
||||
}
|
||||
|
||||
public String getOrigen() {
|
||||
return origen;
|
||||
}
|
||||
|
||||
public void setOrigen(String origen) {
|
||||
this.origen = origen;
|
||||
}
|
||||
|
||||
public float getPrecio() {
|
||||
return precio;
|
||||
}
|
||||
|
||||
public void setPrecio(float precio) {
|
||||
this.precio = precio;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Antiguedad [anio=" + anio + ", origen=" + origen + ", precio=" + precio + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque02_ColeccionAntiguedades;
|
||||
|
||||
public class Joya extends Antiguedad {
|
||||
|
||||
private String material;
|
||||
|
||||
public Joya() {
|
||||
}
|
||||
|
||||
public Joya(int anio, String origen, float precio) {
|
||||
super(anio, origen, precio);
|
||||
}
|
||||
|
||||
public String getMaterial() {
|
||||
return material;
|
||||
}
|
||||
|
||||
public void setMaterial(String material) {
|
||||
this.material = material;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString() + " material: " + this.material;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||
|
||||
public class ArtNoPerecedero extends Articulo {
|
||||
|
||||
public ArtNoPerecedero() {
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
public ArtNoPerecedero(String codigo, String nombre, float precio) {
|
||||
super(codigo, nombre, precio);
|
||||
// TODO Auto-generated constructor stub
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||
|
||||
public class ArtPerecedero extends Articulo {
|
||||
|
||||
private String fechaCaducidad;
|
||||
|
||||
public ArtPerecedero() {
|
||||
}
|
||||
|
||||
public ArtPerecedero(String codigo, String nombre, float precio) {
|
||||
super(codigo, nombre, precio);
|
||||
}
|
||||
|
||||
public String getFechaCaducidad() {
|
||||
return fechaCaducidad;
|
||||
}
|
||||
|
||||
public void setFechaCaducidad(String fechaCaducidad) {
|
||||
this.fechaCaducidad = fechaCaducidad;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ArtPerecedero [fechaCaducidad=" + fechaCaducidad + ", getCodigo()=" + getCodigo() + ", getNombre()="
|
||||
+ getNombre() + ", getPrecio()=" + getPrecio() + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||
|
||||
public class Articulo {
|
||||
|
||||
private String codigo;
|
||||
private String nombre;
|
||||
private float precio;
|
||||
|
||||
public Articulo() {
|
||||
}
|
||||
|
||||
public Articulo(String codigo, String nombre, float precio) {
|
||||
super();
|
||||
this.codigo = codigo;
|
||||
this.nombre = nombre;
|
||||
this.precio = precio;
|
||||
}
|
||||
|
||||
public String getCodigo() {
|
||||
return codigo;
|
||||
}
|
||||
|
||||
public void setCodigo(String codigo) {
|
||||
this.codigo = codigo;
|
||||
}
|
||||
|
||||
public String getNombre() {
|
||||
return nombre;
|
||||
}
|
||||
|
||||
public void setNombre(String nombre) {
|
||||
this.nombre = nombre;
|
||||
}
|
||||
|
||||
public float getPrecio() {
|
||||
return precio;
|
||||
}
|
||||
|
||||
public void setPrecio(float precio) {
|
||||
this.precio = precio;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Articulo [codigo=" + codigo + ", nombre=" + nombre + ", precio=" + precio + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Tienda {
|
||||
|
||||
public Tienda() {
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
Articulo articulos[] = new Articulo[4];
|
||||
|
||||
for (int i = 0; i < articulos.length; i++) {
|
||||
if (i < 2) {
|
||||
System.out.println("Introduzca datos de Art perecedero: ");
|
||||
ArtPerecedero artPer = new ArtPerecedero();
|
||||
artPer.setCodigo(Utils.
|
||||
obtenerCadenaConDescripcion("Dame código:"));
|
||||
artPer.setNombre(Utils.
|
||||
obtenerCadenaConDescripcion("Dame nombre:"));
|
||||
artPer.setPrecio(Utils.
|
||||
pideNumeroFloat("Dame precio"));
|
||||
artPer.setFechaCaducidad(Utils.
|
||||
obtenerCadenaConDescripcion("Dame fecha caducidad:"));
|
||||
articulos[i] = artPer;
|
||||
}
|
||||
else {
|
||||
System.out.println("Introduzca datos de Art perecedero: ");
|
||||
ArtNoPerecedero artNoPer = new ArtNoPerecedero();
|
||||
artNoPer.setCodigo(Utils.
|
||||
obtenerCadenaConDescripcion("Dame código:"));
|
||||
artNoPer.setNombre(Utils.
|
||||
obtenerCadenaConDescripcion("Dame nombre:"));
|
||||
artNoPer.setPrecio(Utils.
|
||||
pideNumeroFloat("Dame precio"));
|
||||
articulos[i] = artNoPer;
|
||||
}
|
||||
}
|
||||
|
||||
// Muestro los valores
|
||||
for (int i = 0; i < articulos.length; i++) {
|
||||
System.out.println(articulos[i].toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user