jueves, 30 de abril de 2015

package corredoresporpaises;
import java.util.ArrayList;
/**
*
* @author juan
*/
public class Pais {
private String nombre;
private ArrayList corredores;
public ArrayList getCorredores() {
return corredores;
}

public void setCorredores(ArrayList corredores) {
this.corredores = corredores;
}

public Pais(String nombre) {
this.nombre = nombre;
corredores = new ArrayList<>();
}
public String getNombre() {
return nombre;
}

public boolean anhade(Corredor nuevo) {
boolean retorno = true;

try {
if (corredores.contains(nuevo)){
System.out.println("El corredor ya estaba incluido");

}
else
{
nuevo.setPais(this);
corredores.add(nuevo);
}

} catch (Exception e) {
retorno=false;
}
return retorno;
}
public void listarCorredores(){
System.out.println(nombre);
System.out.println("==============");
for (Corredor corredor : corredores) {
System.out.println(corredor);
}
}





}
package corredoresporpaises;
/**
*
* @author juan
*/
public class Corredor {
private String nombre;
private int dorsal;
private Pais pais;
public void setPais(Pais pais) {
this.pais = pais;
}

public Corredor(String nombre, int dorsal, Pais pais) {
this.nombre = nombre;
this.dorsal = dorsal;
this.pais = pais;
}

public Corredor(String nombre, int dorsal) {
this.nombre = nombre;
this.dorsal = dorsal;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getDorsal() {
return dorsal;
}
public void setDorsal(int dorsal) {
this.dorsal = dorsal;
}
@Override
public String toString() {
return (""+ nombre + ", dorsal=" + dorsal + ", pais=" + pais.getNombre() );
}


}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package corredoresporpaises;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author juan
*/
public class CorredoresPorPaises {
/**
* @param args the command line arguments
*/
public static Scanner leer = new Scanner(System.in);
public static void main(String[] args) {
Pais pais;
Pais pais2;
int dorsal;
String nombre="";
pais = new Pais("Carballeira");
do {
try {
System.out.println("Nombre: ");
nombre = leer.nextLine();
if (!nombre.equalsIgnoreCase("fin")) {
System.out.println("Número de dorsal: ");
dorsal = leer.nextInt();
leer.nextLine();
if (!pais.anhade(new Corredor(nombre, dorsal,pais))) {
System.out.println("No se ha podido añadir");
};
}
}
catch (InputMismatchException ex) {
System.out.println("Debe introducir un número");
System.out.println(ex);
leer.nextLine();
}
catch (Exception e) {
System.out.println("No se ha podido añadir: " + e);
}
} while (!nombre.equalsIgnoreCase("fin"));
pais.listarCorredores();

pais2 = new Pais("Dam1");
pais2.setCorredores(pais.getCorredores());
}
}

No hay comentarios: