Programming 1 Unit 8 Written Assignment

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

App.

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

public class Employee {


1 private String name;
2 private int age;
3 private String department;
4 private double salary;
5
6 // Constructor
7 public Employee(String name, int age, String department, double salary)
8 {
9 this.name = name;
10 this.age = age;
11 this.department = department;
12 this.salary = salary;
13 }
14
15 // Getters
16 public String getName() {
17 return name;
18 }
19
20 public int getAge() {
21 return age;
22 }
23
24 public String getDepartment() {
25 return department;
26 }
27
28 public double getSalary() {
29 return salary;
30 }
31 }

EmployeeDatabase.java

import java.util.ArrayList;
1 import java.util.List;
import java.nio.file.*;
2 import java.io.*;

3 public class EmployeeDatabase {


public static List<Employee> readDataset(String filePath) {
4 List<Employee> employees = new ArrayList<>();
Path path = Paths.get(filePath);
5
try (BufferedReader br = Files.newBufferedReader(path)) {
6 String line;
while ((line = br.readLine()) != null) {
7 String[] data = line.split(",");
8

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.

Purpose of the Program:

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).

Characteristics and Usage of the Function Interface:


1. Single Abstract Method: The Function interface is a functional interface,
meaning it has a single abstract method. In the case of Function, this
method is apply(Object), which takes one input and returns an output.
2. Type Parameters: It is generic, with two type parameters: Function<T, R>.
Here, T is the type of the input to the function, and R is the type of the result.
3. Lambda Expressions: One of the main advantages of the Function interface
is its compatibility with lambda expressions. This makes the code more
concise and readable. For instance, (Employee employee) ->
employee.getName() + " - " + employee.getDepartment() is a lambda
expression used in the program that concatenates an employee’s name and
department.
4. Chaining and Composition: Function interfaces can be chained or
composed together to create more complex operations. Methods
like andThen and compose are used for this purpose.
5. Streams API Integration: In the context of this program,
the Function interface is extensively used with the Streams API, which allows
for operations on collections to be performed in a functional style. This
includes transforming, filtering, and aggregating data efficiently.
6. Versatility: The Function interface is versatile and can be used in a wide
range of scenarios, from simple transformations (like converting objects to
strings) to more complex data processing tasks.

In Practice:

In the program, the Function interface is used to define how to


transform Employee objects into strings (concatenating name and department). It's
also used in stream operations to filter and process collections of these objects
based on user-defined criteria, such as age or department.

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy