用Java开发小型客户管理系统可掌握面向对象与CRUD操作,1. 设计Customer类封装客户信息,2. 通过CustomerService管理客户增删改查,3. 使用Scanner实现控制台交互界面,4. 主函数中循环显示菜单并调用对应方法,5. 后续可扩展文件存储或图形界面。

做一个小型客户管理系统用Java是很好的入门项目,既能练手又能理解基础的软件结构。核心思路是用面向对象的方式管理客户数据,搭配简单的界面或控制台交互。下面分几个部分说明怎么做。
小型客户管理系统一般包含以下基本功能:
功能不用多,先把增删改查(CRUD)做通。
用一个Java类来表示客户,封装属性和行为。
立即学习“Java免费学习笔记(深入)”;
public class Customer {
private int id;
private String name;
private String phone;
private String email;
private String address;
public Customer(int id, String name, String phone, String email, String address) {
this.id = id;
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
}
// getter 和 setter 方法
public int getId() { return id; }
public void setId(int id) { this.id = id; }
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; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
@Override
public String toString() {
return "ID: " + id + ", 姓名: " + name + ", 电话: " + phone +
", 邮箱: " + email + ", 地址: " + address;
}
}
写一个服务类来管理客户列表,使用ArrayList存储数据。
import java.util.ArrayList;
import java.util.List;
public class CustomerService {
private List<Customer> customers;
private int nextId = 1;
public CustomerService() {
customers = new ArrayList<>();
}
public void addCustomer(String name, String phone, String email, String address) {
Customer customer = new Customer(nextId++, name, phone, email, address);
customers.add(customer);
System.out.println("客户添加成功:" + customer);
}
public void listCustomers() {
if (customers.isEmpty()) {
System.out.println("暂无客户信息。");
} else {
for (Customer c : customers) {
System.out.println(c);
}
}
}
public Customer findCustomerByName(String name) {
for (Customer c : customers) {
if (c.getName().equalsIgnoreCase(name)) {
return c;
}
}
return null;
}
public boolean updateCustomer(int id, String name, String phone, String email, String address) {
for (Customer c : customers) {
if (c.getId() == id) {
c.setName(name);
c.setPhone(phone);
c.setEmail(email);
c.setAddress(address);
System.out.println("客户信息已更新:" + c);
return true;
}
}
return false;
}
public boolean deleteCustomer(int id) {
Customer toRemove = null;
for (Customer c : customers) {
if (c.getId() == id) {
toRemove = c;
break;
}
}
if (toRemove != null) {
customers.remove(toRemove);
System.out.println("客户已删除,ID:" + id);
return true;
}
System.out.println("未找到ID为 " + id + " 的客户。");
return false;
}
}
用Scanner接收用户输入,调用服务类的方法。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
CustomerService service = new CustomerService();
Scanner scanner = new Scanner(System.in);
boolean running = true;
System.out.println("=== 小型客户管理系统 ===");
while (running) {
System.out.println("\n1. 添加客户");
System.out.println("2. 查看所有客户");
System.out.println("3. 查询客户(按姓名)");
System.out.println("4. 修改客户");
System.out.println("5. 删除客户");
System.out.println("6. 退出");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
scanner.nextLine(); // 消费换行符
switch (choice) {
case 1:
System.out.print("姓名:"); String name = scanner.nextLine();
System.out.print("电话:"); String phone = scanner.nextLine();
System.out.print("邮箱:"); String email = scanner.nextLine();
System.out.print("地址:"); String address = scanner.nextLine();
service.addCustomer(name, phone, email, address);
break;
case 2:
service.listCustomers();
break;
case 3:
System.out.print("输入客户姓名:");
String searchName = scanner.nextLine();
Customer found = service.findCustomerByName(searchName);
if (found != null) {
System.out.println("找到客户:" + found);
} else {
System.out.println("未找到该客户。");
}
break;
case 4:
System.out.print("输入要修改的客户ID:");
int id = scanner.nextInt();
scanner.nextLine();
System.out.print("新姓名:"); String n = scanner.nextLine();
System.out.print("新电话:"); String p = scanner.nextLine();
System.out.print("新邮箱:"); String e = scanner.nextLine();
System.out.print("新地址:"); String a = scanner.nextLine();
service.updateCustomer(id, n, p, e, a);
break;
case 5:
System.out.print("输入要删除的客户ID:");
int delId = scanner.nextInt();
service.deleteCustomer(delId);
break;
case 6:
System.out.println("系统退出。");
running = false;
break;
default:
System.out.println("无效选择,请重试。");
}
}
scanner.close();
}
}
基本上就这些。运行程序后,你可以在控制台进行客户管理操作。
不复杂但容易忽略的是细节:比如ID自增、输入校验、空值处理。后续可以加文件保存(用ObjectOutputStream)、Swing图形界面,或者换成数据库(如SQLite)。
以上就是如何使用Java制作小型客户管理系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号