//customer类 public class Customer { String name; char sex; int age; String phone; String email; public Customer(){ } public Customer(String name,char sex,int age,String phone,String email) { this.name = name; this.sex=sex; this.phone=phone; this.age=age; this.email=email; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public void setPhone(String phone) { this.phone = phone; } public void setSex(char sex) { this.sex = sex; } public void setEmail(String email) { this.email = email; } }
//CustomerView类
public class CustomerView { CustomerList customerList=new CustomerList(10);//初始化数组构造器 boolean flag=true; public CustomerView(){ Customer customer=new Customer("罗志伟",'男',125,"153468465","92929@qq.com"); customerList.addCustomer(customer); } public void enterMainMenu() { while (flag) { System.out.println("------------------------客户信息管理软件------------------------"); 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(); 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') { flag = false; } } } } private void addNewCustomer(){ System.out.println("------------------------添加用户------------------------"); System.out.print("姓名: "); String name=CMUtility.readString(10); System.out.println(); System.out.print("性别: "); char sex=CMUtility.readChar(); System.out.println(); System.out.print("年龄: "); int age=CMUtility.readInt(); System.out.println(); System.out.print("电话: "); String phone=CMUtility.readString(11); System.out.println(); System.out.print("邮箱:"); String email=CMUtility.readString(30); System.out.println(); //将上面的数据封装到对象 Customer customer = new Customer(name,sex,age,phone,email); boolean istrue=customerList.addCustomer(customer); if(istrue){ System.out.println("------------------------添加完成------------------------"); } else{ System.out.println("------------------------客户已满添加失败------------------------"); } } private void modifyCustomer() { System.out.println("------------------------修改客户------------------------"); int id; for (; ; ) { System.out.println("请选择待修改客户编号(-1退出):"); id = CMUtility.readInt(); if (id == -1) { return;//方法结束返回菜单 } if (id > customerList.getTotal()) { System.out.println("无法找到指定的客户"); } else {//找到了对应用户的编号 break; } } id=id-1; System.out.print("姓名("+customerList.getCustomer(id).name+"):"); String name=CMUtility.readString(10,customerList.getCustomer(id).name); System.out.print("性别("+customerList.getCustomer(id).sex+"):"); char sex=CMUtility.readChar( customerList.getCustomer(id).sex); System.out.print("年龄("+customerList.getCustomer(id).age+"):"); int age=CMUtility.readInt(customerList.getCustomer(id).age); System.out.print("电话("+customerList.getCustomer(id).phone+"):"); String phone=CMUtility.readString(11,customerList.getCustomer(id).phone); System.out.print("邮箱("+customerList.getCustomer(id).email+"):"); String email=CMUtility.readString(30,customerList.getCustomer(id).email); Customer newcust=new Customer(name,sex,age,phone,email); boolean istrue2= customerList.replaceCustomer(id,newcust); if(istrue2){ System.out.println("------------------------修改完成------------------------"); } else { System.out.println("------------------------修改失败------------------------"); } } private void deleteCustomer() { System.out.println("------------------------删除用户------------------------"); int id; for (; ; ) { System.out.println("请选择待删除客户编号(-1退出):"); id = CMUtility.readInt(); if(id==-1){ return; } if (id > customerList.getTotal() && id < 0) { System.out.println("无法找到指定客户编号!"); } else {//找到了 break; } } id = id - 1; System.out.println("确认是否删除(Y/N):"); char istrue3 = CMUtility.readConfirmSelection(); if (istrue3 == 'Y') { boolean istrue4=customerList.deleteCustomer(id); if (istrue4) { System.out.println("------------------------删除完成------------------------"); } else{ System.out.println("------------------------删除失败------------------------"); } } 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邮箱"); Customer[] custs=customerList.getAllCustomer(); for(int i=0;i<custs.length;i++){ System.out.println(i+1+"\t "+custs[i].name+"\t "+custs[i].sex+"\t "+custs[i].age+"\t "+custs[i].phone+"\t "+custs[i].email); } System.out.println("---------------------------客户列表完成---------------------------"); } } }
import java.util.Scanner; import java.util.*; /** CMUtility类: 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。 */ public class CMUtility { private static Scanner scanner = new Scanner(System.in); /** 用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。 */ 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() { String str = readKeyBoard(1, false); return str.charAt(0); } /** 从键盘读取一个字符,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 */ public static char readChar(char defaultValue) { String str = readKeyBoard(1, true); return (str.length() == 0) ? defaultValue : str.charAt(0); } /** 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 */ 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; } /** 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 */ public static int readInt(int defaultValue) { int n; for (; ; ) { String str = readKeyBoard(3, 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); } /** 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。 */ public static String readString(int limit, String defaultValue) { String str = readKeyBoard(limit, true); return str.equals("")? defaultValue : str; } /** 用于确认选择的输入。该方法从键盘读取‘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; } 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; } }