简体   繁体   中英

Return string from boolean method

I have the following class diagram.

在此处输入图片说明

I need to output a toString that looks like " James, Sid (1234): Director at £100000.0 (£29822.0 tax) and is eligible for bonus "

However i cannot find a way to print out the part where it return whether an employee is eligible for bonus or not.

Any help is much appreciated!

Here is my attempt to create the class.

package org.com1027.formative.ye00036;

public class Employee {
    //Class Field(s)
    private int id = 0;
    private String forename = null;
    private String surname = null;
    private Salary salary = null;
    private CompanyPosition companyPosition = null;

    //Parameterised constructor using all fields, allowing creation of objects
    public Employee(int id, String forename, String surname, Salary salary, CompanyPosition companyPosition) {
        super();
        this.id = id;
        this.forename = forename;
        this.surname = surname;
        this.salary = salary;
        this.companyPosition = companyPosition;
    }

    //Getters-Accessors
    //Returns the employee's ID
    public int getId() {
        return id;
    }
    //Returns the employee's Forename
    public String getForename() {
        return forename;
    }
    //Returns the employee's Surname
    public String getSurname() {
        return surname;
    }
    //Returns the employee's Salary
    public Salary getSalary() {
        return salary;
    }
    //Returns the employee's Company Position
    public CompanyPosition getPositionName() {
        return companyPosition;
    }
    //Checks if an employee is eligible for bonus
    public boolean eligibleForBonus(){
        if (salary.getSalary() >= 40000) 
            return true;
        else 
            return false;
    }

    @Override
    public String toString() {
        return getForename() + ", " + getSurname() + "(" + getId() +
                "): " + getPositionName() + "at " + salary.getSalary() + " (" + salary.calculateTax() + ") and is ";
    }

}

You can make use of ternary operator here.

Inside your toString method, declare a String type variable and set the value of this variable depending on whether the salary is greater than 40,000 or not. Then append the value of this variable at the end of the returning String .

Here's how you can do it

@Override
public String toString() {
    String val = (eligibleForBonus()) ? "eligible for bonus" : "not eligible for bonus";

    return getForename() + ", " + getSurname() + "(" + getId() +
                "): " + getPositionName() + "at " + salary.getSalary() + " (" + salary.calculateTax() + ") and is "+val;
    }

I would suggest just changing your toString method to have a condition for being eligible or not:

        return getForename() + ", " + getSurname() + "(" + getId() +
            "): " + getPositionName() + "at " + salary.getSalary() + " (" + salary.calculateTax() + ") and is "
            + (eligibleForBonus()? "eligible for a bonus" : "not eligible for a bonus");

You can also have more complex logic in toString methods, they don't have to return right away:

    @Override
public String toString() {
    String returnString = getForename() + ", " + getSurname() + "(" + getId() +
            "): " + getPositionName() + "at " + salary.getSalary() + " (" + salary.calculateTax() + ") and is ";

    if(eligibleForBonus()){
        returnString += "eligible for bonus.";
    }else{
        returnString += "not eligible for bonus.";
    }

    return returnString;

}

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