Object oriented (middle) project 2.0

dog_ IT 2021-01-24 00:36:38
object oriented middle project


 Tool class CMUtility
 1 package day1_18;
 2
 3 import java.util.Scanner;
 4
 5 public class CMUtility {
 6
 7 // Read the main menu options 
 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(" Input option error , Please re-enter :");
 14 } else {
 15 return c;
 16  }
 17  }
 18  }
 19
 20 // Read string 
 21 public static String readString(int limit) {
 22 return readKeyBoard(limit, false);
 23  }
 24
 25 // Read string ( direct Enter In a new line , Return the default value 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 // Read integers 
 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(" Please enter no more than two digits :");
 41  }
 42  }
 43  }
 44
 45 // Read integers ( direct Enter In a new line , Return the default value 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(" Please enter no more than two digits :");
 56  }
 57 return 111111111;
 58  }
 59
 60 // Read character 
 61 public static char readChar() {
 62 String str = readKeyBoard(1, false);
 63 return str.charAt(0);
 64  }
 65
 66
 67 // Read character ( direct Enter In a new line , Return the default value 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 // Are you sure you want to exit 
 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(" Input option error , Please re-enter :");
 81 } else {
 82 break;
 83  }
 84  }
 85 return selection;
 86  }
 87
 88
 89 // Read user input 
 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(" Please re-enter :");
102 }else if (str.length() > limit) {
103 System.out.print(" The input length cannot be greater than " + limit + ", Please re-enter :");
104 } else {
105 break;
106  }
107  }
108  }
109 return str;
110  }
111
112 }

Customer entity class 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 }

 Operation customer class 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 // Add customers 
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 // Query customer list 
21 public boolean listCustomers() {
22 if (total == 0) {
23 return false;
24  }
25 System.out.print(" Number \t full name \t Gender \t Age \t Phone number \t email \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 // Modify the customer information in the specified location 
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 // Find customers in a specific location 
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 // Delete the customer in the specified location 
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 }

 

 Client interaction class 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 // The main menu 
 16 public void enterMainMenu() {
 17 boolean isFlag = true;
 18 while (isFlag) {
 19 System.out.println("\n----------------- Customer information management software -----------------\n");
 20 System.out.println(" 1 add Add customer Household ");
 21 System.out.println(" 2 repair Change customer Household ");
 22 System.out.println(" 3 Delete except customer Household ");
 23 System.out.println(" 4 customer Household Column surface ");
 24 System.out.println(" 5 refund Out \n");
 25 System.out.print(" Please select (1-5):");
 26 char menu = CMUtility.readMenuSelection();
 27 switch (menu) {
 28 case '1':
 29 //System.out.println(" Add customer action ");
 30  saveCustomer();
 31 break;
 32 case '2':
 33 //System.out.println(" Modify customer actions ");
 34  replaceCustomer();
 35 break;
 36 case '3':
 37 //System.out.println(" Delete customer action ");
 38  removeCustomer();
 39 break;
 40 case '4':
 41 //System.out.println(" Customer list operation ");
 42  listCustomers();
 43 break;
 44 case '5':
 45 //System.out.println(" Exit operation ");
 46 System.out.print(" Confirm whether to exit (Y/N):");
 47 char isExit = CMUtility.readConfirmSelection();
 48 if (isExit == 'Y') {
 49 isFlag = false;
 50  }
 51  }
 52  }
 53  }
 54
 55 // Add customers 
 56 public void saveCustomer() {
 57 System.out.println("---------- Add customers ----------");
 58 System.out.print(" full name :");
 59 String name = CMUtility.readString(4);
 60 System.out.print(" Gender :");
 61 char gender = CMUtility.readChar();
 62 System.out.print(" Age :");
 63 int age = CMUtility.readInt();
 64 System.out.print(" Phone number :");
 65 String phone = CMUtility.readString(13);
 66 System.out.print(" email :");
 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("---------- Add customer successfully ----------");
 72 } else {
 73 System.out.println("---------- Add customer successfully ----------");
 74  }
 75  }
 76
 77 // Check out the list of customers 
 78 public boolean listCustomers() {
 79 System.out.println("---------- Customer list ----------");
 80 boolean isListed = customerList.listCustomers();
 81 if (!isListed) {
 82 System.out.println(" Customer information is empty ");
 83  }
 84 System.out.println("---------- Customer list complete ----------");
 85 return isListed;
 86  }
 87
 88 // Modify the customer information in the specified location 
 89 public void replaceCustomer() {
 90 int index;
 91  Customer customer;
 92 if (listCustomers()) {
 93 System.out.print(" Please enter the number of the customer to be modified :");
 94 for (; ; ) {
 95 index = CMUtility.readInt() -1;
 96 customer = customerList.getCustomer(index);
 97 if (customer == null) {
 98 System.out.print(" I'm sorry , Without this client , Please re-enter :");
 99 } else {
100 break;
101  }
102  }
103 System.out.print(" full name (" + customer.getName() + "):");
104 String name = CMUtility.readString(4, customer.getName());
105 System.out.print(" Gender (" + customer.getGender() + "):");
106 char gender = CMUtility.readChar(customer.getGender());
107 System.out.print(" Age (" + customer.getAge() + "):");
108 int age = CMUtility.readInt(customer.getAge());
109 System.out.print(" Telephone (" + customer.getPhone() + "):");
110 String phone = CMUtility.readString(13, customer.getPhone());
111 System.out.print(" mailbox (" + 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("----------- Modify customer successfully -----------");
118 } else {
119 System.out.println("----------- Failed to modify customer -----------");
120  }
121 } else {
122 System.out.print("\n reminder : The customer list is empty , Please add customers first \n");
123  enterMainMenu();
124  }
125  }
126
127 // Delete customer 
128 public void removeCustomer() {
129 if (listCustomers()) {
130 System.out.print(" Please enter the number of the customer you want to delete :");
131 for (; ; ) {
132 int index = CMUtility.readInt() -1;
133 boolean isRemove = customerList.deleteCustomer(index);
134 if (isRemove) {
135 System.out.println("---------- Delete successful ----------");
136 break;
137 } else {
138 System.out.print(" The customer you are about to delete does not exist , Please re select the number :");
139  }
140  }
141 } else {
142 System.out.print("\n reminder : The customer list is empty , Please select another action \n");
143  enterMainMenu();
144  }
145  }
146 }
 Main menu effect 

 

  note

 About customer management software project programming ideas
