Created
March 29, 2016 08:41
-
-
Save mchayapol/3b779b39f9915b0584a8 to your computer and use it in GitHub Desktop.
OOP Staff Employee Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.au.hr; | |
public class Employee { | |
protected String id; | |
protected String name; | |
protected String dob; | |
static public String org = "AU"; | |
public Employee(String id, String name, String dob) { | |
this.id = id; | |
this.name = name; | |
this.dob = dob; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.au.org; | |
public class Office { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.au.hr; | |
import edu.au.org.Office; | |
public class Staff extends Employee { | |
private Office office; | |
public Staff(String id, String name, String dob) { | |
super(id, name, dob); | |
} | |
@Override | |
public String getName() { | |
return name.toUpperCase(); | |
} | |
public String getName(int stars) { | |
StringBuffer sb = new StringBuffer(); | |
for(int i=0;i<stars;i++) sb.append('*'); | |
sb.append(name); | |
for(int i=0;i<stars;i++) sb.append('*'); | |
return sb.toString(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.au.hr; | |
import static org.junit.Assert.*; | |
import org.junit.Test; | |
public class StaffTest { | |
@Test | |
public void test() { | |
Employee e1 = new Employee("005","Jack Bond","01/01/01"); | |
Staff s1 = new Staff("007","James Bond","01/01/01"); | |
Staff s2 = new Staff("006","Q","02/02/02"); | |
Staff s3 = new Staff("001","Boss","07/07/07"); | |
System.out.println( e1.getName() ); | |
System.out.println( s1.getName() ); | |
System.out.println( s1.getName(5) ); | |
System.out.println( s1.org + " "+s2.org+" "+s3.org); | |
s3.org = "ABAC"; | |
System.out.println( s1.org + " "+s2.org+" "+s3.org); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment