Skip to content

Commit 82ae390

Browse files
committed
5.4 Method Overloading
The program contains modifyData() method which is overloaded by passing diffrent parameter.
1 parent 7b8354c commit 82ae390

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

TestEmployeeModify.java

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import java.util.Scanner;
2+
class Employee
3+
{
4+
String name;
5+
private int e_id;
6+
private float salary;
7+
private boolean status;
8+
9+
Scanner sc = new Scanner(System.in);
10+
void setData()
11+
{
12+
13+
System.out.println("Enter Name (String) of Employee: " );
14+
name=sc.nextLine();
15+
System.out.println("Enter Employee ID (Integer) of Employee: " );
16+
e_id=sc.nextInt();
17+
System.out.println("Enter Salary (Float) of Employee: " );
18+
salary=sc.nextFloat();
19+
System.out.println("Enter Working Satus (Boolean) of Employee: " );
20+
status=sc.nextBoolean();
21+
22+
}
23+
void getData()
24+
{
25+
System.out.println("Name:"+name+"\nEmp Id:"+e_id+"\nSalary:"+salary+"\nStatus:"+status);
26+
27+
}
28+
29+
void modifyData(String name) //modifyData methods are overloaded
30+
{
31+
this.name = name;
32+
System.out.println("Name changed successfully " );
33+
}
34+
void modifyData(int e_id)
35+
{
36+
this.e_id = e_id;
37+
System.out.println("ID changed successfully " );
38+
}
39+
void modifyData(float salary)
40+
{
41+
this.salary = salary;
42+
System.out.println("Salary changed successfully " );
43+
}
44+
void modifyData(boolean status , float salary)
45+
{
46+
this.status = status;
47+
if(status == true)
48+
this.salary = salary;
49+
else
50+
this.salary = 0.0f;
51+
52+
System.out.println("Working Status and Salary changed successfully " );
53+
}
54+
void modify(Employee e)
55+
{
56+
System.out.println("\nSelect Details to Modify : ");
57+
System.out.println("\n1. Name \n2.ID \n3. Salary \n4.Status and Salary");
58+
Scanner sc = new Scanner(System.in);
59+
int ch = sc.nextInt();
60+
switch(ch)
61+
{
62+
case 1: System.out.println("Enter new Name (String) of Employee: " );
63+
String name=sc.nextLine();
64+
e.modifyData(name);
65+
break;
66+
case 2: System.out.println("Enter new ID (Integer) of Employee: " );
67+
int e_id=sc.nextInt();
68+
e.modifyData(e_id);
69+
break;
70+
case 3: System.out.println("Enter new Salary (Float) of Employee: " );
71+
float salary=sc.nextFloat();
72+
e.modifyData(salary);
73+
break;
74+
case 4: System.out.println("Enter new status (Boolean) and Salary (Float) of Employee: " );
75+
boolean status=sc.nextBoolean();
76+
salary=sc.nextFloat();
77+
e.modifyData(status, salary);
78+
break;
79+
default: System.out.println("This feature is not yet available" );
80+
}
81+
}
82+
83+
84+
}
85+
86+
/*** Main Class***/
87+
88+
class TestEmployeeModify
89+
{
90+
public static void main(String args[])
91+
{
92+
93+
Employee e = new Employee();
94+
95+
e.setData();
96+
System.out.println("Details of :"+e.name);
97+
e.getData();
98+
e.modify(e);
99+
e.getData();
100+
101+
}
102+
}

0 commit comments

Comments
 (0)