eclipes常见操作总结及项目2经验总结

eclipes提示:

打开eclipes 选择window->perference->java->editor->content assistant

  1. * Eclipse中的快捷键:
  2. * 1.补全代码的声明:alt + /
  3. * 2.快速修复: ctrl + 1
  4. * 3.批量导包:ctrl + shift + o
  5. * 4.使用单行注释:ctrl + /
  6. * 5.使用多行注释: ctrl + shift + /
  7. * 6.取消多行注释:ctrl + shift + \
  8. * 7.复制指定行的代码:ctrl + alt + down ctrl + alt + up
  9. * 8.删除指定行的代码:ctrl + d
  10. * 9.上下移动代码:alt + up alt + down
  11. * 10.切换到下一行代码空位:shift + enter
  12. * 11.切换到上一行代码空位:ctrl + shift + enter
  13. * 12.如何查看源码:ctrl + 选中指定的结构 ctrl + shift + t
  14. * 13.退回到前一个编辑的页面:alt + left
  15. * 14.进入到下一个编辑的页面(针对于上面那条来说的):alt + right
  16. * 15.光标选中指定的类,查看继承树结构:ctrl + t
  17. * 16.复制代码: ctrl + c
  18. * 17.撤销: ctrl + z
  19. * 18.反撤销: ctrl + y
  20. * 19.剪切:ctrl + x
  21. * 20.粘贴:ctrl + v
  22. * 21.保存: ctrl + s
  23. * 22.全选:ctrl + a
  24. * 23.格式化代码: ctrl + shift + f
  25. * 24.选中数行,整体往后移动:tab
  26. * 25.选中数行,整体往前移动:shift + tab
  27. * 26.在当前类中,显示类结构,并支持搜索指定的方法、属性等:ctrl + o
  28. * 27.批量修改指定的变量名、方法名、类名等:alt + shift + r
  29. * 28.选中的结构的大小写的切换:变成大写: ctrl + shift + x
  30. * 29.选中的结构的大小写的切换:变成小写:ctrl + shift + y
  31. * 30.调出生成getter/setter/构造器等结构: alt + shift + s
  32. * 31.显示当前选择资源(工程 or 文件)的属性:alt + enter
  33. * 32.快速查找:参照选中的Word快速定位到下一个 ctrl + k
  34. *
  35. * 33.关闭当前窗口:ctrl + w
  36. * 34.关闭所有的窗口:ctrl + shift + w
  37. * 35.查看指定的结构使用过的地方:ctrl + alt + g
  38. * 36.查找与替换:ctrl + f
  39. * 37.最大化当前的Viewctrl + m
  40. * 38.直接定位到当前行的首位:home
  41. * 39.直接定位到当前行的末位:end
  42. //注意:部分操作需要选中代码

当eclipes中默认快捷键与电脑冲突时,可以设置eclipses的默认快捷键:

项目二:

