Как распечатать список сотрудников, которые присоединились к определенному месяцу, из списка сотрудников?
Привет, я пытаюсь распечатать список сотрудников, которые присоединились к месяцу, предположим, "июнь"? Ниже мой код, класс Pojo: -
import java.time.LocalDate;
public class Employee {
private String name;
private String empID;
private Designation designation;
private LocalDate dateOfJoining;
private int monthlySalary;
public Employee(String name, String empID, Designation designation, LocalDate dateOfJoining, int monthlySalary) {
super();
this.name = name;
this.empID = empID;
this.designation = designation;
this.dateOfJoining = dateOfJoining;
this.monthlySalary = monthlySalary;
}
public Employee() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
public Designation getDesignation() {
return designation;
}
public void setDesignation(Designation designation) {
this.designation = designation;
}
public LocalDate getDOJ() {
return dateOfJoining;
}
public void setDOJ(LocalDate dOJ) {
dateOfJoining = dOJ;
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dateOfJoining == null) ? 0 : dateOfJoining.hashCode());
result = prime * result + ((designation == null) ? 0 : designation.hashCode());
result = prime * result + ((empID == null) ? 0 : empID.hashCode());
result = prime * result + monthlySalary;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@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;
if (dateOfJoining == null) {
if (other.dateOfJoining != null)
return false;
} else if (!dateOfJoining.equals(other.dateOfJoining))
return false;
if (designation == null) {
if (other.designation != null)
return false;
} else if (!designation.equals(other.designation))
return false;
if (empID == null) {
if (other.empID != null)
return false;
} else if (!empID.equals(other.empID))
return false;
if (monthlySalary != other.monthlySalary)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + name + ", empID=" + empID + ", designation=" + designation + ", DOJ=" + dateOfJoining
+ ", monthlySalary=" + monthlySalary + "]";
}
}
Я создал отдельный класс для даты объединения деталей.
import java.time.LocalDate;
public class JoiningDate {
LocalDate date1 = LocalDate.of(2019, 06, 15);
LocalDate date2 = LocalDate.of(2009, 06, 06);
LocalDate date3 = LocalDate.of(2007, 05, 10);
LocalDate date4 = LocalDate.of(2000, 05, 30);
LocalDate date5 = LocalDate.of(1998, 07, 31);
LocalDate date6 = LocalDate.of(1995, 12, 12);
}
Теперь я пытаюсь напечатать имя сотрудникакоторый присоединился в июне, используя Hashmap, принимая doj в качестве ключа и name в качестве значения. Но я не могу продолжить. Может кто-нибудь, пожалуйста, помогите? Ниже мой код: -
import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Employecomparable {
public static void main(String[] args) {
Employee emp = new Employee();
JoiningDate jd = new JoiningDate();
List<Employee> listofemployee = new ArrayList<>();
listofemployee.add(new Employee("Pink", "12345", Designation.ASE, jd.date1, 20000));
listofemployee.add(new Employee("Red", "24680", Designation.SE, jd.date2, 30000));
listofemployee.add(new Employee("Blue", "13570", Designation.SSE, jd.date3, 40000));
listofemployee.add(new Employee("Orange", "13690", Designation.TL, jd.date4, 60000));
listofemployee.add(new Employee("Green", "10909", Designation.AM, jd.date5, 800000));
listofemployee.add(new Employee("Yellow", "89076", Designation.M, jd.date6, 2000));
LocalDate today = LocalDate.now();
System.out.println(today);
Period time1 = Period.between(jd.date1, today);
Period time2 = Period.between(jd.date2, today);
Period time3 = Period.between(jd.date3, today);
Period time4 = Period.between(jd.date4, today);
Period time5 = Period.between(jd.date5, today);
Period time6 = Period.between(jd.date6, today);
Map<LocalDate,String> hashmap =new HashMap<>();
for(Employee employee: listofemployee) {
LocalDate key = employee.getDOJ();
String value = employee.getName();
if (hashmap.containsKey(key)) {
ArrayList<Employee> list = new ArrayList<Employee>();
list.add(employee);
hashmap.put(key, value);
}
}
System.out.println(hashmap);