Advertisement
mmayoub

inheritance, exercise 02, slide 28

Jul 16th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.91 KB | None | 0 0
  1. inheritance, exercise 02, slide 28
  2.  
  3. /*
  4.  * class Testrer
  5.  *******************************************************/
  6. import packege02.Employee;
  7. import packege02.Programmer;
  8. import packege02.Secretary;
  9.  
  10. public class Tester {
  11.  
  12.     public static void main(String[] args) {
  13.         // create 3 employees
  14.         Employee emp = new Employee("Ali", 1000);
  15.         Programmer prog = new Programmer("Hani", 16000);
  16.         Secretary sec = new Secretary("Miriam", 4600, 35);
  17.  
  18.         // print employees information
  19.         System.out.println(emp);
  20.         System.out.println(prog);
  21.         System.out.println(sec);
  22.  
  23.         // update each employee salary
  24.         System.out.println("\nupdate each salary by bonus . . .");
  25.         emp.updateSalary(emp.calcBonus());
  26.         prog.updateSalary(prog.calcBonus());
  27.         sec.updateSalary(sec.calcBonus());
  28.  
  29.         // print updated employees information
  30.         System.out.println("updated records:\n");
  31.         System.out.println(emp);
  32.         System.out.println(prog);
  33.         System.out.println(sec);
  34.     }
  35.  
  36. }
  37.  
  38. /*
  39.  * class Employee
  40.  *******************************************************/
  41. package packege02;
  42.  
  43. public class Employee {
  44.     // class properties, shared for all employees
  45.     private static int nextIdNo = 1000; // next available id number
  46.     private static final double BONUS = 0; // bonus for employees
  47.     private static final double BONUS_FACTOR = 0; // factor of bonus for
  48.                                                     // employees
  49.  
  50.     // employee properties
  51.     protected String name; // employee name, two characters or more
  52.     private int idNo; // starting from 1000
  53.     protected double salary; // employee salary, a positive value
  54.  
  55.     // constructors
  56.     public Employee(String name, double salary) {
  57.         this.idNo = Employee.nextIdNo; // auto number from the employee class
  58.  
  59.         // if invalid name then set it to employee id number
  60.         if (!this.setName(name)) {
  61.             this.name = "" + idNo;
  62.         }
  63.         // if invalid salary then set it to employee id number
  64.         if (!this.setSalary(salary)) {
  65.             this.salary = idNo;
  66.         }
  67.  
  68.         Employee.nextIdNo += 1;
  69.     }
  70.  
  71.     // setters and getters
  72.     public String getName() {
  73.         return this.name;
  74.     }
  75.  
  76.     public boolean setName(String name) {
  77.         // if invalid argument do nothing and return false
  78.         if (name.length() < 2) {
  79.  
  80.             return false;
  81.         }
  82.  
  83.         // otherwise update and return true
  84.         this.name = name;
  85.         return true;
  86.     }
  87.  
  88.     public double getSalary() {
  89.         return this.salary;
  90.     }
  91.  
  92.     public boolean setSalary(double salary) {
  93.         // if invalid argument do nothing and return false
  94.         if (salary < 0) {
  95.             return false;
  96.         }
  97.         // otherwise update and return true
  98.         this.salary = salary;
  99.         return true;
  100.     }
  101.  
  102.     public int getIdNo() {
  103.         return this.idNo;
  104.     }
  105.  
  106.     // update employee salary
  107.     public void updateSalary(double moreMoney) {
  108.         // if (moreMoney > 0) // positive argument only
  109.         this.salary += moreMoney;
  110.     }
  111.  
  112.     // clacBonus method, calculate bonus for employee
  113.     // should be overridden in each derived class
  114.     public double calcBonus() {
  115.         return calcBonus(Employee.BONUS, Employee.BONUS_FACTOR);
  116.     }
  117.  
  118.     // clacBonus method, calculate bonus by arguments
  119.     protected double calcBonus(double bonus, double bonusFactor) {
  120.         return bonus + bonusFactor * this.salary;
  121.     }
  122.  
  123.     // toString
  124.     @Override
  125.     public String toString() {
  126.         return this.getClass().getSimpleName() + " ["
  127.                 + cascadeFields(Employee.BONUS, Employee.BONUS_FACTOR) + "]";
  128.     }
  129.  
  130.     // get all fields and values as a string
  131.     // should be overridden in each derived class to add more fields
  132.     protected String cascadeFields(double bonus, double bonusFactor) {
  133.         return String.format("name=%s, idNo=%d, salary=%.2f%s%s", this.name,
  134.                 this.idNo, this.salary, bonus > 0 ? ", bonus=" + bonus : "",
  135.                 bonusFactor > 0 ? ", bonusFactor=" + bonusFactor : "");
  136.     }
  137.  
  138. }
  139.  
  140. /*
  141.  * class Programmer
  142.  *******************************************************/
  143. package packege02;
  144.  
  145. public class Programmer extends Employee {
  146.     // class properties, shared for all programmers
  147.     private static final double BOUNUS = 0;// bonus for programmer
  148.     private static final double BONUS_FACTOR = 1.5; // factor of bonus for
  149.                                                     // programmer
  150.  
  151.     // constructor
  152.     public Programmer(String name, double salary) {
  153.         super(name, salary);
  154.     }
  155.  
  156.     @Override
  157.     public double calcBonus() {
  158.         // using base method to calculate the bonus
  159.         return super.calcBonus(Programmer.BOUNUS, Programmer.BONUS_FACTOR);
  160.     }
  161.  
  162.     @Override
  163.     protected String cascadeFields(double bonus, double bonusFactor) {
  164.         // using base method
  165.         return super.cascadeFields(Programmer.BOUNUS, Programmer.BONUS_FACTOR);
  166.     }
  167.  
  168. }
  169.  
  170. /*
  171.  * class Secretary
  172.  *******************************************************/
  173. package packege02;
  174.  
  175. public class Secretary extends Employee {
  176.     // class properties, shared for all secretaries
  177.     private static final double BONUS = 500;// bonus for secretary
  178.     private static final double BONUS_FACTOR = 0;// factor of bonus for
  179.                                                     // secretary
  180.  
  181.     // secretary properties
  182.     private int wordsPerMinute = 10; // number of words printed in a minute,
  183.                                         // positive
  184.                                         // value
  185.  
  186.     // constructor
  187.     public Secretary(String name, double salary, int wordsPerMinute) {
  188.         // calling base constructor
  189.         super(name, salary);
  190.  
  191.         // set additional properties
  192.         this.setWordsPerMinute(wordsPerMinute);
  193.     }
  194.  
  195.     // getter and setter for field wordsPerMinute
  196.     public int getWordsPerMinute() {
  197.         return this.wordsPerMinute;
  198.     }
  199.  
  200.     public boolean setWordsPerMinute(int wordsPerMinute) {
  201.         // if invalid argument then return false
  202.         if (wordsPerMinute <= 0) {
  203.             return false;
  204.         }
  205.  
  206.         // otherwise update and return true
  207.         this.wordsPerMinute = wordsPerMinute;
  208.         return true;
  209.     }
  210.  
  211.     @Override
  212.     public double calcBonus() {
  213.         return calcBonus(Secretary.BONUS, Secretary.BONUS_FACTOR);
  214.     }
  215.  
  216.     @Override
  217.     protected String cascadeFields(double bonus, double bonusFactor) {
  218.         // TODO Auto-generated method stub
  219.         return String.format("%s, wordsPerMinute=%d",
  220.                 super.cascadeFields(Secretary.BONUS, Secretary.BONUS_FACTOR),
  221.                 this.wordsPerMinute);
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement