Commit 50858cc8 by Patryk Czarnik

emps.strumieniowo - pierwsze kroki

parent 8b7188e6
package emps.strumieniowo;
import java.time.LocalDate;
import java.util.Objects;
// To jest przykład "normalnej klasy" przygotowanej po to, aby tworzyć obiekty tej klasy.
// Na takie klasy mówi się "klasa modelu".
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 streetAddress;
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 streetAddress, 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.streetAddress = streetAddress;
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 getStreetAddress() {
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
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 String toString() {
return "Employee [employeeId=" + employeeId + ", firstName=" + firstName + ", lastName=" + lastName
+ ", jobTitle=" + jobTitle + ", salary=" + salary + ", hireDate=" + hireDate + ", departmentName="
+ departmentName + ", streetAddress=" + streetAddress + ", postalCode=" + postalCode + ", city=" + city
+ ", country=" + country + "]";
}
@Override
public int hashCode() {
return Objects.hash(city, country, departmentName, employeeId, firstName, hireDate, jobTitle, lastName,
postalCode, salary, streetAddress);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
return Objects.equals(city, other.city) && Objects.equals(country, other.country)
&& Objects.equals(departmentName, other.departmentName) && employeeId == other.employeeId
&& Objects.equals(firstName, other.firstName) && Objects.equals(hireDate, other.hireDate)
&& Objects.equals(jobTitle, other.jobTitle) && Objects.equals(lastName, other.lastName)
&& Objects.equals(postalCode, other.postalCode) && salary == other.salary
&& Objects.equals(streetAddress, other.streetAddress);
}
}
package emps.strumieniowo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ObslugaCSV {
private static final String NAGLOWEK = "employee_id;first_name;last_name;job_title;salary;hire_date;department_name;address;postal_code;city;country";
private static final String SEP = ";";
private ObslugaCSV() {
// nie wolno tworzyć obiektu
}
public static List<Employee> wczytaj(File plik) {
List<Employee> emps = new ArrayList<>();
try(Scanner scanner = new Scanner(plik)) {
scanner.nextLine();
while(scanner.hasNextLine()) {
String linia = scanner.nextLine();
String[] t = linia.split(SEP, 11);
Employee emp = new Employee(Integer.parseInt(t[0]), t[1], t[2], t[3],
Integer.parseInt(t[4]), LocalDate.parse(t[5]), t[6], t[7], t[8], t[9], t[10]);
emps.add(emp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return emps;
}
public static List<Employee> wczytaj(String sciezka) {
return wczytaj(new File(sciezka));
}
public static void zapisz(List<Employee> lista, File plik) {
try(PrintWriter out = new PrintWriter(plik)) {
out.println(NAGLOWEK);
for (Employee emp : lista) {
out.print(emp.getEmployeeId());
out.print(SEP);
out.print(emp.getFirstName());
out.print(SEP);
out.print(emp.getLastName());
out.print(SEP);
out.print(emp.getJobTitle());
out.print(SEP);
out.print(emp.getSalary());
out.print(SEP);
out.print(emp.getHireDate());
out.print(SEP);
out.print(emp.getDepartmentName());
out.print(SEP);
out.print(emp.getStreetAddress());
out.print(SEP);
out.print(emp.getPostalCode());
out.print(SEP);
out.print(emp.getCity());
out.print(SEP);
out.print(emp.getCountry());
out.println();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void zapisz(List<Employee> lista, String sciezka) {
zapisz(lista, new File(sciezka));
}
}
package emps.strumieniowo;
import java.util.List;
// W tej wersji operację forEach wywołujemy bezpośrednio na liście, bez jawnego użycia strumieni.
public class P0_ForEach {
public static void main(String[] args) {
List<Employee> emps = ObslugaCSV.wczytaj("emps.csv");
int zmienna = 0;
// Zamiast pisać pętlę for(Employee emp : emps) { ROBOTA; }
// można wywołać operację forEach i "ROBOTĘ" przekazać w formie wyrażenia lambda:
emps.forEach(emp -> {
System.out.print(" * ");
System.out.println(emp.getFirstName() + " " + emp.getLastName());
// w obrębie wyrażeń lambda nie wolno modyfikować zmiennych lokalnych zdef. przed lambdą
// zmienna++;
});
System.out.println("-----------------");
// Gdy do wykonania jest tylko jedna czynność, można bez nawiasów klamrowych (i bez średnika):
emps.forEach(emp -> System.out.print(emp.getCity() + ", "));
System.out.println();
System.out.println("-----------------");
System.out.println();
// Jeśli wykonujemy metodę dla całego obiektu, np. System.out.println(emp);
// to zamiast lambdy możemy użyć referencji do metody
emps.forEach(System.out::println);
}
}
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