从布尔方法返回字符串

时间:2017-10-14 19:33:50

标签: java

我有以下类图。

enter image description here

我需要输出一个看起来像的詹姆斯,希德(1234):导演100000.0英镑(税收29822.0英镑)并且有资格获得奖金 < / p>

但是,无论员工是否有资格获得奖金,我都找不到打印退货部分的方法。

非常感谢任何帮助!

这是我尝试创建课程。

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 ";
    }

}

2 个答案:

答案 0 :(得分:1)

您可以在这里使用三元运算符。

toString方法中,声明一个String类型变量,并根据工资是否大于40,000来设置此变量的值。然后在返回String

的末尾附加此变量的值

以下是如何做到的

@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;
    }

答案 1 :(得分:0)

我建议只更改你的toString方法,使其符合条件:

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

你也可以在toString方法中使用更复杂的逻辑,它们不必立即返回:

    @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;

}