面向对象(中)项目二 2.0

工具类 CMUtility
  1 package day1_18;
  2 
  3 import java.util.Scanner;
  4 
  5 public class CMUtility {
  6 
  7     //读取主菜单选项
  8     public static char readMenuSelection() {
  9         for (; ; ) {
 10             String str = readKeyBoard(1, false);
 11             char c = str.charAt(0);
 12             if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
 13                 System.out.println("输入选项错误,请从新输入:");
 14             } else {
 15                 return c;
 16             }
 17         }
 18     }
 19 
 20     // 读取字符串
 21     public static String readString(int limit) {
 22         return readKeyBoard(limit, false);
 23     }
 24 
 25     //读取字符串(直接Enter换行的话,就返回默认值defaultValue)
 26     public static String readString(int limit,String defaultValue) {
 27         String str = readKeyBoard(limit, true);
 28         return str.equals("") ? defaultValue : str;
 29     }
 30 
 31 
 32     //读取整数
 33     public static int readInt() {
 34         for (; ; ) {
 35             String str = readKeyBoard(2, false);
 36             try {
 37                 int n = Integer.parseInt(str);
 38                 return n;
 39             } catch (Exception e) {
 40                 System.out.print("请输入不超过两位数的数字:");
 41             }
 42         }
 43     }
 44 
 45     //读取整数(直接Enter换行的话,就返回默认值defaultValue)
 46     public static int readInt(int defaultValue) {
 47         String str = readKeyBoard(2, true);
 48         if (str.equals("")) {
 49             return defaultValue;
 50         }
 51         try {
 52             int n = Integer.parseInt(str);
 53             return n;
 54         } catch (Exception e) {
 55             System.out.print("请输入不超过两位数的数字:");
 56         }
 57         return 111111111;
 58     }
 59 
 60     //读取字符
 61     public static char readChar() {
 62         String str = readKeyBoard(1, false);
 63         return str.charAt(0);
 64     }
 65 
 66 
 67     //读取字符(直接Enter换行的话,就返回默认值defaultValue)
 68     public static char readChar(char defaultValue) {
 69         String str = readKeyBoard(1, true);
 70         return str.equals("") ? defaultValue : str.charAt(0);
 71     }
 72 
 73     //是否确认退出选项
 74     public static char readConfirmSelection() {
 75         char selection;
 76         for (; ; ) {
 77             String str = readKeyBoard(1, false);
 78             selection = str.toUpperCase().charAt(0);
 79             if (selection != 'Y' && selection != 'N') {
 80                 System.out.println("输入选项错误,请从新输入:");
 81             } else {
 82                 break;
 83             }
 84         }
 85         return selection;
 86     }
 87 
 88 
 89     //读取用户输入
 90     private static String readKeyBoard(int limit, boolean blankEnter) {
 91 
 92         String str = "";
 93         for (; ; ) {
 94             Scanner scanner = new Scanner(System.in);
 95             if (scanner.hasNextLine()) {
 96                 str = scanner.nextLine().trim();
 97                 if (str.length() == 0) {
 98                     if (blankEnter) {
 99                         return str;
100                     }
101                     System.out.print("请从新输入:");
102                 }else if (str.length() > limit) {
103                     System.out.print("输入长度不能大于" + limit + ",请从新输入:");
104                 } else {
105                     break;
106                 }
107             }
108         }
109         return str;
110     }
111 
112 }

客户实体类 Customer
 1 package day1_18;
 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 
13     public Customer(String name, char gender, int age, String phone, String email) {
14         this.name = name;
15         this.gender = gender;
16         this.age = age;
17         this.phone = phone;
18         this.email = email;
19     }
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public char getGender() {
30         return gender;
31     }
32 
33     public void setGender(char gender) {
34         this.gender = gender;
35     }
36 
37     public int getAge() {
38         return age;
39     }
40 
41     public void setAge(int age) {
42         this.age = age;
43     }
44 
45     public String getPhone() {
46         return phone;
47     }
48 
49     public void setPhone(String phone) {
50         this.phone = phone;
51     }
52 
53     public String getEmail() {
54         return email;
55     }
56 
57     public void setEmail(String email) {
58         this.email = email;
59     }
60 }
 
