【发布时间】:2019-09-23 11:52:15
【问题描述】:
我有一个客户,客户和主类。我在客户类中有一个 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();
}
}
【问题讨论】:
-
你应该为
customer实现toString()、equals()和hashCode()。 -
我该怎么做?哪个类的什么代码?