How do I search my ArrayList for a specific persons details? How would i then edit or remove them?

user1999 :

I have an ArrayList which asks for user input then stores each employees details. How do I display everything stored in this ArrayList or search for a specific employee? How would I then edit or remove this employee from the ArrayList? I have two classes, one salesPerson - where I have all variables, setters, getters and constructors and salesPersonMain - where I have the prompting for user input, storing to an array etc.

I can add employees to the ArrayList and view them when I end the input loop.

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

The salesPerson class:

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

Well what you have tried will take care of searching part in List Of SalesPerson Object and display correcting. You can do little more modification like below code to handle remove part as well.

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

}

Try the above code and hope you get the idea where to change what more.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=157562&siteId=1