Use date class in Java

0

I have made a class date but when I try to visualize the day, month and year those that are initialized in the constructor appear and not those that I enter by keyboard, I visualize it by means of the toString () method

Class code 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 is this with the toString () method:

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 + "\n\nFecha de nacimiento"+
            "\nDia = " + fecha_nacimiento.dia + "\nMes = " + fecha_nacimiento.mes + "\nAño = " + fecha_nacimiento.anio + 
            "\nCurso = " + curso + "\n1 Nota = "+ notas[0] + "\n2 Nota = " + notas[1] + "\n3 Nota = " + notas[2];
    }   
}

And my main class:

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];

        //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");
            System.out.print("Dia: ");
            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());
            tecla.nextLine();//Limpiar buffer
            System.out.print("Curso: ");
            objAlumnos.setCurso(tecla.nextLine());
            for(int j=0;j<3;j++)
            {
                System.out.print("Nota "+(j+1)+" : ");
                notas[j]=tecla.nextInt();
            }
            objAlumnos.setNotas(notas);
            ArrayListAlumnos.add(objAlumnos);
        }

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

When the data is displayed, everyone does it correctly, less day, month and year than appears 1/1/2000

Thank you very much in advance.

I add my personal data class just in case something is wrong:

public class Datos_Personales
{
    public String dni;
    public String nombre;
    public Fecha fecha_nacimiento;

    public Datos_Personales()
    {
        super();
        dni = new String();
        nombre = new String();
        fecha_nacimiento = new Fecha();
    }

    public Datos_Personales(String dni,String nombre,Fecha fecha_nacimiento)
    {
        super();
        this.dni=dni;
        this.nombre=nombre;
        this.fecha_nacimiento=fecha_nacimiento;
    }

    public String getDni() {
        return dni;
    }

    public void setDni(String dni) {
        this.dni = dni;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public Fecha getFecha_nacimiento() {
        return fecha_nacimiento;
    }

    public void setFecha_nacimiento(Fecha fecha_nacimiento) {
        this.fecha_nacimiento = fecha_nacimiento;
    }   
}
    
asked by Mario Guiber 26.08.2017 в 14:02
source

2 answers

1

In your code, you initialize the variable Fecha objFecha and assign the values by setters correctly, however you never associate its value with the attributes of objAlumnos . Change the code like this:

objAlumnos.setNotas(notas);
//aquí la nueva línea
objAlumnos.setFecha_nacimiento(objFecha);
ArrayListAlumnos.add(objAlumnos);
    
answered by 26.08.2017 / 15:58
source
-1

Your error may be that you are calling the empty constructor this initializes the date in 1/1/2000 calls the other constructor with the data of the date you want.

In the main class, change:

Fecha objFecha1 = new Fecha(); 

By:

Fecha objFecha2 = new Fecha(8, 7, 2005);  
    
answered by 26.08.2017 в 14:46