customer类:

  1. public class Customer {
  2. private String name;
  3. private char gender;
  4. private int age;
  5. private String phone;
  6. private String email;
  7. public Customer(String name, char gender, int age) {
  8. this(name, gender, age, "", "");
  9. }
  10. public Customer(String name, char gender, int age, String phone,
  11. String email) {
  12. this.name = name;
  13. this.gender = gender;
  14. this.age = age;
  15. this.phone = phone;
  16. this.email = email;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24. public char getGender() {
  25. return gender;
  26. }
  27. public void setGender(char gender) {
  28. this.gender = gender;
  29. }
  30. public int getAge() {
  31. return age;
  32. }
  33. public void setAge(int age) {
  34. this.age = age;
  35. }
  36. public String getPhone() {
  37. return phone;
  38. }
  39. public void setPhone(String phone) {
  40. this.phone = phone;
  41. }
  42. public String getEmail() {
  43. return email;
  44. }
  45. public void setEmail(String email) {
  46. this.email = email;
  47. }
  48. public String getDetails() {
  49. return name + "\t" + gender + "\t" + age + "\t\t" + phone + "\t" + email;
  50. }
  51. }

CustomerView类:

  1. public class CustomerView {
  2. private CustomerList customers = new CustomerList(10);
  3. public CustomerView() {
  4. Customer cust = new Customer("张三", '男', 30, "010-56253825",
  5. "abc@email.com");
  6. customers.addCustomer(cust);
  7. }
  8. public void enterMainMenu() {
  9. boolean loopFlag = true;
  10. do {
  11. System.out
  12. .println("\n-----------------客户信息管理软件-----------------\n");
  13. System.out.println(" 1 添 加 客 户");
  14. System.out.println(" 2 修 改 客 户");
  15. System.out.println(" 3 删 除 客 户");
  16. System.out.println(" 4 客 户 列 表");
  17. System.out.println(" 5 退 出\n");
  18. System.out.print(" 请选择(1-5):");
  19. char key = CMUtility.readMenuSelection();
  20. System.out.println();
  21. switch (key) {
  22. case '1':
  23. addNewCustomer();
  24. break;
  25. case '2':
  26. modifyCustomer();
  27. break;
  28. case '3':
  29. deleteCustomer();
  30. break;
  31. case '4':
  32. listAllCustomers();
  33. break;
  34. case '5':
  35. System.out.print("确认是否退出(Y/N):");
  36. char yn = CMUtility.readConfirmSelection();
  37. if (yn == 'Y')
  38. loopFlag = false;
  39. break;
  40. }
  41. } while (loopFlag);
  42. }
  43. private void addNewCustomer() {
  44. System.out.println("---------------------添加客户---------------------");
  45. System.out.print("姓名:");
  46. String name = CMUtility.readString(4);
  47. System.out.print("性别:");
  48. char gender = CMUtility.readChar();
  49. System.out.print("年龄:");
  50. int age = CMUtility.readInt();
  51. System.out.print("电话:");
  52. String phone = CMUtility.readString(15);
  53. System.out.print("邮箱:");
  54. String email = CMUtility.readString(15);
  55. Customer cust = new Customer(name, gender, age, phone, email);
  56. boolean flag = customers.addCustomer(cust);
  57. if (flag) {
  58. System.out
  59. .println("---------------------添加完成---------------------");
  60. } else {
  61. System.out.println("----------------记录已满,无法添加-----------------");
  62. }
  63. }
  64. private void modifyCustomer() {
  65. System.out.println("---------------------修改客户---------------------");
  66. int index = 0;
  67. Customer cust = null;
  68. for (;;) {
  69. System.out.print("请选择待修改客户编号(-1退出):");
  70. index = CMUtility.readInt();
  71. if (index == -1) {
  72. return;
  73. }
  74. cust = customers.getCustomer(index - 1);
  75. if (cust == null) {
  76. System.out.println("无法找到指定客户!");
  77. } else
  78. break;
  79. }
  80. System.out.print("姓名(" + cust.getName() + "):");
  81. String name = CMUtility.readString(4, cust.getName()); //如果没有输入字符串就以原先对象的值为返回值
  82. System.out.print("性别(" + cust.getGender() + "):");
  83. char gender = CMUtility.readChar(cust.getGender());
  84. System.out.print("年龄(" + cust.getAge() + "):");
  85. int age = CMUtility.readInt(cust.getAge());
  86. System.out.print("电话(" + cust.getPhone() + "):");
  87. String phone = CMUtility.readString(15, cust.getPhone());
  88. System.out.print("邮箱(" + cust.getEmail() + "):");
  89. String email = CMUtility.readString(15, cust.getEmail());
  90. cust = new Customer(name, gender, age, phone, email);
  91. boolean flag = customers.replaceCustomer(index - 1, cust); //将修改信息创建新的对象调用flag判断成立
  92. if (flag) {
  93. System.out
  94. .println("---------------------修改完成---------------------");
  95. } else {
  96. System.out.println("----------无法找到指定客户,修改失败--------------");
  97. }
  98. }
  99. private void deleteCustomer() {
  100. System.out.println("---------------------删除客户---------------------");
  101. int index = 0;//将index和cust定义在循环外,使循环内的各处都可以访问变量
  102. Customer cust = null;
  103. for (;;) {//这种for循环的目的是为了在输入错误的条件下继续一直输入,直至正确
  104. System.out.print("请选择待删除客户编号(-1退出):");
  105. index = CMUtility.readInt();
  106. if (index == -1) {
  107. return; //跳出循环,即进入entMainMune(因为在entMainMune中被调用)
  108. }
  109. cust = customers.getCustomer(index - 1);
  110. if (cust == null) {
  111. System.out.println("无法找到指定客户!");
  112. } else
  113. break;
  114. }
  115. System.out.print("确认是否删除(Y/N):");
  116. char yn = CMUtility.readConfirmSelection();
  117. if (yn == 'N')
  118. return;
  119. boolean flag = customers.deleteCustomer(index - 1);
  120. if (flag) {
  121. System.out
  122. .println("---------------------删除完成---------------------");
  123. } else {
  124. System.out.println("----------无法找到指定客户,删除失败--------------");
  125. }
  126. }
  127. private void listAllCustomers() {
  128. System.out.println("---------------------------客户列表---------------------------");
  129. Customer[] custs = customers.getAllCustomers();
  130. if (custs.length == 0) {
  131. System.out.println("没有客户记录!");
  132. } else {
  133. System.out.println("编号\t姓名\t性别\t年龄\t\t电话\t\t邮箱");
  134. for (int i = 0; i < custs.length; i++) {
  135. // System.out.println(i + 1 + "\t" + custs[i].getName() + "\t" + custs[i].getGender() + "\t" + custs[i].getAge() + "\t\t" + custs[i].getPhone() + "\t" + custs[i].getEmail());
  136. System.out.println((i+1) + "\t" + custs[i].getDetails());
  137. }
  138. }
  139. System.out.println("-------------------------客户列表完成-------------------------");
  140. }
  141. public static void main(String[] args) {
  142. CustomerView cView = new CustomerView();
  143. cView.enterMainMenu();
  144. }
  145. }

CustomerList类:

  1. public class CustomerList {
  2. private Customer[] customers;
  3. private int total = 0;
  4. public CustomerList(int totalCustomer) {
  5. customers = new Customer[totalCustomer];
  6. }
  7. public boolean addCustomer(Customer customer) {
  8. if (total >= customers.length) return false;
  9. customers[total++] = customer;
  10. return true;
  11. }
  12. public boolean replaceCustomer(int index, Customer cust) {
  13. if (index < 0 || index >= total) return false;
  14. customers[index] = cust;
  15. return true;
  16. }
  17. public boolean deleteCustomer(int index) {
  18. if (index < 0 || index >= total) return false;
  19. for (int i = index; i < total - 1; i++) {
  20. customers[i] = customers[i + 1];
  21. }
  22. customers[--total] = null;
  23. return true;
  24. }
  25. public Customer[] getAllCustomers() {
  26. Customer[] custs = new Customer[total];
  27. for (int i = 0; i < total; i++) {
  28. custs[i] = customers[i];
  29. }
  30. return custs;
  31. }
  32. public int getTotal() {
  33. return total;
  34. }
  35. public Customer getCustomer(int index) {
  36. if (index < 0 || index >= total) return null;
  37. return customers[index];
  38. }
  39. }

CMUtility类:

  1. import java.util.*;
  2. /**
  3. CMUtility工具类:
  4. 将不同的功能封装为方法,就是可以直接通过调用方法使用它的功能,而无需考虑具体的功能实现细节。
  5. */
  6. public class CMUtility {
  7. private static Scanner scanner = new Scanner(System.in);
  8. /**
  9. 用于界面菜单的选择。该方法读取键盘,如果用户键入’1’-’5’中的任意字符,则方法返回。返回值为用户键入字符。
  10. */
  11. public static char readMenuSelection() {
  12. char c;
  13. for (; ; ) {
  14. String str = readKeyBoard(1, false);
  15. c = str.charAt(0);
  16. if (c != '1' && c != '2' &&
  17. c != '3' && c != '4' && c != '5') {
  18. System.out.print("选择错误,请重新输入:");
  19. } else break;
  20. }
  21. return c;
  22. }
  23. /**
  24. 从键盘读取一个字符,并将其作为方法的返回值。
  25. */
  26. public static char readChar() {
  27. String str = readKeyBoard(1, false);
  28. return str.charAt(0);
  29. }
  30. /**
  31. 从键盘读取一个字符,并将其作为方法的返回值。
  32. 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
  33. */
  34. public static char readChar(char defaultValue) {
  35. String str = readKeyBoard(1, true);
  36. return (str.length() == 0) ? defaultValue : str.charAt(0);
  37. }
  38. /**
  39. 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
  40. */
  41. public static int readInt() {
  42. int n;
  43. for (; ; ) {
  44. String str = readKeyBoard(2, false);
  45. try {
  46. n = Integer.parseInt(str);
  47. break;
  48. } catch (NumberFormatException e) {
  49. System.out.print("数字输入错误,请重新输入:");
  50. }
  51. }
  52. return n;
  53. }
  54. /**
  55. 从键盘读取一个长度不超过2位的整数,并将其作为方法的返回值。
  56. 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
  57. */
  58. public static int readInt(int defaultValue) {
  59. int n;
  60. for (; ; ) {
  61. String str = readKeyBoard(2, true);
  62. if (str.equals("")) {
  63. return defaultValue;
  64. }
  65. try {
  66. n = Integer.parseInt(str);
  67. break;
  68. } catch (NumberFormatException e) {
  69. System.out.print("数字输入错误,请重新输入:");
  70. }
  71. }
  72. return n;
  73. }
  74. /**
  75. 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
  76. */
  77. public static String readString(int limit) {
  78. return readKeyBoard(limit, false);
  79. }
  80. /**
  81. 从键盘读取一个长度不超过limit的字符串,并将其作为方法的返回值。
  82. 如果用户不输入字符而直接回车,方法将以defaultValue 作为返回值。
  83. */
  84. public static String readString(int limit, String defaultValue) {
  85. String str = readKeyBoard(limit, true);
  86. return str.equals("")? defaultValue : str;
  87. }
  88. /**
  89. 用于确认选择的输入。该方法从键盘读取‘Y’或’N’,并将其作为方法的返回值。
  90. */
  91. public static char readConfirmSelection() {
  92. char c;
  93. for (; ; ) {
  94. String str = readKeyBoard(1, false).toUpperCase();
  95. c = str.charAt(0);
  96. if (c == 'Y' || c == 'N') {
  97. break;
  98. } else {
  99. System.out.print("选择错误,请重新输入:");
  100. }
  101. }
  102. return c;
  103. }
  104. private static String readKeyBoard(int limit, boolean blankReturn) {
  105. String line = "";
  106. while (scanner.hasNextLine()) {
  107. line = scanner.nextLine();
  108. if (line.length() == 0) {
  109. if (blankReturn) return line;
  110. else continue;
  111. }
  112. if (line.length() < 1 || line.length() > limit) {
  113. System.out.print("输入长度(不大于" + limit + ")错误,请重新输入:");
  114. continue;
  115. }
  116. break;
  117. }
  118. return line;
  119. }
  120. }

项目三:

软件设计架构:

  1. package com.atguigu.team.service;
  2. import com.atguigu.team.domain.*;
  3. import static com.atguigu.team.service.Data.*;//直接导入data类,可直接引用data中的静态量
  4. public class NameListService {
  5. private Employee[] employees;//employees多处使用在外声明
  6. /*根据项目提供的Data类构建相应大小的employees数组
  7. 再根据Data类中的数据构建不同的对象,包括Employee、Programmer、Designer和Architect对象,以及相关联的Equipment子类的对象
  8. 将对象存于数组中
  9. */
  10. public NameListService() {
  11. employees = new Employee[EMPLOYEES.length];//如果前面没有导入Data类,必须写成Data.EMPLOYEES.length,下同
  12. for (int i = 0; i < employees.length; i++) {
  13. // 获取通用的属性
  14. int type = Integer.parseInt(EMPLOYEES[i][0]);
  15. int id = Integer.parseInt(EMPLOYEES[i][1]);
  16. String name = EMPLOYEES[i][2];
  17. int age = Integer.parseInt(EMPLOYEES[i][3]);
  18. double salary = Double.parseDouble(EMPLOYEES[i][4]);
  19. //
  20. Equipment eq; //case下的多种情况都需要使用,直接定义在循环外可减少多次定义相同变量的问题,下同
  21. double bonus;
  22. int stock;
  23. switch (type) {
  24. case EMPLOYEE: //case 变量名可以防止之后变量的值改变导致的程序修改问题
  25. employees[i] = new Employee(id, name, age, salary);
  26. break;
  27. case PROGRAMMER:
  28. eq = createEquipment(i); //多态
  29. employees[i] = new Programmer(id, name, age, salary, eq);
  30. break;
  31. case DESIGNER:
  32. eq = createEquipment(i);//因为每个人的equipment不一样,所以应该写一个方法选择,且data中知与employees一一对应
  33. bonus = Integer.parseInt(EMPLOYEES[i][5]);
  34. employees[i] = new Designer(id, name, age, salary, eq, bonus);
  35. break;
  36. case ARCHITECT:
  37. eq = createEquipment(i);
  38. bonus = Integer.parseInt(EMPLOYEES[i][5]);
  39. stock = Integer.parseInt(EMPLOYEES[i][6]);
  40. employees[i] = new Architect(id, name, age, salary, eq, bonus,
  41. stock);
  42. break;
  43. }
  44. }
  45. }
  46. private Equipment createEquipment(int index) {
  47. int type = Integer.parseInt(EQIPMENTS[index][0]);
  48. switch (type) {
  49. case PC:
  50. return new PC(EQIPMENTS[index][1], EQIPMENTS[index][2]); //返回一个对象
  51. case NOTEBOOK:
  52. int price = Integer.parseInt(EQIPMENTS[index][2]);
  53. return new NoteBook(EQIPMENTS[index][1], price);
  54. case PRINTER:
  55. return new Printer(EQIPMENTS[index][1], EQIPMENTS[index][2]);
  56. }
  57. return null;
  58. }
  59. public Employee[] getAllEmployees() {
  60. return employees;
  61. }
  62. public Employee getEmployee(int id) throws TeamException {
  63. for (Employee e : employees) {//java for each循环 :遍历数组或集合 for(var i: arr)
  64. if (e.getId() == id)
  65. return e;
  66. }
  67. throw new TeamException("该员工不存在");//注意这里为什么不用try catch,因为这里的需要被调用才执行,所以因写成throw
  68. }//向被调用者抛异常,再对被调用者try catch
  69. }

Data类:

  1. package service;
  2. public class Data {
  3. public static final int EMPLOYEE = 10;
  4. public static final int PROGRAMMER = 11;
  5. public static final int DESIGNER = 12;
  6. public static final int ARCHITECT = 13;
  7. public static final int PC = 21;
  8. public static final int NOTEBOOK = 22;
  9. public static final int PRINTER = 23;
  10. //Employee : 10, id, name, age, salary
  11. //Programmer: 11, id, name, age, salary
  12. //Designer : 12, id, name, age, salary, bonus
  13. //Architect : 13, id, name, age, salary, bonus, stock
  14. public static final String[][] EMPLOYEES = {
  15. {"10", "1", "马云", "22", "3000"},
  16. {"13", "2", "马化腾", "32", "18000", "15000", "2000"},
  17. {"11", "3", "李彦宏", "23", "7000"},
  18. {"11", "4", "刘强东", "24", "7300"},
  19. {"12", "5", "雷军", "28", "10000", "5000"},
  20. {"11", "6", "任志强", "22", "6800"},
  21. {"12", "7", "柳传志", "29", "10800","5200"},
  22. {"13", "8", "杨元庆", "30", "19800", "15000", "2500"},
  23. {"12", "9", "史玉柱", "26", "9800", "5500"},
  24. {"11", "10", "丁磊", "21", "6600"},
  25. {"11", "11", "张朝阳", "25", "7100"},
  26. {"12", "12", "杨致远", "27", "9600", "4800"}
  27. };
  28. //如下的EQUIPMENTS数组与上面的EMPLOYEES数组元素一一对应
  29. //PC :21, model, display
  30. //NoteBook:22, model, price
  31. //Printer :23, name, type
  32. public static final String[][] EQUIPMENTS = {
  33. {},
  34. {"22", "联想T4", "6000"},
  35. {"21", "戴尔", "NEC17寸"},
  36. {"21", "戴尔", "三星 17寸"},
  37. {"23", "佳能 2900", "激光"},
  38. {"21", "华硕", "三星 17寸"},
  39. {"21", "华硕", "三星 17寸"},
  40. {"23", "爱普生20K", "针式"},
  41. {"22", "惠普m6", "5800"},
  42. {"21", "戴尔", "NEC 17寸"},
  43. {"21", "华硕","三星 17寸"},
  44. {"22", "惠普m6", "5800"}
  45. };
  46. }

自定义的异常类:

  1. public class TeamException extends Exception {
  2. static final long serialVersionUID = -33875169124229948L;
  3. public TeamException() {
  4. }
  5. public TeamException(String message) {
  6. super(message);
  7. }
  8. }

teamservice:

  1. package com.atguigu.team.service;
  2. import com.atguigu.team.domain.*;
  3. public class TeamService {
  4. private static int counter = 1;//用于自动生成团队成员的memberId
  5. private final int MAX_MEMBER = 5;//团队人数上限
  6. private Programmer[] team = new Programmer[MAX_MEMBER];//保存当前团队成员
  7. private int total = 0;//团队实际人数
  8. public TeamService() {
  9. }
  10. //返回team中所有程序员构成的数组
  11. public Programmer[] getTeam() {
  12. Programmer[] team = new Programmer[total];
  13. for (int i = 0; i < total; i++) {
  14. team[i] = this.team[i];//this使用当前类的属性或方法
  15. }
  16. return team;
  17. }
  18. public void addMember(Employee e) throws TeamException {
  19. if (total >= MAX_MEMBER)
  20. throw new TeamException("成员已满,无法添加");
  21. if (!(e instanceof Programmer))
  22. throw new TeamException("该成员不是开发人员,无法添加");
  23. Programmer p = (Programmer)e; //因为对象e一定是programer或其子类,可将其强转为programer
  24. //(因为如果不是的话上面必定报错,同时上面数组也是programer)
  25. if (isExist(p))
  26. throw new TeamException("该员工已在本团队中");
  27. if(p.getStatus().getNAME().equals("BUSY")) { //避免出现空指针异常,getName方法得到的是一个String类型的返回值
  28. //像String、Date、File、包装类都重写了object类中的equals()方法,重写以后比较的不是引用地址是否相同,而是比较两个对象的“实体内容”是否相同
  29. throw new TeamException("该员工已是某团队成员");
  30. }else if(p.getStatus().getNAME().equals("VOCATION")) {
  31. throw new TeamException("该员正在休假,无法添加");
  32. }
  33. // switch (p.getStatus()) {
  34. // case BUSY :throw new TeamException("该员工已是某团队成员");
  35. // case VOCATION:throw new TeamException("该员正在休假,无法添加");
  36. // }
  37. int numOfArch = 0, numOfDsgn = 0, numOfPrg = 0;
  38. for (int i = 0; i < total; i++) {
  39. if (team[i] instanceof Architect) numOfArch++; //注意先判断类型小的
  40. else if (team[i] instanceof Designer) numOfDsgn++;
  41. else if (team[i] instanceof Programmer) numOfPrg++;
  42. }
  43. //面向对象的角度来说p指向的是一个地址值
  44. if (p instanceof Architect) {
  45. if (numOfArch >= 1) throw new TeamException("团队中至多只能有一名架构师");
  46. } else if (p instanceof Designer) {
  47. if (numOfDsgn >= 2) throw new TeamException("团队中至多只能有两名设计师");
  48. } else if (p instanceof Programmer) {
  49. if (numOfPrg >= 3) throw new TeamException("团队中至多只能有三名程序员");
  50. }
  51. //添加到数组
  52. p.setStatus(Status.BUSY);
  53. p.setMemberId(counter++);
  54. team[total++] = p;
  55. }
  56. private boolean isExist(Programmer p) {
  57. for (int i = 0; i < total; i++) {
  58. if (team[i].getId() == p.getId()) return true;
  59. }
  60. return false;
  61. }
  62. //删除指定memberId的程序员
  63. public void removeMember(int memberId) throws TeamException {
  64. int n = 0;
  65. //找到指定memberId的员工,并删除
  66. for (; n < total; n++) {
  67. if (team[n].getMemberId() == memberId) {
  68. team[n].setStatus(Status.FREE);
  69. break;
  70. }
  71. }
  72. //如果遍历一遍,都找不到,则报异常
  73. if (n == total)
  74. throw new TeamException("找不到该成员,无法删除");
  75. //后面的元素覆盖前面的元素
  76. for (int i = n + 1; i < total; i++) {
  77. team[i - 1] = team[i];
  78. }
  79. team[--total] = null;
  80. }
  81. }

Team View类:

  1. import com.atguigu.team.domain.*;
  2. import com.atguigu.team.service.*;
  3. public class TeamView {
  4. private NameListService listSvc = new NameListService();
  5. private TeamService teamSvc = new TeamService();
  6. public void enterMainMenu() {
  7. boolean loopFlag = true;
  8. char key = 0;
  9. do {
  10. if (key != '1') { //在进行1操作时不执行entMainMenu
  11. listAllEmployees();
  12. }
  13. System.out.print("1-团队列表 2-添加团队成员 3-删除团队成员 4-退出 请选择(1-4):");
  14. key = TSUtility.readMenuSelection();
  15. System.out.println();
  16. switch (key) {
  17. case '1':
  18. listTeam();
  19. break;
  20. case '2':
  21. addMember();
  22. break;
  23. case '3':
  24. deleteMember();
  25. break;
  26. case '4':
  27. System.out.print("确认是否退出(Y/N):");
  28. char yn = TSUtility.readConfirmSelection();
  29. if (yn == 'Y')
  30. loopFlag = false;
  31. break;
  32. }
  33. } while (loopFlag);
  34. }
  35. // 显示所有的员工成员
  36. private void listAllEmployees() {
  37. System.out
  38. .println("\n-------------------------------开发团队调度软件--------------------------------\n");
  39. Employee[] emps = listSvc.getAllEmployees();
  40. if (emps.length == 0) {
  41. System.out.println("没有客户记录!");
  42. } else {
  43. System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
  44. }
  45. for (Employee e : emps) {
  46. System.out.println(" " + e);//注意:这里要重写Employ类及其子类的toString()方法,不然输出的是地址值
  47. }
  48. System.out
  49. .println("-------------------------------------------------------------------------------");
  50. }
  51. // 显示开发团队成员列表
  52. private void listTeam() {
  53. System.out
  54. .println("\n--------------------团队成员列表---------------------\n");
  55. Programmer[] team = teamSvc.getTeam();
  56. if (team.length == 0) {
  57. System.out.println("开发团队目前没有成员!");
  58. } else {
  59. System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票");
  60. }
  61. for (Programmer p : team) {
  62. System.out.println(" " + p.getDetailsForTeam()); //每个子类根据拥有内容的差异分别被
  63. //重写getDetailsForTeam()方法
  64. /*1.employee类中的getDetails()方法:
  65. protected String getDetails() {
  66. return id + "\t" + name + "\t" + age+ "\t" +salary;
  67. }
  68. 2.programmer中的getDetailsForTeam()方法:
  69. public String getDetailsForTeam() {
  70. return getMemberDetails() + "\t程序员";
  71. }
  72. */
  73. }
  74. System.out
  75. .println("-----------------------------------------------------");
  76. }
  77. // 添加成员到团队
  78. private void addMember() {
  79. System.out.println("---------------------添加成员---------------------");
  80. System.out.print("请输入要添加的员工ID:");
  81. int id = TSUtility.readInt();
  82. try {
  83. Employee e = listSvc.getEmployee(id);
  84. teamSvc.addMember(e);
  85. System.out.println("添加成功");
  86. } catch (TeamException e) {
  87. System.out.println("添加失败,原因:" + e.getMessage());
  88. }
  89. // 按回车键继续...
  90. TSUtility.readReturn();
  91. }
  92. // 从团队中删除指定id的成员
  93. private void deleteMember() {
  94. System.out.println("---------------------删除成员---------------------");
  95. System.out.print("请输入要删除员工的TID:");
  96. int id = TSUtility.readInt();
  97. System.out.print("确认是否删除(Y/N):");
  98. char yn = TSUtility.readConfirmSelection();
  99. if (yn == 'N')
  100. return;
  101. try {
  102. teamSvc.removeMember(id);
  103. System.out.println("删除成功");
  104. } catch (TeamException e) {
  105. System.out.println("删除失败,原因:" + e.getMessage());
  106. }
  107. // 按回车键继续...
  108. TSUtility.readReturn();
  109. }
  110. public static void main(String[] args) {
  111. TeamView view = new TeamView();
  112. view.enterMainMenu();
  113. }
  114. }

eclipes常见操作总结及项目2和3经验总结的更多相关文章

  1. react-native 常见操作 及 git 补充

    一. react-native 常见操作 1.创建项目 react-native init Market(项目名称,首字母大写) 2.安装常用插件 npm install react-native-t ...

  2. github常见操作和常见错误及其解决办法

    一.常见操作 1. 使用git在本地创建一个项目的过程 $ makdir ~/hello-world //创建一个项目hello-world $ cd ~/hello-world //打开这个项目 $ ...

  3. 动态单链表的传统存储方式和10种常见操作-C语言实现

    顺序线性表的优点:方便存取(随机的),特点是物理位置和逻辑为主都是连续的(相邻).但是也有不足,比如:前面的插入和删除算法,需要移动大量元素,浪费时间,那么链式线性表 (简称链表) 就能解决这个问题. ...

  4. C#路径/文件/目录/I/O常见操作汇总

    文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...

  5. X-Cart 学习笔记(四)常见操作

    目录 X-Cart 学习笔记(一)了解和安装X-Cart X-Cart 学习笔记(二)X-Cart框架1 X-Cart 学习笔记(三)X-Cart框架2 X-Cart 学习笔记(四)常见操作 五.常见 ...

  6. 转:jQuery 常见操作实现方式

    http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...

  7. jQuery 常见操作实现方式

    一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备忘. jQuery 常见操作实现方式 $("标签名") //取html元素 document. ...

  8. C#路径/文件/目录/I/O常见操作汇总<转载>

    文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...

  9. [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组

    1.数组基础 1.什么是数组:           同一类型数据的集合,就是一个容器. 2.数组的好处:           可以自动为数组中的元素从零开始编号,方便操作这些数据. 3.格式:  (一 ...

随机推荐

  1. 乘风破浪,遇见Android Jetpack之Compose声明式UI开发工具包,逐渐大一统的原生UI绘制体系

    什么是Android Jetpack https://developer.android.com/jetpack Android Jetpack是一个由多个库组成的套件,可帮助开发者遵循最佳做法.减少 ...

  2. 字节跳动上传了一份“面试官版Android面试小册”,不讲一句废话,全是精华

    前言 金三银四马上就到了,很多粉丝朋友私信希望我出一篇面试专题或者分享面试相关的笔记来学习,这不今天就给大家安排上了?(都是干货,错过就是亏.) 下面的面试笔记都是精心整理好免费分享给大家的,希望新朋 ...

  3. React Class组件生命周期

    一.react组件的两种定义方式 1.函数组件,简单的函数组件像下面这样,接收Props,渲染DOM,而不关注其他逻辑 function Welcome(props) { return <h1& ...

  4. JavaGUI三种布局管理器FlowLayout,BorderLayout,GridLayout的使用

    三种布局管理器 流式布局FlowLayout package GUI; import java.awt.*; import java.awt.event.WindowAdapter; import j ...

  5. Create Virtual Machines with Vagrant and Puppet

    Create the following puppet manifest and start VM with vagrant, you get a base production environmen ...

  6. 批量删除gmail邮件

    以删除tor.com发送的邮件为例说明. 首先点击邮件搜索框右边的三角,在"发件人"下面写上"tor.com": 点"搜索"按钮,看一下范围 ...

  7. ES6继承和ES5继承是完全一样的么?

    继承方式 ES5 prototype 继承 通过原型链(构造函数 + [[prototype]])指向实现继承. (备注:后续__proto__我都会写成[[prototype]]这种形式) 子类的 ...

  8. vue 快速入门 系列 —— vue-cli 下

    其他章节请看: vue 快速入门 系列 Vue CLI 4.x 下 在 vue loader 一文中我们已经学会从零搭建一个简单的,用于单文件组件开发的脚手架:本篇,我们将全面学习 vue-cli 这 ...

  9. Git-07-分支管理

    创建与合并分支 为什么要创建分支? 假设你准备开发一个新功能,但是需要两周才能完成, 第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了. 如果等代码全部写完 ...

  10. HCNA Routing&Switching之访问控制列表ACL

    前文我们了解了DHCP服务相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15147870.html:今天我们来聊一聊访问控制列表ACL: ACL(ac ...