我有一个顾客,顾客和主要阶层。我在客户类中有一个ArrayList来存储每个客户。我想我已经成功添加了客户。如何在ArrayList中显示所有客户,以及如何删除特定客户?我正在尝试在客户类中创建一个方法,然后在主类中调用它。
客户类别:
import java.util.ArrayList;
import java.util.Scanner;
public class customer {
//create variables
private int Id;
private String firstName;
private String lastName;
public customer() {
}
//setters and getters
public int getId() {
return Id;
}
public void setId(int id) {
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;
}
//constructor
public customer(int Id, String firstName, String lastName) {
this.Id = Id;
this.firstName = firstName;
this.lastName = lastName;
}
//get user input
public void customerInfo() {
Scanner input = new Scanner(System.in);
{
while (true) {
//ask user for input and get input
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();
//add customer
customers.add(new customer(id, firstName, lastName));
}
}
}
public void displayCustomers() {
System.out.println("Customer List : ");
}
}
客户类别:
import java.util.ArrayList;
//creates an array of the customers
public final class customers {
public static ArrayList<customer> customers;
public customers() {
customers = new ArrayList<customer>();
}
public static void add(customer customer) {
}
}
主要类别:
public class Main {
public static void main(String[] args) {
customer customerInfoObject = new customer();
customerInfoObject.customerInfo();
customer displayCustomersObject = new customer();
displayCustomersObject.displayCustomers();
}
}
答案 0 :(得分:1)
要打印列表中所有客户的信息,请使用循环。 用户删除客户的方式有多种。您可以从用户那里收到一个整数,然后删除给定索引处的客户。您还可以接收客户名称,并在循环中将其与客户名称进行比较,然后删除具有接收名称的客户名称。
此外,请勿使用空括号。只需将!
放在if条件前面即可将其反转