« Back to the main page

Language: Java, Submitted: 7/07/10, Country: Canada - Raw | Download
package model;

import java.sql.Date;

public class Human extends Creature{
	private String name;
	private Date dob;
	private Date now;

	/**
	 * Constructor
	 * @param name
	 * @param dob
	 */
	public Human(String name, String dob) {
		super();
		this.name = name;
		if (dob != "") {
			this.dob = Date.valueOf(dob);
			this.now = new Date(System.currentTimeMillis());	
		}
	}

	/**
	 * Overloaded Constructor 
	 * @param name
	 */
	public Human(String name) {
		this(name,""); // call other constructor
	}

	
	public String getName(){
		return this.name;
	}
	
	public int getAge() {          
		if (this.dob != null){
	          if (this.now.before(this.dob)) {
	        	  throw new IllegalArgumentException("Date of birth cannot be in the future.");
	          } else {
	        	  long diff = this.now.getTime() - this.dob.getTime();
	        	  double age =  diff / 1000 / 60 /60 / 24 / 365.26;
	        	  return (int) age;
	          }
		} else {
			return super.getAge();
		}
	}
}