Skip to content

Commit 0c70ffd

Browse files
authored
End of section project files
Employee class hierarchy and EmployeeDemo (Projects 9-1 and 9-2)
1 parent 7e385a8 commit 0c70ffd

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

section9/Employee.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
public abstract class Employee {
3+
private String firstName;
4+
private String lastName;
5+
private int age;
6+
7+
public Employee(String firstName, String lastName, int age) {
8+
this.firstName = firstName;
9+
this.lastName = lastName;
10+
this.age = age;
11+
}//end ctor
12+
13+
public String getFirstName() {
14+
return firstName;
15+
}
16+
17+
public void setFirstName(String firstName) {
18+
this.firstName = firstName;
19+
}
20+
21+
public String getLastName() {
22+
return lastName;
23+
}
24+
25+
public void setLastName(String lastName) {
26+
this.lastName = lastName;
27+
}
28+
29+
public int getAge() {
30+
return age;
31+
}
32+
33+
public void setAge(int age) {
34+
this.age = age;
35+
}
36+
37+
public abstract String work();
38+
}

section9/EmployeeDemo.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
import java.util.Random;
3+
4+
5+
public class EmployeeDemo {
6+
7+
private enum EmployeeType {
8+
WORKER,
9+
MANAGER
10+
}
11+
public static void main(String[] args) {
12+
Employee[] employees = new Employee[5];
13+
14+
for(int i = 0; i < 5; i++) {
15+
EmployeeType et = getEmployeeType();
16+
Employee currentEmployee;
17+
18+
if(et == EmployeeType.WORKER) {
19+
currentEmployee = new Worker("Will", "Jones", 20);
20+
}
21+
else {
22+
currentEmployee = new Manager("Sam", "Jones", 38);
23+
}
24+
25+
employees[i] = currentEmployee;
26+
}//end for creating employees
27+
28+
for(Employee employee : employees) {
29+
System.out.println("Name: " + employee.getFirstName() +
30+
" " + employee.getLastName() + ", age " +
31+
employee.getAge() + " says , " );
32+
System.out.println("\t" + employee.work() + "\n");
33+
}//end for
34+
35+
}//end main
36+
37+
public static EmployeeType getEmployeeType() {
38+
EmployeeType result;
39+
40+
Random rand = new Random();
41+
final int UPPER_BOUND = 2;
42+
int whichOne = rand.nextInt(UPPER_BOUND);
43+
44+
if(whichOne == 0) {
45+
result = EmployeeType.WORKER;
46+
}
47+
else {
48+
result = EmployeeType.MANAGER;
49+
}
50+
51+
return result;
52+
}
53+
}

section9/Manager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
public class Manager extends Employee {
3+
public Manager(String first, String last, int age) {
4+
super(first, last, age);
5+
}
6+
7+
@Override
8+
public String work() {
9+
return "I'm busy bossing everyone around!";
10+
}
11+
}

section9/Worker.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
public class Worker extends Employee {
3+
4+
public Worker(String first, String last, int age) {
5+
super(first, last, age);
6+
}
7+
8+
@Override
9+
public String work() {
10+
return "I'm busy getting work done!";
11+
}
12+
13+
}

0 commit comments

Comments
 (0)