-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Employee class hierarchy and EmployeeDemo (Projects 9-1 and 9-2)
- Loading branch information
1 parent
7e385a8
commit 0c70ffd
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
public abstract class Employee { | ||
private String firstName; | ||
private String lastName; | ||
private int age; | ||
|
||
public Employee(String firstName, String lastName, int age) { | ||
this.firstName = firstName; | ||
this.lastName = lastName; | ||
this.age = age; | ||
}//end ctor | ||
|
||
public String getFirstName() { | ||
return firstName; | ||
} | ||
|
||
public void setFirstName(String firstName) { | ||
this.firstName = firstName; | ||
} | ||
|
||
public String getLastName() { | ||
return lastName; | ||
} | ||
|
||
public void setLastName(String lastName) { | ||
this.lastName = lastName; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public abstract String work(); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
import java.util.Random; | ||
|
||
|
||
public class EmployeeDemo { | ||
|
||
private enum EmployeeType { | ||
WORKER, | ||
MANAGER | ||
} | ||
public static void main(String[] args) { | ||
Employee[] employees = new Employee[5]; | ||
|
||
for(int i = 0; i < 5; i++) { | ||
EmployeeType et = getEmployeeType(); | ||
Employee currentEmployee; | ||
|
||
if(et == EmployeeType.WORKER) { | ||
currentEmployee = new Worker("Will", "Jones", 20); | ||
} | ||
else { | ||
currentEmployee = new Manager("Sam", "Jones", 38); | ||
} | ||
|
||
employees[i] = currentEmployee; | ||
}//end for creating employees | ||
|
||
for(Employee employee : employees) { | ||
System.out.println("Name: " + employee.getFirstName() + | ||
" " + employee.getLastName() + ", age " + | ||
employee.getAge() + " says , " ); | ||
System.out.println("\t" + employee.work() + "\n"); | ||
}//end for | ||
|
||
}//end main | ||
|
||
public static EmployeeType getEmployeeType() { | ||
EmployeeType result; | ||
|
||
Random rand = new Random(); | ||
final int UPPER_BOUND = 2; | ||
int whichOne = rand.nextInt(UPPER_BOUND); | ||
|
||
if(whichOne == 0) { | ||
result = EmployeeType.WORKER; | ||
} | ||
else { | ||
result = EmployeeType.MANAGER; | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
public class Manager extends Employee { | ||
public Manager(String first, String last, int age) { | ||
super(first, last, age); | ||
} | ||
|
||
@Override | ||
public String work() { | ||
return "I'm busy bossing everyone around!"; | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
public class Worker extends Employee { | ||
|
||
public Worker(String first, String last, int age) { | ||
super(first, last, age); | ||
} | ||
|
||
@Override | ||
public String work() { | ||
return "I'm busy getting work done!"; | ||
} | ||
|
||
} |