简体   繁体   中英

How to set N/A for this particular variable?

I'd like to set N/A for the variable experience if the value is 0 for the employee experience.

So if the users inputs 0 number of experience for a job, the program should return N/A. How do I do that in this given class?

public class EmployeeService () {
    private String experience;
    private String position;
    
    public EmployeeService (String experience, String position) {
        this.setExperience (experience);
        this.setPosition(position);
    }
    
    public String getExperience() {
        return experience;
    }

    public void setExperience(String experience) {
        this.experience = experience;
    }
    
    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }
    
    public String toString() {
        return "Experience - " + experience + "Position" + " - " + position;
    }
    
}

Just do it in the setter as follows:

public void setExperience(String experience) {
    if ("0".equals(experience)) {
        this.experience = "N/A";
    } else {
        this.experience = experience;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM