Search code examples
javaoopgetter-setter

How to set location if null OOP


How can I get this to print the words "whole house" if the location wasn't originally entered. I think it isn't working because when the task is created it is using the constructor that doesn't have instructions for location. Is there a way to get the location to be changed from null to the string I specify?

Here is my Task class:

public class Task{

    private final String choreName; 
    private final int maxCompletionInMinutes;
    private final String difficulty;
    private String location;

    public Task(String choreName, int maxCompletionInMinutes, String difficulty){
       this.choreName = choreName;
       this.maxCompletionInMinutes = maxCompletionInMinutes;
       this.difficulty = difficulty;
    }

    public Task(String choreName, int maxCompletionInMinutes, String difficulty, String location){
       this(choreName, maxCompletionInMinutes, difficulty);
       this.location = location;
    }

    public String getChoreName(){
        return choreName; 
    }    
    public int getMaxCompletionInMinutes(){
        return maxCompletionInMinutes;
    }
    public String getDifficulty(){
        return difficulty;
    }
    public String getLocation(){
        return location;
    }

    public void setLocation(String location){
        if(location == null)
        location = "Whole house";

        this.location = location;
    }

    public void prettyPrint(){
        System.out.printf("Complete the chore: %s (%s) in location: %s. This chore should take a maximum of %d minutes.%n", choreName, difficulty, location, maxCompletionInMinutes);
    }

}

Here is my driver class:

public class Assignment5{
    public static void main (String[] args){
        Task task1 = new Task("Dishes", 40, "Hard", "Kitchen");
        Task task2 = new Task("Dust", 45, "Hard");
        Task task3 = new Task("Vacuum", 30, "Medium");
        Task task4 = new Task("Make beds", 15, "Easy");
        Task task5 = new Task("Water plants", 15, "Easy", "Living Room");

        task1.prettyPrint();
        task2.prettyPrint();
        task3.prettyPrint();
        task4.prettyPrint();
        task5.prettyPrint();
    }

}

Solution

  • I would probably just switch up the ordering of the constructors. Make the three argument constructor call the four argument constructor and supply the default value:

    public Task(String choreName, int maxCompletionInMinutes, String difficulty) {
       this(choreName, maxCompletionInMinutes, difficulty, "Whole house");
    }
    
    public Task(String choreName, int maxCompletionInMinutes, String difficulty, String location) {
       this.choreName = choreName;
       this.maxCompletionInMinutes = maxCompletionInMinutes;
       this.difficulty = difficulty;
       this.location = location;
    }
    

    Then just remove the null checking logic in the setter.

    This is also nice because now you can make location a final field as well, making the Task class immutable and thread-safe