1. Main menu list options
1.1 Add customers
① Get keyboard input , Depending on the type of the attribute and the length of the character , Write in the tool class
The corresponding method of getting keyboard input
② After the client input data is assigned to each attribute , Use constructors to create Customer object , Add more
Add to customers Array , And what's actually stored in the array Customer Number of objects total Add 1
1.2 Check out the list of customers
① Traverse customers Every... In the array customer object , Get the value of each property
1.3 Modify the customer information of a number in the customer list
① The number is in the customer list, for example 1,2,3,... A series of natural numbers , Notice how it relates to the array index : Number -1 = Array index
② After the client keyboard input receives the specified number , First of all, we need to determine whether this number exists in the customer list , That is to ( Designated number -1)
As array index , Check whether the object of this index in the array is empty , If it is empty , It means that there is no such user in the user list , since
It's illogical to modify a customer without this user
③ If there are customers in the customer list that need to be modified , Then the method of receiving keyboard input as attribute value needs to be redefined according to the situation ,
Because if a customer's attribute doesn't want to be modified , Then it is designed to input directly Enter, Indicates that when an empty string is received , new customer
The property value of the object is still the original property value
1.4 Delete the customer information of a number in the customer list
① The number is in the customer list, for example 1,2,3,... A series of natural numbers , Notice how it relates to the array index : Number -1 = Array index
② After the client keyboard input receives the specified number , First of all, we need to determine whether this number exists in the customer list , That is to ( Designated number -1)
As array index , Check whether the object of this index in the array is empty , If it is empty , It means that there is no such user in the user list , since
It's illogical to delete customers without this user
③ If there are customers in the customer list that need to be deleted , From the client to be deleted in the array customers Start at the index position of , Yuan at the back
The prime covers the previous element , In this way, the deletion operation is realized
1.5 Exit the main menu
Get keyboard input , Take character , If it's a capital letter ‘Y’, The exit ; If ‘N’ You don't quit 

 

 
 
版权声明
本文为[dog_ IT]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210124003606754I.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课程百度云