1

I wanted to create an employeeID automatically whenever I create a new employee object. I am implementing OOP concept for my assignment. My problem is I wanted to use getter and setter to create for the id. However, since the id should be generated automatically so I can't put any value for the parameter when creating instance. How can I solve this?

private String employeeID;

public void setEmployeeID(String employeeID){
    Random rand = new Random();
    int randint = rand.nextInt(100000);
    char subId = 'E';
    employeeID = subId + String.valueOf(randint);
    this.employeeID = employeeID;
}
public String getEmployeeID(){
    return employeeID;
}
3
  • 2
    If you don't pass the employeeId to the method, then it's not a setter. Commented Oct 27, 2020 at 8:18
  • 3
    make this method private and call it from your constructor. Remove the parameter. Commented Oct 27, 2020 at 8:19
  • Or if it requires you to set some other values first and have to call that method afterwards, change the name to initEmployeeID or something like that. It is not a setter. Commented Oct 27, 2020 at 8:30

3 Answers 3

2

To keep it simple: You need or want to use public void setEmployeeId(String id) and its a basic requirement. But if so, the idea of having such a method means that the class Employee should have any logic of id generation, instead by declaring such a method you say to the programmer that will use the Employee class: "Look, this class doesn't have an id, but you can create it and set".

If these are your intentions, then extract the logic of the id generation outside the employee class and use the "pure" setter:

main:
String id = generateId(...) // in this method there will be all the code with Random 
Employee emp = new Employee();
emp.setId(id);

.....


class Employee {
    private String id;
    public void setId(String id) {this.id = id;}
}

Otherwise, if you do think that the Employee should generate an id by its own, you don't need a setter. In this case you can either call the generation code in constructor (not really recommended, but will work) or create an "init" method that will set the initial state of id to the employee object. In this case, whoever creates an Employee, will have to call this method:

public class Employee {
  private String employeeId;
  public class Employee(...) {...}

  public void init() {
     setupId();
  }

  private void generateEmpId() {
    Random rand = new Random();
    int randint = rand.nextInt(100000);
    char subId = 'E';
    employeeID = subId + String.valueOf(randint);
    this.employeeID = employeeID;
  }
}

Then in the main:

main:
  Employee e = new Employee();
  e.init(); // otherwise id won't be generated

This is not really convenient because whoever uses creates the Employee object will have to call init on it. In this case you might consider a factory method:

public class Employee {
   private final String name;
   private String empId;
   private Employee(String name) {
      this.name = name;
   }

   public static Employee create(String name) {
       Employee emp = new Employee(name);
       emp.init(); 
   }

   private void init() {
     setupId();
  }

  private void generateEmpId() {
    Random rand = new Random();
    int randint = rand.nextInt(100000);
    char subId = 'E';
    employeeID = subId + String.valueOf(randint);
    this.employeeID = employeeID;
  }
}

Notice that the constructor is private now, which means that the only way to create Employee from main is using the create method. The init method also went private:

main:
Employee emp = Employee.create("John");
// the id is created - no need to remember to call "init" like in the previous example
Sign up to request clarification or add additional context in comments.

Comments

2

Employee class should look like:

public class Employee {
    private String employeeID;

    public Employee() {
    }

    public void setEmployeeId(String employeeID){
        this.employeeID = employeeID;
    }
    public String getEmployeeId(){
        return employeeID;
    }
}

From the class you want to assign new id, use the following:

public String generateId(String subId) {
    Random rand = new Random();
    int randint = rand.nextInt(100000);
    return  subId.concat(String.valueOf(randint));
}

Employee employee = new Employee();
employee.setEmployeeId(generateId('E'));

Comments

0
public class Emp {
    private String id;
    public Emp(){
        id=getRandomId();
    }
    public void setId(String x){
        id=new String(x);
    }
    public String getId(){
        return new String (id);
    }
    public static String getRandomId(){
        Random rand = new Random();
        return "E"+rand.nextInt(100000);
    }
}

getRandomId will return a random id.

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.