CMUtility 工具类
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; else return 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; } }
Customer 客户信息
public class Customer { private String name; //客户姓名 private char gender;//性别 private int age;//年龄 private String phone; //电子号码 private String email;//电子邮箱 public String getName() { return name; } public void setName(String name) { this.name = name; } public char getGender() { return gender; } public void setGender(char gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } 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 Customer() { } 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; } }
CustomerList 类的设计
Customer[] customers; //用于保存客户对象的数组 int total; //记录已保存客户对象的数量 //用来初始化customers数组的构造器 //totaCustomer指定数组的长度s public CustomerList(int totalCustomer){ customers=new Customer[totalCustomer]; } /** * //将指定的客户添加到数组中 * @param customer * @return ture:添加成功 false:添加失败 */ public boolean addCustomer(Customer customer){ //判断我的total是否添加满了,如果满了则返回false,添加失败 if (total>=customers.length) { return false; } //每调用一场这个方法则total客户的个数增加1.最后reture true;表示返回成功。 customers[total++]=customer; return true; } /** * 修改指定索引位置的客户信息 * @param index * @param cust * @return ture:修改成功 false:修改失败 */ public boolean replaceCustomer(int index,Customer cust){ //判断成功失败与index有关,判断index的值是否小于0或者大于了我客户的个数,如果超过了则flase. if (index<0||index>=total) { return false; } //如果没有执行上面的则,代表修改的值是正确的,把修改的值传给customers[index]里面的值,还会ture。 //从内存解析来看,等于是每修改一个等于把原有的数组值回收,重新new出一个对象在给customers[index]赋值 customers[index]=cust; return true; } /** * 删除指定索引位置上的客户 * @param index * @return ture:删除成功 false:删除失败 */ public boolean deleteCustomer(int index){ // 如果我的数组长度是10,但我值存了5个客户,但是能删的范围只有0-4,所有只能删除0-4里面的,所以不能大于 if (index<0||index>=total) { return false; }
//要删除指定的元素,要删index元素,但是要把前面的元素一个个往前移动,所以就要用到for循环 //从index开始,如果total不减1他就是最后一个数,但是customers[i+1]是要从最后一个元素往前移动 // total不减一的话我的customers[i+1]就会指向数组中的最后一个数组的在后面一个数组,但是他为空,所以不能用total for (int i = index; i <total-1 ; i++) { //后一个数赋给前一个数 customers[i]=customers[i+1]; } //最后有数据的元素要指空,与就是说要把他删除,我们把里面的元素删除了,那么我的total就要减一,要不然索引编号就会变成2 //最后返回ture customers[--total]=null; return true; } /** * 获取客户所有的信息 * @return */ public Customer[] getAllCustomers(){ //不能直接return customers,现在里面的数组还是为空的,里面没有赋值 //要 new 一个Customer[] 来获取他里面的值,然后如果我们添加的只有total个,就展示多少个,而不能直接return. Customer[] cust=new Customer[total]; for (int i = 0; i <total ; i++) { //一开始里面都是空的元素需要给每个元素赋值 // ,new 一个对象的地址值然后把cust的地址值指向了原有的已经在列表里面的数组值 //最后返回接收来的值 cust[i]=customers[i]; } return cust; } /** * 获取指定索引位置上的客户 * @param index * @return 如果找到则返回,如果没有找到则返回,null */ public Customer getCustomer(int index){ //要判断我的index是否合格,如上所述//判断成功失败与index有关, // 判断index的值是否小于0或者大于了我客户的个数,如果超过了则flase. if (index<0||index>=total) { return null; } //如果正确则显示我的索引的值 return customers[index]; } /** * 获取客户的数量 * @return */ public int getTotal(){ return total; } }
用户界面:CustomerView
public class CustomerView {
private CustomerList customerList= new CustomerList(10); public CustomerView(){ //添加客户就要用Customer来添加 Customer customer = new Customer("张三",'男',18,"1024468356","19974218872@qq.com"); //添加客户方法addCustomer customerList.addCustomer(customer); } // public void enterMainMenu(){} // private void addNewCustomer(){} // private void modeifyCustomer(){} // private void deleteCustomer(){} // private void listAllCustomers(){} //显示信息管理的界面 public void enterMainMenu(){ //先显示页面,没有确定的循环次数。所以要无限循环 boolean isFlag=true; while (isFlag){ System.out.println("\n-------------客户信息管理软件----------------"); 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):"); //先运行工具类的readMenuSelection判断是否输入的是1-5的数字如果不是则显示错误重新输入 char menu = CMUtility.readMenuSelection(); //得到我的工具类中的方法menu就要判断我输入的是多少然后用switch判断进入对应的程序 switch (menu){ //输入1时是添加客户相应的就要调用下面的方法addNewCustomer添加客户 case '1': addNewCustomer(); break; //如上输入几调用上面方法 case '2': modeifyCustomer(); break; //如上输入几调用上面方法 case '3': deleteCustomer(); break; //如上输入几调用上面方法 case '4': listAllCustomers(); break; //没有写退出的方法所以自己判断是否退出,当输入5时进入此方法 case '5': System.out.println("确认是否退出(Y/N)"); //调用工具类的方法readConfirmSelection来判断客户输入的是否是正确的 char out = CMUtility.readConfirmSelection(); //判断我的out是否是Y或者是N,因为是char类型的不是String的不能使用.eqs。 if (out=='Y') { //如果是想退出则把我的boolean =ture 变成false isFlag=false; }//不用写N了,因为输入的是N代表着程序没有进入我的if判断里面这就代表着我的swithch结束了重新进入我的while的无限循环里面 } //要想结束相互就等执行第五种的方法结束,就要调用工具类中的 //isFlag=false; } } //添加客户 private void addNewCustomer(){ System.out.println("---------------添加客户-------------------"); System.out.print("姓名:"); String name = CMUtility.readString(10);//调用工具类的方法readString这个方法就是你输入的姓名限制的长度,如不懂进入方法查看 System.out.print("性别:"); char gender = CMUtility.readChar(); System.out.print("年龄:"); int age = CMUtility.readInt(); System.out.print("电话:");//readString是方法中判断他输入数字的长度 String phone = CMUtility.readString(13); System.out.print("邮箱:"); String email = CMUtility.readString(30); //要想把上面的存储到数组中,数组是Customer类型的,所以就要把上面的东西封装到对象当中 Customer customer = new Customer(name, gender, age, phone, email); //在调用CustomerList中的addCustomer,这个方法就是添加客户的 boolean isSuccess = customerList.addCustomer(customer);//但是呢,添加的时候有可能还会失败,就要判断它是否成功 if (isSuccess) { System.out.println("---------------------添加完成-------------"); } else { System.out.println("---------------------客户目录已满,添加失败-------------"); } } //修改客户 private void modeifyCustomer(){ System.out.println("-----------------修改客户-----------------"); //没有提示你执行多少次所以要用for(;;) Customer cust; int nbumer; for (;;){ System.out.print("请选择待修改客户的编号(-1退出):"); nbumer = CMUtility.readInt();//运用工具类来接收它是多少 if (nbumer==-1) { return; } cust = customerList.getCustomer(nbumer - 1);//number要-1因为我的用户输入的是1但是我的索引是从0开始的所以要number-1 //运用CustomerList,CustomerList是获取指定索引位置上的客户,如果这个是bull空 if (cust==null) { System.out.println("无法找到指定的客户"); }else {//找到了相应的编号客户 break; } } //程序跑到这里来了就代表这是执行else然后修改 System.out.print("姓名("+cust.getName()+"):"); String name = CMUtility.readString(10, cust.getName());//从键盘读取如果还是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()+"):"); String phone = CMUtility.readString(13, cust.getPhone()); System.out.print("邮箱("+cust.getEmail()+"):"); String email = CMUtility.readString(30, cust.getEmail()); Customer newCustomer = new Customer(name,gender,age,phone,email);//现在得到到就是用户可以新改的信息 boolean isrf = customerList.replaceCustomer(nbumer - 1, newCustomer);//index就你的number位置的值,所以你要把number也放到外面去,因为我们的数值是从零开始的但是客户是从1开始的 if (isrf) { System.out.println("-----------------修改完成-----------------"); }else { System.out.println("-----------------修改失败-----------------"); } }//删除客户 private void deleteCustomer(){ int number; System.out.println("----------------删除客户-----------------"); for (;;){ System.out.print("请选择待删除客户的编号(-1退出):"); number = CMUtility.readInt(); if (number==-1) { return; } //判断你的number靠不靠谱 Customer customer = customerList.getCustomer(number - 1); if (customer ==null) { System.out.println("无法查到客户!"); }else { break; } } //程序到这,代表找到了指定的客户 System.out.print("确认是否删除(Y/N):"); char isdelet = CMUtility.readConfirmSelection(); if (isdelet=='Y') { //要删除就要调用CustomerList的删除方法 boolean isdelete = customerList.deleteCustomer(number - 1);//我要删除的是index,index又与number有关 if (isdelete) { System.out.println("----------------------删除成功-------------------"); }else { System.out.println("----------------------删除失败-------------------"); } }else { return; } }//查找列表 private void listAllCustomers(){//用户列表可以有原本的客户,着代表着把数据直接放入到了我的默认构造器中,每调用一次这个构造器都会显示在构造器里面的值 System.out.println("----------------------客户列表-------------------"); //当我把客户记录全部删除了是要显示没有客户记录的,这时就要判断我的customerList,customerList这个就代表这我的CustomerList类中的客户元素 int total = customerList.getTotal();//用total来判断我的total有多少个客户,total代表着我存了几个客户的信息 if (total==0) { System.out.println("没有客户记录!"); //但是我的成功页面是又要返回到我的用户界面的 }else { //进入else代表着我找到了客户的记录 System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t邮箱"); //进入了代表着我就要把客户的信息列出来,调用CustomerList中查找全部的用户 Customer[] custs = customerList.getAllCustomers(); for (int i = 0; i <total ; i++) { //为什么要用下面这样的方法,在上面已经有一个数组 Customer[] 用custs来接收,在for循环遍历的我客户我的custs[i]里面的值在重新写一个Customer来接收 //如下意思就是我在上面利用customerList.getAllCustomers方法,这个方法是获取客户的所有信息,而我用一个数组custs把里面的值接收了, // 随后用for循环把他们遍历出来就会有一个custs[i],在用 Customer用户的信息把他接收成custo Customer custo = custs[i]; System.out.println((i+1)+"\t"+custo.getName() +"\t"+custo.getGender() +"\t"+custo.getAge() +"\t"+custo.getPhone() +"\t"+custo.getEmail()); } } System.out.println("---------------------客户列表完成-------------------"); } public static void main(String[] args) { //要想显示界面,必须先用方法enterMainMenu方法来显示界面 CustomerView customerView = new CustomerView(); customerView.enterMainMenu(); } }