0
votes

i wrote some code and i want some help this programe about company

This is a main :

Employee e1=new Employee (43208931,"Zainab","Directer","14/5/1990",15000);
    Employee e2=new Employee (43208932,"Ahmed","Maneger","14/5/1991",18000);
    Employee e3=new Employee (43208932,"Talal","Engeniar","14/5/1989",1000);
    Employee e4=new Employee (43208931,"khaled","software Engeniar","14/5/1978",6000);
    Employee e5=new Employee (43208931,"Mohammed","Director","14/5/1978",9000);
    Employee e6=new Employee (43208931,"Jalel","software Designer","14/5/1978",8000);


     Department d1;
     Department d2;
     Department d3;

    d1 = new Department (e1 , e3 , "Jeddah Branch");
    d2 = new Department (e2 , e4 ,"Hail Branch");
    d3 = new Department (e5 , e6 ,"Musqet Branch"); 

     Project p1=new Project (d3,"Bank System");
     Project p2 = new Project (d2,"Employee System");
     Project p3=new Project (d1,"Student System");
     System.out.println(p1.toString());
     System.out.println(p2.toString());
     System.out.println(p3.toString());

This is class Employee :`public class Employee { private long ID; private String Name; private String position; private String DateOfBirth; private double Salary;

Employee(){

}
public Employee(long ID, String Name, String position, String DateOfBirth, double Salary) {
    this.ID = ID;
    this.Name = Name;
    this.position = position;
    this.DateOfBirth = DateOfBirth;
    this.Salary = Salary;
}

public long getID() {
    return ID;
}

public String getName() {
    return Name;
}

public String getPosition() {
    return position;
}

public String getDateOfBirth() {
    return DateOfBirth;
}

public double getSalary() {
    return Salary;
}

@Override
public String toString() {
    return "Employee{" + "ID=" + ID + ", Name=" + Name + ", position=" + position + ", DateOfBirth=" + DateOfBirth + ", Salary=" + Salary + '}';
}


`

This is class Department :`public class Department extends Employee { Employee Employee; private String DName;

Department(){

}



public Department(Employee Employee,Employee E, String DName) {
    this.Employee = E;
    this.Employee = Employee;
    this.DName = DName;

}

public Department(Employee Employee, Employee E,String DName, long ID, String Name, String position, String DateOfBirth, double Salary) {
    super(ID, Name, position, DateOfBirth, Salary);
    this.Employee = Employee;
    this.DName = DName;

}



 public Employee getEmployee(){
     return Employee;}

 public String getDName(){
     return DName;
 }

@Override
public String toString() {
    return "\n Department:{" + DName + " }" +Employee +  '}';
}

}`

Finally This is Class Project : `public class Project extends Department{

Department Department ;
String projecttName;
Project(){

}

public Project(Department Department, String DepartmentName) {
    this.Department = Department;
    this.projecttName = DepartmentName;
}

public Project(Department Department, String DepartmentName, String DName) {
    this.Department = Department;
    this.projecttName = DepartmentName;
}

public Project(Department Department, String DepartmentName, commpany.Employee Employee, String DName, long ID, String Name, String position, String DateOfBirth, double Salary) {
    super(Employee,Employee , DName, ID, Name, position, DateOfBirth, Salary);
    this.Department = Department;
    this.projecttName = DepartmentName;
}

public Department getDepartment() {
    return Department;
}

public String getDepartmentName() {
    return projecttName;
}

@Override
public String toString() {
    return "\n Project :" +projecttName+" {" + Department + '}';
}

} ` My problem is : Second Employee in Same Department isnt Appering In the print >> whats the problem?

3

3 Answers

0
votes

You are assigning two employees to the same variable meaning one is discarded

this.Employee = E;
this.Employee = Employee;

If you want to same two employees you have to save both of them and print both of them in your toString().

Hint: If you want to know how to do this, I suggest you use your IDE to generate this code. You don't have to write it yourself. ;)

0
votes

Your problems is here

public class Department extends Employee { 
Employee Employee; 
private String DName;

    Department(){

    }



    public Department(Employee Employee,Employee E, String DName) {
        this.Employee = E;
        this.Employee = Employee;
        this.DName = DName;

    }

You have only one instance field Employee Employee (note that name of variables start with lowercase or underscore).

When this code is executed

this.Employee = E;
this.Employee = Employee;

this.Employee which is in other words your private variable is simply rewritten

Solution for this is use: Two instance variables Employee e1; Employee e2

and then your constructor will look like

public Department(Employee e1,Employee e2, String DName) {
    this.e1 = e1;
    this.e2 = e2;
    this.DName = DName;

}

Also if you want to add more than two employees to specific department use List<Employee > myList or array

0
votes

In your Department class, you only have one Employee. To have multiple Employees you should use a List.

private List<Employee> employees = new ArrayList<Employee>();

public Department(Employee Employee, Employee E, String DName) {
    this.employees.add(E);
    this.employees.add(Employee);
    this.DName = DName;
}

@Override
public String toString() {
    return "\n Department:{" + DName + " }" + employees + '}';
}