Java基础学习:尚硅谷 面向对象进阶 客户信息管理软件 ...

打印 上一主题 下一主题

主题 996|帖子 996|积分 2988

1. Customer.java
  1. package chapter07.pinExer;
  2. // 客户类
  3. public class Customer {
  4.     private String name;
  5.     private char gender; //性别
  6.     private int age;
  7.     private String phone;
  8.     private String email;
  9.     // 构造器
  10.     public Customer(){
  11.     }
  12.     public Customer(String name, char gender, int age, String phone, String email){
  13.         this.name = name;
  14.         this.age = age;
  15.         this.gender = gender;
  16.         this.phone = phone;
  17.         this.email = email;
  18.     }
  19.     public void setName(String name) {
  20.         this.name = name;
  21.     }
  22.     public String getName() {
  23.         return name;
  24.     }
  25.     public void setGender(char gender) {
  26.         this.gender = gender;
  27.     }
  28.     public char getGender() {
  29.         return gender;
  30.     }
  31.     public void setAge(int age) {
  32.         this.age = age;
  33.     }
  34.     public int getAge() {
  35.         return age;
  36.     }
  37.     public void setPhone(String phone) {
  38.         this.phone = phone;
  39.     }
  40.     public String getPhone() {
  41.         return phone;
  42.     }
  43.     public void setEmail(String email) {
  44.         this.email = email;
  45.     }
  46.     public String getEmail() {
  47.         return email;
  48.     }
  49. }
复制代码
2. CustomerList.java
  1. package chapter07.pinExer;
  2. // CustomerList为Customer对象管理模块,内部使用数组管理一组Customer对象
  3. public class CustomerList {
  4.     private Customer[] customers;
  5.     private int total = 0; // 最大长度
  6.     public CustomerList(int totalCustomer){
  7.         customers = new Customer[totalCustomer];
  8.     }
  9.     //添加成员,成功返回true,失败返回false
  10.     public boolean addCustomer(Customer customer){
  11.         if(total < customers.length){
  12.             customers[total] = customer;
  13.             total++;
  14.             return true;
  15.         }
  16.         return false;
  17.     }
  18.     // 替换,用参数customer替换数组中由index指定的对象
  19.     public boolean replaceCustomer(int index, Customer cust){
  20.         if(index >= 0 && index < total){
  21.             customers[index] = cust;
  22.             return true;
  23.         }
  24.         return false;
  25.     }
  26.     // 删除,从数组中删除index指定索引位置的客户对象记录
  27.     public boolean deleteCustomer(int index){
  28.         if(index >= 0 && index < total){
  29.             for(int i = index; i < total; i++){
  30.                 customers[i] = customers[i+1];
  31.             }
  32.             //将最后一个有效位置的数值置空
  33.             customers[total - 1] = null;
  34.             total--;
  35.             return true;
  36.         }
  37.         return false;
  38.     }
  39.     // 返回数组中记录的所有客户对象
  40.     public Customer[] getAllCustomers(){
  41.         Customer[] custs = new Customer[total];
  42.         for(int i = 0; i < total; i++){
  43.             custs[i] = customers[i];
  44.         }
  45.         return custs;
  46.     }
  47.     // 返回index指定索引位置客户的记录
  48.     public Customer getCustomer(int index){
  49.         if(index >=0 && index < total){
  50.             return customers[index];
  51.         }
  52.         return null;
  53.     }
  54.     // 获取客户列表中有几个客户
  55.     public int getTotal(){
  56.         return total;
  57.     }
  58. }
