java如何实现简单的客户信息管理系统
这篇文章主要介绍“java如何实现简单的客户信息管理系统”,在日常操作中,相信很多人在java如何实现简单的客户信息管理系统问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java如何实现简单的客户信息管理系统”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
首先给大家看一下总的操作界面(别看简单,里面的还是有东西的),后面就附上实现源码、要求和注释
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(); }}
到此,关于“java如何实现简单的客户信息管理系统”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341