Programming 1 Unit 8 Written Assignment
Programming 1 Unit 8 Written Assignment
Programming 1 Unit 8 Written Assignment
java
import java.util.function.Function;
1 import java.util.stream.Collectors;
import java.util.List;
2 import java.util.OptionalDouble;
import java.util.Scanner;
3
public class App {
4 public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
5 List<Employee> employees =
EmployeeDatabase.readDataset("employees.csv");
6 int choice;
7 do {
// Display the menu
8 System.out.println("\n-- Welcome to Employee Database --");
System.out.println("1. Show all employee data.");
9 System.out.println("2. Show all employees with age filter.");
1 System.out.println("3. Show all employees with department
0 filter.");
1 System.out.println("4. Exit");
1 System.out.print("Enter your choice: ");
1 choice = scanner.nextInt();
2
1 switch (choice) {
3 case 1:
1 // Show all employee data
4 printEmployeeInfo(employees, employee -> true); // no
1 filter
5 break;
1 case 2:
6 // Show employees with age filter
1 System.out.print("Enter age threshold for employees: ");
7 int ageThreshold = scanner.nextInt();
1 printEmployeeInfo(employees, employee -> employee.getAge()
8 > ageThreshold);
1 break;
9 case 3:
2 // Show employees with department filter
0 System.out.print("Enter department: ");
2 scanner.nextLine(); // Consume newline
1 String department = scanner.nextLine();
2 printEmployeeInfo(employees, employee ->
2 employee.getDepartment().equalsIgnoreCase(department));
2 break;
3 case 4:
2 // Exit
4 System.out.println("Exiting...");
2 break;
5 default:
2 System.out.println("Invalid choice. Please enter a number
6 between 1 and 4.");
2 }
7 } while (choice != 4);
2 }
8
2 private static void printEmployeeInfo(List<Employee> employees,
9 Function<Employee, Boolean> filter) {
3 employees.stream()
0 .filter(filter::apply)
3 .map(employee -> employee.getName() + " - " +
1 employee.getDepartment())
3 .forEach(System.out::println);
2 }
3 }
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
4
4
4
5
4
6
4
7
4
8
4
9
5
0
5
1
5
2
5
3
5
4
5
5
5
6
5
7
Employee.java
EmployeeDatabase.java
import java.util.ArrayList;
1 import java.util.List;
import java.nio.file.*;
2 import java.io.*;
9
1
0
1
1
1
2
1
3 Employee employee = new Employee(data[0],
1 Integer.parseInt(data[1].trim()), data[2], Double.parseDouble(data[3].trim()));
4 employees.add(employee);
1 }
5 } catch (IOException e) {
1 e.printStackTrace();
6 }
1
7 return employees;
1 }
8 }
1
9
2
0
2
1
2
2
2
3
2
4
The program you have is a Java-based application designed to manage and
manipulate an employee database. The key characteristic of this program is its use
of the Function interface from Java's java.util.function package. This interface is a
part of Java's functional programming features introduced in Java 8, which facilitate
a more declarative approach to programming, particularly with data manipulation in
collections.
The program serves to read, filter, and process data from an employee database. It
allows operations like listing all employees, filtering employees by age or
department, and calculating average salaries. This is achieved through a
combination of object-oriented programming (to define the data structure) and
functional programming (to process the data).
In Practice:
The use of the Function interface in this way showcases the power of functional
programming in Java, allowing for more expressive, readable, and maintainable
code. It represents a shift from the imperative programming model, focusing more
on what the program should accomplish rather than detailing how it should be
done.