-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dept and 2nd Highest Salary (IPA-35)
- Loading branch information
Showing
2 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Create a class Employee with the below attributes: | ||
EmpId - int | ||
EmpName - String | ||
Dept - String | ||
Rating - int | ||
Salary - int | ||
|
||
Write getters, setters and parameterized constructor in the above mentioned attribute sequence as required. | ||
|
||
Create class Solution with the main method. | ||
|
||
Implement two static methods - findDept and findSecondHighestSalary in Solution class. | ||
|
||
findDept method: | ||
----------------------------------- | ||
This method will take two input parameters - array of Employee objects and int parameter. The method | ||
will return the dept array attribute from employee objects whose salary is matched with int parameter and rating greater than | ||
equal to 3. If no data found of beach objects, then the method should return null. | ||
|
||
findSecondHighestSalary method: | ||
----------------------------------- | ||
This method will take two input parameters - array of Employee objects and string parameter. The method | ||
will return the second highest salary from employee objects whose dept is matched with string parameter. | ||
If no data found of beach objects, then the method should return 0. | ||
|
||
Note: No two employees have same rating. All the searches should be case insensitive. | ||
|
||
These above mentioned static methods should be called from the main method. | ||
For findDept method - The main method should print the dept from the Employee array. If return null, then you should print | ||
"No Department found". | ||
For findSecondHighestSalary method - The main method should print the 2nd highest salary from the Employee array with | ||
the following condition. If return 0, then you should print "No data found". | ||
|
||
|
||
Input -> | ||
101 | ||
Arijit | ||
Computer | ||
4 | ||
35000 | ||
102 | ||
Rakesh | ||
Electronics | ||
2 | ||
18000 | ||
103 | ||
Mahima | ||
Mechanical | ||
3 | ||
35000 | ||
104 | ||
Saniya | ||
Mechanical | ||
5 | ||
68000 | ||
105 | ||
Rajesh | ||
Computer | ||
1 | ||
30000 | ||
35000 | ||
Mechanical | ||
|
||
Output -> | ||
Computer, Mechanical | ||
35000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package IPA52; | ||
import java.util.*; | ||
public class Solution { | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
Employee[] emp = new Employee[5]; | ||
for (int i = 0; i < emp.length; i++) { | ||
int a = sc.nextInt();sc.nextLine(); | ||
String b = sc.nextLine(); | ||
String c = sc.nextLine(); | ||
int d = sc.nextInt();sc.nextLine(); | ||
int e = sc.nextInt();sc.nextLine(); | ||
|
||
emp[i] = new Employee(a,b,c,d,e); | ||
} | ||
int sal = sc.nextInt();sc.nextLine(); | ||
String dept = sc.nextLine(); | ||
String[] ans1 = findDept(emp,sal); | ||
if(ans1!=null) | ||
{ | ||
String dep = Arrays.toString(ans1); | ||
String de = dep.substring(1,dep.length()-1); | ||
System.out.println(de); | ||
} | ||
else | ||
{ | ||
System.out.println("No Department found"); | ||
} | ||
int ans2 = findSecondHighestSalary(emp,dept); | ||
if(ans2!=0) | ||
{ | ||
System.out.println(ans2); | ||
} | ||
else | ||
{ | ||
System.out.println("No data found"); | ||
} | ||
} | ||
public static String[] findDept(Employee[] e, int s) | ||
{ | ||
String[] d = new String[0]; | ||
for (int i = 0; i < e.length; i++) { | ||
if(e[i].getSalary()==s && e[i].getRating()>=3) | ||
{ | ||
d = Arrays.copyOf(d,d.length+1); | ||
d[d.length-1] = e[i].getDept(); | ||
} | ||
} | ||
if(d.length>0) | ||
{ | ||
return d; | ||
} | ||
return null; | ||
} | ||
public static int findSecondHighestSalary(Employee[] e, String d) | ||
{ | ||
int[] sal = new int[0]; | ||
for (int i = 0; i < e.length; i++) { | ||
if(e[i].getDept().equalsIgnoreCase(d)) | ||
{ | ||
sal = Arrays.copyOf(sal, sal.length+1); | ||
sal[sal.length-1] = e[i].getSalary(); | ||
Arrays.sort(sal); | ||
} | ||
} | ||
if(sal.length>0) | ||
{ | ||
for (int i = 0; i < sal.length; i++) { | ||
if(sal[i]<sal[i+1]) | ||
{ | ||
return sal[i]; | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
} | ||
class Employee | ||
{ | ||
int id, rating, salary; | ||
String name, dept; | ||
public Employee(int id, String name, String dept, int rating, int salary) { | ||
this.id = id; | ||
this.rating = rating; | ||
this.salary = salary; | ||
this.name = name; | ||
this.dept = dept; | ||
} | ||
public int getId() { | ||
return id; | ||
} | ||
public void setId(int id) { | ||
this.id = id; | ||
} | ||
public int getRating() { | ||
return rating; | ||
} | ||
public void setRating(int rating) { | ||
this.rating = rating; | ||
} | ||
public int getSalary() { | ||
return salary; | ||
} | ||
public void setSalary(int salary) { | ||
this.salary = salary; | ||
} | ||
public String getName() { | ||
return name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public String getDept() { | ||
return dept; | ||
} | ||
public void setDept(String dept) { | ||
this.dept = dept; | ||
} | ||
|
||
} |