Created
September 6, 2013 03:41
-
-
Save fabioluciano/6459234 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Data { | |
private short ano; | |
private byte mes; | |
private byte dia; | |
public void setAno(short ano) { | |
this.ano = ano; | |
} | |
public short getAno() { | |
return this.ano; | |
} | |
public void setMes(byte mes) { | |
this.mes = mes; | |
} | |
public byte getMes() { | |
return this.mes; | |
} | |
public void setDia(byte dia) { | |
this.dia = dia; | |
} | |
public byte getDia() { | |
return this.dia; | |
} | |
public Data(short ano, byte mes, byte dia) { | |
this.setAno(ano); | |
this.setMes(mes); | |
this.setDia(dia); | |
} | |
public boolean isBissexto() { | |
if((this.getAno() % 4) == 0 && !(this.getAno() % 100 == 0) || (this.getAno() % 400) == 0) { | |
return true; | |
} | |
return false; | |
} | |
public boolean isValida() { | |
// System.out.println(this.getMes()); | |
if(this.getMes() < 1 || this.getMes() > 12) { | |
return false; | |
} | |
if(this.getMes() == 1 | |
|| this.getMes() == 3 | |
|| this.getMes() == 5 | |
|| this.getMes() == 7 | |
|| this.getMes() == 8 | |
|| this.getMes() == 10 | |
||this.getMes() == 12 | |
) { | |
if(this.getDia() < 1 || this.getDia() > 31) { | |
return false; | |
} | |
} | |
if(this.getMes() == 4 | |
|| this.getMes() == 6 | |
|| this.getMes() == 9 | |
|| this.getMes() == 11 | |
) { | |
if(this.getDia() < 1 || this.getDia() > 30) { | |
return false; | |
} | |
} | |
if(this.getMes() == 2) { | |
if(!this.isBissexto() && (this.getDia() < 1 || this.getDia() > 28)) { | |
return false; | |
} | |
if(this.isBissexto() && (this.getDia() < 1 || this.getDia() <= 29)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
public abstract String getData(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataAmericana extends Data { | |
public DataAmericana(short ano, byte mes, byte dia) { | |
super(ano, mes, dia); | |
} | |
public String getData() { | |
if(this.isValida()) { | |
return this.getMes() + "/" + this.getDia() + "/" + this.getAno(); | |
} else { | |
return "A data é invalida"; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataBrasileira extends Data { | |
public DataBrasileira(short ano, byte mes, byte dia) { | |
super(ano, mes, dia); | |
} | |
public String getData() { | |
if(this.isValida()) { | |
return this.getDia() + "/" + this.getMes() + "/" + this.getAno(); | |
} else { | |
return "A data é invalida"; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataEuropeia extends Data { | |
public DataEuropeia(short ano, byte mes, byte dia){ | |
super(ano, mes, dia); | |
} | |
public String getData() { | |
if(this.isValida()) { | |
return this.getAno() + "/" + this.getMes() + "/" + this.getDia(); | |
} else { | |
return "A data é invalida"; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestaData { | |
public static void main(String[] args) { | |
DataEuropeia data1 = new DataEuropeia((short) 2013, (byte) 4, (byte) 31); | |
DataAmericana data2 = new DataAmericana((short) 2013, (byte) 9, (byte) 5); | |
DataBrasileira data3 = new DataBrasileira((short) 2013, (byte) 9, (byte) 5); | |
System.out.println("Data Europeia: " + data1.getData()); | |
System.out.println("Data Americana: " + data2.getData()); | |
System.out.println("Data Brasileira: " + data3.getData()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment