Results 1 to 8 of 8
  1. #1
    emtu is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default How to use date in my constructor?

    Hello,

    I have to creat a abstract class called Person with atributes name(String), birthday(Date) and gender(char).

    for example code:
    PHP Code:
    public abstract class Person{
       private String name;
       private Date date;
       private char gender;
    
       public Person(String name,Date date,char gender){
          statments...
          ...
       }
      
    }
    Employable Class, which extends from Person, and implements Work.
    With atributes, id(int) and proffesion(String).
    Methods, toString and equals.




    And the main class

    Company, that have an ArrayList and company name( name(String) ).

    I need to use Date, or SimpleDateFormat or others, when i creat an instance of Employable, like:

    PHP Code:
    Employable jet = new Employable("jett", 01/12/1990, 'M');
    thanks.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

    and with sdf.parse("01/12/1990") you will get an date object which you can pass to your constrcutor!

  3. #3
    emtu is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    But, how tu put in constructor ?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    I dont understand your question. What problem do you have right now?

    --> new Employable("jett", sdf.parse("01/12/1990"), 'M', ....) //maybe more than these parameters?
    (or pass only the string and parse this string in your Employable class....)

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default

    Your Person class takes three parameters in its constructor so every class extending this Person class has to pass those three parameters to its superclass constructor (otherwise it can't be created). If your extending class takes two additional parameters in its own constructor it has to take five parameters in total: two for itself and three to pass on to its superclass constructor. Something like this:

    Java Code:
    public class Employable extends Person {
       public Employable (String name, Date date, char gender, int ID, String profession) {
          super(name, date, gender); // pass on to the superclass constructor
          // do something with ID and profession here
       }
       ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    emtu is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Hmm.


    Person Class

    PHP Code:
    import java.text.SimpleDateFormat; 
    import java.util.Date;
    import java.text.ParseException;
    
    
    public abstract class Personi{
    	private String emri;
    	private char gjinia;
    	private Date date;
    
    	public Personi(String emri,Date date,char gjinia) throws KompaniaException{
    		if(emri == null || emri.equals(""))
    			throw new KompaniaException("Emri duhet ti ket se paku dy karaktere");
    		if(gjinia != 'M' && gjinia != 'F')
    			throw new KompaniaException("Nuk mund te ket tjeter gjini pos Femer dhe Mashkull [F,M]");
    		this.date = date;
    		this.emri = emri;
    		this.gjinia = gjinia;
    		
    	}		
    
    	public void setEmri(String emri)throws KompaniaException{
    		if(emri == null || emri.equals(""))
    			throw new KompaniaException("Emri duhet ti ket se paku dy karaktere");
    		this.emri = emri;
    	}
    	
    	public String getEmri(){
    		return emri;
    	}
    	
    	public char getGjinia(){
    		return gjinia;
    	}
    	
    	public void setDate(Date date){
    		this.date = date;
    	}
    	
    	public Date getDate(){
    		return date;
    	}
    }
    Employable Class

    PHP Code:
    package Kompania;
    
    import java.text.SimpleDateFormat; 
    import java.util.Date;
    import java.text.ParseException;
    
    public class Punetori extends Personi implements Punon{
    	private int id;
    	private String profesioni;
    	
    	public Punetori(String emri,Date date,char gjinia, int id,String profesioni)throws KompaniaException{
    		super(emri,date,gjinia);
    		
    		this.id = id;
    		this.profesioni = profesioni;
    	}
    	
    
    	public int getId(){
    		return id;
    	}
    	public String getProfesioni(){
    		return profesioni;
    	}
    	
    	public String toString(){
    		return "[ Name: "+getEmri()+"Birthday: "+getDate()+", Gender: "+getGjinia()+", ID: "+getId()+" Profession: "+getProfesioni()+" ]";
    	}
    	
    	public boolean equals(Object ob){
    		if(ob instanceof Punetori){
    			Punetori p = (Punetori)ob;
    		return id == p.getId();
    		}
    	return false;
    	}
    }
    Compay Class

    PHP Code:
    package Kompania;
    
    import java.util.ArrayList;
    import java.text.SimpleDateFormat; 
    import java.util.Date;
    import java.text.ParseException;
    
    public class Kompania{
    	private String emri;
    
    	private ArrayList <Punetori> punetoret = new ArrayList<Punetori>();
    	
    	public Kompania(String emri) throws KompaniaException{
    		if(emri == null || emri.equals(""))
    			throw new KompaniaException("Emri duhet ti ket se paku dy karaktere");
    		this.emri = emri;
    	}
    	
    	public static void main(String [] args){
    		try{
    			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    			
    			Kompania ko = new Kompania("ABC");
    			ko.punetoret.add(new Punetori("Fatos",sdf.parse("01/12/1990"),'M',2010,"Programmer"));
    			ko.punetoret.add(new Punetori("Bertina",sdf.parse("01/12/1990"),'F',2011,"Asistente"));
    			ko.punetoret.add(new Punetori("Samuel",sdf.parse("01/12/1990"),'M',2012,"IT"));
    			
    			
    			for(Punetori pun : ko.punetoret)
    				System.out.println(pun);
    			
    			System.out.println("Para se te fshihej 'Bertina'");
    			
    			Object obj = ko.punetoret.remove(1);
    			
    			for(Punetori pun : ko.punetoret)	
    				System.out.println(pun);
    		}
    		catch(KompaniaException ke){
    			ke.printStackTrace();
    		}
    	}
    }

  7. #7
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    you have an unhandled exception at sdf.parse...so you have to catch the ParseException!
    Add/replace:

    Java Code:
       catch(KompaniaException ke){
                ke.printStackTrace();
            } catch (ParseException e) {
    			e.printStackTrace();
    		}

  8. #8
    emtu is offline Member
    Join Date
    Dec 2010
    Posts
    9
    Rep Power
    0

    Default

    Sorry, i have forget

    PHP Code:
    		catch(ParseException pe){
    			pe.printStackTrace();
    		}
    Thanks eRaaa.

Similar Threads

  1. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  2. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM
  3. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •