fork download
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. abstract class Employee {
  5. protected String name;
  6. protected String id;
  7.  
  8. public Employee(String name, String id) {
  9. this.name = name;
  10. this.id = id;
  11. }
  12.  
  13. public abstract int getSalary();
  14. }
  15.  
  16. class FixedSalaryEmployee extends Employee {
  17. // Write your code here
  18.  
  19. protected int Salary;
  20.  
  21. public FixedSalaryEmployee (String name, String id,int Salary){
  22. super(name,id);
  23. this.Salary =Salary;
  24. }
  25. public int getSalary(){
  26. return this.Salary;
  27. }
  28.  
  29. }
  30.  
  31. class HourlySalaryEmployee extends Employee {
  32. // Write your code here
  33.  
  34. protected int Salary;
  35. protected int hour;
  36.  
  37. public HourlySalaryEmployee (String name, String id,int Salary,int Hour){
  38.  
  39. super(name,id);
  40. this.Salary =Salary;
  41. this.hour=hour;
  42. }
  43. public int getSalary(){
  44. return this.Salary*this.hour;
  45. }
  46. }
  47.  
  48. public class Main {
  49. public static void main(String[] args) {
  50. Scanner scanner = new Scanner(System.in);
  51.  
  52. // 讀取員工人數n
  53. int n = scanner.nextInt();
  54.  
  55. // 創建一個員工列表
  56. ArrayList<Employee> employees = new ArrayList<>();
  57.  
  58. // 讀取n個員工的資料
  59. for (int i = 0; i < n; i++) {
  60. // 讀取員工類型、姓名和ID
  61. String type = scanner.next();
  62. String name = scanner.next();
  63. String id = scanner.next();
  64.  
  65. // 根據員工類型建立對應的員工物件,並加入到employees ArrayList中
  66. switch (type) {
  67. case "F":
  68. // 讀取固定薪資員工的薪資
  69. int fixedSalary = scanner.nextInt();
  70. employees.add(new FixedSalaryEmployee(name, id, fixedSalary));
  71. break;
  72. case "H":
  73. // 讀取計時薪資員工的時薪和工作時數
  74. int hourlyRate = scanner.nextInt();
  75. int hoursWorked = scanner.nextInt();
  76. employees.add(new HourlySalaryEmployee(name, id, hourlyRate, hoursWorked));
  77. break;
  78. }
  79. }
  80.  
  81. // 逐一輸出每個員工的姓名、ID和薪資
  82. for (Employee employee : employees) {
  83. System.out.printf("%s %s %d\n", employee.name, employee.id, employee.getSalary());
  84. }
  85. }
  86. }
  87.  
Success #stdin #stdout 0.14s 56660KB
stdin
4
F Alice A0001 5000
H Bob B0002 100 40
F Carol C0003 6000
H David D0004 120 35
stdout
Alice A0001 5000
Bob B0002 0
Carol C0003 6000
David D0004 0