Skip to content

Instantly share code, notes, and snippets.

@zhelezoglo
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhelezoglo/8481988b0995ee110e80 to your computer and use it in GitHub Desktop.
Save zhelezoglo/8481988b0995ee110e80 to your computer and use it in GitHub Desktop.
public class AdministrationEmployee extends Employee {
public AdministrationEmployee(String name) {
super(name, Employee.TEMPORARY, 0, "Administration");
}
public double getSalary() {
return 18000;
}
}
public interface Employed {
double getSalary();
}
public abstract class Employee implements Employed {
public static final int TEMPORARY = 0, TRAINING = 1, INDEFINITE = 2;
private String name;
private int contract;
private int years;
private String department;
public Employee(String name, int contract, int years, String department) {
if (contract != TEMPORARY && contract != TRAINING && contract != INDEFINITE)
throw new IllegalArgumentException();
if (years < 0) throw new IllegalArgumentException();
if (!"Management".equals(department) &&
!"Engineering".equals(department) &&
!"Administration".equals(department)) throw new IllegalArgumentException();
this.name = name;
this.contract = contract;
this.years = years;
this.department = department;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getContract() {
return contract;
}
public void setContract(int contract) {
this.contract = contract;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
// <name>: <department> department, <type of contract> contract, <years> years in the company, salary of <salary> bitcoins
@Override
public String toString() {
String contractStr;
if (contract == 0)
contractStr = "temporary";
else if (contract == 1) {
contractStr = "training";
} else {
contractStr = "indefinite";
}
return name + ": " + department + " department, " + contractStr + " contract, " +
years + " years in the company, salary of " + getSalary() + " bitcoins";
}
}
public class EngineeringEmployee extends Employee {
public EngineeringEmployee(String name, int contract, int years) {
super(name, contract, years, "Engineering");
}
public double getSalary() {
double startSalary = 25000;
double stepPerYear = 2500;
return startSalary + stepPerYear * getYears();
}
}
public class ManagementEmployee extends Employee {
public ManagementEmployee(String name, int years) {
super(name, INDEFINITE, years, "Management");
}
public double getSalary() {
double startSalary = 40000;
double stepPerYear = 6000;
return startSalary + stepPerYear * getYears();
}
}
public class Staff {
public static void main(String[] args) {
Employee [] staff = new Employee[6];
staff[0] = new ManagementEmployee("Bill", 9);
staff[1] = new EngineeringEmployee("Anna", Employee.INDEFINITE, 9);
staff[2] = new EngineeringEmployee("John", Employee.INDEFINITE, 5);
staff[3] = new EngineeringEmployee("Elizabeth", Employee.TRAINING, 3);
staff[4] = new EngineeringEmployee("Michael", Employee.TRAINING, 2);
staff[5] = new AdministrationEmployee("Albert");
double managersSalaryTotal = 0;
double engineersSalaryTotal = 0;
double adminsSalaryTotal = 0;
double acmeTotal = 0;
for (Employee employee : staff) {
System.out.println(employee);
acmeTotal += employee.getSalary();
if ("Management".equals(employee.getDepartment())) {
managersSalaryTotal += employee.getSalary();
}
if ("Engineering".equals(employee.getDepartment())) {
engineersSalaryTotal += employee.getSalary();
}
if ("Administration".equals(employee.getDepartment())) {
adminsSalaryTotal += employee.getSalary();
}
}
System.out.println();
System.out.println("MANAGEMENT TOTAL SALARY: " + managersSalaryTotal + " bitcoins");
System.out.println("ENGINEERING TOTAL SALARY: " + engineersSalaryTotal + " bitcoins");
System.out.println("ADMINISTRATION TOTAL SALARY: " + adminsSalaryTotal + " bitcoins");
System.out.println("ACME TOTAL SALARY: " + acmeTotal + " bitcoins");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment