我有一个ArrayList,要求用户输入然后存储每个员工的详细信息。如何显示此ArrayList中存储的所有内容或如何搜索特定员工?然后,我该如何编辑或从ArrayList中删除此员工?我有两个类,一个salesPerson-我具有所有变量,setters,getters和构造函数,以及salesPersonMain-我在其中提示用户输入,存储到数组等。

我可以将雇员添加到ArrayList中,并在结束输入循环时对其进行查看。

public class salesPersonMain {

public static void main(String[] args) throws InputValidationException {

    Scanner input = new Scanner(System.in);
    List<salesPerson> sPerson = new ArrayList<salesPerson>();

    while (true) {


        System.out.println("Enter id (press 'q' to quit): ");
        String temp = input.nextLine();
        if (temp.equals("q")) break;

        int id = Integer.parseInt(temp);

        System.out.println("Enter first name:");
        String firstName = input.nextLine();

        System.out.println("Enter last name:");
        String lastName = input.nextLine();


        //save in array list
        sPerson.add(new salesPerson(id, firstName, lastName));

    }
    for (salesPerson salesPerson : sPerson) {
        System.out.println(salesPerson);
       }
    }
 }


salesPerson类:

import java.util.ArrayList;

public class salesPerson{
    //create variables for sales person
    private int id;
    private String firstName;
    private String lastName;

    public salesPerson() {

    }

    //Setters and getters
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) throws InputValidationException {
        if (firstName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.firstName = firstName;
        }
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName)throws InputValidationException {
        if (lastName.matches("\\p{Upper}(\\p{Lower}){2,20}")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.lastName = lastName;
        }
    }



    public void setCurrentCarMake(String currentCarMake) throws InputValidationException {
        if (currentCarMake.matches("(\\p{Alpha}{2,14})")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.currentCarMake = currentCarMake;
        }
    }

    public String getCurrentCarModel() {
        return currentCarModel;
    }

    public void setCurrentCarModel(String currentCarModel) throws InputValidationException {
        if (currentCarModel.matches("(\\p{Alnum}{1,10})")) {
        } else {
            throw new InputValidationException();
        }
        {
            this.currentCarModel = currentCarModel;
        }
    }



    //create constructor
    public salesPerson(int id, String firstName, String lastName) throws InputValidationException {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }
    @Override
    public String toString() {
        return id + ", " + firstName + " " + lastName;
    }
}

最佳答案

那么,您尝试过的内容将照顾到搜索List Of SalesPerson Object并显示更正的内容。您可以像下面的代码那样做更多的修改来处理删除的部分。

SalesPersonMain.java

public class SalesPersonMain {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    CopyOnWriteArrayList<SalesPerson> sPerson = new CopyOnWriteArrayList<>(); //List will fail in case of remove due to ConcurrentModificationException

    while (true) {
        System.out.println("Enter id (press 'q' to quit): ");
        String temp = input.nextLine();
        if (temp.equals("q")) break;

        int id = Integer.parseInt(temp);

        System.out.println("Enter first name:");
        String firstName = input.nextLine();

        System.out.println("Enter last name:");
        String lastName = input.nextLine();
        sPerson.add(new SalesPerson(id, firstName, lastName));

    }
    //Search
    System.out.println("Enter String to search & display");
    String searchString = input.nextLine();
    for (SalesPerson salesPerson : sPerson) {
        if(salesPerson.search(searchString)!=null){
            System.out.println(salesPerson.toString());
        }
    }
    //Remove
    System.out.println("Enter String to search & remove");
    searchString = input.nextLine();
    for (SalesPerson salesPerson : sPerson) {
        if(salesPerson.search(searchString)!=null){
            System.out.println(salesPerson.toString()+" is removed from the List");
            sPerson.remove(salesPerson);
        }
    }
    //Display All
    System.out.println("Final List : ");
    for (SalesPerson salesPerson : sPerson) {
        System.out.println(salesPerson.toString());
    }
}


}

SalesPerson.java

public class SalesPerson {

public SalesPerson(Integer id, String firstName, String lastName) {
    this.id = id;
    this.firstName = firstName;
    this.lastName = lastName;
}

Integer id;
String firstName;
String lastName;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

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

@Override
public String toString() {
    return "{" +
            "'id':" + id +
            ", 'firstName':'" + firstName + '\'' +
            ", 'lastName':'" + lastName + '\'' +
            '}';
}

public SalesPerson search(String search){
    if(firstName.matches("(.*)"+search+"(.*)") || lastName.matches("(.*)"+search+"(.*)")){
        return this;
    }
    return null;
}


}

尝试上面的代码,希望您了解在哪里进行更改。

10-07 15:24