1

I have the following class diagram.

enter image description here

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

}
2
  • I have downvoted this question because there is no evidence of any debugging performed on this code. Please edit your question to show us what your debugging has uncovered, as well as a specific question about a specific line of code. See: How to create a Minimal, Complete, and Verifiable example and How to Debug Small Programs. Commented Oct 14, 2017 at 19:37
  • @bcsb1001 By the salary of the employee.If its more than 40000 then the method should return that they are eligible for bonus.If its under 40000 then it should return that they are not eligible. Commented Oct 14, 2017 at 19:41

2 Answers 2

1

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;
    }
Sign up to request clarification or add additional context in comments.

2 Comments

When determining your val String, you should be able to call the object's eligibleForBonus() method instead of redefining it within the toString().
@luckydog32 i just showed him how he can achieve his goal. Anyways you are right, call eligibleForBonus() method is a better choice. edited my answer.
0

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;

}

1 Comment

whoever down-voted your answer probably did so because of the first code snippet in your answer. Its invalid. It will throw an error saying string cannot be converted to boolean.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.