Создайте класс для хранения информации о сотруднике.
Employee.java
public class Employee {
String firstName;
String lastName;
String deptName;
public Employee() {
}
public Employee(String firstName, String lastName, String deptName) {
this.firstName = firstName;
this.lastName = lastName;
this.deptName = deptName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
@Override
public String toString() {
return "Employee{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", deptName='" + deptName + '\'' +
'}';
}
}
Пример кода для создания нескольких сотрудников и их печати
Employees.java
import java.util.ArrayList;
import java.util.List;
public class Employees {
public static void main(String[] args) {
List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee("FN-1","LN-1","Dept-1"));
employeeList.add(new Employee("FN-2","LN-2","Dept-2"));
employeeList.add(new Employee("FN-3","LN-3","Dept-3"));
//loop
for (int i = 4; i < 8; i++) {
employeeList.add(new Employee("FN-" + i, "LN-" + i, "Dept-" + i));
}
//print
employeeList.forEach(System.out::println);
}
}
Выход:
Employee{firstName='FN-1', lastName='LN-1', deptName='Dept-1'}
Employee{firstName='FN-2', lastName='LN-2', deptName='Dept-2'}
Employee{firstName='FN-3', lastName='LN-3', deptName='Dept-3'}
Employee{firstName='FN-4', lastName='LN-4', deptName='Dept-4'}
Employee{firstName='FN-5', lastName='LN-5', deptName='Dept-5'}
Employee{firstName='FN-6', lastName='LN-6', deptName='Dept-6'}
Employee{firstName='FN-7', lastName='LN-7', deptName='Dept-7'}