A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈红建 中级黑马   /  2012-8-10 13:48  /  884 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 陈红建 于 2012-8-10 13:50 编辑
复制代码
改程序,从键盘可输入多个(如3个)员工的信息(类型可以是两种中的任意一种),然后输出全部员工的信息。
可能的输入输出形式:
显示:1.Add a Staff Member
2.Display all Staff
3.Exit Programe
选择1后,显示 1. Add an Employee
2. Add a Manager
3. return
  1. import java.util.*;


  2. public class ManagerTest
  3. {
  4. public static void main(String[] args)
  5. {
  6. // construct a Manager object
  7. Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
  8. boss.setBonus(5000);

  9. Employee[] staff = new Employee[3];

  10. // fill the staff array with Manager and Employee objects

  11. staff[0] = boss;
  12. staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
  13. staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

  14. // print out information about all Employee objects
  15. for (Employee e : staff)
  16. System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
  17. }
  18. }

  19. class Employee
  20. {
  21. public Employee(String n, double s, int year, int month, int day)
  22. {
  23. name = n;
  24. salary = s;
  25. GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
  26. hireDay = calendar.getTime();
  27. }

  28. public String getName()
  29. {
  30. return name;
  31. }

  32. public double getSalary()
  33. {
  34. return salary;
  35. }

  36. public Date getHireDay()
  37. {
  38. return hireDay;
  39. }

  40. public void raiseSalary(double byPercent)
  41. {
  42. double raise = salary * byPercent / 100;
  43. salary += raise;
  44. }

  45. private String name;
  46. private double salary;
  47. private Date hireDay;
  48. }

  49. class Manager extends Employee
  50. {
  51. /**
  52. * @param n the employee's name
  53. * @param s the salary
  54. * @param year the hire year
  55. * @param month the hire month
  56. * @param day the hire day
  57. */
  58. public Manager(String n, double s, int year, int month, int day)
  59. {
  60. super(n, s, year, month, day);
  61. bonus = 0;
  62. }

  63. public double getSalary()
  64. {
  65. double baseSalary = super.getSalary();
  66. return baseSalary + bonus;
  67. }

  68. public void setBonus(double b)
  69. {
  70. bonus = b;
  71. }

  72. private double bonus;
  73. }

复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马