jueves, 29 de marzo de 2012

PROGRAMA DE CLASES (FECHA)

CLASE MAIN


import javax.swing.*;


public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        Fecha a, b;
        int x;

        a=new Fecha (Integer.parseInt(JOptionPane.showInputDialog("Dia?")),
                    Integer.parseInt(JOptionPane.showInputDialog("MEs?")),
                Integer.parseInt(JOptionPane.showInputDialog("Año?")));

        b=new Fecha (Integer.parseInt(JOptionPane.showInputDialog("Dia?")),
                    Integer.parseInt(JOptionPane.showInputDialog("MEs?")),
                Integer.parseInt(JOptionPane.showInputDialog("Año?")));

        JOptionPane.showMessageDialog(null,"Dia siguiente de: " + a.impFecha() + " es: " + a.diaSiguiente().impFecha());

        x=a.compararFecha(b);
        if (x==0)
            JOptionPane.showMessageDialog(null,"Son iguales");
        else
            if (x==-1)
                JOptionPane.showMessageDialog(null,a.impFecha() + " es menor de " + b.impFecha());
            else
                JOptionPane.showMessageDialog(null,a.impFecha() + " es mayor de " + b.impFecha());



    }

}


CLASE FECHA


public class Fecha {

    private int dia;
    private int mes;
    private int año;

    public Fecha (int dia, int mes, int año) {
        this.dia=dia;
        this.mes=mes;
        this.año=año;
    }

    public int compararFecha(Fecha f) {
        //-1 Si es menor, 0 si son iguales y 1 si es mayor
        int sw=0;
        if (f.getAño()==this.año)
        {
            if (this.mes==f.getMes())
            {
                if (this.dia==f.getDia())
                    sw=0;
                else
                    if (this.dia<f.getDia())
                        sw=-1;
                    else
                        sw=1;
            }
            else
                if (this.mes<f.getMes())
                    sw=-1;
                else
                    sw=1;
        }
        else
            if (this.año<f.getAño())
                sw=-1;
            else
                sw=1;
        return (sw);
    }

    public boolean añoBisiesto() {
        if ((this.año%4) == 0)
            return(true);
        else
            return (false);
    }


    public Fecha diaSiguiente(){
        Fecha t=new Fecha (this.dia, this.mes, this.año);

        t.setDia(t.getDia()+1);

        switch (t.getMes()) {
            case 2: if (t.añoBisiesto())
                        if (t.getDia()>29)
                        {
                            t.setDia(1);
                            t.setMes(3);
                        }
                        else if (t.getDia()>28)
                             {
                                t.setDia(1);
                                t.setMes(3);
                              }

                      break;
            case 11:  //meses de 30
            case 4:
            case 6:
            case 9:  if (t.getDia()>30)
                       {
                          t.setDia(1);
                          t.setMes(t.getMes()+1);
                       }
                     break;
            case 12:  if (t.getDia()>31)
                       {
                          t.setDia(1);
                          t.setMes(1);
                          t.setAño(t.getAño()+1);
                       }
                     break;

            default: //meses de 31 dias
               if (t.getDia()>31)
                       {
                          t.setDia(1);
                          t.setMes(t.getMes()+1);
                       }
        }
        return(t);
    }

   public String impFecha() {
       return (this.dia + "/" + this.mes+ "/" +this.año);
   }


    public int getAño() {
        return año;
    }

    public void setAño(int año) {
        this.año = año;
    }

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


}

No hay comentarios:

Publicar un comentario