Ik ben er maar gewoon aan begonnen =P .
Heb de klasse Persoon, en de klasse Birthdate gemaakt .
Code:
public class Person {
private String name = "Unknown";
private String firstname;
private int age;
public Person(String name, String firstname, int year, int month, int day){
this.setName(name);
this.setFirstName(firstname);
this.setAge(year, month, day);
}
public String getName(){
return this.name;
}
public String getFirstName(){
return this.firstname;
}
public int getAge(){
return this.age;
}
public void setName(String name){
if (name != null && !name.equals("")){
this.name = name;
}
}
public void setFirstName(String firstname){
if (firstname != null){
this.firstname = firstname;
}
}
public void setAge(int year, int month, int day){
Birthdate blaat = new Birthdate(year,month,day);
this.age = blaat.getAge();
}
}
Code:
import java.util.GregorianCalendar;
import java.util.Calendar;
public class Birthdate {
private int year;
private int month;
private int day;
private int age;
public Birthdate(int year, int month, int day){
this.setYear(year);
this.setMonth(month);
this.setDay(day);
this.setAge();
}
public int getYear(){
return this.year;
}
public int getMonth(){
return this.month;
}
public int getDay(){
return this.day;
}
public int getAge(){
return this.age;
}
public void setYear(int year){
if (year >= 1900 && year <= 2100){
this.year = year;
}
}
public void setMonth(int month){
if (month >= 1 && month <= 12){
this.month = month;
}
}
public void setDay(int day){
if (day >= 1 && day <= this.daysInAMonth(this.getMonth())){
this.day = day;
}
}
public void setAge(){
this.age = calculateAge();
}
private boolean isLeapYear(int year) {
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0 );
}
private int daysInAMonth(int month){
int days = 0;
if (month == 2){
days = 28 + (isLeapYear(this.getYear())?1:0);
} else {
if (month <= 7){
days = 30 + month%2;
}
else {
days = 31 - month%2;
}
}
return days;
}
private int calculateAge(){
Calendar current = new GregorianCalendar();
Calendar birthdate = new GregorianCalendar(this.getYear(),this.getMonth(),this.getDay());
int age = current.get(Calendar.YEAR) - birthdate.get(Calendar.YEAR);
if (current.before(new GregorianCalendar(current.get(Calendar.YEAR), month, day))){
age --;
}
return age;
}
}
Bon , dit is gewoon simpel voor de gebruiker. In de krant stond dat je best van RedBull moet afblijven als je onder de 16 bent, daarom de leeftijd erbij ! Dat geeft de gebruiker een waarschuwing :P .
Favorieten/bladwijzers