显示多个String项目Java

时间:2018-08-18 15:30:38

标签: java list arraylist

我还有最后一个Java作业任务,该任务与员工有关, 我的方法应该打印工作了超过“ n”年的员工的姓名和姓氏。

我现在所做的:

    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;
    }
}

我创建了一个参数化的构造函数,用于创建具有多个参数的员工,并且还封装了参数。 不知道下一步要做什么,任务说我可以使用List / ArrayList,但是经过一段时间的搜索之后,我仍然不明白如何实现例如if (employmentYear - currentYear >= n) then return employee1, employee4之类的条件。 你能给我一些提示吗? 谢谢您的关注。

3 个答案:

答案 0 :(得分:2)

您可以创建一个static ArrayList并将所有所有员工添加到该ArrayList中,然后在displayEmployees方法中,您可以根据条件if employee EmploymentYear greater than n print details and add to another list流式传输列表,因此如果您希望您可以只返回员工人数或也可以返回员工列表

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();
   }
 }

答案 1 :(得分:1)

如果您正在寻找“工作了'n'年以上” 的方法,这可能会对您有所帮助。

"@angular/animations": "^6.1.3"

答案 2 :(得分:1)

在Employee类中添加适当的toString()方法以获取所需的输出,此外,我还使用了Stream对象中的filter()方法来过滤Employee对象。我将工作年数作为输入参数,并从employmentYear字段中计算了工作年限。

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;
    }
}