Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- inheritance, exercise 02, slide 28
- /*
- * class Testrer
- *******************************************************/
- import packege02.Employee;
- import packege02.Programmer;
- import packege02.Secretary;
- public class Tester {
- public static void main(String[] args) {
- // create 3 employees
- Employee emp = new Employee("Ali", 1000);
- Programmer prog = new Programmer("Hani", 16000);
- Secretary sec = new Secretary("Miriam", 4600, 35);
- // print employees information
- System.out.println(emp);
- System.out.println(prog);
- System.out.println(sec);
- // update each employee salary
- System.out.println("\nupdate each salary by bonus . . .");
- emp.updateSalary(emp.calcBonus());
- prog.updateSalary(prog.calcBonus());
- sec.updateSalary(sec.calcBonus());
- // print updated employees information
- System.out.println("updated records:\n");
- System.out.println(emp);
- System.out.println(prog);
- System.out.println(sec);
- }
- }
- /*
- * class Employee
- *******************************************************/
- package packege02;
- public class Employee {
- // class properties, shared for all employees
- private static int nextIdNo = 1000; // next available id number
- private static final double BONUS = 0; // bonus for employees
- private static final double BONUS_FACTOR = 0; // factor of bonus for
- // employees
- // employee properties
- protected String name; // employee name, two characters or more
- private int idNo; // starting from 1000
- protected double salary; // employee salary, a positive value
- // constructors
- public Employee(String name, double salary) {
- this.idNo = Employee.nextIdNo; // auto number from the employee class
- // if invalid name then set it to employee id number
- if (!this.setName(name)) {
- this.name = "" + idNo;
- }
- // if invalid salary then set it to employee id number
- if (!this.setSalary(salary)) {
- this.salary = idNo;
- }
- Employee.nextIdNo += 1;
- }
- // setters and getters
- public String getName() {
- return this.name;
- }
- public boolean setName(String name) {
- // if invalid argument do nothing and return false
- if (name.length() < 2) {
- return false;
- }
- // otherwise update and return true
- this.name = name;
- return true;
- }
- public double getSalary() {
- return this.salary;
- }
- public boolean setSalary(double salary) {
- // if invalid argument do nothing and return false
- if (salary < 0) {
- return false;
- }
- // otherwise update and return true
- this.salary = salary;
- return true;
- }
- public int getIdNo() {
- return this.idNo;
- }
- // update employee salary
- public void updateSalary(double moreMoney) {
- // if (moreMoney > 0) // positive argument only
- this.salary += moreMoney;
- }
- // clacBonus method, calculate bonus for employee
- // should be overridden in each derived class
- public double calcBonus() {
- return calcBonus(Employee.BONUS, Employee.BONUS_FACTOR);
- }
- // clacBonus method, calculate bonus by arguments
- protected double calcBonus(double bonus, double bonusFactor) {
- return bonus + bonusFactor * this.salary;
- }
- // toString
- @Override
- public String toString() {
- return this.getClass().getSimpleName() + " ["
- + cascadeFields(Employee.BONUS, Employee.BONUS_FACTOR) + "]";
- }
- // get all fields and values as a string
- // should be overridden in each derived class to add more fields
- protected String cascadeFields(double bonus, double bonusFactor) {
- return String.format("name=%s, idNo=%d, salary=%.2f%s%s", this.name,
- this.idNo, this.salary, bonus > 0 ? ", bonus=" + bonus : "",
- bonusFactor > 0 ? ", bonusFactor=" + bonusFactor : "");
- }
- }
- /*
- * class Programmer
- *******************************************************/
- package packege02;
- public class Programmer extends Employee {
- // class properties, shared for all programmers
- private static final double BOUNUS = 0;// bonus for programmer
- private static final double BONUS_FACTOR = 1.5; // factor of bonus for
- // programmer
- // constructor
- public Programmer(String name, double salary) {
- super(name, salary);
- }
- @Override
- public double calcBonus() {
- // using base method to calculate the bonus
- return super.calcBonus(Programmer.BOUNUS, Programmer.BONUS_FACTOR);
- }
- @Override
- protected String cascadeFields(double bonus, double bonusFactor) {
- // using base method
- return super.cascadeFields(Programmer.BOUNUS, Programmer.BONUS_FACTOR);
- }
- }
- /*
- * class Secretary
- *******************************************************/
- package packege02;
- public class Secretary extends Employee {
- // class properties, shared for all secretaries
- private static final double BONUS = 500;// bonus for secretary
- private static final double BONUS_FACTOR = 0;// factor of bonus for
- // secretary
- // secretary properties
- private int wordsPerMinute = 10; // number of words printed in a minute,
- // positive
- // value
- // constructor
- public Secretary(String name, double salary, int wordsPerMinute) {
- // calling base constructor
- super(name, salary);
- // set additional properties
- this.setWordsPerMinute(wordsPerMinute);
- }
- // getter and setter for field wordsPerMinute
- public int getWordsPerMinute() {
- return this.wordsPerMinute;
- }
- public boolean setWordsPerMinute(int wordsPerMinute) {
- // if invalid argument then return false
- if (wordsPerMinute <= 0) {
- return false;
- }
- // otherwise update and return true
- this.wordsPerMinute = wordsPerMinute;
- return true;
- }
- @Override
- public double calcBonus() {
- return calcBonus(Secretary.BONUS, Secretary.BONUS_FACTOR);
- }
- @Override
- protected String cascadeFields(double bonus, double bonusFactor) {
- // TODO Auto-generated method stub
- return String.format("%s, wordsPerMinute=%d",
- super.cascadeFields(Secretary.BONUS, Secretary.BONUS_FACTOR),
- this.wordsPerMinute);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement