0

I have a last Java homework task, this task is about employees, my method should print employee's names and surnames, worked more than "n" years.

What I've done for now:

    public class LastTask {
    public static void main(String[] args) {
        Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
                "Moskva", 1900, 6);
        Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
                "Krasnodar", 2017, 8);
        Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
                "New-York", 2010, 3);
        Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
                "Auckland", 2000, 11);
        Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
                "Beijing", 2014, 11);
    }

    /**
     * Prints employees' information, which have worked more than 'n' year(s) for now.
     *
     * @param n years quantity
     * @return the String, contained surname, name, patronymic and address of the specific employee(s).
     */
    public static String displayEmployees(int n) {

        return null;
    }
}

class Employee {
    private String surname;
    private String name;
    private String patronymic;
    private String address;
    private int employmentYear;
    private int employmentMonth;


    Employee(String surname, String name, String patronymic, String address, int employmentYear, int employmentMonth) {
        this.surname = surname;
        this.name = name;
        this.patronymic = patronymic;
        this.address = address;
        this.employmentYear = employmentYear;
        this.employmentMonth = employmentMonth;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPatronymic() {
        return patronymic;
    }

    public void setPatronymic(String patronymic) {
        this.patronymic = patronymic;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getEmploymentYear() {
        return employmentYear;
    }

    public void setEmploymentYear(int employmentYear) {
        this.employmentYear = employmentYear;
    }

    public int getEmploymentMonth() {
        return employmentMonth;
    }

    public void setEmploymentMonth(int employmentMonth) {
        this.employmentMonth = employmentMonth;
    }
}

I made a parametrised constructor for creating employees with multiple parameters, also made parameters encapsulated. Have no clue what to do next, task says that I can use List/ArrayList, but after some time googling about it, I still can't understand how to implement a condition like if (employmentYear - currentYear >= n) then return employee1, employee4 for example. Could you give me some tips? Thank you for your attention.

2
  • Make array of Employee class objects and use for-loop. Commented Aug 18, 2018 at 15:37
  • You can do something like add employees to a list; traverse the list; check the condition of each employee: if the condition matches, print the name of his/hers Commented Aug 18, 2018 at 15:38

3 Answers 3

2

You can create a static ArrayList and add those all employees to that ArrayList, and in displayEmployees method you can stream that list based on condition if employee EmploymentYear greater than n print details and add to another list so finally if you want you can just return count of employees or you can return List of employees also

public class LastTask {

 static List<Employee> employee = new ArrayList<>();
public static void main(String[] args) {
    Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
            "Moskva", 1900, 6);
    Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
            "Krasnodar", 2017, 8);
    Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
            "New-York", 2010, 3);
    Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
            "Auckland", 2000, 11);
    Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
            "Beijing", 2014, 11);

    employee.add(employee1);
    employee.add(employee2);
    employee.add(employee3);
    employee.add(employee4);
    employee.add(employee5);
}

/**
 * Prints employees' information, which have worked more than 'n' year(s) for now.
 *
 * @param n years quantity
 * @return the String, contained surname, name, patronymic and address of the specific employee(s).
 */
public static int displayEmployees(int n) {
    List<Employee> finalList = new ArrayList<>();
    employee.stream().forEach(emp->{
        if(emp.getEmploymentYear()-Year.now().getValue()>=n) {
            System.out.println("Employee Name : "+emp.getName()+" Sur Aame : "+emp.getSurname());
             finalList.add(emp);
        }
    });

    return finalList.size();
   }
 }
Sign up to request clarification or add additional context in comments.

6 Comments

thanks for your great answer! I also have to write tests, like private static void displayEmployeesTest() { assertEquals("displayEmployeesTest", "name surname", displayEmployees(3)); } How can I make the method displayEmployees returning "String" or "String[]" ?
what tests yo mean?
why do you want to return String or String[] just return Employee object or List you can get name and surname by using getters right>?
i think for test case you will return one object, so return one employee object and from that get name and surname
but it seems like I need to test the whole result, I mean to test if all required employees are defined.
|
1

If you are looking for a way to find "worked more than 'n' years", this might help you.

Calendar.getInstance().get(Calendar.YEAR) - employmentYear >= n

1 Comment

@artshakhov my pleasure :)
1

Add a proper toString() method in the Employee class to get the desired output, apart from that I have used the filter() method from the Stream object to filter through the Employee objects. I am passing the number of years worked as an input parameter and calculating the years served in employment from the employmentYear field.

package com.company;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

public class LastTask {
    private  static List<Employee>  listEmps;
    public static void main(String[] args) {


        Employee employee1 = new Employee("Dobrobaba", "Irina", "Ivanovna",
                "Moskva", 1900, 6);
        Employee employee2 = new Employee("Shmal", "Anna", "Nikolaevna",
                "Krasnodar", 2017, 8);
        Employee employee3 = new Employee("Kerimova", "Niseimhalum", "Magomedmirzaevna",
                "New-York", 2010, 3);
        Employee employee4 = new Employee("Dobryden", "Yuri", "Viktorovich",
                "Auckland", 2000, 11);
        Employee employee5 = new Employee("Lopata", "Leonid", "Nikolaevich",
                "Beijing", 2014, 11);

        listEmps = new ArrayList<>(Arrays.asList(employee1,employee2,employee3,employee4,employee5));
        //display employee details of employees who worked more than 17 years.
        displayEmployees(17);
    }

    /**
     * Prints employees' information, which have worked more than 'n' year(s) for now.
     *
     * @param n years quantity
     * @return the String, contained surname, name, patronymic and address of the specific employee(s).
     */
   public static void displayEmployees(int n) {
    int year = Calendar.getInstance().get(Calendar.YEAR);
    listEmps.stream()
            .filter(emp ->{
                         return year - emp.getEmploymentYear() > n;
            })
            .collect(Collectors.toList())
            .forEach(System.out::println);
    }
}

class Employee {
    private String surname;
    private String name;
    private String patronymic;
    private String address;
    private int employmentYear;
    private int employmentMonth;


    Employee(String surname, String name, String patronymic, String address, int employmentYear, int employmentMonth) {
        this.surname = surname;
        this.name = name;
        this.patronymic = patronymic;
        this.address = address;
        this.employmentYear = employmentYear;
        this.employmentMonth = employmentMonth;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPatronymic() {
        return patronymic;
    }

    public void setPatronymic(String patronymic) {
        this.patronymic = patronymic;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getEmploymentYear() {
        return employmentYear;
    }

    public void setEmploymentYear(int employmentYear) {
        this.employmentYear = employmentYear;
    }

    public int getEmploymentMonth() {
        return employmentMonth;
    }

    public void setEmploymentMonth(int employmentMonth) {
        this.employmentMonth = employmentMonth;
    }

    @Override
    public String toString(){
        return "Employee details: " + this.name + this.surname + this.address + this.employmentYear;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.