面向对象(中)项目二 2.0

dog_IT 2021-01-24 00:36:09
项目 面向对象 对象 面向


工具类 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 }
主菜单效果

 

 笔记

关于客户管理软件项目编程思路的梳理
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’则不退出

 

 
 
版权声明
本文为[dog_IT]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/zui-ai-java/p/14319698.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云