尚硅谷_宋红康_Project2.pptx

    1. package com.atguigu.project;
    2. /*
    3. 该类封装客户的以下信息:
    4. String name :客户姓名
    5. char gender :性别
    6. int age :年龄
    7. String phone:电话号码
    8. String email :电子邮箱
    9. */
    10. public class Customer {
    11. private String name;
    12. private char gender;
    13. private int age;
    14. private String phone;
    15. private String email;
    16. public Customer() {
    17. }
    18. public Customer(String name, char gender, int age, String phone, String email) {
    19. this.name = name;
    20. this.gender = gender;
    21. this.age = age;
    22. this.phone = phone;
    23. this.email = email;
    24. }
    25. public String getName() {
    26. return name;
    27. }
    28. public void setName(String name) {
    29. this.name = name;
    30. }
    31. public char getGender() {
    32. return gender;
    33. }
    34. public void setGender(char gender) {
    35. this.gender = gender;
    36. }
    37. public int getAge() {
    38. return age;
    39. }
    40. public void setAge(int age) {
    41. this.age = age;
    42. }
    43. public String getPhone() {
    44. return phone;
    45. }
    46. public void setPhone(String phone) {
    47. this.phone = phone;
    48. }
    49. public String getEmail() {
    50. return email;
    51. }
    52. public void setEmail(String email) {
    53. this.email = email;
    54. }
    55. }
    1. package com.atguigu.project;
    2. public class CustomerList {
    3. private Customer[] customers;
    4. private int total = 0;
    5. public CustomerList() {
    6. }
    7. /**
    8. * 用途:构造器,用来初始化customers数组
    9. * 参数:totalCustomer:指定customers数组的最大空间
    10. *
    11. * @param totalCustomer 指定customers数组的最大空间
    12. */
    13. public CustomerList(int totalCustomer) {
    14. customers = new Customer[totalCustomer];
    15. }
    16. /**
    17. * 用途:将参数customer添加到数组中最后一个客户对象记录之后
    18. * 参数:customer指定要添加的客户对象
    19. * 返回:添加成功返回true;false表示数组已满,无法添加
    20. *
    21. * @param customer 指定要添加的客户对象
    22. * @return 添加成功返回true;false表示数组已满,无法添加
    23. */
    24. public boolean addCustomer(Customer customer) {
    25. if (total < customers.length) {
    26. customers[total++] = customer;
    27. return true;
    28. } else {
    29. return false;
    30. }
    31. }
    32. /**
    33. * 用途:用参数customer替换数组中由index指定的对象
    34. * 参数:customer指定替换的新客户对象
    35. * index指定所替换对象在数组中的位置,从0开始
    36. * 返回:替换成功返回true;false表示索引无效,无法替换
    37. *
    38. * @param index 指定所替换对象在数组中的位置,从0开始
    39. * @param cust customer指定替换的新客户对象
    40. * @return 替换成功返回true;false表示索引无效,无法替换
    41. */
    42. public boolean replaceCustomer(int index, Customer cust) {
    43. if (index >= 0 && index < customers.length) {
    44. customers[index] = cust;
    45. return true;
    46. } else {
    47. return false;
    48. }
    49. }
    50. /**
    51. * 用途:从数组中删除参数index指定索引位置的客户对象记录
    52. * 参数: index指定所删除对象在数组中的索引位置,从0开始
    53. * 返回:删除成功返回true;false表示索引无效,无法删除
    54. *
    55. * @param index index指定所删除对象在数组中的索引位置,从0开始
    56. * @return 删除成功返回true;false表示索引无效,无法删除
    57. */
    58. public boolean deleteCustomer(int index) {
    59. if (index >= 0 && index < customers.length) {
    60. customers[index] = null;
    61. return true;
    62. } else {
    63. return false;
    64. }
    65. }
    66. /**
    67. * 用途:返回数组中记录的所有客户对象
    68. * 返回: Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
    69. *
    70. * @return Customer[] 数组中包含了当前所有客户对象,该数组长度与对象个数相同。
    71. */
    72. public Customer[] getAllCustomers() {
    73. return customers;
    74. }
    75. /**
    76. * 用途:返回参数index指定索引位置的客户对象记录
    77. * 参数: index指定所要获取的客户在数组中的索引位置,从0开始
    78. * 返回:封装了客户信息的Customer对象
    79. *
    80. * @param index 指定所要获取的客户在数组中的索引位置,从0开始
    81. * @return 封装了客户信息的Customer对象
    82. */
    83. public Customer getCustomer(int index) {
    84. if (index >= 0 && index < customers.length) {
    85. return customers[index];
    86. } else {
    87. System.out.println("索引有问题,反回第一个客户信息");
    88. return customers[0];
    89. }
    90. }
    91. public int getTotal() {
    92. return this.total;
    93. }
    94. }
    1. package com.atguigu.project;
    2. import java.util.Scanner;
    3. public class CustomerView {
    4. CustomerList customerList = new CustomerList(10);
    5. Scanner sc = new Scanner(System.in);
    6. /**
    7. * 用途:显示主菜单,响应用户输入,根据用户操作分别调用其他相应的成员方法(如addNewCustomer),以完成客户信息处理。
    8. */
    9. public void enterMainMenu() {
    10. System.out.println("-----------------客户信息管理软件-----------------");
    11. System.out.println(" 1 添 加 客 户");
    12. System.out.println(" 2 修 改 客 户");
    13. System.out.println(" 3 删 除 客 户");
    14. System.out.println(" 4 客 户 列 表");
    15. System.out.println(" 5 退 出");
    16. System.out.print(" 请选择(1-5):");
    17. }
    18. /**
    19. *
    20. */
    21. private void addNewCustomer() {
    22. System.out.println("---------------------添加客户---------------------");
    23. Customer customer = new Customer();
    24. System.out.print("姓名:");
    25. customer.setName(sc.next());
    26. System.out.print("性别:");
    27. customer.setGender(sc.next().charAt(0));
    28. System.out.print("年龄:");
    29. customer.setAge(sc.nextInt());
    30. System.out.print("电话:");
    31. customer.setPhone(sc.next());
    32. System.out.print("邮箱:");
    33. customer.setEmail(sc.next());
    34. customerList.addCustomer(customer);
    35. System.out.println("---------------------添加完成---------------------");
    36. }
    37. private void modifyCustomer() {
    38. System.out.println("---------------------修改客户---------------------");
    39. System.out.print("请选择待修改客户编号(-1退出):");
    40. int choice = sc.nextInt()-1;
    41. //返回目标类customerList.getCustomer(choice);
    42. Customer customer = new Customer();
    43. System.out.print("姓名(" + customerList.getCustomer(choice).getName() + "):");
    44. customer.setName(sc.next());
    45. System.out.print("性别(" + customerList.getCustomer(choice).getGender() + "):");
    46. customer.setGender(sc.next().charAt(0));
    47. System.out.print("年龄(" + customerList.getCustomer(choice).getAge() + "):");
    48. customer.setAge(sc.nextInt());
    49. System.out.print("电话(" + customerList.getCustomer(choice).getPhone() + "):");
    50. customer.setPhone(sc.next());
    51. System.out.print("邮箱(" + customerList.getCustomer(choice).getEmail() + "):");
    52. customer.setEmail(sc.next());
    53. customerList.replaceCustomer(choice, customer);
    54. System.out.println("---------------------添加完成---------------------");
    55. }
    56. private void deleteCustomer() {
    57. System.out.println("---------------------删除客户---------------------");
    58. System.out.print("请选择待删除客户编号(-1退出):");
    59. int choice = sc.nextInt();
    60. System.out.println("确认是否删除(Y/N):");
    61. String flag = sc.next();
    62. if (flag.toLowerCase().equals("y")) {
    63. customerList.deleteCustomer(choice);
    64. } else {
    65. System.out.println("删除失败!!!");
    66. }
    67. System.out.println("---------------------删除完成---------------------");
    68. }
    69. private void listAllCustomers() {
    70. System.out.println("---------------------------客户列表---------------------------");
    71. System.out.println("编号 姓名 性别 年龄 电话 邮箱");
    72. //得到客户数组customerList.getAllCustomers();
    73. for (int i = 0; i < customerList.getTotal(); i++) {
    74. if (customerList.getCustomer(i) != null) {
    75. System.out.println(i + 1 + "\t\t" + customerList.getCustomer(i).getName() + "\t\t" + customerList.getCustomer(i).getGender()
    76. +"\t\t"+customerList.getCustomer(i).getAge()
    77. + "\t\t" + customerList.getCustomer(i).getPhone() + "\t\t\t\t" + customerList.getCustomer(i).getEmail());
    78. } else {
    79. System.out.println(i + 1 + "\t" + "什么都没有");
    80. }
    81. }
    82. System.out.println("-------------------------客户列表完成-------------------------\n");
    83. }
    84. public static void main(String[] args) {
    85. Scanner sc = new Scanner(System.in);
    86. CustomerView cv = new CustomerView();
    87. label:
    88. while (true){
    89. cv.enterMainMenu();
    90. int yourChoice=sc.nextInt();
    91. switch (yourChoice){
    92. case 1:
    93. cv.addNewCustomer();
    94. break;
    95. case 2:
    96. cv.modifyCustomer();
    97. break;
    98. case 3:
    99. cv.deleteCustomer();
    100. break;
    101. case 4:
    102. cv.listAllCustomers();
    103. break;
    104. case 5:
    105. break label;
    106. default:
    107. System.out.println("输入有问题,请重新输入");
    108. break;
    109. }
    110. }
    111. }
    112. }