Commit d10ccae5 by Patryk Czarnik

Klasa Employee i ObslugaCSV

parent 03c1e49f
package emps.v2_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 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 + "]";
}
@Override
public int hashCode() {
return Objects.hash(address, city, country, departmentName, employeeId, firstName, hireDate, jobTitle, lastName, postalCode, salary);
}
@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(address, other.address) && 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;
}
}
package emps.v2_obiektowo;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class ObslugaCSV {
public static List<Employee> wczytajDane(String sciezka) {
List<Employee> lista = new ArrayList<>();
try(BufferedReader in = new BufferedReader(new FileReader(sciezka))) {
String linia = in.readLine();
// pierwszą linię ignorujemy - tam są nazwy kolumn
while((linia = in.readLine()) != null) {
String[] t = linia.split(";", -1);
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]);
lista.add(emp);
}
} catch(IOException e) {
// W przypadku błędu wypiszemy info, ale nie przerywamy programu
e.printStackTrace();
}
return lista;
}
}
package emps.v2_obiektowo;
import java.util.List;
public class P0_TestOdczytu {
public static void main(String[] args) {
List<Employee> emps = ObslugaCSV.wczytajDane("pliki/emps.csv");
System.out.println("Odczytano " + emps.size() + " rekordów:");
for(Employee emp : emps) {
System.out.println(emp);
}
}
}
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