详解Java如何使用集合来实现一个客户信息管理系统
1. 概述
在Java中,可以使用集合类来实现一个简单的客户信息管理系统。集合类提供了丰富的数据结构和操作方法,能够满足各种需求。
我们可以使用List或Set来存储客户信息,在程序中实现增删改查等操作。同时,我们可以利用Map来存储客户信息,实现键值对的存储,方便以后的查找和遍历。
2. 使用List实现一个客户信息管理系统
我们可以使用List来存储客户信息,同时利用Scanner类实现控制台输入,简单实现增删改查等操作。
在下面的示例代码中,我们定义一个Customer类,其中包含姓名和电话两个字段。接着,我们使用ArrayList来存储Customer对象,实现客户信息的管理。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class CustomerManagementSystem {
private List<Customer> customers = new ArrayList<>();
public static void main(String[] args) {
CustomerManagementSystem system = new CustomerManagementSystem();
system.run();
}
private void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
addCustomer(scanner);
break;
case 2:
deleteCustomer(scanner);
break;
case 3:
updateCustomer(scanner);
break;
case 4:
queryCustomer(scanner);
break;
case 5:
showCustomers();
break;
case 6:
System.out.println("Goodbye!");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice, please try again.");
break;
}
}
}
private void displayMenu() {
System.out.println("-------- Customer Management System --------");
System.out.println("1. Add customer");
System.out.println("2. Delete customer");
System.out.println("3. Update customer");
System.out.println("4. Query customer");
System.out.println("5. Show all customers");
System.out.println("6. Exit");
System.out.print("Please enter your choice: ");
}
private void addCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
System.out.print("Please enter customer phone: ");
String phone = scanner.next();
Customer customer = new Customer(name, phone);
customers.add(customer);
System.out.println("Add customer success!");
}
private void deleteCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
for (Iterator<Customer> iterator = customers.iterator(); iterator.hasNext();) {
Customer customer = iterator.next();
if (customer.getName().equals(name)) {
iterator.remove();
System.out.println("Delete customer success!");
return;
}
}
System.out.println("Customer not found!");
}
private void updateCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
for (Customer customer : customers) {
if (customer.getName().equals(name)) {
System.out.print("Please enter new customer phone: ");
String phone = scanner.next();
customer.setPhone(phone);
System.out.println("Update customer success!");
return;
}
}
System.out.println("Customer not found!");
}
private void queryCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
for (Customer customer : customers) {
if (customer.getName().equals(name)) {
System.out.println(customer.toString());
return;
}
}
System.out.println("Customer not found!");
}
private void showCustomers() {
for (Customer customer : customers) {
System.out.println(customer.toString());
}
}
}
class Customer {
private String name;
private String phone;
public Customer(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return String.format("Name: %s, Phone: %s", name, phone);
}
}
在上述代码中,我们利用Scanner类获取控制台输入,并使用ArrayList来存储Customer对象。通过迭代器进行遍历,并实现增删改查等操作。
3. 使用Map实现一个客户信息管理系统
我们也可以使用Map来存储客户信息,同时实现键值对的存储,方便以后的查找和遍历。在下面的示例代码中,我们依旧定义一个Customer类,其中包含姓名和电话两个字段。接着,我们使用HashMap来存储Customer对象,并利用键值对存储客户信息。
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CustomerManagementSystem {
private Map<String, Customer> customers = new HashMap<>();
public static void main(String[] args) {
CustomerManagementSystem system = new CustomerManagementSystem();
system.run();
}
private void run() {
Scanner scanner = new Scanner(System.in);
while (true) {
displayMenu();
int choice = scanner.nextInt();
switch (choice) {
case 1:
addCustomer(scanner);
break;
case 2:
deleteCustomer(scanner);
break;
case 3:
updateCustomer(scanner);
break;
case 4:
queryCustomer(scanner);
break;
case 5:
showCustomers();
break;
case 6:
System.out.println("Goodbye!");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice, please try again.");
break;
}
}
}
private void displayMenu() {
System.out.println("-------- Customer Management System --------");
System.out.println("1. Add customer");
System.out.println("2. Delete customer");
System.out.println("3. Update customer");
System.out.println("4. Query customer");
System.out.println("5. Show all customers");
System.out.println("6. Exit");
System.out.print("Please enter your choice: ");
}
private void addCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
System.out.print("Please enter customer phone: ");
String phone = scanner.next();
Customer customer = new Customer(name, phone);
customers.put(name, customer);
System.out.println("Add customer success!");
}
private void deleteCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
if (customers.containsKey(name)) {
customers.remove(name);
System.out.println("Delete customer success!");
} else {
System.out.println("Customer not found!");
}
}
private void updateCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
if (customers.containsKey(name)) {
System.out.print("Please enter new customer phone: ");
String phone = scanner.next();
Customer customer = new Customer(name, phone);
customers.put(name, customer);
System.out.println("Update customer success!");
} else {
System.out.println("Customer not found!");
}
}
private void queryCustomer(Scanner scanner) {
System.out.print("Please enter customer name: ");
String name = scanner.next();
if (customers.containsKey(name)) {
Customer customer = customers.get(name);
System.out.println(customer.toString());
} else {
System.out.println("Customer not found!");
}
}
private void showCustomers() {
for (String name : customers.keySet()) {
System.out.println(customers.get(name).toString());
}
}
}
class Customer {
private String name;
private String phone;
public Customer(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return String.format("Name: %s, Phone: %s", name, phone);
}
}
在上述代码中,我们使用HashMap来存储Customer对象,通过键值对实现客户信息的存储和查找。同样的,我们通过Scanner类获取控制台输入,并实现增删改查等操作。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:详解Java如何使用集合来实现一个客户信息管理系统 - Python技术站