复制代码
CMUtility.java 工具类
  1. package chapter07.pinExer;
  2. import java.util.*;
  3. public class CMUtility {
  4.     private static Scanner scanner = new Scanner(System.in);
  5.     /**
  6.      用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
  7.      */
  8.     public static char readMenuSelection() {
  9.         char c;
  10.         for (; ; ) {
  11.             String str = readKeyBoard(1, false);
  12.             c = str.charAt(0);
  13.             if (c != '1' && c != '2' &&
  14.                     c != '3' && c != '4' && c != '5') {
  15.                 System.out.print("选择错误,请重新输入:");
  16.             } else break;
  17.         }
  18.         return c;
  19.     }
  20.     /**
  21.      从键盘读取一个字符,并将其作为方法的返回值。
  22.      */
  23.     public static char readChar() {
  24.         String str = readKeyBoard(1, false);
  25.         return str.charAt(0);
  26.     }
  27.     /**
  28.      从键盘读取一个字符,并将其作为方法的返回值。
  29.      如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
  30.      */
  31.     public static char readChar(char defaultValue) {
  32.         String str = readKeyBoard(1, true);
  33.         return (str.length() == 0) ? defaultValue : str.charAt(0);
  34.     }
  35.     /**
  36.      从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
  37.      */
  38.     public static int readInt() {
  39.         int n;
  40.         for (; ; ) {
  41.             String str = readKeyBoard(2, false);
  42.             try {
  43.                 n = Integer.parseInt(str);
  44.                 break;
  45.             } catch (NumberFormatException e) {
  46.                 System.out.print("数字输入错误,请重新输入:");
  47.             }
  48.         }
  49.         return n;
  50.     }
  51.     /**
  52.      从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
  53.      如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
  54.      */
  55.     public static int readInt(int defaultValue) {
  56.         int n;
  57.         for (; ; ) {
  58.             String str = readKeyBoard(2, true);
  59.             if (str.equals("")) {
  60.                 return defaultValue;
  61.             }
  62.             try {
  63.                 n = Integer.parseInt(str);
  64.                 break;
  65.             } catch (NumberFormatException e) {
  66.                 System.out.print("数字输入错误,请重新输入:");
  67.             }
  68.         }
  69.         return n;
  70.     }
  71.     /**
  72.      从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
  73.      */
  74.     public static String readString(int limit) {
  75.         return readKeyBoard(limit, false);
  76.     }
  77.     /**
  78.      从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
  79.      如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
  80.      */
  81.     public static String readString(int limit, String defaultValue) {
  82.         String str = readKeyBoard(limit, true);
  83.         return str.equals("")? defaultValue : str;
  84.     }
  85.     /**
  86.      用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
  87.      */
  88.     public static char readConfirmSelection() {
  89.         char c;
  90.         for (; ; ) {
  91.             String str = readKeyBoard(1, false).toUpperCase();
  92.             c = str.charAt(0);
  93.             if (c == 'Y' || c == 'N') {
  94.                 break;
  95.             } else {
  96.                 System.out.print("选择错误,请重新输入:");
  97.             }
  98.         }
  99.         return c;
  100.     }
  101.     private static String readKeyBoard(int limit, boolean blankReturn) {
  102.         String line = "";
  103.         while (scanner.hasNextLine()) {
  104.             line = scanner.nextLine();
  105.             if (line.length() == 0) {
  106.                 if (blankReturn) return line;
  107.                 else continue;
  108.             }
  109.             if (line.length() < 1 || line.length() > limit) {
  110.                 System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
  111.                 continue;
  112.             }
  113.             break;
  114.         }
  115.         return line;
  116.     }
  117. }
