java - How do I search my ArrayList for a specific persons details? How would i then edit or remove them?
问题描述
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;
}
}
解决方案
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.
推荐阅读
- android - CoordinatorLayout 中的渲染错误 - 我该如何解决?
- javascript - 如何使用javascript将csv更改为json数组
- css - css 属性问题“背景剪辑:文本;” 和 W3C 验证器
- jestjs - Jest-Puppeteer 测试随机失败而没有输出
- groovy - 如何在 Groovy 中使用 Dagger?
- javascript - 奇数值和 1 如何在 js 中给出“真”?
- python - 在python中将一些列转换为行,将一列的行转换为列
- python - 使用 AJAX、Python 和 CGI 上传文件
- google-bigquery - 从行中为每个成功条件返回多行
- uitableview - 没有 Xamarin.Forms 的 UITableView 标题背景颜色