




Besser lernen dank der zahlreichen Ressourcen auf Docsity
Heimse Punkte ein, indem du anderen Studierenden hilfst oder erwirb Punkte mit einem Premium-Abo
Diese Seite wird in der Vorschau nicht angezeigt
Lass dir nichts Wichtiges entgehen!





CS 1102 Prof.Brenda Delee
import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; class Employee { private String name; private int age; private String department; private double salary; public Employee(String name, int age, String department, double salary) { this.name = name; this.age = age; this.department = department; this.salary = salary; } public String getName() { return name; } public int getAge() { return age; } public String getDepartment() { return department; } public double getSalary() { return salary; } @Override public String toString() { return String.format("%s (%s) - Age: %d, Salary: %.2f", name, department, age, salary); }
switch (choice) { case 1 -> addEmployee(); case 2 -> viewAllEmployees(); case 3 -> showNameAndDepartment(); case 4 -> filterByAge(); case 5 -> calculateAverageSalary(); case 6 -> showSummary(); default -> System.out.println("Invalid choice. Please try again."); } } while (choice != 6); } // 1 ⃣ Add employee private static void addEmployee() { System.out.print("Enter employee name: "); String name = scanner.nextLine(); System.out.print("Enter age: "); int age = scanner.nextInt(); scanner.nextLine(); System.out.print("Enter department: "); String dept = scanner.nextLine(); System.out.print("Enter salary: "); double salary = scanner.nextDouble(); scanner.nextLine();
employees.add(new Employee(name, age, dept, salary)); System.out.println("⃣ Employee added successfully!"); } // 2 ⃣ View all employees private static void viewAllEmployees() { if (employees.isEmpty()) { System.out.println("⚠ No employees available."); return; } System.out.println("\n=== Employee List ==="); employees.forEach(System.out::println); } // 3 ⃣ Function Interface + Stream mapping private static void showNameAndDepartment() { if (employees.isEmpty()) { System.out.println("⚠ No employees available."); return; } Function<Employee, String> nameDeptFunction = emp -> emp.getName() + " - " + emp.getDepartment(); List
// 5 ⃣ Stream average private static void calculateAverageSalary() { if (employees.isEmpty()) { System.out.println("⚠ No employees available."); return; } double avgSalary = employees.stream() .mapToDouble(Employee::getSalary) .average() .orElse(0.0); System.out.printf("Average Salary of All Employees: %.2f%n", avgSalary); } // 6 ⃣ Summary private static void showSummary() { System.out.println("\n=== Summary ==="); System.out.println("The Function interface in Java is a functional interface that takes an input and returns an output."); System.out.println("In this program, we used it to map Employee objects to strings showing name and department."); System.out.println("Streams are used to process the collection efficiently — performing transformations, filtering, and aggregation.");
System.out.println("This approach simulates real-world database data manipulation using functional programming principles."); System.out.println("Program exited. Goodbye!"); } }