gpt4 book ai didi

java - 显示多个字符串项目Java

转载 作者:行者123 更新时间:2023-12-01 17:49:33 25 4
gpt4 key购买 nike

我有最后一个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) 这样的条件然后返回employee1,employee4 例如。你能给我一些建议吗?感谢您的关注。

最佳答案

您可以创建一个静态ArrayList并将所有员工添加到该ArrayList中,并且在displayEmployees方法中您可以根据条件如果员工EmploymentYear大于n打印详细信息并添加到另一个列表所以最后如果你愿意你可以只返回员工计数或者你也可以返回员工列表

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

关于java - 显示多个字符串项目Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com