以下是使用Java语言实现一个简单的客户信息管理页面的示例代码:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class CustomerManagementPage {
private List<Map<String, String>> customerList;
public CustomerManagementPage() {
customerList = new ArrayList<>();
}
public static void main(String[] args) {
CustomerManagementPage page = new CustomerManagementPage();
page.run();
}
public void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
printMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
switch (choice) {
case 1:
addCustomer(scanner);
break;
case 2:
editCustomer(scanner);
break;
case 3:
searchCustomer(scanner);
break;
case 4:
displayAllCustomers();
break;
case 0:
System.out.println("Exiting...");
return;
default:
System.out.println("Invalid choice, please try again.");
break;
}
}
}
private void printMenu() {
System.out.println("---- Customer Management Page ----");
System.out.println("1. Add Customer");
System.out.println("2. Edit Customer");
System.out.println("3. Search Customer");
System.out.println("4. Display All Customers");
System.out.println("0. Exit");
System.out.println("----------------------------------");
System.out.print("Enter your choice: ");
}
private void addCustomer(Scanner scanner) {
Map<String, String> customer = new HashMap<>();
System.out.print("Enter customer's name: ");
String name = scanner.nextLine();
customer.put("name", name);
System.out.print("Enter customer's company: ");
String company = scanner.nextLine();
customer.put("company", company);
System.out.print("Enter customer's phone: ");
String phone = scanner.nextLine();
customer.put("phone", phone);
System.out.print("Enter customer's mobile: ");
String mobile = scanner.nextLine();
customer.put("mobile", mobile);
System.out.print("Enter customer's QQ: ");
String qq = scanner.nextLine();
customer.put("qq", qq);
System.out.print("Enter customer's email: ");
String email = scanner.nextLine();
customer.put("email", email);
customerList.add(customer);
System.out.println("Customer added successfully.");
}
private void editCustomer(Scanner scanner) {
System.out.print("Enter customer's name to edit: ");
String searchName = scanner.nextLine();
List<Map<String, String>> matchedCustomers = searchCustomersByName(searchName);
if (matchedCustomers.isEmpty()) {
System.out.println("No customers found with the given name.");
return;
}
System.out.println("List of matched customers:");
displayCustomers(matchedCustomers);
System.out.print("Enter the number of the customer to edit: ");
int customerIndex = scanner.nextInt();
scanner.nextLine(); // consume the newline character
if (customerIndex < 1 || customerIndex > matchedCustomers.size()) {
System.out.println("Invalid customer number, please try again.");
return;
}
Map<String, String> customer = matchedCustomers.get(customerIndex - 1);
System.out.print("Enter new name (press enter to keep the same): ");
String newName = scanner.nextLine();
if (!newName.isEmpty()) {
customer.put("name", newName);
}
System.out.print("Enter new company (press enter to keep the same): ");
String newCompany = scanner.nextLine();
if (!newCompany.isEmpty()) {
customer.put("company", newCompany);
}
System.out.print("Enter new phone (press enter to keep the same): ");
String newPhone = scanner.nextLine();
if (!newPhone.isEmpty()) {
customer.put("phone", newPhone);
}
System.out.print("Enter new mobile (press enter to keep the same): ");
String newMobile = scanner.nextLine();
if (!newMobile.isEmpty()) {
customer.put("mobile", newMobile);
}
System.out.print("Enter new QQ (press enter to keep the same): ");
String newQq = scanner.nextLine();
if (!newQq.isEmpty()) {
customer.put("qq", newQq);
}
System.out.print("Enter new email (press enter to keep the same): ");
String newEmail = scanner.nextLine();
if (!newEmail.isEmpty()) {
customer.put("email", newEmail);
}
System.out.println("Customer edited successfully.");
}
private void searchCustomer(Scanner scanner) {
System.out.println("---- Search Customer ----");
System.out.println("1. Search by Name");
System.out.println("2. Search by Company");
System.out.println("3. Search by Name (Fuzzy)");
System.out.println("4. Search by Company (Fuzzy)");
System.out.println("-------------------------");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume the newline character
List<Map<String, String>> matchedCustomers;
switch (choice) {
case 1:
System.out.print("Enter customer's name: ");
String name = scanner.nextLine();
matchedCustomers = searchCustomersByName(name);
break;
case 2:
System.out.print("Enter customer's company: ");
String company = scanner.nextLine();
matchedCustomers = searchCustomersByCompany(company);
break;
case 3:
System.out.print("Enter customer's name (fuzzy search): ");
String fuzzyName = scanner.nextLine();
matchedCustomers = searchCustomersByNameFuzzy(fuzzyName);
break;
case 4:
System.out.print("Enter customer's company (fuzzy search): ");
String fuzzyCompany = scanner.nextLine();
matchedCustomers = searchCustomersByCompanyFuzzy(fuzzyCompany);
break;
default:
System.out.println("Invalid choice, returning to main menu.");
return;
}
if (matchedCustomers.isEmpty()) {
System.out.println("No customers found with the given search criteria.");
} else {
System.out.println("List of matched customers:");
displayCustomers(matchedCustomers);
}
}
private void displayAllCustomers() {
System.out.println("---- All Customers ----");
if (customerList.isEmpty()) {
System.out.println("No customers found.");
} else {
displayCustomers(customerList);
}
}
private List<Map<String, String>> searchCustomersByName(String name) {
List<Map<String, String>> matchedCustomers = new ArrayList<>();
for (Map<String, String> customer : customerList) {
if (customer.get("name").equalsIgnoreCase(name)) {
matchedCustomers.add(customer);
}
}
return matchedCustomers;
}
private List<Map<String, String>> searchCustomersByCompany(String company) {
List<Map<String, String>> matchedCustomers = new ArrayList<>();
for (Map<String, String> customer : customerList) {
if (customer.get("company").equalsIgnoreCase(company)) {
matchedCustomers.add(customer);
}
}
return matchedCustomers;
}
private List<Map<String, String>> searchCustomersByNameFuzzy(String fuzzyName) {
List<Map<String, String>> matchedCustomers = new ArrayList<>();
for (Map<String, String> customer : customerList) {
if (customer.get("name").toLowerCase().contains(fuzzyName.toLowerCase())) {
matchedCustomers.add(customer);
}
}
return matchedCustomers;
}
private List<Map<String, String>> searchCustomersByCompanyFuzzy(String fuzzyCompany) {
List<Map<String, String>> matchedCustomers = new ArrayList<>();
for (Map<String, String> customer : customerList) {
if (customer.get("company").toLowerCase().contains(fuzzyCompany.toLowerCase())) {
matchedCustomers.add(customer);
}
}
return matchedCustomers;
}
private void displayCustomers(List<Map<String, String>> customers) {
for (int i = 0; i < customers.size(); i++) {
Map<String, String> customer = customers.get(i);
System.out.println("Customer " + (i + 1) + ":");
System.out.println("Name: " + customer.get("name"));
System.out.println("Company: " + customer.get("company"));
System.out.println("Phone: " + customer.get("phone"));
System.out.println("Mobile: " + customer.get("mobile"));
System.out.println("QQ: " + customer.get("qq"));
System.out.println("Email: " + customer.get("email"));
System.out.println();
}
}
}
这个示例代码实现了客户信息的输入、存储、修改、查询等功能。用户可以根据提示进行相关操作。用户输入的客户信息将存储在一个列表中,在查询时根据用户提供的条件进行匹配,并将匹配到的客户信息显示出来。