In: Computer Science
Create a new project for Constructor Injection. Create three classes, Address.java, Employee.java and Department.java with the following fields :
Address.java
1) int houseNumber
2) String streetName
3) String zipCode
Department.java
int departmentId
String departmentName
Employee.java
1) String firstName
2) String lastName
3) String ssn;
4) Address address
5) Department department
Using Spring Constructor Injection inject Address and Department into the Employee class.
java code
import java.io.*;
class Address
{
int houseNumber;
String streetName;
String zipcode;
public String.toString()
{
return "streetname"+"zipcode";
}
public Integer.toString()
{ return "houseNumber";
}
}
class Department
{
String departmentName;
int departmentd;
public String.toString()
{
return "departmentName";
}
public Integer.toString()
{ return "departmentId";
}
}
class Employee
{
String firstname;
String ssn;
String lastname;
Address address=new Address();
Department department=new Department()
Employee(String t,int y,String f,String l,String m ,int a,string b,string c) //Constructor Injection
{
Address.streetName=b;
Address.zipcode=c;
Address.houseNumber=a
Department.departmentName=t;
Department.departmentId=y;
firstname=f;
ssn=m;
lastname=l;
}
}
void display()
{
System.out.println(address);
System.out.println(department);
System.out.println(firstname+""+lastname+""+ssn+""+Address.streetName+""+Address.zipcode+""+Address.houseNumber +""+Department.departmentName+""+Department.departmentId);
}
}class Emain
{
public static void main(String args[])
{Employee e=new Employee("computerSc",23,"Rahul","sharma","abc",56,"madis_street",3454);
e.display();
}
}