BAEL-5558 (#12222)
* [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:
parent
9c40bfe297
commit
26224e4686
@ -25,5 +25,4 @@
|
||||
<properties>
|
||||
<commons-lang.version>2.2</commons-lang.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -5,14 +5,10 @@ import java.util.Date;
|
||||
public class Employee implements Comparable<Employee>{
|
||||
|
||||
private String name;
|
||||
private int age;
|
||||
private double salary;
|
||||
private Date joiningDate;
|
||||
|
||||
public Employee(String name, int age, double salary, Date joiningDate) {
|
||||
public Employee(String name, Date joiningDate) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
this.salary = salary;
|
||||
this.joiningDate = joiningDate;
|
||||
}
|
||||
|
||||
@ -24,22 +20,6 @@ public class Employee implements Comparable<Employee>{
|
||||
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() {
|
||||
return joiningDate;
|
||||
}
|
||||
@ -57,10 +37,9 @@ public class Employee implements Comparable<Employee>{
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuffer().append("(")
|
||||
.append(getName()).append(",")
|
||||
.append(getAge())
|
||||
.append(getName())
|
||||
.append(",")
|
||||
.append(getSalary()).append(",").append(getJoiningDate())
|
||||
.append(getJoiningDate())
|
||||
.append(")")
|
||||
.toString();
|
||||
}
|
||||
|
@ -22,36 +22,37 @@ public class EmployeeSortingByDateUnitTest {
|
||||
public void initVariables() {
|
||||
|
||||
Collections.addAll(employees,
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("John", 23, 5000, new Date())
|
||||
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("John", new Date())
|
||||
);
|
||||
|
||||
Collections.addAll(employeesSortedByDateDesc,
|
||||
new Employee("John", 23, 5000, new Date()),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1))
|
||||
new Employee("John", new Date()),
|
||||
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Pearl", DateUtils.addYears(new Date(), -1))
|
||||
);
|
||||
|
||||
Collections.addAll(employeesSortedByDateAsc,
|
||||
new Employee("Pearl", 33, 6000, DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("Jessica", 23, 4000, DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Earl", 43, 10000, DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", 33, 7000, DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", 26, 6000, DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("John", 23, 5000, new Date())
|
||||
new Employee("Pearl", DateUtils.addYears(new Date(), -1)),
|
||||
new Employee("Jessica", DateUtils.addMonths(new Date(), -6)),
|
||||
new Employee("Earl", DateUtils.addMonths(new Date(), -2)),
|
||||
new Employee("Frank", DateUtils.addDays(new Date(), -20)),
|
||||
new Employee("Steve", DateUtils.addDays(new Date(), -10)),
|
||||
new Employee("John", new Date())
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinNaturalOrder() {
|
||||
Collections.sort(employees);
|
||||
System.out.println(employees);
|
||||
|
||||
assertEquals(employees, employeesSortedByDateAsc);
|
||||
}
|
||||
@ -83,7 +84,7 @@ public class EmployeeSortingByDateUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenSortedListinAscOrder() {
|
||||
public void givenEmpList_SortEmpList_thenSortedListinDescOrder() {
|
||||
Collections.sort(employees, Collections.reverseOrder());
|
||||
|
||||
assertEquals(employees, employeesSortedByDateDesc);
|
||||
@ -102,12 +103,10 @@ public class EmployeeSortingByDateUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListAscV1() {
|
||||
public void givenEmpList_SortEmpList_thenCheckSortedListDescV1() {
|
||||
|
||||
Collections.sort(employees, new Comparator<Employee>() {
|
||||
public int compare(Employee emp1, Employee emp2) {
|
||||
if (emp1.getJoiningDate() == null || emp2.getJoiningDate() == null)
|
||||
return 0;
|
||||
return emp2.getJoiningDate().compareTo(emp1.getJoiningDate());
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user