'I need to create a class that represents an employee. if the user types nothing then it should output "(not set)"
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.
}
}
Solution 1:[1]
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 :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Omar RB |
