尚硅谷 java基础第二个项目之客户关系管理系统。

做了一些完善,增加性别,电话,邮箱有效性验证。其中电话和邮箱验证直接“饮用”了网友的果汁。

在此感谢各位原著大佬们的分享。

具体结构如下:

分四个包:bean,service,ui,util

分别如下:

各文件如下

Customer.java:

/*** */
package com.qixian.q2.bean;/*** @Description 封装客户信息* @author * @version* @date 2021年2月28日上午10:18:52**/
public class Customer {private String name;//客户姓名private char gender;//性别private int age;//年龄private String phone;//电话号码private String email;//邮箱public Customer() {}/*** @param name* @param gender* @param age* @param phone* @param email*/public Customer(String name, char gender, int age, String phone, String email) {this.name = name;this.gender = gender;this.age = age;this.phone = phone;this.email = email;}/*** @return the name*/public String getName() {return name;}/*** @param name the name to set*/public void setName(String name) {this.name = name;}/*** @return the gender*/public char getGender() {return gender;}/*** @param gender the gender to set*/public void setGender(char gender) {this.gender = gender;}/*** @return the age*/public int getAge() {return age;}/*** @param age the age to set*/public void setAge(int age) {this.age = age;}/*** @return the phone*/public String getPhone() {return phone;}/*** @param phone the phone to set*/public void setPhone(String phone) {this.phone = phone;}/*** @return the email*/public String getEmail() {return email;}/*** @param email the email to set*/public void setEmail(String email) {this.email = email;}}

CustomerList.java

/*** */
package com.qixian.q2.service;import com.qixian.q2.bean.Customer;/*** @Description* @author * @version* @date 2021年2月28日上午10:20:27**/
public class CustomerList {private Customer[] customers;//用来保存客户对象的数组private int total;//记录已保存客户对象的数量/*** 初始化customers数组的构造器* @param totalCustomer*/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>=total||index<0) {return false;}customers[index]=cust;return true;}//删除指定位置的客户信息public boolean deleteCustomer(int index) {if(index>=total||index<0) {return false;}for(int i=index;i<total-1;i++) {customers[i]=customers[i+1];}customers[--total]=null;return true;}//查看所有客户信息public Customer[] getAllCustomers() {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>=total||index<0) {return null;}return customers[index];}//获取存储的客户数量public int getTotal() {return total;}
}

CustomerView.java

/*** */
package com.qixian.q2.ui;import com.qixian.q2.bean.Customer;
import com.qixian.q2.service.CustomerList;
import com.qixian.q2.util.CMUtility;
import com.qixian.q2.util.PhoneFormatCheckUtils;/*** @Description 主模块,负责菜单的显示和处理用户操作* @author * @version* @date 2021年2月28日上午10:21:35**/
public class CustomerView {private CustomerList customerlist=new CustomerList(10);public CustomerView() {Customer customer=new Customer("王小二",'男',18,"19978945632","99999@qq.com");customerlist.addCustomer(customer);}private void enterMainMenu() {boolean loopFlag=true;do {System.out.println("-----------------客户信息管理软件-----------------");System.out.println("                 1 添加客户");System.out.println("                 2 修改客户");System.out.println("                 3 删除客户");System.out.println("                 4 客户列表");System.out.println("                 5 退   出\n");System.out.print("               请选择(1-5):");char menu=CMUtility.readMenuSelection();System.out.println();switch(menu) {case '1':addNewCustomer();break;case '2':modifyCustomer();break;case '3':deleteCustomer();break;case '4':listAllCustomers();break;case '5':System.out.print("确认是否退出(Y/N):");char isExit=CMUtility.readConfirmSelection();if(isExit=='Y') {loopFlag=false;}//break;}//loopFlag=false;}while(loopFlag);}private void addNewCustomer() {String tempemail,email,addphone,phone;System.out.println("--------------------添加客户----------------------");System.out.print("姓名:");String name=CMUtility.readString(10);System.out.print("性别:");char gender=CMUtility.readChar();System.out.print("年龄:");int age=CMUtility.readInt();//电话输入,判断合法后赋值。for(;;) {System.out.print("电话:");addphone=CMUtility.readString(13);boolean isphone=PhoneFormatCheckUtils.isPhoneLegal(addphone);if(isphone) {phone=addphone;break;}else {System.out.println("输入的电话(或手机)格式不准确,请重新输入!");}}//邮箱输入,判断合法后赋值。for(;;) {System.out.print("邮箱:");tempemail=CMUtility.readString(20);boolean isEmail=CMUtility.isEmail(tempemail);if(isEmail) {email=tempemail;break;}else {System.out.println("输入的邮箱格式不准确,请重新输入!");}}Customer customer=new Customer(name,gender,age,phone,email);boolean isSuccess=customerlist.addCustomer(customer);if(isSuccess) {System.out.println("客户信息添加成功!");}else {System.out.println("客户目录已满,添加失败!");}}private void modifyCustomer() {System.out.println("--------------------修改客户信息----------------------");Customer cust;int innum;String email,mdemail,addphone,phone;for(;;){System.out.print("请选择待修改客户编号(-1退出):");innum=CMUtility.readInt();if(innum==-1) {return;   }cust=customerlist.getCustomer(innum-1);if(cust==null) {System.out.print("无法找到指定客户!");}else {break;}}System.out.print("姓名("+cust.getName()+"):");String name=CMUtility.readString(10, cust.getName());System.out.print("性别("+cust.getGender()+"):");char gender=CMUtility.readChar(cust.getGender());System.out.print("年龄("+cust.getAge()+"):");int age=CMUtility.readInt(cust.getAge());//        System.out.print("电话("+cust.getPhone()+"):");
//      phone=CMUtility.readString(13, cust.getPhone());//电话输入,判断合法后赋值。for(;;) {System.out.print("电话("+cust.getPhone()+"):");addphone=CMUtility.readString(13,cust.getPhone());boolean isphone=PhoneFormatCheckUtils.isPhoneLegal(addphone);if(isphone) {phone=addphone;break;}else {System.out.println("输入的电话(或手机)格式不准确,请重新输入!");}}for(;;) {System.out.print("邮箱("+cust.getEmail()+"):");mdemail=CMUtility.readString(20, cust.getEmail());boolean isEmail=CMUtility.isEmail(mdemail);if(isEmail) {email=mdemail;break;}else {System.out.println("输入的邮箱格式不准确,请重新输入!");}}Customer newCust=new Customer(name,gender,age,phone,email);boolean isReplaced=customerlist.replaceCustomer(innum-1, newCust);if(isReplaced) {System.out.println("客户信息修改成功!");}else {System.out.println("客户信息修改失败!");}}private void deleteCustomer() {System.out.println("--------------------删除客户信息----------------------");Customer custd;int innum;for(;;){System.out.print("请选择待删除客户编号(-1退出):");innum=CMUtility.readInt();if(innum==-1) {return;}custd=customerlist.getCustomer(innum-1);if(custd==null) {System.out.print("无法找到指定客户!");}else {break;}}System.out.print("是否确认删除?(Y/N):");char isDelete=CMUtility.readConfirmSelection();if(isDelete=='Y') {boolean deleteSuccess=customerlist.deleteCustomer(innum-1);if(deleteSuccess) {System.out.print("客户信息删除成功!\n");}else {System.out.print("客户信息删除失败!\n");}}else {return;}}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年龄\t\t电话\t\t\t\t邮箱");Customer[] custs=customerlist.getAllCustomers();for(int i=0;i<custs.length;i++) {Customer cust=custs[i];System.out.println((i+1)+"\t\t"+cust.getName()+"\t\t"+cust.getGender()+"\t\t"+cust.getAge()+"\t\t"+cust.getPhone()+"\t\t"+cust.getEmail());}}System.out.println("");System.out.println("------------------客户列表完成------------------");}public static void main(String[] args) {CustomerView view=new CustomerView();view.enterMainMenu();}
}

CMUtility.java

/*** */
package com.qixian.q2.util;import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;/*** @Description 工具类* @author * @version* @date 2021年2月28日上午10:23:44**/
public class CMUtility {private static Scanner scanner=new Scanner(System.in);public static char readMenuSelection() {char c;for(;;) {String str=readKeyBoard(1,false);c=str.charAt(0);if(c!='1' && c!='2' && c!='3' && c!='4'&& c!='5') {System.out.print("选择错误,请重新输入: ");}else break;}return c;}public static char readChar() {char sex;String str;//String str=readKeyBoard(1,false);//return str.charAt(0);for(;;) {str=readKeyBoard(1,false);if(str.equals("男")) {sex='男';break;}else if(str.equals("女")){sex='女';break;}else {System.out.println("性别输入错误,请重新输入(男或女):");}//return str.charAt(0);//return sex;}return sex;}public static char readChar(char defaultValue) {String str;char sex;//return (str.length()==0)?defaultValue:str.charAt(0);for(;;) {str=readKeyBoard(1,true);if(str.length()==0) {sex=defaultValue;break;}if(str.equals("男")) {sex='男';break;}else if(str.equals("女")){sex='女';break;}else {System.out.println("性别输入错误,请重新输入(男或女):");}}return sex;}/** 用于确认选择的输入。* 该方法从键盘读取‘Y’或‘N’,并将其作为方法的返回值。*/public static char readConfirmSelection() {char c;for(;;) {String str=readKeyBoard(1,false).toUpperCase();c=str.charAt(0);if(c=='Y'||c=='N') {break;}else {System.out.print("选择错误,请重新输入: ");}}return c;}public static int readInt() {int n;for(;;) {String str=readKeyBoard(2,false);try {n=Integer.parseInt(str);break;}catch(NumberFormatException e){System.out.print("数字输入错误,请重新输入: ");}}return n;}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;}/** * 该方法从键盘读取一个不超过limit长度的字符串,并将其作为方法的返回值。*/public static String readString(int limit) {return readKeyBoard(limit,false);}public static String readString(int limit,String defaultValue) {String str=readKeyBoard(limit,true);return str.equals("")?defaultValue:str;}//判断邮箱格式是否正确public static boolean isEmail(String string) {if (string == null)return false;String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";Pattern p;Matcher m;p = Pattern.compile(regEx1);m = p.matcher(string);if (m.matches())return true;elsereturn false;}private static String readKeyBoard(int limit,boolean blankReturn) {String line="";while(scanner.hasNextLine()) {line=scanner.nextLine();if(line.length()==0) {if(blankReturn) return line;else continue;}if(line.length()<1||line.length()>limit) {System.out.print("输入长度(不大于"+limit+")");continue;}break;}return line;}
}

PhoneFormatCheckUtils.java

/*** */
package com.qixian.q2.util;/*** @Description* @author 某大佬* @version* @date 2021年2月28日下午6:30:26**/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;public class PhoneFormatCheckUtils {/*** 大陆号码或香港号码均可*/public static boolean isPhoneLegal(String str) throws PatternSyntaxException {return isChinaPhoneLegal(str) || isHKPhoneLegal(str);}/*** 大陆手机号码11位数,匹配格式:前三位固定格式+后8位任意数* 此方法中前三位格式有:* 13+任意数* 145,147,149* 15+除4的任意数(不要写^4,这样的话字母也会被认为是正确的)* 166* 17+3,5,6,7,8* 18+任意数* 198,199*/public static boolean isChinaPhoneLegal(String str) throws PatternSyntaxException {// ^ 匹配输入字符串开始的位置// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d// $ 匹配输入字符串结尾的位置String regExp = "^((13[0-9])|(14[5,7,9])|(15[0-3,5-9])|(166)|(17[3,5,6,7,8])" +"|(18[0-9])|(19[8,9]))\\d{8}$";Pattern p = Pattern.compile(regExp);Matcher m = p.matcher(str);return m.matches();}/*** 香港手机号码8位数,5|6|8|9开头+7位任意数*/public static boolean isHKPhoneLegal(String str) throws PatternSyntaxException {// ^ 匹配输入字符串开始的位置// \d 匹配一个或多个数字,其中 \ 要转义,所以是 \\d// $ 匹配输入字符串结尾的位置String regExp = "^(5|6|8|9)\\d{7}$";Pattern p = Pattern.compile(regExp);Matcher m = p.matcher(str);return m.matches();}}

部分运行结果:

尚硅谷 java基础第二个项目之客户关系管理系统相关推荐

  1. Java项目:CRM客户关系管理系统(java+Springboot+maven+mysql)

    源码获取:博客首页 "资源" 里下载! Springboot项目CRM客户关系管理系统: 系统实现了CRM客户关系系统的基本功能,主要有看板(当月参与的业务机会.当月转化情况.将要 ...

  2. 项目-企业客户关系管理系统(登录+首页操作菜单)

    第2个项目–企业客户关系管理系统 创建我们的项目cms_system,直接使用第1个后台项目的模板,将第1个后台项目jsp文件夹直接拷贝到项目的WEB-INF中,还有css,images等文件夹拷贝到 ...

  3. java计算机毕业设计-移动公司crm客户关系管理系统开发与实现-源程序+mysql+系统+lw文档+远程调试

    java计算机毕业设计-移动公司crm客户关系管理系统开发与实现-源程序+mysql+系统+lw文档+远程调试 java计算机毕业设计-移动公司crm客户关系管理系统开发与实现-源程序+mysql+系 ...

  4. 尚硅谷java基础学习笔记

    小郑 Java基础 常用DOS命令 dir:列出当前目录下的文件以及文件夹 md:创建目录 rd: 删除目录 cd:进入指定的目录 cd- : 退回到上一级目录 cd\ : 退回到根目录 del : ...

  5. 尚硅谷Java基础学习--常用类部分例题解答(仅使用String类方法)

    以下为不借助StringBuffer等类的方法,直接使用String类方法及算法实现: No.1 public class Test1010 {public static void main(Stri ...

  6. 开源项目-CRM客户关系管理系统

    哈喽,大家好,今天给大家带来一个开源系统-CRM客户关系管理系统 主要功能包括客户管理,客户流失,销售机会,客户关怀等模块 系统开发环境以及版本 操作系统: Windows_7 集成开发工具: Ecl ...

  7. 尚硅谷Java、HTML5前端、全栈式开发视频

    Java基础阶段: 一.20天横扫Java基础(课堂实录) https://pan.baidu.com/s/1htTzZRQ 二.尚硅谷Java基础实战--Bank项目 http://pan.baid ...

  8. oracle客户关系系统,Java swing Oracle实现的客户关系管理系统项目源码附带详细设计文档...

    <p style="font-family:"font-size:16px;text-indent:2em;color:#666666;background-color:#F ...

  9. 基于java的CRM客户关系管理系统的设计与实现

    本科毕业设计(论文) 题 目: 基于java的CRM客户关系管理系统的设计与实现 专题题目: 说 明 请按以下顺序编排: 封面 任务书 开题报告 中外文摘要及关键词 目录 正文 附录(可选) 参考文献 ...

最新文章

  1. 【跟我一起学Unity3D】做一个2D的90坦克大战之AI系统
  2. golang 防知乎 中文验证码 源码
  3. KRKR简单使用实例开发
  4. 在java中图片随机播放_如何在Java中随机播放列表
  5. 动态链接库.so和静态链接库.a的区别
  6. mycat mysql ha 方案_7、基于 HA 机制的 Mycat 高可用--mycat
  7. 微软著名程序员、歌手、NBA球队老板保罗·艾伦逝世,盖茨、库克等大佬发文悼念...
  8. Python数据挖掘与分析常用库官方文档
  9. python模拟浏览器代码_python 模拟浏览器
  10. 必知必会JVM垃圾回收——对象搜索算法与回收算法
  11. java设计模式2--工厂模式
  12. Ubuntu下自定义调整CPU工作频率(用于省电或提高性能都好用)
  13. MySQL的回表查询与索引覆盖查询
  14. HDFS- 架构图详细解析
  15. 这个神器5秒20个爆款标题,关键还免费,做自媒体不会写标题?
  16. 【Python编写漏洞测试工具入门】
  17. spring boot中的banner制作
  18. 如何把多个pdf文件合并成一个pdf
  19. Android系统分区备份与还原
  20. 元气骑士+蒲公英联机平台联机教程

热门文章

  1. shell脚本基础知识-什么是shell、环境变量
  2. 【IT运维小知识】安全组是什么意思?
  3. IAP Cannot connect to iTunes Store
  4. 刷题之旅第33站,CTFshow web12
  5. 【安全狗高危安全通告】OpenSSL存在远程代码执行漏洞和拒绝服务漏洞
  6. 单片机红外通信c语言,用51单片机实现红外通讯源码
  7. 定理在数学中的简写形式_数学最奇葩的九个定理是什么
  8. 支付宝/钉钉小程序实现蓝牙打印
  9. 科学计算模块Numpy-初级 (2)
  10. 二分网络上的电影推荐