复制代码
4. CustomerView.java 主模块
  1. package chapter07.pinExer;
  2. import chapter07.pinExer.Customer;
  3. // 主模块,负责菜单的显示和处理用户操作
  4. public class CustomerView {
  5.     int totalCustomers = 10;
  6.     CustomerList customerList = new CustomerList(totalCustomers);
  7.     // 进入主界面
  8.     public void enterMainMenu(){
  9.         boolean flag = true;
  10.         while(flag){
  11.             System.out.println("\n------------------拼电商管理系统------------------\n");
  12.             System.out.println("                    1.添 加 客 户");
  13.             System.out.println("                    2.修 改 客 户");
  14.             System.out.println("                    3.删 除 客 户");
  15.             System.out.println("                    4.客 户 列 表");
  16.             System.out.println("                    5.退       出\n");
  17.             System.out.println("                    请选择(1-5)");
  18.             char key = CMUtility.readMenuSelection();
  19.             switch(key){
  20.                 case '1':
  21.                     addNewCustomer();
  22.                     break;
  23.                 case '2':
  24.                     modifyCustomer();;
  25.                     break;
  26.                 case '3':
  27.                     deleteCustomer();
  28.                     break;
  29.                 case '4':
  30.                     listAllCustomers();
  31.                     break;
  32.                 case '5':
  33.                     System.out.print("确认是否退出(Y/N):");
  34.                     char isExit = CMUtility.readConfirmSelection();
  35.                     if(isExit == 'Y'){
  36.                         flag = false;
  37.                     }
  38.                     break;
  39.             }
  40.         }
  41.     }
  42.     private void addNewCustomer(){
  43.         System.out.println("\n---------------------添加客户---------------------");
  44.         if (customerList.getTotal() >= totalCustomers) {
  45.             System.out.println("----------------客户列表已满,无法添加---------------");
  46.             return;
  47.         }
  48.         System.out.print("姓名:");
  49.         String name = CMUtility.readString(10);
  50.         System.out.print("性别:");
  51.         char gender = CMUtility.readChar();
  52.         System.out.print("年龄:");
  53.         int age = CMUtility.readInt();
  54.         System.out.print("电话:");
  55.         String phone = CMUtility.readString(13);
  56.         System.out.print("邮箱:");
  57.         String email = CMUtility.readString(20);
  58.         Customer cust = new Customer(name, gender, age, phone, email);
  59.         boolean isAdded = customerList.addCustomer(cust);
  60.         if (isAdded) {
  61.             System.out.println("---------------------添加完成---------------------");
  62.         } else {
  63.             System.out.println("---------------------添加失败---------------------");
  64.         }
  65.     }
  66.     private void modifyCustomer(){
  67.         System.out.println("\n---------------------修改客户---------------------");
  68.         while (true) {
  69.             System.out.print("请选择待修改客户编号(-1退出):");
  70.             int number = CMUtility.readInt();
  71.             if (number == -1) {
  72.                 return; //如果输入-1,则直接结束方法
  73.             } else if (number <= 0 || number > customerList.getTotal()) {
  74.                 System.out.println("无法找到指定客户!");
  75.             } else {
  76.                 System.out.print("姓名(" + customerList.getCustomer(number - 1).getName() + "):");
  77.                 String newName = CMUtility.readString(10, customerList.getCustomer(number - 1).getName());
  78.                 System.out.print("性别(" + customerList.getCustomer(number - 1).getGender() + "):");
  79.                 char newGender = CMUtility.readChar(customerList.getCustomer(number - 1).getGender());
  80.                 System.out.print("性别(" + customerList.getCustomer(number - 1).getAge() + "):");
  81.                 int newAge = CMUtility.readInt(customerList.getCustomer(number - 1).getAge());
  82.                 System.out.print("电话(" + customerList.getCustomer(number - 1).getPhone() + "):");
  83.                 String newPhone = CMUtility.readString(13, customerList.getCustomer(number - 1).getPhone());
  84.                 System.out.print("邮箱(" + customerList.getCustomer(number - 1).getEmail() + "):");
  85.                 String newEmail = CMUtility.readString(20, customerList.getCustomer(number - 1).getEmail());
  86.                 Customer cust = new Customer(newName, newGender, newAge, newPhone, newEmail);
  87.                 boolean isReplaced = customerList.replaceCustomer(number - 1, cust);
  88.                 if (isReplaced) {
  89.                     System.out.println("---------------------修改完成---------------------");
  90.                 } else {
  91.                     System.out.println("---------------------无法修改---------------------");
  92.                 }
  93.                 return;
  94.             }
  95.         }
  96.     }
  97.     private void deleteCustomer(){
  98.         System.out.println("\n---------------------删除客户---------------------");
  99.         while (true) {
  100.             System.out.print("请选择待删除客户编号(-1退出):");
  101.             int number = CMUtility.readInt();
  102.             if (number == -1) {
  103.                 return; //如果输入-1,则直接结束方法
  104.             } else if (number <= 0 || number > customerList.getTotal()) {
  105.                 System.out.println("无法找到指定客户!");
  106.             } else {
  107.                 System.out.print("确认是否删除(Y/N):");
  108.                 char isDelete = CMUtility.readConfirmSelection();
  109.                 if (isDelete == 'Y') {
  110.                     boolean isDeleted = customerList.deleteCustomer(number - 1);
  111.                     if (isDeleted) {
  112.                         System.out.println("---------------------删除完成---------------------");
  113.                     } else {
  114.                         System.out.println("---------------------删除失败---------------------");
  115.                     }
  116.                 }
  117.                 return;
  118.             }
  119.         }
  120.     }
  121.     private void listAllCustomers(){
  122.         System.out.println("----------------------------------客户列表----------------------------------");
  123.         if (customerList.getTotal() == 0) {
  124.             System.out.println("没有客户记录!");
  125.         } else {
  126.             System.out.println("编号\t\t姓名\t\t性别\t\t年龄\t\t电话\t\t\t\t邮箱");
  127.             Customer[] custs = customerList.getAllCustomers();
  128.             for (int i = 0; i < custs.length; i++) {
  129.                 System.out.println((i + 1) + "\t\t" + custs[i].getName() + "\t\t" + custs[i].getGender() + "\t\t"
  130.                         + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t\t" + custs[i].getEmail());
  131.             }
  132.         }
  133.         System.out.println("--------------------------------客户列表完成---------------------------------");
  134.     }
  135.     public static void main(String[] args) {
  136.         CustomerView view = new CustomerView();
  137.         view.enterMainMenu();
  138.     }
  139. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表