Java 客户信息管理软件 (面向对象 封装 总结)

 1 package com.bytezero.cim.bean;
 2 
 3 /**
 4  * 
 5  * @Description   Customer为实体对象,用来封装客户信息
 6  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 7  * @version
 8  * @date 2021年9月16日下午6:34:59
 9  * @
10  *
11  */
12 public class Customer 
13 {
14     private String name;    //客户姓名
15     private    char gender;    //性别
16     private    int age;        // 年龄
17     private    String phone;   //电话
18     private    String email;   //电子邮箱
19     
20     
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public char getGender() {
28         return gender;
29     }
30     public void setGender(char gender) {
31         this.gender = gender;
32     }
33     public int getAge() {
34         return age;
35     }
36     public void setAge(int age) {
37         this.age = age;
38     }
39     public String getPhone() {
40         return phone;
41     }
42     public void setPhone(String phone) {
43         this.phone = phone;
44     }
45     public String getEmail() {
46         return email;
47     }
48     public void setEmail(String email) {
49         this.email = email;
50     }
51     public Customer() {
52         
53     }
54     public Customer(String name, char gender, int age, String phone, String email) 
55     {
56         
57         this.name = name;
58         this.gender = gender;
59         this.age = age;
60         this.phone = phone;
61         this.email = email;
62     }
63     
64     
65     
66 
67     
68     
69     
70     
71     
72     
73     
74     
75     
76     
77     
78     
79     
80     
81     
82     
83 }
  1 package com.bytezero.cim.service;
  2 
  3 import com.bytezero.cim.bean.Customer;
  4 
  5 /**
  6  * 
  7  * @Description   CustomerList为Customer对象的管理模块,内部
  8  * 用数组管理一组Customer对象,并提供相应的添加,修改,删除和
  9  * 遍历方法,供CustomerView调用
 10  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 11  * @version
 12  * @date 2021年9月16日下午6:37:42
 13  * @
 14  *
 15  */
 16 public class CustomerList
 17 {
 18     private Customer[] customers;  //用来保存客户对象数组
 19     private int total = 0;         //记录已保存客户对象的数量
 20     
 21     
 22     /**
 23      * 用来初始化 customers数组的构造器  
 24      * @param totalCustomer  指定 customer是数组最大的空间
 25      */
 26     public CustomerList(int totalCustomer)
 27     {
 28         customers = new Customer[totalCustomer];
 29     }
 30     
 31     
 32     /**
 33      * 
 34      * @Description 将指定的客户添加到数组中
 35      * @author  Bytezero·zhenglei!
 36      * @date 2021年9月17日上午10:40:03
 37      * @param customer
 38      * @return  true:添加成功  false:添加失败
 39      *
 40      */
 41     public boolean addCustomer(Customer customer)
 42     {
 43         if(total >= customers.length)
 44         {
 45             return false;
 46         }
 47         
 48 //        customers[total] = customer;
 49 //        total++;
 50         //
 51         customers[total++] = customer;
 52         
 53         return true;
 54         
 55         
 56         
 57         
 58         
 59     }
 60     /**
 61      * 
 62      * @Description  修改指定索引位置的客户信息
 63      * @author  Bytezero·zhenglei!
 64      * @date 2021年9月17日上午10:43:53
 65      * @param index
 66      * @param cust
 67      * @return  true:修改成功     false:修改失败
 68      *
 69      */
 70     
 71     public boolean replaceCustomer(int index,Customer cust)
 72     {
 73         if(index < 0|| index >= total)
 74         {
 75             return false;
 76         }
 77         else
 78         {
 79             customers[index] = cust;
 80             return true;
 81         }
 82     }
 83     
 84     /**
 85      * 
 86      * @Description  删除指定索引位置上的客户
 87      * @author  Bytezero·zhenglei!
 88      * @date 2021年9月17日上午10:53:57
 89      * @param index
 90      * @return  true:删除成功   false:删除失败
 91      *
 92      */
 93     public boolean deleteCustomer(int index)
 94     {
 95         if(index < 0|| index >= total)
 96         {
 97             return false;
 98         }
 99         else
100         {
101             for(int i = index; i< total-1;i++)
102             {
103                 customers[i] = customers[i+1];
104             }
105             
106             //最后有数据的元素需要置空
107             //customers[total -1 ] = null;
108             //total--;
109             //
110             customers[--total ] = null;
111             return true;
112         }
113     }
114     /**
115      * 
116      * @Description  获取所有客户的信息
117      * @author  Bytezero·zhenglei!
118      * @date 2021年9月17日上午11:02:11
119      * @return
120      *
121      */
122     public Customer[] getAllCustomers()
123     {
124         Customer[] custs = new Customer[total];
125         for(int i =0; i <total; i++)
126         {
127             custs[i] = customers[i];
128         }
129         return custs;
130         
131     }
132     
133     /**
134      * 
135      * @Description 获取指定索引位置上的客户
136      * @author  Bytezero·zhenglei!
137      * @date 2021年9月17日上午11:06:45
138      * @param index
139      * @return 如果找到了元素 则返回 ; 如果没有找到 返回null
140      *
141      */
142     public Customer getCustomer(int index)
143     {
144         if(index < 0|| index >= total)
145         {
146             return null;
147         }
148         return customers[index];
149     }
150     
151     /**
152      * 
153      * @Description  获取存储的客户的数量
154      * @author  Bytezero·zhenglei!
155      * @date 2021年9月17日上午11:08:31
156      * @return
157      *
158      */
159     public int getTotal()
160     {
161         return total;
162     }
163     
164     
165 }
  1 package com.bytezero.cim.view;
  2 
  3 
  4 
  5 import com.bytezero.cim.bean.Customer;
  6 import com.bytezero.cim.service.CustomerList;
  7 import com.bytezero.cim.util.CMUtility;
  8 
  9 /**
 10  * 
 11  * @Description   CustomerView为主模块,负责菜单的显示和处理用户操作
 12  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 13  * @version
 14  * @date 2021年9月16日下午6:40:43
 15  * @
 16  *
 17  */
 18 public class CustomerView 
 19 {
 20     private CustomerList customerList = new CustomerList(10);
 21     
 22     
 23     
 24     
 25     public CustomerView()
 26     {
 27         Customer customer = new Customer("zhenglei", '男', 21, "zl420498246","zl420498246@qq.com" );
 28         customerList.addCustomer(customer);
 29     }
 30     
 31     /**
 32      * 
 33      * @Description  显示《客户信息管理软件》界面的操作
 34      * @author  Bytezero·zhenglei!
 35      * @date 2021年9月17日上午11:14:51
 36      *
 37      */
 38     public void enterMainMenu()
 39     {
 40         
 41         boolean isFlag = true;
 42         while(isFlag)
 43         {
 44         
 45         System.out.println("\n--------------------客户信息管理软件--------------------\n");
 46         System.out.println("                       1.添加客户");
 47         System.out.println("                       2.修改客户");
 48         System.out.println("                       3.删除客户");
 49         System.out.println("                       4.客户列表");
 50         System.out.println("                       5.退    出\n");
 51         System.out.print("          请选择(1-5):");
 52         
 53         char menu = CMUtility.readMenuSelection();
 54         switch(menu)
 55         {
 56             case '1':
 57                 addNewCustomer();
 58                 break;
 59                 
 60             case '2':
 61                 modifyCustomer();
 62                 break;
 63                 
 64             case '3':
 65                 deleteCustomer();
 66                 break;
 67                 
 68             case '4':
 69                 listAllCustomers();
 70                 break;
 71                 
 72             case '5':
 73                 //System.out.println("退出");
 74                 System.out.println("确认是否退出(Y/N):");
 75                 char isExit =  CMUtility.readConfirmSelection();
 76                 if(isExit == 'Y')
 77                 {
 78                     isFlag = false;
 79                 }
 80                 
 81                 
 82                 break;
 83                 
 84         }
 85 //        isFlag = false;
 86         
 87         }
 88         
 89         
 90         
 91         
 92         
 93         
 94         
 95         
 96     }
 97     
 98     /**
 99      * 
100      * @Description  添加客户的操作
101      * @author  Bytezero·zhenglei!
102      * @date 2021年9月17日上午11:13:37
103      *
104      */
105     private void addNewCustomer()
106     {
107         //System.out.println("添加客户的操作");
108         System.out.println("------------------添加客户------------------");
109         
110         System.out.print("姓名:");
111         String name = CMUtility.readString(10);
112         
113         System.out.print("性别:");
114         char gender = CMUtility.readChar();
115         
116         System.out.print("年龄:");
117         int age = CMUtility.readInt();
118         
119         System.out.print("电话:");
120         String phone = CMUtility.readString(13);
121         
122         System.out.print("邮箱:");
123         String email = CMUtility.readString(30);
124         
125         //将上述数据封装到对象中
126         Customer customer = new Customer(name,gender,age,phone,email);
127         
128         boolean isSuccess = customerList.addCustomer(customer);
129         if(isSuccess)
130         {
131             System.out.println("------------------添加完成------------------");
132         }
133         else
134         {
135             System.out.println("------------------添加目录已满,添加失败------------------");
136         }
137     }
138     
139     /**
140      * 
141      * @Description 修改客户的操作
142      * @author  Bytezero·zhenglei!
143      * @date 2021年9月17日上午11:14:03
144      *
145      */
146     private void modifyCustomer()
147     {
148         //System.out.println("修改客户的操作");
149         System.out.println("------------------添修改客户------------------");
150         
151         Customer cust;
152         int number;
153         for(;;)
154         {
155             System.out.print("请选择待修改客户编号(-1退出):");
156             number = CMUtility.readInt();    
157             
158             if(number == -1)
159             {
160                 return;
161             }
162             
163             cust = customerList.getCustomer(number - 1 );
164             if(cust == null)
165             {
166                 System.out.println("无法找到指定的客户!");
167             }
168             else
169             {
170                 //找到了相应的编号客户
171                 break;
172             }
173             
174         }
175         
176         //修改客户信息
177         System.out.println("姓名("+ cust.getName()+"):");
178         String name = CMUtility.readString(10,cust.getName());
179         
180         System.out.println("性别("+ cust.getGender()+"):");
181         char gender = CMUtility.readChar(cust.getGender());
182         
183         System.out.println("年龄("+ cust.getAge()+"):");
184         int age = CMUtility.readInt(cust.getAge());
185         
186         
187         System.out.println("电话("+ cust.getPhone()+"):");
188         String phone = CMUtility.readString(13,cust.getPhone());
189         
190         System.out.println("邮箱("+ cust.getEmail()+"):");
191         String email = CMUtility.readString(30,cust.getEmail());
192         
193         Customer newCust  = new Customer(name,gender,age,phone,email);
194         
195         boolean isreplaced = customerList.replaceCustomer(number - 1, newCust);
196         
197         if(isreplaced)
198         {
199             System.out.println("------------------修改完成------------------");
200             
201         }
202         else
203         {
204             System.out.println("------------------修改失败------------------");
205         }
206     }
207     
208     /**
209      * 
210      * @Description  删除客户的操作
211      * @author  Bytezero·zhenglei!
212      * @date 2021年9月17日上午11:14:19
213      *
214      */
215     private void deleteCustomer()
216     {
217         //System.out.println("删除客户的操作");
218         System.out.println("------------------删除客户------------------");
219         int number;
220         for(;;)
221         {
222             
223             System.out.println("选择待删除客户的编号(-1退出)");
224             
225             
226              number = CMUtility.readInt();
227             
228             if(number == -1)
229             {
230                 return;
231             }
232             
233             Customer customer = customerList.getCustomer(number - 1);
234                 
235             if(customer == null)
236             {
237                 System.out.println("无法找到指定的客户!");
238             }
239             else
240             {
241                 break;
242             }
243 
244             
245         }
246         //找到了指定的客户
247         System.out.print("确认是否删除(Y/N):");
248         
249         char isDelete = CMUtility.readConfirmSelection();
250         if(isDelete == 'Y')
251         {
252             boolean deleteSuccess = customerList.deleteCustomer(number - 1);
253             if(deleteSuccess)
254             {
255                 System.out.println("------------------删除完成------------------");
256             }
257             else
258             {
259                 System.out.println("------------------删除失败------------------");
260             }
261         
262         }
263         else
264         {
265             return;
266         }
267         
268         
269     }
270     
271     /**
272      * 
273      * @Description   显示客户列表的操作
274      * @author  Bytezero·zhenglei!
275      * @date 2021年9月17日上午11:14:29
276      *
277      */
278     private void listAllCustomers()
279     {
280         //System.out.println("显示客户列表的操作");
281         
282         System.out.println("------------------客户列表------------------\n");
283         
284         int total = customerList.getTotal();
285         if(total == 0)
286         {
287             System.out.println("没有客户记录。");
288             
289         }
290         else
291         {
292             System.out.println("编号\t姓名\t\t性别\t年龄\t电话\t\t邮箱\t");
293             Customer[] custs = customerList.getAllCustomers();
294             for(int i =0; i <custs.length;i++)
295             {
296                 Customer cust = custs[i];
297                 System.out.println((i+1)+"\t"+cust.getName()+"\t"+cust.getGender()
298                 +"\t"+cust.getAge()+"\t"+cust.getPhone()+"\t"+cust.getEmail());
299             }
300                 
301         }
302         
303 
304         
305         System.out.println("------------------客户列表完成------------------\n");
306         
307         
308         
309     }
310     
311     
312     public static void main(String[] args) 
313     {
314         CustomerView view = new CustomerView();
315         view.enterMainMenu();
316     }
317     
318 }
  1 package com.bytezero.cim.util;
  2 
  3 import java.util.*;
  4 
  5 /**
  6  * 
  7  * @Description  CMUtility工具类
  8  * 将不同的功能封装为方法,就是可以直接通过调方法使用它的功能,而无需考虑具体的功能实现细节
  9  * @author  Bytezero·zhenglei!      Email:420498246@qq.com
 10  * @version
 11  * @date 2021年9月16日下午6:42:52
 12  * @
 13  *
 14  */
 15 public class CMUtility 
 16 {
 17     private static Scanner scanner = new Scanner(System.in);
 18     /**
 19      * 用于界面菜单的选择,该方法读取键盘,如果用户键入‘1’-‘5’中的任意字符,则返回
 20      * 方法,返回值为用户键入字符
 21      */
 22     public static char readMenuSelection()
 23     {
 24         char c;
 25         for(;;)
 26         {
 27             String str = readKeyBoard(1,false);
 28             c = str.charAt(0);
 29             if(c != '1'&& c != '2' && c != '3' && c != '4' && c != '5')
 30             {
 31                 System.out.print("选择错误,请重新选择:");
 32             }
 33             else break;
 34         }
 35         return c;
 36     }
 37     
 38     /**
 39      * 从键盘读取一个字符,并将其作为方法的返回值
 40      */
 41     public static char readChar()
 42     {
 43         String str = readKeyBoard(1,false);
 44         return str.charAt(0);
 45     }
 46     
 47     /**
 48      * 从键盘读取一个字符,并将其作为方法的返回值
 49      * 如果用户不输入字符而直接回车,方法将以 defaultValue 作为返回值
 50      */
 51     public static char readChar(char defaultValue)
 52     {
 53         String str = readKeyBoard(1,true);
 54         return (str.length()==0)?defaultValue : str.charAt(0);
 55     }
 56     
 57     /**
 58      * 从键盘读取一个长度不超过 2位的整数,并将其作为方法的返回值
 59      */
 60     public static int readInt()
 61     {
 62         int n;
 63         for(;;)
 64         {
 65             String str = readKeyBoard(2,false);
 66             try 
 67             {
 68                 n = Integer.parseInt(str);
 69                 break;
 70             }
 71             catch(NumberFormatException e)
 72             {
 73                 System.out.println("数字输入错误,请重新输入:");
 74             }
 75         }
 76         return n;
 77     }
 78     
 79     /**
 80      * 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值
 81      * 如果用户不输入字符而直接回车,方法将以defaultValue作为返回值
 82      */
 83     public static int readInt(int defaultValue)
 84     {
 85         int n;
 86         for(;;)
 87         {
 88             String str = readKeyBoard(2,true);
 89             if(str.equals(""))
 90             {
 91                 return defaultValue;
 92             }
 93             try
 94             {
 95                 n = Integer.parseInt(str);
 96                 break;
 97             }
 98             catch(NumberFormatException e)
 99             {
100                 System.out.println("数字输入错误,请重新输入:");
101             }
102         }
103         return n;
104     }
105     
106     /**
107      *  从键盘读取一个长度不超过 limit 的字符串,并将其作为方法的返回值
108      */
109     public static String readString(int limit)
110     {
111         return readKeyBoard(limit,false);
112     }
113     
114     /**
115      * 从键盘读取一个长度不超过 limit 的字符串,并将其作为方法的返回值
116      * 如果用户不输入字符而直接回车,方法以 defaultValue作为返回值
117      */
118     public static String readString(int limit,String defaultValue)
119     {
120         String str = readKeyBoard(limit,true);
121         return str.equals("")? defaultValue : str;
122     }
123     /**
124      * 用于确认选择的输入。该方法从键盘读取‘Y’或‘N’,并将其作为方法的返回值
125      * 
126      */
127     public static char readConfirmSelection()
128     {
129         char c;
130         for(;;)
131         {
132             String str = readKeyBoard(1,false).toUpperCase();
133             c = str.charAt(0);
134             if(c == 'Y' || c == 'N')
135             {
136                 break;
137             }
138             else
139             {
140                 System.out.println("选择错误,请重新输入:");
141             }
142         }
143         return c;
144     }
145     
146     private static String  readKeyBoard(int limit,boolean blankReturn)
147     {
148         String line ="";
149         while(scanner.hasNextLine())
150         {
151             line  = scanner.nextLine();
152             if(line.length() == 0)
153             {
154                 if(blankReturn) return line;
155                 else continue;
156             }
157             if(line.length() <1 || line.length() > limit)
158             {
159                 System.out.print("输入长度(不大于"+limit+ ")错误,请重新输入:");
160                 continue;
161             }
162             break;
163         }
164         return line;
165     }
166     
167     
168     
169 }

//界面

 

 // 1

 

 

 

 //2

 

 

 

 

//3

 

 

 

 //4

 

 

//5

 

posted on 2021-09-17 16:38  Bytezero!  阅读(101)  评论(0编辑  收藏  举报