public class JobTest {
public static void main(String[] args) {
Manager kanok;
Secretary supa;
Employee suwat;
suwat = new Employee("Suwat", 8000);
supa = new Secretary("Supa", 10000);
kanok = new Manager("Kanok", 25000, supa);
System.out.println(suwat);
System.out.println(supa);
System.out.println(kanok);
suwat.raiseSalary();
supa.raiseSalary();
kanok.raiseSalary();
System.out.println(suwat);
System.out.println(supa);
System.out.println(kanok);
}
}
class Employee {
private String name;
protected double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void raiseSalary() {
salary += salary * 0.1;
}
public String toString() {
String str;
str = "name : " + name + "\n" +
"salary: " + salary + "\n";
return(str);
}
}
class Secretary extends Employee {
public Secretary(String name, double salary) {
super(name, salary);
}
public void raiseSalary() {
salary += salary * 0.15;
}
}
class Manager extends Employee {
private Secretary secretary;
public Manager(String name,
double salary,
Secretary secretary) {
super(name, salary);
this.secretary = secretary;
}
public void raiseSalary() {
salary += salary * 0.2;
}
public String toString() {
String str;
str = super.toString() +
"secretary :\n" +
secretary;
return(str);
}
}
JobTest.java output
name : Suwat
salary: 8000.0
name : Supa
salary: 10000.0
name : Kanok
salary: 25000.0
secretary :
name : Supa
salary: 10000.0
name : Suwat
salary: 8800.0
name : Supa
salary: 11500.0
name : Kanok
salary: 30000.0
secretary :
name : Supa
salary: 11500.0
Tag: Study Code Program Java
No comments:
Post a Comment