• Source
    1. /* package whatever; // don't place package name! */
    2.  
    3. import java.util.*;
    4. import java.lang.*;
    5. import java.io.*;
    6.  
    7.  
    8. abstract class Employee {
    9. private String name;
    10. private String address;
    11. protected double salary;
    12.  
    13. public Employee(String name, String address, double salary) {
    14. this.name = name;
    15. this.address = address;
    16. this.salary = salary;
    17. }
    18. public String getName(){
    19. return name;
    20. }
    21. public abstract double raise(); // abstract method
    22. public abstract String getEmployeeType(); // abstract method
    23.  
    24. }
    25.  
    26. class Programmer extends Employee{
    27. private double commission;
    28. private double projectRate;
    29. private double projectNum;
    30.  
    31. public Programmer(String name, String address, double salary, double projectRate, double projectNum) {
    32. super(name, address, salary);
    33. setProjectRate(projectRate);
    34. setProjectNum(projectNum);
    35. }
    36. private void setProjectRate(double projectRate){
    37. this.projectRate= (projectRate>0)? (salary*projectRate) : 0 ;
    38. // checking: ensure no negative value
    39. }
    40. private void setProjectNum(double projectNum){
    41. this.projectNum= (projectNum>0)? projectNum : 0 ;
    42. }
    43. private double getProjectRate(){
    44. return projectRate;
    45. }
    46. private double getProjectNum(){
    47. return projectNum;
    48. }
    49.  
    50. public double raise() {
    51. commission= getProjectRate() * getProjectNum(); // what a nice Idea
    52. salary = salary + commission;
    53. return salary;
    54. }
    55. public String getEmployeeType(){
    56. return "Programmer";
    57. }
    58. }
    59.  
    60. class Manager extends Employee {
    61. Manager(String name, String address,double salary) {
    62. super(name, address, salary);
    63. }
    64. public double raise(){
    65. salary = salary + salary * .05;
    66. return salary;
    67. }
    68. public String getEmployeeType(){
    69. return "Manager";
    70. }
    71. }
    72.  
    73. class Worker extends Employee {
    74. Worker(String name, String address, double salary) {
    75. super(name, address, salary);
    76. }
    77. public double raise() {
    78. return salary;
    79. }
    80.  
    81. public String getEmployeeType(){
    82. return "Worker";
    83. }
    84. }
    85. public class Main {
    86.  
    87. public static void main(String[] args) {
    88. Programmer a=new Programmer("Ashis","Chanda", 3500, 0.30, 2);
    89. Manager b=new Manager("hossein","shiraz", 4000);
    90. Worker c= new Worker("reza","tehran",1000);
    91.  
    92.  
    93. Employee ArrayEmployee[]=new Employee[3];
    94. ArrayEmployee[0]=a;
    95. ArrayEmployee[1]=b;
    96. ArrayEmployee[2]=c;
    97.  
    98. for(int i=0;i<ArrayEmployee.length;i++){
    99. double s=ArrayEmployee[i].raise();
    100. System.out.println("Name : "+ ArrayEmployee[i].getName()
    101. + " Post: "+ ArrayEmployee[i].getEmployeeType() + " salary: "+s);
    102. // you can write two in this manner
    103. }
    104. // getName() comes from the first class: Employee
    105. // getEmployeeType() comes from diff class which is abstract method
    106. }
    107. }
    108.