-1

Create a class that represents an employee. This class will have three constructors to initialize variables. If the constructor doesn't provide a parameter for a field, make it either "(not set)" or "0" as appropriate. Name: Employee Fields:

  • name : String
  • idNumber : int
  • department : String
  • position : String Methods:
  • Employee()
  • Employee(name : String, idNumber : int)
  • Employee(name : String, idNumber : int, department : String, position : String)
  • getName() : String
  • getDepartment() : String
  • getPosition() : String
  • getIdNumber() : int

My issue is that I don't know if the Employee() field is a void Also not sure why public class Employee is included in the comment rather than the code.

public class Employee {
    private String name;
    private int idNumber;
    private string department;
    private string position;

    /* public void setEmployee (String n)
    {
        name = n;

        if (n == null)
        {
            n = "(not set)";
        }
    }

    public void Employee (string n, int id)
    {
        name = n;
        idNumber =id;
    }

    public void Employee (string n, int id, string d, string p)
    {
        name = n;
        idNumber = id;
        department = d;
        position = p;
    } */

    public String getName ()
    {
        return name;
    }

    public string getDepartment ()
    {
        return department;
    }

    public string getPosition ()
    {
        return position;
    }

    public int getIdNumber ()
    {
        return idNumber;
    }
}

Also having an issue with this second part Not sure where to go from here

import java.util.Scanner;

public class EmployeeDem {

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

        String option;

        do {
            System.out.println("-- Employee ID Menu --\n Enter 'exit' to quit");
            option = keyboard.nextLine();

            System.out.println("What is your name?");
            String name = keyboard.nextLine();

            System.out.println("What is your ID number?");
            int idNumber = keyboard.nextInt();

            System.out.println("What is your department?");
            String department = keyboard.nextLine();

            System.out.println("What is your position?");
            String position = keyboard.nextLine();
        } while (option != "exit");


        //class  obj        instance
        Employee myEmployee = new Employee();
        myEmployee.

    }

}

Agent51
  • 11
  • 3
  • 1
    `Employee()` is a constructor. It doesn't have any return type. [read this](https://stackoverflow.com/questions/1788312/why-do-constructors-not-return-values) – sittsering Jan 28 '22 at 19:46
  • My guess is that the Employee code is left for you do do. The Employee will be called with new Employee () it will not be void. – cliff2310 Jan 28 '22 at 19:49
  • I have another set of code called "EmployeeDemo" to call this. Not sure how to post another set of code. – Agent51 Jan 28 '22 at 19:50
  • public Employee(String name){ this.name = (name == null || name.isEmpty()) ? "(not set) : name; } – Mohammad Reza Khahani Jan 28 '22 at 19:53

1 Answers1

0

You can initialize the variables first when creating the class, if you don't change them using setters or constructors they will have the initial value that you setted. I initialized them with "(not set)" and 0

public class Employee {

    private String name = "(not set)";
    private int idNumber = 0;
    private String department = "(not set)";
    private String position = "(not set)";
    
    public Employee() {}
    
    public Employee(String name, int idNumber) {
        this.name = name;
        this.idNumber = idNumber;
    }
    
    public Employee(String name, int idNumber, String position) {
        this.name = name;
        this.idNumber = idNumber;
        this.position = position;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getIdNumber() {
        return idNumber;
    }

    public void setIdNumber(int idNumber) {
        this.idNumber = idNumber;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

}

Hope this is what you want :)

Omar RB
  • 197
  • 1
  • 5