Skip to content

Commit

Permalink
End of section project files
Browse files Browse the repository at this point in the history
Employee class hierarchy and EmployeeDemo (Projects 9-1 and 9-2)
  • Loading branch information
profjpbaugh authored Jul 4, 2023
1 parent 7e385a8 commit 0c70ffd
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
38 changes: 38 additions & 0 deletions section9/Employee.java
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();
}
53 changes: 53 additions & 0 deletions section9/EmployeeDemo.java
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;
}
}
11 changes: 11 additions & 0 deletions section9/Manager.java
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!";
}
}
13 changes: 13 additions & 0 deletions section9/Worker.java
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!";
}

}

0 comments on commit 0c70ffd

Please sign in to comment.