Fecha refactor

package PracticasUnidad2; /** * @author David Rico */ import java.util.Scanner; public class Fecha { //I would do this class immutable for thread safaty, since the actual Date class is not in java and it gives alot of headaches :p private final int dia,mes,año; private final static int[] DIAS_MES = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //If you want to give access to the "constant DIAS_MES" just let it be public :), //I don't see any problem, but I rly think that this constant is more for internal use. //I would rather use the Item 1 from Effective Java and create only static factories for this one //So lats make the constructor private :) //in order to get the older date possible I would rather create a static factory private Fecha(){ dia = 1; mes= 1; año=1900; } public static oldestDate(){ return new Fecha(); } private Fecha(int dia,int mes,int año){ //I would rly rather throw an exception if the data is invalid to advice the developer in how to create this class if(!isDiaValid(dia, mes)){ throw new Exception("Dia must be between x and y and for the month Z need to be between x and p"); } this.dia=dia; this.mes=mes; this.año=año; } //Instead of making this this validation here I would rather create a private method and use in the private constructor. private boolean isDateValid(int dia, int mes, int año){ int diasmes=DIAS_MES[mes]; boolean isDayValid = !(mes==2&& isBisiesto(año)) || (dia >= 1 && dia <= diasmes); boolean isMonthValid = (mes>=1&&mes<=12); return isDayValid && isMonthValid; } private boolean isBisiesto(int año){ return ((año%4==0 && año%100!=0) || año%400==0); } //I think would be more clear to create a function "clone" than have a constructor that receive a Fecha object. public static Fecha clone(){ return new Fecha(this.dia, this.mes, this.año); } //I would create another object with the actual value incremented by one to keep the use of immutable objects // If u need a sum method you should use it on your increment :) public Fecha incremento(){ return this.suma(1); } public Fecha suma(int numeroDias){ int newMonth = this.mes; int diaMes = DIAS_MES(newMonth); int newYear = this.año; while(numeroDias%diaMes!=0){ if(diaMes ==2 && isBisiesto()){ diaMes = 29; } numeroDias -= diaMes; newMonth++; if(newMonth == 13){ newMonth = 1; newYear +=1; } diaMes = DIAS_MES(newMonth) } int newDay = this.dia+numeroDias; if(this.mes ==2 && isBisiesto()) diasMes = 29; if(newDay >diasMes){ newDay = 1; int newMonth += 1; if(this.mes == 13){ newMonth = 1; newYear +=1; } } return new Fecha(newDay,newMonth,newYear); } public int getDia(){return this.dia;} public int getMes(){return this.mes;} public int getAño(){return this.año;} public boolean isBisiesto(){ return isBisiesto(this.año); } @Override public String toString(){ return ((dia<10)?"0"+ dia:dia) + "/" + ((mes<10)?"0"+mes:mes)+ "/"+año; } public static void main5(String[] args) { Fecha primeroMarzo =new Fecha(01,3,2016); Fecha visitahp = new Fecha(25,2,2016); Fecha navidad = new Fecha(25,12,16); Fecha Vacaciones = new Fecha(18,03,16); Fecha f = new Fecha(); System.out.println("primeroMarzo = "+ primeroMarzo); System.out.println("Visita HP = "+visitahp); System.out.println("Final = "+f); System.out.println("Vacaciones!!! = "+ Vacaciones); System.out.println("Navidad = "+navidad); } public static void main(String[] args) { Fecha f1 = new Fecha(25,2,2016); Fecha f2 = new Fecha (31,12,2016); Fecha f3 = f1.suma(14); Fecha f4 = f2.suma(60); System.out.println("F3 = "+f3); System.out.println("F4 = "+f4); } }
Refactor of the code developed by David Rico ( https://codepad.co/snippet/OLzB70he).

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.