vendredi 23 février 2018

Startegy Design Pattern

Hello I'm trying to implement Strategy Design Pattern but I'm getting a null pointer exception in the main method

This is my Interface Course

public interface Course {


    public String courseName();

}



And These are my Implementation Classes

public class JavaCourse implements Course{

    @Override
    public String courseName() {
        return "Java Course is completed and job is ready";
    }

}



public class Python implements Course{

    @Override
    public String courseName() {
        return "Python course is completed Job is ready";
    }

}



This is my Factory class

public class CourseFactory {

    public static JobSeeker getInstance(String course){
        Course cou= null;
        JobSeeker js= null;
        if(course.equals("java")){
            cou= new JavaCourse();
            js= new JobSeeker();
            js.setCourse(cou);
            System.out.println("JAAAAAVAAAVAVAVAVAVAVA");
        }else if(course.equals(".net")){
            cou= new DotNet();
            js= new JobSeeker();
            js.setCourse(cou);
        }else if(course.equals("python")){
            cou= new Python();
            js= new JobSeeker();
            js.setCourse(cou);
        }
       return null; 
    }
}



This is the Service class

public class JobSeeker {
    private Course course;

    public void setCourse(Course course){
        this.course= course;
    }

    public String getJob(String company){
        String status = null;
        status= course.courseName();
        return "Since "+status+" for the Company "+company;
    }
}



This is the Main Class

public class Test {
    public static void main(String[] args){
        //CourseFactory cf= new CourseFactory();

        JobSeeker js= new JobSeeker();
        js= CourseFactory.getInstance("java");
        System.out.println("----------");
        System.out.println(js.getJob("IBM"));
    }
}


I'm getting Null Pointer Exception when I'm calling js.getJob("IBM")
I'm expecting output like:::
Since Java Course Is completed job is ready for the company IBM

Aucun commentaire:

Enregistrer un commentaire