Problem with arrayList data in Java

0

I have discovered a problem in the execution of my code and I have no idea why ... It turns out that I have a for to enter data of 2 students, all perfect entering the data but when viewing it with the method toString (), I visualized all the data except the date of birth and the notes, the two students keep both data, of the last student introduced precisely. For example in the data of student 1 I tell him that he was born on 2/2/2000 with the notes: 2,2,2 and to the student 2 I tell him that he was born on 3/3/2003 with the notes: 6, 6.6. In the visualization of the data, both students have the last date and notes entered. The name, dni etc appears each one his

Here the code:

public class Principal 
{

     public static void main(String[] args)
    {
         System.out.println("EJERCICIO ARRAYLIST EN JAVA\n");

         Scanner tecla = new Scanner (System.in);
         ArrayList <Alumnos> ArrayListAlumnos = new ArrayList();
         Alumnos objAlumnos = new Alumnos();
         Fecha objFecha = new Fecha();
         int notas[] = new int [3];
         int j;

         //Introducimos datos
         for(int i=0;i<2;i++)
         {
             objAlumnos = new Alumnos();

             System.out.print("\nNumero de expediente del "+(i+1)+" alumno: ");
             objAlumnos.setNumeroExpediente(tecla.nextInt());
             tecla.nextLine();//Limpiar buffer
             System.out.print("Dni: ");
             objAlumnos.setDni(tecla.nextLine());
             System.out.print("Nombre y apellidos: ");
             objAlumnos.setNombre(tecla.nextLine());
             System.out.println("Introduca fecha de nacimiento");
            do{             
                 System.out.print("Dia(1-31): ");
                 objFecha.setDia(tecla.nextInt());                              
                 System.out.print("Mes(1-12): ");
                 objFecha.setMes(tecla.nextInt());
                 System.out.print("Año(1980-2020): ");
                 objFecha.setAnio(tecla.nextInt());
                 if(objFecha.fechaCorrecta()==false)
                 {
                      System.out.println("\nFecha de nacimento incorrecta, introduzca de nuevo");
                 }
             }while(objFecha.fechaCorrecta()==false);           
             tecla.nextLine();//Limpiar buffer
             System.out.print("Curso: ");
             objAlumnos.setCurso(tecla.nextLine());
             System.out.println("\nIntroduzca notas correspondientes(1-10)");           
             for(j=0;j<3;j++)
             {              
                do{
                     System.out.print("Nota "+(j+1)+" : ");
                     notas[j]=tecla.nextInt();  
                     if(notas[j]<1 || notas[j]>10)
                     {
                         System.out.println("\nLa notas deben ir del 1 al 10, introduzca de nuevo");
                     }
                 }while(notas[j]<1 || notas[j]>10);
             }                      
             objAlumnos.setNotas(notas);        
             objAlumnos.setFecha_nacimiento(objFecha);//IMPORTANTE
             ArrayListAlumnos.add(objAlumnos);
         }

         //Visualizamos
         System.out.println("\n\nDATOS INTRODUCIDOS\n");
         for(int i=0;i<ArrayListAlumnos.size();i++)
         {
             objAlumnos = ArrayListAlumnos.get(i);
             System.out.println(objAlumnos.toString());
         }
     }  
 }

My class date:

 public class Fecha
 {
     public int dia;
     public int mes;
     public int anio;

     public Fecha()
     {
        super();
        dia=1;
        mes=1;
        anio=2000;
     }

     public Fecha(int dia,int mes,int anio)
     {
         super();
         this.dia=dia;
         this.mes=mes;
         this.anio=anio;
     }

     public int getDia() {
         return dia;
     }

     public void setDia(int dia) {
         this.dia = dia;
     }

     public int getMes() {
         return mes;
     }

     public void setMes(int mes) {
         this.mes = mes;
     }

     public int getAnio() {
         return anio;
    }

    public void setAnio(int anio) {
        this.anio = anio;
    }

    public boolean fechaCorrecta()
    {
        boolean diaCorrecto,mesCorrecto,anioCorrecto;

        anioCorrecto = anio >= 1980 && anio <=2020;
        mesCorrecto = mes >=1 && mes <=12;

        switch(mes)
        {
            case 2:
                if(anioBisiesto())
                {
                    diaCorrecto = dia >= 1 && dia <=29;
                }
                else
                {
                diaCorrecto = dia >= 1 && dia <=28;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                diaCorrecto = dia >=1 && dia <=30;
                break;
             default:
                diaCorrecto = dia >=1 && dia <=31;
        }
        return diaCorrecto && mesCorrecto && anioCorrecto;
    }

    private boolean anioBisiesto() {

        return (anio % 4 == 0 && anio % 100 !=0 || anio % 400 == 0);
    }       
}

My student class:

public class Alumnos extends Datos_Personales
{
    private int numeroExpediente;
    private String curso;
    private int notas[];

    public Alumnos()
    {
        super();
        numeroExpediente=0;
        curso = new String();
        int notas[] = new int [3];
    }

    public Alumnos(String dni,String nombre,Fecha fecha_nacimiento,int numeroExp,String curso,int notas[])
    {
        super(dni,nombre,fecha_nacimiento);
        this.numeroExpediente=numeroExp;
        this.curso=curso;
        this.notas[3]=notas[3];
    }

    public int getNumeroExpediente() {
        return numeroExpediente;
    }

    public void setNumeroExpediente(int numeroExpediente) {
        this.numeroExpediente = numeroExpediente;
    }

    public String getCurso() {
        return curso;
    }

     public void setCurso(String curso) {
         this.curso = curso;
    }

    public int[] getNotas() {
        return notas;
    }

    public void setNotas(int[] notas) {
        this.notas = notas;
   }

    public String toString()
    {
         return "\nNumero de expediente = " + numeroExpediente + "\nDni = " + dni + "\nNombre = " + nombre + "\nFecha de nacimiento = "+
            fecha_nacimiento.dia+"/"+fecha_nacimiento.mes+"/"+fecha_nacimiento.anio + 
            "\nCurso = " + curso + "\n1 Nota = "+ notas[0] + "\n2 Nota = " + notas[1] + "\n3 Nota = " + notas[2];
    }   
 }
    
asked by Mario Guiber 26.08.2017 в 18:59
source

1 answer

2

The problem is that you are referring to the same date reference, Create the object Fecha within for

for(int i=0;i<2;i++){
    objAlumnos = new Alumnos();
    Fecha fechaNacimiento = new Fecha();

int notes [] = new int [3];

After this it gives the values

do{             
    System.out.print("Dia(1-31): ");
    fechaNacimiento.setDia(tecla.nextInt());                              
    System.out.print("Mes(1-12): ");
    fechaNacimiento.setMes(tecla.nextInt());
    System.out.print("Año(1980-2020): ");
    fechaNacimiento.setAnio(tecla.nextInt());
    if(fechaNacimiento.fechaCorrecta()==false)
    {
       System.out.println("\nFecha de nacimento incorrecta, introduzca de nuevo");
    }
}while(fechaNacimiento.fechaCorrecta()==false); 

and Finally it is stored in the Alummo

objAlumnos.setFecha_nacimiento(fechaNacimiento);
    
answered by 26.08.2017 / 21:06
source