1
public class Employee {

     public  Employee(){
        id=-1;
        year=-1;
        salary=-1; 
        name="NA";
        department="NA"; 
    }

    public  Employee(int id ,String name ,String department, int year,double salary){
        String s="Sales";
        String i="IT";
        String W="Warehouse";
        if(department.equals(W)) {
            this.department=department;}
        else 
            if(department.equals(s)) {
                this.department=department;}
            else 
                if(department.equals(i)) {
                    this.department=department;}
                else 
                    System.out.print("Department was not set"); 
    }

main

int input,id ,year ;
double salary;
String name ,department; 

System.out.print("Enter Employee 1 details (id, name, department, years, salary)");
id=kb.nextInt(); 
name=kb.next(); 
department=kb.next();
year=kb.nextInt(); 
salary=kb.nextDouble();  
Employee Employee=new Employee(id, name, department, year, salary);

My issue is I declared values in the default constructor that set

id=-1;
year=-1;
salary=-1; 
name="NA";
department="NA";

But when the user enters invalid values it prints null or 0

I tried using

Employee Employee=new Employee();
Employee=new Employee(id, name, department, year, salary);

but it the same prints null or 0. Any ideas where is the issue

2
  • You have more than one constructor. Commented Apr 18, 2020 at 9:46
  • Do you assign the arguments to the class variables ? public Employee(int id ,String name ,String department, int year,double salary){this.id=id;this.name=name; ........} Commented Apr 18, 2020 at 10:27

1 Answer 1

1

Include a call to the default constructor from your all-argument constructor.

public Employee(int id, String name, String department, int year, double salary) {
    this();
    // the rest

}

The problem is

new Employee()

and

new Employee(id, name, department, year, salary)

are independent and the latter sets only one department field, while you want them all to be set.

 this();

would mean "populate with the default values first".

This constructor

 Employee(int id, String name, String department, int year, double salary)

is also an issue. It takes a lot of arguments but works with one. Consider setting them

this.id = id;
// and others

The validation condition could be simplified

if (department.equals(W) || department.equals(s) || department.equals(i)) {
    this.department = department;
} else {
    // a message or exception 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Usually this is done the other way around. A constructor with fewer parameters calls a constructor with more parameters and passes default values, i.e. this(-1, "NA", "NA", -1, -1)

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.