本文实例为大家分享了java实现简单客户信息管理系统的具体代码,供大家参考,具体内容如下
全篇文章开源,源码供读者使用。这是一篇关于java的客户信息管理系统的文章,里面简单实现了数据库管理系统的基本功能,可以算是算笔者的学习笔记,也为大家学习提供便利。所以代码都是在一个包下完成的,所以没有使用导包的操作,省去了外卖project的申明,剩下的就写的文章里了。话不多说,看文章吧。
首先给大家看一下总的操作界面(别看简单,里面的还是有东西的),后面就附上实现源码、要求和注释
Customer类
下面是关于Customer类的编写要求:
* 用来封装客户的以下信息
* Sting name
* int age
* char gender
* Stirng phone
* String email
* 提供getset方法
* 构造器自行设定
public class Customer {
private String name;
private int age;
private String gender;
private String phone;
private String email;
public Customer() {
}
public Customer(String name,int age,String gender,String phone,String email) {
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
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;
}
}
CustomerList类
下面是关于CustomerList类的编写要求:
* CustomerList类的设计
* Customer[] 用来保存客户信息
* int total 用来保存当前存入客户数量
* 该类至少提供以下的构造器和方法
* public CustomerLIst(int totalCustomer);
* public boolean addCustomer(Customer customer);
* public boolean replaceCustomer(int index,Customer cust);
* public boolean deleteCustomer(int index);
* public Customer[] getallCustomer();
* public Customer getCustomer(int index);
* public int getToal();
public class CustomerList {
private static Customer customers[];
private static int total;
public CustomerList(int totalCustomer) {
customers = new Customer[totalCustomer];
}
// 添加客户
public boolean addCustomer(Customer customer) {
if(total >= customers.length) {
return false;
}
customers[total++] = customer;
return true;
}
// 修改指定位置的客户信息
public boolean replaceCustomer(int index,Customer cust) {
if(index < 0 || index >= total) {
return false;
}
customers[index] = cust;
return true;
}
// 删除指定位置的客户
public boolean deleteCustomer(int index) {
if(index < 0 || index >= total) {
return false;
}
for(int i = index;i < total - 1;i++) {
customers[i] = customers[i+1];
}
customers[total - 1] = null;
total--;
return true;
}
// 得到所有客户的信息
public static Customer[] getallCustomer() {
Customer[] custs = new Customer[total];
for(int i = 0;i < total;i++) {
custs[i] = customers[i];
}
return custs;
}
// 得到指定客户的信息
public Customer getCustomer(int index) {
if(index < 0 || index >= total) {
return null;
}
return customers[index];
}
// 得到Customers中所有客户的个数
public static int getTotal() {
return total;
}
}
CustomerView类
CustomerView类的编写
* 主模块:用于用户界面的展示、与用户交互
* CustomerList customerList = new CustomerList(10)
* 应含有的构造器和方法
* private void enterMainMenue()
* private void addNewCustomer()
* private void modifyCustomer()
* private void deleteCustomer()
* private void listAllCustomers()
* private static void main(String[] args)
public class CustomerView {
CustomerList customerList = new CustomerList(10);
public CustomerView() {
Customer customer = new Customer("王龙", 20, "Male", "18965391649", "465989777@qq.com");
customerList.addCustomer(customer);
}
// 用户主菜单
private void enterMainMenue() {
System.out.println("-----------------客户信息管理系统 --------------------");
System.out.println(" 1-添加客户");
System.out.println(" 2-修改客户");
System.out.println(" 3-删除客户");
System.out.println(" 4-客户列表");
System.out.println(" 5-退出");
System.out.println("请选择1-5:");
}
// 添加客户
private void addNewCustomer() {
// System.out.println("添加客户的操作");
System.out.println("-----------------添加客户-------------------------");
Scanner in = new Scanner(System.in);
System.out.print("姓名:");
String name = in.nextLine();
System.out.print("年龄:");
int age = in.nextInt();
System.out.print("性别:");
String gender = in.nextLine();
System.out.print("电话:");
String phone = in.nextLine();
System.out.print("邮箱:");
String email = in.nextLine();
// 将上述数据封装到Customer中
Customer customer = new Customer(name, age, gender, phone, email);
boolean isSuccess = customerList.addCustomer(customer);
if (isSuccess == true) {
System.out.println("---------------添加完成---------------------");
} else {
System.out.println("---------------客户目录已满,添加失败!---------------------");
}
}
// 修改客户
private void modifyCustomer() {
// System.out.println("修改客户的操作");
Scanner in = new Scanner(System.in);
Customer cust;
int num;
System.out.print("请选择待修改客户的编号(-1退出):");
while (true) {
num = in.nextInt();
if (num == -1) {
return;
}
cust = customerList.getCustomer(num - 1);
if (cust == null) {
System.out.print("无法找到指定客户,请选择待修改客户的编号(-1退出):");
} else {
break;
}
}
// 开始修改客户信息
System.out.print("姓名(" + cust.getName() + "):");
String name = in.nextLine();
System.out.print("性别(" + cust.getGender() + "):");
String gender = in.nextLine();
System.out.print("年龄(" + cust.getAge() + "):");
int age = in.nextInt();
System.out.print("电话(" + cust.getPhone() + "):");
String phone = in.nextLine();
System.out.print("邮箱(" + cust.getEmail() + "):");
String email = in.nextLine();
Customer cust2 = new Customer(name, age, gender, phone, email);
boolean isreplace = customerList.replaceCustomer(num - 1, cust2);
if (isreplace == true) {
System.out.println("---------------修改完成---------------------");
} else {
System.out.println("---------------修改失败---------------------");
}
}
// 删除用户
private void deleteCustomer() {
// System.out.println("删除客户的操作");
Scanner in = new Scanner(System.in);
int num;
System.out.println("------------------刪除客戶-----------------------");
while (true) {
System.out.println("输入要删除的客户的序号(-1退出):");
num = in.nextInt();
if (num == -1) {
return;
}
Customer customer = customerList.getCustomer(num - 1);
if (customer == null) {
System.out.println("------------------刪除失败!-----------------------");
return;
}
// 执行删除操作
System.out.print("是否确认删除(y/n):");
char isdelete = in.nextLine().charAt(0);
if (isdelete == 'y') {
boolean is = customerList.deleteCustomer(num - 1);
if(is) {
System.out.println("------------------刪除成功-----------------------");
}else {
System.out.println("------------------刪除失败-----------------------");
}
}else {
break;
}
}
}
// 列出所有客户信息
private void listAllCustomers() {
System.out.println("---------------客户列表--------------------");
int total = CustomerList.getTotal();
if (total == 0) {
System.out.println("没有客户记录!");
} else {
System.out.println("编号\t姓名\t性别\t电话\t\t邮箱\t");
Customer custs[] = CustomerList.getallCustomer();
for (int i = 0; i < custs.length; i++) {
Customer cust = custs[i];
System.out.println((i + 1) + "\t" + cust.getName() + "\t" + cust.getGender() + "\t" + cust.getPhone()
+ "\t" + cust.getEmail());
}
}
System.out.println("---------------客户列表加载完成--------------------");
}
// 主函数
public static void main(String[] args) {
CustomerView view = new CustomerView();
Scanner in = new Scanner(System.in);
boolean isFlag = true;
while (isFlag) {
view.enterMainMenue();
char menu = in.nextLine().charAt(0);
switch (menu) {
case '1':
view.addNewCustomer();
break;
case '2':
view.modifyCustomer();
break;
case '3':
view.deleteCustomer();
break;
case '4':
view.listAllCustomers();
break;
case '5':
System.out.print("确认是否退出(y/n):");
char flag = in.nextLine().charAt(0);
if (flag == 'y') {
isFlag = false;
}
break;
}
// isFlag = false;
}
in.close();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。