From ef61b86af31c3d5c5186905fff2ec2046c483348 Mon Sep 17 00:00:00 2001 From: Satyam Date: Thu, 11 Oct 2018 20:55:55 +0530 Subject: [PATCH] BAEL-2197:Code rework --- .../src/findItems/FindItemsBasedOnValues.java | 111 +++++++----------- 1 file changed, 45 insertions(+), 66 deletions(-) diff --git a/core-java-8/findItemsBasedOnValues/src/findItems/FindItemsBasedOnValues.java b/core-java-8/findItemsBasedOnValues/src/findItems/FindItemsBasedOnValues.java index d6a320a8ec..a0dcdddd85 100644 --- a/core-java-8/findItemsBasedOnValues/src/findItems/FindItemsBasedOnValues.java +++ b/core-java-8/findItemsBasedOnValues/src/findItems/FindItemsBasedOnValues.java @@ -1,114 +1,93 @@ package findItems; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; + import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.stream.Collectors; + import org.junit.Test; public class FindItemsBasedOnValues { - static SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy"); + List EmplList = new ArrayList(); + List deptList = new ArrayList(); public static void main(String[] args) throws ParseException { FindItemsBasedOnValues findItems = new FindItemsBasedOnValues(); - findItems.givenListOfCompanies_thenListOfClientsIsFilteredCorrectly(); + findItems.givenDepartmentList_thenEmployeeListIsFilteredCorrectly(); } @Test - public void givenListOfCompanies_thenListOfClientsIsFilteredCorrectly() throws ParseException { - List expectedList = new ArrayList(); - Client expectedClient = new Client(1001, DATE_FORMAT.parse("01-02-2018")); - expectedList.add(expectedClient); + public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() { + Integer expectedId = 1002; - List listOfCompanies = new ArrayList(); - List listOfClients = new ArrayList(); - populate(listOfCompanies, listOfClients); + populate(EmplList, deptList); - List filteredList = listOfClients.stream() - .filter(client -> listOfCompanies.stream() - .anyMatch(company -> company.getType() - .equals("eMart") && company.getProjectId() - .equals(client.getProjectId()) && company.getStart() - .after(client.getDate()) && company.getEnd() - .before(client.getDate()))) + List filteredList = EmplList.stream() + .filter(empl -> deptList.stream() + .anyMatch(dept -> dept.getDepartment() + .equals("sales") && empl.getEmployeeId() + .equals(dept.getEmployeeId()))) .collect(Collectors.toList()); - assertEquals(expectedClient.getProjectId(), filteredList.get(0) - .getProjectId()); - assertEquals(expectedClient.getDate(), filteredList.get(0) - .getDate()); + assertEquals(expectedId, filteredList.get(0) + .getEmployeeId()); } - private void populate(List companyList, List clientList) throws ParseException { - Company company1 = new Company(1001, DATE_FORMAT.parse("01-03-2018"), DATE_FORMAT.parse("01-01-2018"), "eMart"); - Company company2 = new Company(1002, DATE_FORMAT.parse("01-02-2018"), DATE_FORMAT.parse("01-04-2018"), "commerce"); - Company company3 = new Company(1003, DATE_FORMAT.parse("01-06-2018"), DATE_FORMAT.parse("01-02-2018"), "eMart"); - Company company4 = new Company(1004, DATE_FORMAT.parse("01-03-2018"), DATE_FORMAT.parse("01-06-2018"), "blog"); + private void populate(List EmplList, List deptList) { + Employee employee1 = new Employee(1001, "empl1"); + Employee employee2 = new Employee(1002, "empl2"); + Employee employee3 = new Employee(1003, "empl3"); - Collections.addAll(companyList, company1, company2, company3, company4); + Collections.addAll(EmplList, employee1, employee2, employee3); - Client client1 = new Client(1001, DATE_FORMAT.parse("01-02-2018")); - Client client2 = new Client(1003, DATE_FORMAT.parse("01-04-2018")); - Client client3 = new Client(1005, DATE_FORMAT.parse("01-07-2018")); - Client client4 = new Client(1007, DATE_FORMAT.parse("01-08-2018")); + Department department1 = new Department(1002, "sales"); + Department department2 = new Department(1003, "marketing"); + Department department3 = new Department(1004, "sales"); - Collections.addAll(clientList, client1, client2, client3, client4); + Collections.addAll(deptList, department1, department2, department3); } } -class Company { - Long projectId; - Date start; - Date end; - String type; +class Employee { + Integer employeeId; + String employeeName; - public Company(long projectId, Date start, Date end, String type) { + public Employee(Integer employeeId, String employeeName) { super(); - this.projectId = projectId; - this.start = start; - this.end = end; - this.type = type; + this.employeeId = employeeId; + this.employeeName = employeeName; } - public Long getProjectId() { - return projectId; + public Integer getEmployeeId() { + return employeeId; } - public Date getStart() { - return start; - } - - public Date getEnd() { - return end; - } - - public String getType() { - return type; + public String getEmployeeName() { + return employeeName; } } -class Client { - Long projectId; - Date date; +class Department { + Integer employeeId; + String department; - public Client(long projectId, Date date) { + public Department(Integer employeeId, String department) { super(); - this.projectId = projectId; - this.date = date; + this.employeeId = employeeId; + this.department = department; } - public Long getProjectId() { - return projectId; + public Integer getEmployeeId() { + return employeeId; } - public Date getDate() { - return date; + public String getDepartment() { + return department; } } \ No newline at end of file