mardi 12 mai 2015

Java: Classes do not find each other in package (factory design pattern)

I have three different classes:

file Woman.java

package human;
public class Woman extends Human{
    private final String pnr;
    private final String namn;

    public Woman(String n, String p){
        namn = n;
        pnr = p;
    }
    public String toString(){
        return "My name is "+namn+" and I am a woman.";
    }

}

file Man.java

package human;
public class Man extends Human{
    private final String pnr;
    private final String namn;

    public Man(String n, String p){
        namn = n;
        pnr = p;
    }
    public String toString(){
        return "My name is "+namn+" and I am a man.";
    }   
}

file Human.java

package human;
public abstract class Human{

    public static Human create(String namn, String pnr){
        char nastsist_char = pnr.charAt(9); // takes second last char in pnr
        String nastsist_string = Character.toString(nastsist_char);
        float siffra = Float.parseFloat(nastsist_string);   //Converts to float
        if ((siffra % 2) == 0){                 //Checks if even
            return new Man(namn, pnr);          
        }
        else{
            return new Woman(namn, pnr);
        }

    }
}

When I try to compile the file Human.java, I get the error cannot find symbol: class Man, and cannot find symbol: class Woman. When I try to compile the file Man.java or Woman.java, I get the error cannot find symbol: class Human. No changing of public/private visibility of the classes have helped. All the files are located in a directory called human.

What is the problem here?

I should add that the goal I'm trying to achieve is to have an abstract class Human that can not be instantiated, and in that class I want to have a create method that returns instances of either Man or Woman depending on pnr. Also, I do not want Man or Woman to be possible to instantiate in any other way than through the create method.

Thanks

Aucun commentaire:

Enregistrer un commentaire