Skip to content

Commit 100beec

Browse files
committed
feat: add EmployeeFileCreation program with stream-based analytics
Implemented EmployeeFileCreation.java to generate 'employees_output.txt' containing employee records and analytical results. Demonstrates Java Streams for grouping, filtering, partitioning, and aggregating employee data such as average salary, gender ratio, department insights, and experience metrics. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent c3ab80c commit 100beec

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import java.io.File;
2+
import java.io.FileWriter;
3+
import java.io.IOException;
4+
import java.util.*;
5+
import java.util.stream.Collectors;
6+
7+
record Employee(int id,
8+
String name,
9+
int age,
10+
String gender,
11+
String department,
12+
int yearOfJoining,
13+
double salary)
14+
{}
15+
16+
public class EmployeeFileCreation {
17+
public static void main(String[] args) {
18+
List<Employee> emps = new ArrayList<>();
19+
20+
emps.add(new Employee(1, "Jhansi", 32, "Female", "HR", 2011, 25000.0));
21+
emps.add(new Employee(2, "Smith", 25, "Male", "Sales", 2015, 13500.0));
22+
emps.add(new Employee(3, "David", 29, "Male", "Infrastructure", 2012, 18000.0));
23+
emps.add(new Employee(4, "Orlen", 28, "Male", "Development", 2014, 32500.0));
24+
emps.add(new Employee(5, "Charles", 27, "Male", "HR", 2013, 22700.0));
25+
emps.add(new Employee(6, "Cathy", 43, "Male", "Security", 2016, 10500.0));
26+
emps.add(new Employee(7, "Ramesh", 35, "Male", "Finance", 2010, 27000.0));
27+
emps.add(new Employee(8, "Suresh", 31, "Male", "Development", 2015, 34500.0));
28+
emps.add(new Employee(9, "Gita", 24, "Female", "Sales", 2016, 11500.0));
29+
emps.add(new Employee(10, "Mahesh", 38, "Male", "Security", 2015, 11000.5));
30+
emps.add(new Employee(11, "Gouri", 27, "Female", "Infrastructure", 2014, 15700.0));
31+
emps.add(new Employee(12, "Nithin", 25, "Male", "Development", 2016, 28200.0));
32+
emps.add(new Employee(13, "Swathi", 27, "Female", "Finance", 2013, 21300.0));
33+
emps.add(new Employee(14, "Buttler", 24, "Male", "Sales", 2017, 10700.5));
34+
emps.add(new Employee(15, "Ashok", 23, "Male", "Infrastructure", 2018, 12700.0));
35+
emps.add(new Employee(16, "Sanvi", 26, "Female", "Development", 2015, 28900.0));
36+
37+
try {
38+
// Create file
39+
File file = new File("employees_output.txt");
40+
FileWriter writer = new FileWriter(file);
41+
42+
// Write all employee data to file
43+
writer.write("===== EMPLOYEE DATA =====\n\n");
44+
for (Employee emp : emps) {
45+
writer.write(emp.toString() + "\n");
46+
}
47+
48+
writer.write("\n===== QUERY RESULTS =====\n\n");
49+
50+
// 1. Male and Female count
51+
writer.write("1. Male and Female Employee Count:\n");
52+
Map<String, Long> map1 = emps.stream()
53+
.collect(Collectors.groupingBy(Employee::gender, Collectors.counting()));
54+
writer.write(map1.toString() + "\n\n");
55+
56+
// 2. All departments
57+
writer.write("2. All Departments:\n");
58+
List<String> departments = emps.stream()
59+
.map(Employee::department)
60+
.distinct()
61+
.toList();
62+
departments.forEach(dept -> {
63+
try {
64+
writer.write(dept + "\n");
65+
} catch (IOException e) {
66+
e.printStackTrace();
67+
}
68+
});
69+
writer.write("\n");
70+
71+
// 3. Average age by gender
72+
writer.write("3. Average Age by Gender:\n");
73+
Map<String, Double> map = emps.stream()
74+
.collect(Collectors.groupingBy(Employee::gender, Collectors.averagingInt(Employee::age)));
75+
writer.write(map.toString() + "\n\n");
76+
77+
// 4. Highest paid employee
78+
writer.write("4. Highest Paid Employee:\n");
79+
Optional<Employee> optional = emps.stream()
80+
.collect(Collectors.maxBy(Comparator.comparingDouble(Employee::salary)));
81+
if(optional.isPresent()) {
82+
writer.write(optional.get().toString() + "\n\n");
83+
}
84+
85+
// 5. Employees joined after 2015
86+
writer.write("5. Employees Joined After 2015:\n");
87+
List<String> names = emps.stream()
88+
.filter(e -> e.yearOfJoining() > 2015)
89+
.map(Employee::name)
90+
.toList();
91+
names.forEach(name -> {
92+
try {
93+
writer.write(name + "\n");
94+
} catch (IOException e) {
95+
e.printStackTrace();
96+
}
97+
});
98+
writer.write("\n");
99+
100+
// 6. Employee count per department
101+
writer.write("6. Employee Count Per Department:\n");
102+
Map<String, Long> map2 = emps.stream()
103+
.collect(Collectors.groupingBy(Employee::department, Collectors.counting()));
104+
writer.write(map2.toString() + "\n\n");
105+
106+
// 7. Average salary per department
107+
writer.write("7. Average Salary Per Department:\n");
108+
Map<String, Double> map3 = emps.stream()
109+
.collect(Collectors.groupingBy(Employee::department, Collectors.averagingDouble(Employee::salary)));
110+
writer.write(map3.toString() + "\n\n");
111+
112+
// 8. Youngest male in Development
113+
writer.write("8. Youngest Male in Development Department:\n");
114+
Optional<Employee> optional2 = emps.stream()
115+
.filter(e -> e.gender().equals("Male") && e.department().equals("Development"))
116+
.min(Comparator.comparing(Employee::age));
117+
if(optional2.isPresent()) {
118+
writer.write(optional2.get().toString() + "\n\n");
119+
}
120+
121+
// 9. Most experienced employee
122+
writer.write("9. Most Experienced Employee:\n");
123+
Optional<Employee> optional1 = emps.stream()
124+
.collect(Collectors.minBy(Comparator.comparing(Employee::yearOfJoining)));
125+
if(optional1.isPresent()) {
126+
writer.write(optional1.get().toString() + "\n\n");
127+
}
128+
129+
// 10. Male and Female in Sales
130+
writer.write("10. Male and Female in Sales Team:\n");
131+
Map<String, Long> map4 = emps.stream()
132+
.filter(e -> e.department().equals("Sales"))
133+
.collect(Collectors.groupingBy(Employee::gender, Collectors.counting()));
134+
writer.write(map4.toString() + "\n\n");
135+
136+
// 11. Average and Total salary
137+
writer.write("11. Average and Total Salary of Organization:\n");
138+
String result4 = emps.stream()
139+
.collect(Collectors.teeing(
140+
Collectors.averagingDouble(Employee::salary),
141+
Collectors.summingDouble(Employee::salary),
142+
(avg, total) -> "Average: " + avg + ", Total: " + total)
143+
);
144+
writer.write(result4 + "\n\n");
145+
146+
// 12. Partition by age
147+
writer.write("12. Employees <= 25 years:\n");
148+
Map<Boolean, List<Employee>> collect2 = emps.stream()
149+
.collect(Collectors.partitioningBy(emp -> emp.age() <= 25));
150+
List<Employee> list1 = collect2.get(true);
151+
List<Employee> list2 = collect2.get(false);
152+
writer.write(list1.toString() + "\n\n");
153+
writer.write("12b. Employees > 25 years:\n");
154+
writer.write(list2.toString() + "\n\n");
155+
156+
// 13. Oldest employee
157+
writer.write("13. Oldest Employee:\n");
158+
Optional<Employee> max = emps.stream()
159+
.max(Comparator.comparing(Employee::age));
160+
max.ifPresentOrElse(e -> {
161+
try {
162+
writer.write(e.toString() + "\n");
163+
} catch (IOException ex) {
164+
ex.printStackTrace();
165+
}
166+
}, () -> {
167+
try {
168+
writer.write("No employee available\n");
169+
} catch (IOException ex) {
170+
ex.printStackTrace();
171+
}
172+
});
173+
174+
writer.close();
175+
System.out.println("File 'employees_output.txt' created successfully!");
176+
177+
} catch (IOException e) {
178+
System.out.println("Error creating file: " + e.getMessage());
179+
e.printStackTrace();
180+
}
181+
}
182+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
===== EMPLOYEE DATA =====
2+
3+
Employee[id=1, name=Jhansi, age=32, gender=Female, department=HR, yearOfJoining=2011, salary=25000.0]
4+
Employee[id=2, name=Smith, age=25, gender=Male, department=Sales, yearOfJoining=2015, salary=13500.0]
5+
Employee[id=3, name=David, age=29, gender=Male, department=Infrastructure, yearOfJoining=2012, salary=18000.0]
6+
Employee[id=4, name=Orlen, age=28, gender=Male, department=Development, yearOfJoining=2014, salary=32500.0]
7+
Employee[id=5, name=Charles, age=27, gender=Male, department=HR, yearOfJoining=2013, salary=22700.0]
8+
Employee[id=6, name=Cathy, age=43, gender=Male, department=Security, yearOfJoining=2016, salary=10500.0]
9+
Employee[id=7, name=Ramesh, age=35, gender=Male, department=Finance, yearOfJoining=2010, salary=27000.0]
10+
Employee[id=8, name=Suresh, age=31, gender=Male, department=Development, yearOfJoining=2015, salary=34500.0]
11+
Employee[id=9, name=Gita, age=24, gender=Female, department=Sales, yearOfJoining=2016, salary=11500.0]
12+
Employee[id=10, name=Mahesh, age=38, gender=Male, department=Security, yearOfJoining=2015, salary=11000.5]
13+
Employee[id=11, name=Gouri, age=27, gender=Female, department=Infrastructure, yearOfJoining=2014, salary=15700.0]
14+
Employee[id=12, name=Nithin, age=25, gender=Male, department=Development, yearOfJoining=2016, salary=28200.0]
15+
Employee[id=13, name=Swathi, age=27, gender=Female, department=Finance, yearOfJoining=2013, salary=21300.0]
16+
Employee[id=14, name=Buttler, age=24, gender=Male, department=Sales, yearOfJoining=2017, salary=10700.5]
17+
Employee[id=15, name=Ashok, age=23, gender=Male, department=Infrastructure, yearOfJoining=2018, salary=12700.0]
18+
Employee[id=16, name=Sanvi, age=26, gender=Female, department=Development, yearOfJoining=2015, salary=28900.0]
19+
20+
===== QUERY RESULTS =====
21+
22+
1. Male and Female Employee Count:
23+
{Male=11, Female=5}
24+
25+
2. All Departments:
26+
HR
27+
Sales
28+
Infrastructure
29+
Development
30+
Security
31+
Finance
32+
33+
3. Average Age by Gender:
34+
{Male=29.818181818181817, Female=27.2}
35+
36+
4. Highest Paid Employee:
37+
Employee[id=8, name=Suresh, age=31, gender=Male, department=Development, yearOfJoining=2015, salary=34500.0]
38+
39+
5. Employees Joined After 2015:
40+
Cathy
41+
Gita
42+
Nithin
43+
Buttler
44+
Ashok
45+
46+
6. Employee Count Per Department:
47+
{Sales=3, Finance=2, Development=4, Infrastructure=3, HR=2, Security=2}
48+
49+
7. Average Salary Per Department:
50+
{Sales=11900.166666666666, Finance=24150.0, Development=31025.0, Infrastructure=15466.666666666666, HR=23850.0, Security=10750.25}
51+
52+
8. Youngest Male in Development Department:
53+
Employee[id=12, name=Nithin, age=25, gender=Male, department=Development, yearOfJoining=2016, salary=28200.0]
54+
55+
9. Most Experienced Employee:
56+
Employee[id=7, name=Ramesh, age=35, gender=Male, department=Finance, yearOfJoining=2010, salary=27000.0]
57+
58+
10. Male and Female in Sales Team:
59+
{Female=1, Male=2}
60+
61+
11. Average and Total Salary of Organization:
62+
Average: 20231.3125, Total: 323701.0
63+
64+
12. Employees <= 25 years:
65+
[Employee[id=2, name=Smith, age=25, gender=Male, department=Sales, yearOfJoining=2015, salary=13500.0], Employee[id=9, name=Gita, age=24, gender=Female, department=Sales, yearOfJoining=2016, salary=11500.0], Employee[id=12, name=Nithin, age=25, gender=Male, department=Development, yearOfJoining=2016, salary=28200.0], Employee[id=14, name=Buttler, age=24, gender=Male, department=Sales, yearOfJoining=2017, salary=10700.5], Employee[id=15, name=Ashok, age=23, gender=Male, department=Infrastructure, yearOfJoining=2018, salary=12700.0]]
66+
67+
12b. Employees > 25 years:
68+
[Employee[id=1, name=Jhansi, age=32, gender=Female, department=HR, yearOfJoining=2011, salary=25000.0], Employee[id=3, name=David, age=29, gender=Male, department=Infrastructure, yearOfJoining=2012, salary=18000.0], Employee[id=4, name=Orlen, age=28, gender=Male, department=Development, yearOfJoining=2014, salary=32500.0], Employee[id=5, name=Charles, age=27, gender=Male, department=HR, yearOfJoining=2013, salary=22700.0], Employee[id=6, name=Cathy, age=43, gender=Male, department=Security, yearOfJoining=2016, salary=10500.0], Employee[id=7, name=Ramesh, age=35, gender=Male, department=Finance, yearOfJoining=2010, salary=27000.0], Employee[id=8, name=Suresh, age=31, gender=Male, department=Development, yearOfJoining=2015, salary=34500.0], Employee[id=10, name=Mahesh, age=38, gender=Male, department=Security, yearOfJoining=2015, salary=11000.5], Employee[id=11, name=Gouri, age=27, gender=Female, department=Infrastructure, yearOfJoining=2014, salary=15700.0], Employee[id=13, name=Swathi, age=27, gender=Female, department=Finance, yearOfJoining=2013, salary=21300.0], Employee[id=16, name=Sanvi, age=26, gender=Female, department=Development, yearOfJoining=2015, salary=28900.0]]
69+
70+
13. Oldest Employee:
71+
Employee[id=6, name=Cathy, age=43, gender=Male, department=Security, yearOfJoining=2016, salary=10500.0]

0 commit comments

Comments
 (0)