Commit 8fa435ee by Patryk Czarnik

emps.funkcyjnie - początek

parent 23470d07
package emps.streamy;
import java.time.LocalDate;
import java.util.Objects;
public class Employee {
private int employeeId;
private String firstName;
private String lastName;
private String jobTitle;
private int salary;
private LocalDate hireDate;
private String departmentName;
private String address;
private String postalCode;
private String city;
private String country;
public Employee(int employeeId, String firstName, String lastName, String jobTitle, int salary, LocalDate hireDate, String departmentName, String address, String postalCode, String city, String country) {
this.employeeId = employeeId;
this.firstName = firstName;
this.lastName = lastName;
this.jobTitle = jobTitle;
this.salary = salary;
this.hireDate = hireDate;
this.departmentName = departmentName;
this.address = address;
this.postalCode = postalCode;
this.city = city;
this.country = country;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public LocalDate getHireDate() {
return hireDate;
}
public void setHireDate(LocalDate hireDate) {
this.hireDate = hireDate;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return employeeId == employee.employeeId && salary == employee.salary && Objects.equals(firstName, employee.firstName) && Objects.equals(lastName, employee.lastName) && Objects.equals(jobTitle, employee.jobTitle) && Objects.equals(hireDate, employee.hireDate) && Objects.equals(departmentName, employee.departmentName) && Objects.equals(address, employee.address) && Objects.equals(postalCode, employee.postalCode) && Objects.equals(city, employee.city) && Objects.equals(country, employee.country);
}
@Override
public int hashCode() {
return Objects.hash(employeeId, firstName, lastName, jobTitle, salary, hireDate, departmentName, address, postalCode, city, country);
}
@Override
public String toString() {
return "Employee{" +
"employeeId=" + employeeId +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", jobTitle='" + jobTitle + '\'' +
", salary=" + salary +
", hireDate=" + hireDate +
", departmentName='" + departmentName + '\'' +
", address='" + address + '\'' +
", postalCode='" + postalCode + '\'' +
", city='" + city + '\'' +
", country='" + country + '\'' +
'}';
}
}
package emps.streamy;
import java.io.File;
import java.io.FileNotFoundException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ObslugaCSV {
// to jest klasa narzędziowa - tu są tylko metody statyczne
// mogą zakazać tworzenia obiektów tej klasy oznaczając konstruktor jako prywatny
private ObslugaCSV() { }
public static List<Employee> readCSV(File file) throws FileNotFoundException {
List<Employee> emps = new ArrayList<>();
try(Scanner scanner = new Scanner(file)) {
scanner.nextLine();
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split(";", -1);
Employee emp = new Employee(Integer.parseInt(fields[0]), fields[1], fields[2], fields[3], Integer.parseInt(fields[4]),
LocalDate.parse(fields[5]), fields[6], fields[7], fields[8], fields[9], fields[10]);
emps.add(emp);
}
}
return emps;
}
public static List<Employee> readCSV(String path) throws FileNotFoundException {
return readCSV(new File(path));
}
public static List<Employee> readCSV() throws FileNotFoundException {
return readCSV("pliki/emps.csv");
}
}
package emps.streamy;
import java.io.FileNotFoundException;
public class P0_WypiszObiekty {
public static void main(String[] args) throws FileNotFoundException {
ObslugaCSV.readCSV().forEach(System.out::println);
}
}
package emps.streamy;
import java.io.FileNotFoundException;
import java.util.List;
public class P1_WypiszWybranePola {
public static void main(String[] args) throws FileNotFoundException {
List<Employee> emps = ObslugaCSV.readCSV();
System.out.println("Wczytano " + emps.size() + " rekordów.");
// emps.forEach(emp -> System.out.println(emp.getFirstName()
// + " " + emp.getLastName() + " zarabia " + emp.getSalary()));
// Też OK:
// emps.stream()
// .forEach(emp -> System.out.println(emp.getFirstName()
// + " " + emp.getLastName() + " zarabia " + emp.getSalary()));
emps.stream()
.map(emp -> emp.getFirstName() + " " + emp.getLastName() + " zarabia " + emp.getSalary())
.forEach(System.out::println);
}
}
package emps.streamy;
import java.io.FileNotFoundException;
import java.util.List;
public class P2_WypiszBogatych {
public static final int GRANICA_ZAROBKOW = 10000;
public static void main(String[] args) throws FileNotFoundException {
List<Employee> emps = ObslugaCSV.readCSV();
System.out.println("Wczytano " + emps.size() + " rekordów.");
System.out.println("Pracownicy zarabiający co najmniej "+GRANICA_ZAROBKOW+":");
emps.stream()
.filter(emp -> emp.getSalary() >= GRANICA_ZAROBKOW)
.forEach(emp -> System.out.println(emp.getFirstName()
+ " " + emp.getLastName() + " zarabia " + emp.getSalary()));
System.out.println("\n----------------\n");
emps.parallelStream()
.filter(emp -> emp.getSalary() >= GRANICA_ZAROBKOW)
.map(emp -> emp.getFirstName() + " " + emp.getLastName() + " zarabia " + emp.getSalary())
.forEachOrdered(System.out::println);
System.out.println("\nLista nadal zawiera " + emps.size() + " elementów.");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment