我有一个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;
}
}
答案 0 :(得分:1)
好吧,您将尝试在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;
}
}
尝试上面的代码,希望您知道在哪里进行更多更改。
答案 1 :(得分:0)
假设有一个salesPerson类,例如:
resolved = typing.get_type_hints(C)
f = dataclasses.fields(C)[0]
ftype = resolved[f.name]
}
您可以执行以下操作来搜索要删除的特定内容:
gcloud beta run deploy --image [image] --allow-unauthenticated
您可以对其进行编辑以搜索多个值,或者将其放入带有搜索值等参数的方法中。