如何利用java实现一个客户信息管理系统
短信预约 -IT技能 免费直播动态提醒
这篇文章将为大家详细讲解有关如何利用java实现一个客户信息管理系统,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
类图:
Customer类:
public class Customer { private String name; private String sex; private int age; private String phone; private String email; public Customer(){}; public Customer(String name,String sex,int age,String phone,String email){ this.name=name; this.sex=sex; this.age=age; this.phone=phone; this.email=email; } public String getName(){ return this.name; } public void setName(String name){ this.name=name; } public String getSex(){ return this.sex; } public void setSex(String sex){ this.sex=sex; } public String getPhone(){ return phone; } public void setPhone(String phone){ this.phone=phone; } public int getAge(){ return this.age; } public void setAge(int age){ this.age=age; } public String getEmail(){ return this.email; } public void setEmail(String email){ this.email=email; }}
CustomerList 类:
public class CustomerList { private Customer [] customers; private static int total = 0; public CustomerList(int totalCustmoers){ customers = new Customer[totalCustmoers]; } public boolean addCustomer(Customer customer){ if(customer!=null&&total<customers.length) {customers[total]=customer; total++; return true;} else { return false;} } public boolean replaceCustomer(int index,Customer cust){ if(index>=0 && index <total ) { customers[index]=cust;return true; } else { return false; } } public boolean deleteCustomer(int index){ if(index<customers.length) { for(int i=index;i<total-1;i++) { customers[i]=customers[i+1]; } customers[total-1]=null; total--; return true; } else { return false; } } public Customer getCustomer(int index){ if(index>=0 && index<total) {return customers[index];} else { return null; } } public Customer[] getAllCustomers(){ Customer [] cust = new Customer[total]; for(int i=0;i<total;i++){ cust[i]=customers[i]; } return cust; } public int getTotal(){ return total; }}
CustomerVIew类:
public class CustomerView { private CustomerList customerList = new CustomerList(10); public void enterMainMenu(){ while(true) {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("-----------------------------------------------------"); Scanner input = new Scanner(System.in); int op = input.nextInt(); switch(op) { case 1 :this.addNewCustomer();break; case 2 :this.modifyCustomer();break; case 3 :this.deleteCustomer();break; case 4 :this.listAllCustomers();break; case 5 :System.exit(0);break; default: System.out.println("重新选择功能");break; } } } private void addNewCustomer(){ System.out.println("-------------------添加客户-------------------"); Scanner input = new Scanner(System.in); System.out.println("姓名:"); String name = input.next(); System.out.println("性别:"); String sex=input.next(); System.out.println("年龄:"); int age = input.nextInt(); System.out.println("电话号码:"); String phone = input.next(); System.out.println("电子邮箱:"); String email = input.next(); Customer person = new Customer(name,sex,age,phone,email); Boolean flag=customerList.addCustomer(person); if(flag) { System.out.println("-------------------添加成功-------------------"); } else { System.out.println("-------------------添加失败-------------------"); } } private void modifyCustomer(){ System.out.println("-------------------修改客户-------------------"); System.out.println("要修改的客户id:"); Scanner input = new Scanner(System.in); int number = input.nextInt(); Customer customer = customerList.getCustomer(number); System.out.println("姓名:"+customer.getName()); String name = CMUtility.readString(5,customer.getName()); System.out.println("性别:"+customer.getSex()); String sex = CMUtility.readString(5,customer.getSex()); System.out.print("年龄(" + customer.getAge() + "):"); int age = CMUtility.readInt(customer.getAge()); System.out.print("电话(" + customer.getPhone() + "):"); String phone = CMUtility.readString(13, customer.getPhone()); System.out.print("邮箱(" + customer.getEmail() + "):"); String email = CMUtility.readString(15, customer.getEmail()); customer = new Customer(name,sex,age,phone,email); Boolean flag = customerList.replaceCustomer(number,customer); if(flag) { System.out.println("-------------------修改成功-------------------"); } else { System.out.println("-------------------修改失败-------------------"); } } private void deleteCustomer(){ System.out.println("-------------------删除客户-------------------"); System.out.println("要删除的客户id:"); Scanner input = new Scanner(System.in); int number = input.nextInt(); while(true){ System.out.println("退出(-1)"); if(number>=0 && number<customerList.getTotal()) { System.out.println("找到指定客户"); } else if(number==-1) { return; } else { System.out.println("输入错误");break; }} System.out.println("是否确认删除该客户?Y/N"); String ch = input.next(); char o = ch.charAt(0); if(o=='Y') { boolean flag = customerList.deleteCustomer(number); if(flag){ System.out.println("-------------------删除成功-------------------"); } else { System.out.println("-------------------删除失败-------------------"); } } else{ return; }}private void listAllCustomers(){ Customer [] customer=customerList.getAllCustomers(); if(customer.length==0) { System.out.println("没有任何客户数据"); } else{ for(int i=0;i< customer.length;i++) { System.out.println("姓名:"+customer[i].getName()+"\t"+"性别"+customer[i].getSex()+"\t"+"年龄:"+customer[i].getAge()+"\t"+"电话号码:"+customer[i].getPhone()+"\t"+"电子邮箱:"+customer[i].getEmail()+"\t"); }}} public static void main(String[] args) { CustomerView co = new CustomerView(); co.enterMainMenu(); }}
工具类:
public class CMUtility { private static Scanner scanner = new Scanner(System.in); public static String readString(int limit) { return readKeyBoard(limit, false); } public static int readInt(int defaultValue) { int n; for (; ; ) { String str = readKeyBoard(2, true); if (str.equals("")) { return defaultValue; } try { n = Integer.parseInt(str); break; } catch (NumberFormatException e) { System.out.print("数字输入错误,请重新输入:"); } } return n; }
关于“如何利用java实现一个客户信息管理系统”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341