Wednesday, 7 September 2016

One-one

1.       One –to-One Association:
It is a relationship between entity classes, where one instance of an entity should be mapped with exactly one instance of another entity.

Ex :-
Every Employee HAS exactly one Account



Ex code:-
class Account
{
                String accNo;
                String accName;
                String accType;
                Account(String accNo, String accName, String accType)
                {
                                this.accNo=accNo;
                                this.accName=accName;
                                this.accType=accType;
                }
}
class Employee
{
                String eid;
                String ename;
                float esal;
                String eaddr;
                Account acc;
                Employee(String eid, String ename, float esal, String eaddr, Account acc)
                {
                                this.eid=eid;
                                this.ename=ename;
                                this.esal=esal;
                                this.eaddr=eaddr;
                                this.acc=acc;
                }
                public void getEmpDetails()
                {
                                System.out.println("Employee Details");
                                System.out.println("---------------------");
                                System.out.println("Employee Id      :"+eid);
                                System.out.println("Employee Name    :"+ename);
                                System.out.println("Employee Salary  :"+esal);
                                System.out.println("Employee Address :"+eaddr);
                                System.out.println("Account Details");
                                System.out.println("---------------------");
                                System.out.println("Account Number   :"+acc.accNo);
                                System.out.println("Account Name     :"+acc.accName);
                                System.out.println("Account Type     :"+acc.accType);
                }
}
class OneToOneEx
{
                public static void main(String[] args)
                {
                                Account acc=new Account("abc123","Sai","Savings");
                                Employee emp=new Employee("E-111","Sai",50000.0f,"Hyd", acc);
                                emp.getEmpDetails();
                }
}

Output:-
Employee Details
---------------------
Employee Id      :E-111
Employee Name    :Sai
Employee Salary  :50000.0
Employee Address :Hyd
Account Details
---------------------
Account Number   :abc123
Account Name     :Sai
Account Type     :Saving


Flow of execution:-



No comments:

Post a Comment