操做客户类 CustomerList
 1 package day1_18;
 2 
 3 public class CustomerList {
 4     private Customer[] customers;
 5     private int total;
 6 
 7     public CustomerList(int totalCustomer) {
 8         customers = new Customer[totalCustomer];
 9     }
10 
11     //添加客户
12     public boolean addCustomer(Customer customer) {
13         if (total >= customers.length) {
14             return false;
15         }
16         customers[total++] = customer;
17         return true;
18     }
19 
20     //查询客户列表
21     public boolean listCustomers() {
22         if (total == 0) {
23             return false;
24         }
25         System.out.print("编号\t姓名\t性别\t年龄\t手机号码\t电子邮箱\n");
26         for (int i=0;i< total;i++) {
27             System.out.print((i+1) + "\t\t" +  customers[i].getName() + "\t" + customers[i].getGender() +
28                     "\t\t" + customers[i].getAge() + "\t\t" + customers[i].getPhone() +
29                     "\t\t" + customers[i].getEmail() + "\n");
30         }
31         return true;
32     }
33 
34 
35     //修改指定位置的客户信息
36     public boolean replaceCustomer(int index,Customer customer) {
37         if (index < 0 || index >= total) {
38             return false;
39         }
40         customers[index] = customer;
41         return true;
42     }
43 
44     //查找指定位置的客户
45     public Customer getCustomer(int index){
46         if (index < 0 || index >= total) {
47             return null;
48         } else {
49             return customers[index];
50         }
51     }
52 
53     //删除指定位置的客户
54     public boolean deleteCustomer(int index) {
55         if (index < 0 || index >= total) {
56             return false;
57         }
58         for (int i = index; i < total; i++) {
59             customers[i] = customers[i + 1];
60         }
61         total--;
62         return true;
63     }
64 }

 

客户端交互类 CustomerView
  1 package day1_18;
  2 
  3 public class CustomerView {
  4     public CustomerList customerList;
  5 
  6     public static void main(String[] args) {
  7         CustomerView view = new CustomerView();
  8         view.enterMainMenu();
  9     }
 10 
 11     public CustomerView() {
 12         customerList = new CustomerList(10);
 13     }
 14 
 15     //主菜单
 16     public void enterMainMenu() {
 17         boolean isFlag = true;
 18         while (isFlag) {
 19             System.out.println("\n-----------------客户信息管理软件-----------------\n");
 20             System.out.println("                   1 添 加 客 户");
 21             System.out.println("                   2 修 改 客 户");
 22             System.out.println("                   3 删 除 客 户");
 23             System.out.println("                   4 客 户 列 表");
 24             System.out.println("                   5 退    出\n");
 25             System.out.print("           请选择(1-5):");
 26             char menu = CMUtility.readMenuSelection();
 27             switch (menu) {
 28                 case '1':
 29                     //System.out.println("添加客户操做");
 30                     saveCustomer();
 31                     break;
 32                 case '2':
 33                     //System.out.println("修改客户操做");
 34                     replaceCustomer();
 35                     break;
 36                 case '3':
 37                     //System.out.println("删除客户操做");
 38                     removeCustomer();
 39                     break;
 40                 case '4':
 41                     //System.out.println("客户列表操做");
 42                     listCustomers();
 43                     break;
 44                 case '5':
 45                     //System.out.println("退出操做");
 46                     System.out.print("确认是否退出(Y/N):");
 47                     char isExit = CMUtility.readConfirmSelection();
 48                     if (isExit == 'Y') {
 49                         isFlag = false;
 50                     }
 51             }
 52         }
 53     }
 54 
 55     //添加客户
 56     public void saveCustomer() {
 57         System.out.println("----------添加客户----------");
 58         System.out.print("姓名:");
 59         String name = CMUtility.readString(4);
 60         System.out.print("性别:");
 61         char gender = CMUtility.readChar();
 62         System.out.print("年龄:");
 63         int age = CMUtility.readInt();
 64         System.out.print("手机号码:");
 65         String phone = CMUtility.readString(13);
 66         System.out.print("电子邮箱:");
 67         String email = CMUtility.readString(20);
 68         Customer customer = new Customer(name, gender, age, phone, email);
 69         boolean isSaved = customerList.addCustomer(customer);
 70         if (isSaved) {
 71             System.out.println("----------添加客户成功----------");
 72         } else {
 73             System.out.println("----------添加客户成功----------");
 74         }
 75     }
 76 
 77     //查看客户列表
 78     public boolean listCustomers() {
 79         System.out.println("----------客户列表----------");
 80         boolean isListed = customerList.listCustomers();
 81         if (!isListed) {
 82             System.out.println("客户信息为空");
 83         }
 84         System.out.println("----------客户列表完成----------");
 85         return isListed;
 86     }
 87 
 88     //修改指定位置的客户信息
 89     public void replaceCustomer() {
 90         int index;
 91         Customer customer;
 92         if (listCustomers()) {
 93             System.out.print("请输入要修改客户的编号:");
 94             for (; ; ) {
 95                 index = CMUtility.readInt() -1;
 96                 customer = customerList.getCustomer(index);
 97                 if (customer == null) {
 98                     System.out.print("抱歉,没有这个客户,请从新输入:");
 99                 } else {
100                     break;
101                 }
102             }
103             System.out.print("姓名(" + customer.getName() + "):");
104             String name = CMUtility.readString(4, customer.getName());
105             System.out.print("性别(" + customer.getGender() + "):");
106             char gender = CMUtility.readChar(customer.getGender());
107             System.out.print("年龄(" + customer.getAge() + "):");
108             int age = CMUtility.readInt(customer.getAge());
109             System.out.print("电话(" + customer.getPhone() + "):");
110             String phone = CMUtility.readString(13, customer.getPhone());
111             System.out.print("邮箱(" + customer.getEmail() + "):");
112             String email = CMUtility.readString(20, customer.getEmail());
113 
114             customer = new Customer(name, gender, age, phone, email);
115             boolean isReplaced = customerList.replaceCustomer(index, customer);
116             if (isReplaced) {
117                 System.out.println("-----------修改客户成功-----------");
118             } else {
119                 System.out.println("-----------修改客户失败-----------");
120             }
121         } else {
122             System.out.print("\n舒适提示:客户列表为空,请先添加客户\n");
123             enterMainMenu();
124         }
125     }
126 
127     //删除客户
128     public void removeCustomer() {
129         if (listCustomers()) {
130             System.out.print("请输入要删除客户的编号:");
131             for (; ; ) {
132                 int index = CMUtility.readInt() -1;
133                 boolean isRemove = customerList.deleteCustomer(index);
134                 if (isRemove) {
135                     System.out.println("----------删除成功----------");
136                     break;
137                 } else {
138                     System.out.print("您将删除的客户不存在,请从新选择编号:");
139                 }
140             }
141         } else {
142             System.out.print("\n舒适提示:客户列表为空,请选择其余操做\n");
143             enterMainMenu();
144         }
145     }
146 }
主菜单效果

 

 笔记java

