0

Still learning Java, bit of an idiot and I apologize for the trouble. I have a question with regards to using user input from a main.class with an employee.class with getters and setters and returning those back to the main.class. Is this the correct way of using methods from the employee.class with user input? Would it be okay to just point me to the right direction.

Here's the Employee.class which needed a default value of true.

 public class Employee() {
        private String id = "100";
        private boolean manager = true;

public Employee(String id, String name, int numOfYears) {
    this.id = id;
    this.name = name;
    this.numOfYears = numOfYears;
}

public Employee(String id, String name, int numOfYears, double 
    monthlySalary, boolean manager) {
    this.id = id;
    this.name = name;
    this.numOfYears = numOfYears;
    this.monthlySalary = monthlySalary;
    this.manager = manager;
}

public String getId() {
    return id;
    }

public void setId(String id) {
    this.id = id;
    }
public boolean getManager() {
    return manager;
    }

public void setManager(boolean manager) {
    if (manager == true) {
        System.out.println("Yes");
        this.manager = manager;
    } else {
        System.out.println("No");
        this.manager = manager;
        }
    }
}

And here's the Main.class

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Enter employee ID:");
        int i = input.nextInt();
        String id = Integer.toString(i);

        System.out.println("In management? True/False");
        boolean manager = input.nextBoolean();

        Employee empId = new Employee(id);
        empId.setId(id);

        Employee empManager = new Employee();
        empManager.setManager();

    }
}

Would the correct way to input the user values be

        Employee empManager = new Employee(manager);
        empManager.setManager();

I'm not sure if I could ask, but I feel like I probably have more errors, if it'd be okay to just nudge in the right direction.

2
  • @OneCricketeer Thanks for the edit Commented Jan 28, 2021 at 20:19
  • Sure, but public Employee(String id) itself is missing for new Employee(id); to work correctly Commented Jan 28, 2021 at 20:21

2 Answers 2

2

It's not the correct way.

The correct way would be to define a proper constructor:

public Employee(String id, boolean isManager)
{
    this.id = id;
    this.isManager = isManager;
}

and call it like that:

Employee employee = new Employee(id,isManager);

Brief hint: For booleans, it's a good practice to name them with "isXYZ" - that's why I've changed the name. Also, an if(var == true) can be written as if(var) - the true is implicit.

Sign up to request clarification or add additional context in comments.

5 Comments

Ah, apologies. I forgot to attach the constructors. I was asked to add 2 constructors. Thanks for the response maio and for the tip about == true. Did I write out the employee object correctly? It was also requested that the data member for checking if the employee is a manager or not to be 'manager' employee.class, would I still need to change it to isManager?
It's homework, isn't it? You wouldn't print anything in a set method. But creating two employee objects is the wrong thing to do. You can replace the primitive values with their class counterpart in order to null them. So you could just pass null for the primitive values in the constructor. Also you need to define all the fields you reference with this in your class. You can name your variables like you want - the name doesn't imply any logic at all ;)
Yeah, for an assignment. Are we allowed to ask for help on assignments? Apologies if it's not allowed and I'll delete if it is. Appreciate the help regardless maio.
It's fine as long as you don't ask for others to do your work.
Oh, for sure. I wasn't trying to get the work done for me. That's why I just asked for a nudge to the right direction. I know I'd just be screwing myself over if I didn't try to understand what I was doing incorrectly. Sorry again if it came out that way.
1

The right answer is that there is no "right" answer. This is 100% a philosophical question with a number of "right" answers. That said, there are some things most developers will agree with that you need to consider for you to decide which "right" answer is the right one FOR YOU.

  1. What parameters should you pass via constructor? The constructor should be used to enforce REQUIRED values at the time of construction. For example, name and id we can agree should be required fields. But, what about managerName? Maybe the manager has not been assigned at the time of hire for some reason (i.e. position is vacant).

  2. Since it's OK to have setter methods, might as well use them. There is nothing wrong with providing a setter method and use such method to set a value AFTER the object is created. As a general rule, we include getter/setter methods for all fields. HOWEVER, I think it is better to decide which fields (if any) are constant and provide setters (obviously) for variable fields only.

For argument's sake, let's assume that a person should not be able to change his or her name. IN that case, I would design my Employee class like this:

public class Employee {
    private final String name; // Required (constant)
    private final String id; // Required (constant)
    private String phoneNo; // optional
    private String managerName; // "optional"
    private boolean isManager; // required (variable)

    public Employee (String name, String id) {
        this.name = name;
        this.id = id;
    }

    public void setPhoneNo(String phoneNo) { this.phoneNo = phoneNo; }

    public void setManagerName(String managerName) { this.managerName = managerName; }
}

Also, consider default values. In Java, Primitive Data Types have default values as follows: numeric primitives have a default value of 0. Boolean primitives default to false, character primitive default to the Unicode character value of NULL (\u0000). This means that, unless you need to override the default value, setting the value might be optional (even though the value is required). What does that mean? In your example, you have a boolean flag to indicate whether an employee is in management. Knowing that the majority of employees are not going to be in management, the above example's constructor should be enough. For someone that is hired as manager, you can provide three-argument constructor to pass "true" to this boolean field OR simply build the employee object with the existing constructor and then use the setter method to set this flag to true.

Employee hector = new Employee("Hector", "100");
hector.setManager(true);

The above will be as acceptable as

Employee hector = new Employee ("Hector", "100", true);

Having this three-argument constructor does not invalidate the need for the two argument one. It is fine to keep both. However, if you must do so, use a telescoping pattern...

public Employee (String name, String id, boolean isManager) {
    this(name, id); // calling the two-arg constructor
    this.isManager = isManager;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.