* [BAEL-5438] Added Criteria Queries for Employee

* [BAEL-5558] Sorting By Date in Java

* BAEL-5558: Added and refactored tests name

* [BAEL-5558] Refactored the code.

* [BAEL-5558] Resolved PMD violation.

* [BAEL-5558] Indented with 4 spaces

* Refactored the Employee class

Co-authored-by: Mayank Agarwal <mayankaggarwal@zeta.tech>
This commit is contained in:
Mayank Aggarwal 2022-05-27 20:37:59 +05:30 committed by GitHub
parent 9c40bfe297
commit 26224e4686
3 changed files with 136 additions and 159 deletions

View File

@ -25,5 +25,4 @@
<properties> <properties>
<commons-lang.version>2.2</commons-lang.version> <commons-lang.version>2.2</commons-lang.version>
</properties> </properties>
</project> </project>

View File

@ -5,14 +5,10 @@ import java.util.Date;
public class Employee implements Comparable<Employee>{ public class Employee implements Comparable<Employee>{
private String name; private String name;
private int age;
private double salary;
private Date joiningDate; private Date joiningDate;
public Employee(String name, int age, double salary, Date joiningDate) { public Employee(String name, Date joiningDate) {
this.name = name; this.name = name;
this.age = age;
this.salary = salary;
this.joiningDate = joiningDate; this.joiningDate = joiningDate;
} }
@ -24,22 +20,6 @@ public class Employee implements Comparable<Employee>{
this.name = name; this.name = name;
} }
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public Date getJoiningDate() { public Date getJoiningDate() {
return joiningDate; return joiningDate;
} }
@ -57,10 +37,9 @@ public class Employee implements Comparable<Employee>{
@Override @Override
public String toString() { public String toString() {
return new StringBuffer().append("(") return new StringBuffer().append("(")
.append(getName()).append(",") .append(getName())
.append(getAge())
.append(",") .append(",")
.append(getSalary()).append(",").append(getJoiningDate()) .append(getJoiningDate())
.append(")") .append(")")
.toString(); .toString();
} }

View File

@ -22,36 +22,37 @@ public class EmployeeSortingByDateUnitTest {
public void initVariables() { public void initVariables() {
Collections.addAll(employees, Collections.addAll(employees,
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), new Employee("Frank", DateUtils.addDays(new Date(), -20)),
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), new Employee("Steve", DateUtils.addDays(new Date(), -10)),
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)), new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
new Employee("John", 23, 5000, new Date()) new Employee("John", new Date())
); );
Collections.addAll(employeesSortedByDateDesc, Collections.addAll(employeesSortedByDateDesc,
new Employee("John", 23, 5000, new Date()), new Employee("John", new Date()),
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), new Employee("Steve", DateUtils.addDays(new Date(), -10)),
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), new Employee("Frank", DateUtils.addDays(new Date(), -20)),
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)) new Employee("Pearl", DateUtils.addYears(new Date(), -1))
); );
Collections.addAll(employeesSortedByDateAsc, Collections.addAll(employeesSortedByDateAsc,
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)), new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)), new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)), new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)), new Employee("Frank", DateUtils.addDays(new Date(), -20)),
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)), new Employee("Steve", DateUtils.addDays(new Date(), -10)),
new Employee("John", 23, 5000, new Date()) new Employee("John", new Date())
); );
} }
@Test @Test
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() { public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
Collections.sort(employees); Collections.sort(employees);
System.out.println(employees);
assertEquals(employees, employeesSortedByDateAsc); assertEquals(employees, employeesSortedByDateAsc);
} }
@ -83,7 +84,7 @@ public class EmployeeSortingByDateUnitTest {
} }
@Test @Test
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() { public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
Collections.sort(employees, Collections.reverseOrder()); Collections.sort(employees, Collections.reverseOrder());
assertEquals(employees, employeesSortedByDateDesc); assertEquals(employees, employeesSortedByDateDesc);
@ -102,12 +103,10 @@ public class EmployeeSortingByDateUnitTest {
} }
@Test @Test
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() { public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {
Collections.sort(employees, new Comparator<Employee>() { Collections.sort(employees, new Comparator<Employee>() {
public int compare(Employee emp1, Employee emp2) { public int compare(Employee emp1, Employee emp2) {
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
return 0;
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate()); return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
} }
}); });