• Java-CMS


     

    Utility

    1. package com.atguigu.p2.util;
    2. import java.util.*;
    3. public class CMUtility {
    4. public static void main(String[] args) {
    5. //System.out.println(readMenuSelection());
    6. }
    7. private static Scanner scanner = new Scanner(System.in);
    8. private static String readKeyBoard(int limit,boolean blank){
    9. for (;;) {
    10. String str = scanner.nextLine();
    11. if (str.length()>0 && str.length()<=limit) {
    12. return str;
    13. } else if (blank) {
    14. return str;
    15. }else{
    16. System.out.println("请输入长度不超过"+limit+"的指令");
    17. }
    18. }
    19. }
    20. public static char readMenuSelection(){
    21. //获取功能选择
    22. char c;
    23. for(;;){
    24. String str = readKeyBoard(1,false);
    25. c = str.charAt(0);
    26. if (c=='1' || c=='2' || c=='3' || c=='4' || c=='5' ) {
    27. return c;
    28. } else {
    29. System.out.println("选择错误,请重新输入。");
    30. }
    31. }
    32. }
    33. public static char readChar(){
    34. //获取性别
    35. String str = readKeyBoard(1,false);
    36. return str.charAt(0);
    37. }
    38. public static char readChar(char defaultValue){
    39. //修改性别信息时,不输入信息直接回车
    40. String str = readKeyBoard(1,true);
    41. return (str.length()==0)? defaultValue : str.charAt(0);
    42. }
    43. public static int readInt(){
    44. //获取年龄
    45. int n;
    46. for(;;){
    47. String str = readKeyBoard(2,false);
    48. try{
    49. n = Integer.parseInt(str);
    50. return n;
    51. }catch (NumberFormatException e) {
    52. System.out.println("数字输入错误,请重新输入。");
    53. }
    54. }
    55. }
    56. public static int readInt(int defaultValue){
    57. //修改年龄信息时,不输入信息直接回车
    58. int n;
    59. for(;;){
    60. String str = readKeyBoard(2,true);
    61. if (str.equals("")) {
    62. return defaultValue;
    63. }
    64. try{
    65. n = Integer.parseInt(str);
    66. return n;
    67. }catch (NumberFormatException e) {
    68. System.out.println("数字输入错误,请重新输入。");
    69. }
    70. }
    71. }
    72. public static String readString(int limit){
    73. //姓名、电话、邮箱的输入
    74. return readKeyBoard(limit,false);
    75. }
    76. public static String readString(int limit,String defaultValue){
    77. //修改姓名、电话、邮箱时,不输入信息直接回车
    78. String str = readKeyBoard(limit,true);
    79. return str.equals("") ? defaultValue : str;
    80. }
    81. public static char readConfirmSelection(){
    82. //获取确认的输入
    83. for(;;){
    84. String str = readKeyBoard(1,false).toUpperCase();
    85. char c = str.charAt(0);
    86. if (c=='Y' || c=='N') {
    87. return c;
    88. } else {
    89. System.out.println("选择错误,请输入Y/N");
    90. }
    91. }
    92. }
    93. }

    Customer模块

    1. package com.cms3;
    2. public class Customer {
    3. private String name;
    4. private char gender;
    5. private int age;
    6. private String tele;
    7. private String mail;
    8. public Customer() {
    9. }
    10. public Customer(String name,char gender, int age, String tele, String mail) {
    11. this.name = name;
    12. this.gender = gender;
    13. this.age = age;
    14. this.tele = tele;
    15. this.mail = mail;
    16. }
    17. public String getName() {
    18. return name;
    19. }
    20. public void setName(String name) {
    21. this.name = name;
    22. }
    23. public char getGender() {
    24. return gender;
    25. }
    26. public void setGender(char gender) {
    27. this.gender = gender;
    28. }
    29. public int getAge() {
    30. return age;
    31. }
    32. public void setAge(int age) {
    33. this.age = age;
    34. }
    35. public String getTele() {
    36. return tele;
    37. }
    38. public void setTele(String tele) {
    39. this.tele = tele;
    40. }
    41. public String getMail() {
    42. return mail;
    43. }
    44. public void setMail(String mail) {
    45. this.mail = mail;
    46. }
    47. }

    CustomerList模块

    1. package com.cms3;
    2. public class CustomerList {
    3. private int total = 0;
    4. Customer[] custList;
    5. private Customer customer = new Customer("Tom",'m',10,"17717777777","tom@gmail.com");
    6. public CustomerList() {
    7. }
    8. public CustomerList(int total) {
    9. custList = new Customer[total];
    10. addCustomer(customer);
    11. }
    12. public boolean addCustomer(Customer customer) {
    13. if(total < custList.length) {
    14. custList[total ++] = customer;
    15. return true;
    16. }else {
    17. return false;
    18. }
    19. }
    20. public boolean replaceCustomer(int index, Customer customer) {
    21. if(index < 0 || index >= total) {
    22. return false;
    23. }else {
    24. custList[index] = customer;
    25. return true;
    26. }
    27. }
    28. public boolean deleteCustomer(int index) {
    29. if(index < 0 || index >= total) {
    30. return false;
    31. }else {
    32. custList[index] = null;
    33. for(int i = index; i < total - 1; i ++) {
    34. custList[i] = custList[i + 1];
    35. }
    36. total --;
    37. return true;
    38. }
    39. }
    40. public Customer getCustomer(int index) {
    41. if(index >= 0 && index < total) {
    42. return custList[index];
    43. }else {
    44. System.out.println("获取失败");
    45. return null;
    46. }
    47. }
    48. public Customer[] getAllCustomer() {
    49. return custList;
    50. }
    51. public int getTotal() {
    52. return total;
    53. }
    54. }

    CustomerView

    1. package com.cms3;
    2. import com.cms3.Customer;
    3. import com.cms3.CustomerList;
    4. import com.atguigu.p2.util.CMUtility;
    5. public class CustomerView {
    6. private CustomerList custList = new CustomerList(10);
    7. public static void main(String[] args) {
    8. CustomerView CV = new CustomerView();
    9. CV.showMenu();
    10. }
    11. public void showMenu() {
    12. boolean isFlag = true;
    13. while(isFlag) {
    14. System.out.println("客户关系管理软件");
    15. System.out.println("1: 添加客户");
    16. System.out.println("2: 修改客户");
    17. System.out.println("3: 删除客户");
    18. System.out.println("4: 显示客户列表");
    19. System.out.println("5: 退出");
    20. int choiceMenu = CMUtility.readMenuSelection();
    21. switch(choiceMenu) {
    22. case '1':
    23. addClient();
    24. break;
    25. case '2':
    26. replaceClient();
    27. break;
    28. case '3':
    29. deleteClient();
    30. break;
    31. case '4':
    32. getAllClient();
    33. break;
    34. case '5':
    35. System.out.println("确定要退出吗?(Y/N):");
    36. char choiceYN = CMUtility.readConfirmSelection();
    37. if(choiceYN == 'Y') {
    38. isFlag = false;
    39. }
    40. break;
    41. default:
    42. System.out.println("输入错误,请重新输入");
    43. }
    44. }
    45. }
    46. public void addClient() {
    47. System.out.println("添加客户");
    48. System.out.println("姓名:");
    49. String name = CMUtility.readString(10);
    50. System.out.println("性别");
    51. char gender = CMUtility.readChar();
    52. System.out.println("年龄");
    53. int age = CMUtility.readInt();
    54. System.out.println("电话");
    55. String tele = CMUtility.readString(11);
    56. System.out.println("邮箱");
    57. String mail = CMUtility.readString(30);
    58. Customer customer = new Customer(name,gender,age,tele,mail);
    59. boolean addResult = custList.addCustomer(customer);
    60. if(addResult) {
    61. System.out.println("添加成功");
    62. }else {
    63. System.out.println("列表已满,添加失败");
    64. }
    65. }
    66. public void replaceClient() {
    67. System.out.println("修改客户");
    68. System.out.println("请输入要修改的客户编号(-1退出)");
    69. int index = CMUtility.readInt();
    70. if(index == -1) {
    71. return;
    72. }else {
    73. Customer customer = custList.getCustomer(index - 1);
    74. System.out.println("姓名(" + customer.getName() + "):");
    75. String name = CMUtility.readString(10,customer.getName());
    76. System.out.println("性别(" + customer.getGender() + "):");
    77. char gender = CMUtility.readChar(customer.getGender());
    78. System.out.println("年龄(" + customer.getAge() + "):");
    79. int age = CMUtility.readInt(customer.getAge());
    80. System.out.println("电话(" + customer.getTele() + "):");
    81. String tele = CMUtility.readString(11,customer.getTele());
    82. System.out.println("邮箱(" + customer.getMail() + "):");
    83. String mail = CMUtility.readString(20,customer.getMail());
    84. Customer newCustomer = new Customer(name,gender,age,tele, mail);
    85. boolean replaceResult = custList.replaceCustomer(index - 1, newCustomer);
    86. if(replaceResult) {
    87. System.out.println("修改成功");
    88. }else {
    89. System.out.println("修改失败");
    90. }
    91. }
    92. }
    93. public void deleteClient() {
    94. System.out.println("删除客户");
    95. System.out.println("请输入要删除的客户编号,(-1退出)");
    96. int index = CMUtility.readInt();
    97. if(index == -1) {
    98. return;
    99. }else {
    100. boolean deleteResult = custList.deleteCustomer(index - 1);
    101. if(deleteResult) {
    102. System.out.println("删除成功");
    103. }else {
    104. System.out.println("删除失败");
    105. }
    106. }
    107. }
    108. public void getAllClient() {
    109. System.out.println("客户列表");
    110. int total = custList.getTotal();
    111. Customer customer[] = custList.getAllCustomer();
    112. System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
    113. if(total == 0) {
    114. System.out.println("列表为空");
    115. return;
    116. }else {
    117. for(int i = 0; i < total; i ++) {
    118. System.out.println("" + (i + 1)
    119. + "\t" + customer[i].getName()
    120. + "\t" + customer[i].getGender()
    121. + "\t" + customer[i].getAge()
    122. + "\t" + customer[i].getTele()
    123. + "\t" + customer[i].getMail());
    124. }
    125. }
    126. }
    127. }

  • 相关阅读:
    Python实现点选验证码识别, B站模拟登陆
    从GDPR和个保法看,为什么要做数据合规?
    E: Unable to locate package libboost-all-dev
    JDK9相比于JDK8,究竟变强了多少
    #include <sensor_msgs/Imu.h>这个ROS头文件包含的功能有哪些?
    薪酬不变,每周只上四天班,英国试行全球最大规模“四天工作制”
    【Linux】HTTPS协议
    IOS手机耗电量测试
    Linux线程安全
    算法选修(J.琴和可莉)(为选修画上句号)
  • 原文地址:https://blog.csdn.net/JSWANGCHANG/article/details/127729965