简体   繁体   English

从布尔方法返回字符串

[英]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 "我需要输出一个类似于“James, Sid (1234): Director at £100000.0 (£29822.0 tax) and arequalified for bonus ”的 toString

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.在你的toString方法中,声明一个String类型的变量,并根据薪水是否大于40,000设置这个变量的值。 Then append the value of this variable at the end of the returning String .然后在返回的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:我建议只更改您的 toString 方法以具有符合条件的条件:

        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:您还可以在 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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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