|
| 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 | +} |
0 commit comments