Commit df8b57b0 by Patryk Czarnik

emps.obiektowo

parent 9fa77233
package emps.obiektowo;
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.obiektowo;
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(String path) throws FileNotFoundException {
List<Employee> emps = new ArrayList<>();
try(Scanner scanner = new Scanner(new File(path))) {
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;
}
}
package emps.obiektowo;
import java.io.FileNotFoundException;
import java.util.List;
public class P0_WypiszObiekty {
public static void main(String[] args) throws FileNotFoundException {
List<Employee> emps = ObslugaCSV.readCSV("pliki/emps.csv");
System.out.println("Odczytano " + emps.size() + " rekordów.");
for (Employee emp : emps) {
System.out.println(emp);
}
}
}
/*
1 - wypisz wybrane pola, np "Pracownik Steven King zarabia 24000"
2 - wypisz pracowników, którzy zarabiają >= 10 tys
3 - średnia pensja wszystkich
4 - średnia pensja na wybranym stanowisku
5 - wypisz dane pracowników, który zarabia najmniej, i który zarabia najwięcej
6 - sortowanie - wypisz pracowników w kolejności rosnących pensji
7 - wypisz nazwy jobów bez powtórzeń
8 - (grupowanie) - dla każdego stanowiska (jobTitle) wypisz ilu pracownikóœ pracuje na tym stanowisku i jaka jest ich średnia pensja
*/
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