关于客户管理软件项目编程思路的梳理 1.主菜单列表选项 1.1 添加客户 ①获取键盘输入,根据属性的类型不一样和字符长度的限制,在工具类里编写 相应的获取键盘输入的方法 ②客户端输入数据赋值给每一个属性后,利用构造器建立Customer对象,再添 加到customers数组中,而且数组中实际存储的Customer对象的个数total加1 1.2 查看客户列表 ①遍历customers数组中每一个customer对象,获取每一个属性的值 1.3 修改客户列表中某个编号的客户信息 ①编号是指在客户列表中例如1,2,3,...的一系列天然数,注意它与数组索引的关系:编号-1 = 数组索引 ②客户端键盘输入接收到指定编号后,首先须要判断这个编号在客户列表中是否存在,即以(指定编号-1) 做为数组索引,查看数组中这个索引的对象是否为空,若是为空,说明用户列表中是没有这个用户的,既然 没有这个用户那么修改客户也是不合逻辑的 ③若是客户列表中存在须要修改的客户,那么接收键盘输入做为属性值的方法就须要根据状况来从新定义了, 由于若是客户的某个属性不想修改,那么就设计为输入时直接Enter,表示当接收空字符串时,新customer 对象的属性值仍是为原来的属性值 1.4 删除客户列表中某个编号的客户信息 ①编号是指在客户列表中例如1,2,3,...的一系列天然数,注意它与数组索引的关系:编号-1 = 数组索引 ②客户端键盘输入接收到指定编号后,首先须要判断这个编号在客户列表中是否存在,即以(指定编号-1) 做为数组索引,查看数组中这个索引的对象是否为空,若是为空,说明用户列表中是没有这个用户的,既然 没有这个用户那么删除客户也是不合逻辑的 ③若是客户列表中存在须要删除的客户,从须要删除的这个客户在数组customers的索引位置开始,后面的元 素覆盖前面前一个元素,这样实现删除操做 1.5 退出主菜单 获取键盘输入,取字符,转为大写字母后若是为‘Y’,则退出;若是为‘N’则不退出
相关文章
相关标签/搜索