我添加了两个班级。 我的问题是,在我将新工程师添加到ArrayList之后,如何选择一个特定的工程师并且"编辑"他的身份证或工资(通过"菜单"使用扫描仪)?那我怎么能从ArrayList中删除一个特定的工程师?希望有人能理解我的代码以便提供帮助。感谢。
package logic;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeList {
ArrayList<Employee> Emp = new ArrayList<Employee>();
public void startMenu() {
StartLogo();
Scanner s = new Scanner(System.in);
boolean flag = true;
int k;
while (flag) {
printMenuInstructions();
k = s.nextInt();
switch (k) {
case 1:
addWorkerMenu();
break;
case 2:
removeWorkerMenu();
break;
case 3:
editWorkerMenu();
break;
case 4:
printGetdetails();
break;
case 5:
printSalary();
break;
case 6:
System.out.println("Bye Bye");
flag = false;
break;
default:
System.out.println("Invalid key please re enter");
break;
}
}
}
public void printSalary() {
for (int i = 0; i < Emp.size(); i++) {
if (Emp != null)
Emp.get(i).printSalary();
System.out.println();
}
}
public void printGetdetails() {
for (int i = 0; i < Emp.size(); i++) {
if (Emp != null)
Emp.get(i).printDetails();
System.out.println();
}
}
private void printMenuInstructions() {
System.out.println("Enter a number 1-4 ");
System.out.println("1. Add Employee");
System.out.println("2. Remove Employee");
System.out.println("3. Edit Employee details");
System.out.println("4. Employee Report");
System.out.println("5. Salery Report");
System.out.println("6. Exit");
}
private void printMenuAddWorkerInstructions() {
System.out.println("Enter a number 1-4 ");
System.out.println("1. Add Engineer");
System.out.println("2. Add Manager");
System.out.println("3. Add Secretary");
System.out.println("4. Add Cleaner");
System.out.println("5. return");
}
private void printMenuEditWorkerInstructions() {
System.out.println("Enter a number 1-4 ");
System.out.println("1. Edit Engineer");
System.out.println("2. Edit Manager");
System.out.println("3. Edit Secretary");
System.out.println("4. Edit Cleaner");
System.out.println("5. return");
}
private void addWorkerMenu() {
Scanner s = new Scanner(System.in);
boolean flag = true;
int k;
while (flag) {
printMenuAddWorkerInstructions();
k = s.nextInt();
switch (k) {
case 1:
Emp.add(new Engineer());
Emp.get(index).addmenu();
index++;
break;
case 2:
Emp.add(new Manager());
Emp.get(index).addmenu();
index++;
break;
case 3:
Emp.add(new Secretary());
Emp.get(index).addmenu();
index++;
break;
case 4:
Emp.add(new Cleaner());
Emp.get(index).addmenu();
index++;
break;
case 5:
flag = false;
break;
default:
System.out.println("Invalid key please re enter");
break;
}
}
}
private void removeWorkerMenu() {
Scanner d = new Scanner(System.in);
boolean flag = true;
int k;
while (flag) {
printMenuReomoveWorkerInstructions();
k = d.nextInt();
switch (k) {
case 1:
Scanner e = new Scanner(System.in);
int l;
while (flag) {
printMenuReomoveEngineerInstructions();
l = e.nextInt();
switch (l) {
case 1:
// try{
Emp.remove(new Engineer());
Emp.remove(0);
// }catch (ArrayIndexOutOfBoundsException y){
// Emp.isEmpty();
// System.err.println("array out of bound " +
// y.getMessage());
//
// }
// break;
// case 2:
// Emp.remove(new Engineer());
// Emp.remove(1);
// break;
// case 3:
// Emp.remove(new Engineer());
// Emp.remove(2);
// break;
//
// default:
// break;
// }
System.out.println("Engineer was removed succesfully");
break;
}
}
case 2:
Emp.remove(new Manager());
Emp.remove(0);
System.out.println("Manager was removed succesfully");
break;
case 3:
Emp.remove(new Secretary());
Emp.remove(0);
System.out.println("secratary was removed succesfully");
break;
case 4:
Emp.remove(new Cleaner());
Emp.remove(0);
System.out.println("Cleaner was removed succesfully");
break;
case 5:
flag = false;
break;
default:
System.out.println("Invalid key please re enter");
break;
}
}
}
private void editWorkerMenu() {
Scanner f = new Scanner(System.in);
boolean flag = true;
int m;
while (flag) {
printMenuEditWorkerInstructions();
m = f.nextInt();
switch (m) {
case 1:
Emp.set(0, new Engineer());
addWorkerMenu();
System.out.println("Engineer was edited succesfully");
break;
case 2:
Emp.set(0, new Manager());
addWorkerMenu();
System.out.println("Manager was edited succesfully");
break;
case 3:
Emp.set(0,new Secretary());
addWorkerMenu();
System.out.println("secratary was edited succesfully");
break;
case 4:
Emp.set(0,new Cleaner());
addWorkerMenu();
System.out.println("Cleaner was edited succesfully");
break;
case 5:
flag = false;
break;
default:
System.out.println("Invalid key please re enter");
break;
}
}
}
private void printMenuReomoveEngineerInstructions() {
System.out.println("which engineer would you like to remove?");
System.out.println("for the first engineer please press 1");
System.out.println("for the second engineer please press 2");
System.out.println("for the third engineer please press 3");
}
private void printMenuReomoveWorkerInstructions() {
System.out.println("Enter a number 1-4 ");
System.out.println("1. Remove Engineer");
System.out.println("2. Remove Manager");
System.out.println("3. Remove Secretary");
System.out.println("4. Remove Cleaner");
System.out.println("5. return");
}
final static void StartLogo() {
System.err.println("welcome to daniel corporation");
System.err.println("*** ***");
System.err.println(" ** **");
System.err.println(" *** ***");
System.err.println(" ** **");
System.err.println(" ***");
System.err.println();
}
}
And class Engineer is:
package logic;
import java.util.Scanner;
public class Engineer extends Employee {
private float Bonus;
public Engineer() {
}
public Engineer(String name, int age, int id, int year, float salary,
float bonus) {
super(name, year, year, year, bonus);
Bonus = bonus;
}
public float getBonus() {
return Bonus;
}
public void setBonus(float bonus) {
Bonus = bonus;
}
public void printDetails() {
String details = " Name is: " + getName() + "," + " the age is: "
+ getAge() + " the id is: " + getId() + ","
+ " the year is: " + getYear()
+ " the salary in ILS is: " + super.getSalary() + ","
+ " vacation days left : " + getBonus();
System.out.println(details);
}
public void addmenu() {
//TODO
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Engineer name ");
String strName = scanner.next();
try {
String Name = String.valueOf(strName);
setName(Name);
} catch (Exception e) {
System.err.println("not a number " + e.getMessage());
}
// super.setName(scanner.next());
System.out.println("Enter Engineer Age");
String strAge = scanner.next();
try {
int age = Integer.valueOf(strAge);
// TODO if (strAge.) ){
//
// }
setAge(age);
} catch (Exception e) {
System.err.println("not a number " + e.getMessage());
}
// super.setAge(scanner.nextInt());
System.out.println("Enter Engineer ID number");
String strId = scanner.next();
try {
int Id = Integer.valueOf(strId);
setId(Id);
} catch (Exception e) {
System.err.println("not a number " + e.getMessage());
}
//super.setId(scanner.nextInt());
System.out.println("Enter Engineer year of Start");
String strYear = scanner.next();
try {
int Year = Integer.valueOf(strYear);
setYear(Year);
} catch (Exception e) {
System.err.println("not a number " + e.getMessage());
}
//super.setYear(scanner.nextInt());
System.out.println("Enter Engineer salary in dollars ");
String strSalary = scanner.next();
try {
float Salary = Float.valueOf(strSalary);
setSalary(Salary);
} catch (Exception e) {
System.err.println("not a number " + e.getMessage());
} System.out.println("Enter Engineer bonus ");
String strBonus = scanner.next();
try {
float Salary = Float.valueOf(strBonus);
setSalary(Salary);
} catch (Exception e) {
System.err.println("not a number " + e.getMessage());
System.out.println("Engineer Entered successfuly ");
}
}
@Override
public void printSalary() {
System.out.println("Salary of " + super.getName() + " is "
+ super.getSalary());
}
@Override
public void editDetails() {
}
}
答案 0 :(得分:1)
使用java.util.Map
(键:ID,值=员工对象)更容易:
Map<Integer, Employee> theMap = new HashMap<>();
theMap.put(someId, new Engineer("Bob The Builder", 21, someId, 1911, 100000.0, 20.0)); // add
theMap.get(someId); // get
theMap.remove(someId); // remove
使用List
,您必须迭代检查ID然后删除,但您无法从正在迭代的列表中删除(因此请创建副本或使用列表iterator
):
// with a copy
List<Employee> remainingEmps = new ArrayList<>();
for (Employee e : Emps) {
if (!e.getId().equals(someId)) { // assumes no employee with null ID
remainingEmps.add(e);
}
}
Emps = remainingEmps;
答案 1 :(得分:0)
您可以通过以下方式选择特定工程师:
1.按索引:
Engineer engineer = Emp.get(i);//i is the index of the element to be removed.
2。具体工程师ID:
for(Engineer engineer : Emp)
{
if(engineer.getId() == (<specific id>) //provided Employee class has a getter method for id.
{
engineer.setId(<new id>);
}
}
同样,从列表中删除有以下几种方法:
1.按索引:
Emp.remove(i);//i is the index of the element to be removed.
答案 2 :(得分:0)
你应该使用 HashMap insted of ArrayList Like
使用
HashMap<Integer,Employee> mapEmployeeList = new HashMap<Integer,Employeer>();
而不是
ArrayList<Employee> Emp = new ArrayList<Employee>();