Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package oops.SOLID.openClosePrinciple.after.client;

import oops.SOLID.openClosePrinciple.after.employees.Employee;
import oops.SOLID.openClosePrinciple.after.persistence.EmployeeRepository;
import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculator;

import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;


public class CalculateTaxesClient {
public static void main(String[] args) {

EmployeeRepository repository = new EmployeeRepository();

// Grab employees
List<Employee> employees = repository.findAll();

// Calculate taxes
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);


double totalTaxes = 0;
for (Employee employee: employees){

// compute individual tax
double tax = employee.TaxCalculate();
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += tax;
}
}
}
63 changes: 63 additions & 0 deletions src/oops/SOLID/openClosePrinciple/after/employees/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package oops.SOLID.openClosePrinciple.after.employees;

/*
Models an employee form a business perspective
*/
public abstract class Employee {
private String firstName;
private String lastName;
private int monthlyIncome;
private int nbHoursPerWeek;

public Employee(String fullName, int monthlyIncome){
setMonthlyIncome(monthlyIncome);

String[] names = fullName.split(" ");
this.firstName = names[0];
this.lastName = names[1];
}

public String getEmail() {
return this.firstName + "." +
this.lastName +
"@globomanticshr.com";
}

@Override
public String toString() {
return this.firstName + " " +
this.lastName + " - " +
this.monthlyIncome;
}

public int getMonthlyIncome() {
return monthlyIncome;
}

public void setMonthlyIncome(int monthlyIncome) {
if(monthlyIncome < 0){
throw new IllegalArgumentException("Income must be positive");
}

this.monthlyIncome = monthlyIncome;
}

public int getNbHoursPerWeek() {
return nbHoursPerWeek;
}

public void setNbHoursPerWeek(int nbHoursPerWeek) {
if(nbHoursPerWeek <= 0){
throw new IllegalArgumentException("Income must be positive");
}

this.nbHoursPerWeek = nbHoursPerWeek;
}

public String getFullName(){
return this.firstName + " " + this.lastName;
}

public abstract double TaxCalculate();
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package oops.SOLID.openClosePrinciple.after.employees;

import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculator;
import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculatorFullTimeEmp;

public class FullTimeEmployee extends Employee {
public FullTimeEmployee(String fullName, int monthlyIncome) {
super(fullName, monthlyIncome);
this.setNbHoursPerWeek(40);
}

public double TaxCalculate(){
TaxCalculator calculator = new TaxCalculatorFullTimeEmp();
return calculator.calculate(this);
}
}
16 changes: 16 additions & 0 deletions src/oops/SOLID/openClosePrinciple/after/employees/Intern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package oops.SOLID.openClosePrinciple.after.employees;

import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculator;
import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculatorIntern;

public class Intern extends Employee {
public Intern(String fullName, int monthlyIncome, int nbHours) {
super(fullName, monthlyIncome);
setNbHoursPerWeek(nbHours);
}

public double TaxCalculate(){
TaxCalculator calculator = new TaxCalculatorIntern();
return calculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package oops.SOLID.openClosePrinciple.after.employees;

import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculator;
import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculatorIntern;
import oops.SOLID.openClosePrinciple.after.taxes.TaxCalculatorPartTimeEmp;

public class PartTimeEmployee extends Employee {
public PartTimeEmployee(String fullName, int monthlyIncome) {
super(fullName, monthlyIncome);
this.setNbHoursPerWeek(20);
}

public double TaxCalculate(){
TaxCalculator calculator = new TaxCalculatorPartTimeEmp();
return calculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package oops.SOLID.openClosePrinciple.after.persistence;

import oops.SOLID.openClosePrinciple.after.employees.Employee;
import oops.SOLID.openClosePrinciple.after.employees.FullTimeEmployee;
import oops.SOLID.openClosePrinciple.after.employees.PartTimeEmployee;

import java.util.Arrays;
import java.util.List;

public class EmployeeRepository {

public List<Employee> findAll(){

// Employees are kept in memory for simplicity
Employee anna = new FullTimeEmployee("Anna Smith", 2000);
Employee billy = new FullTimeEmployee("Billy Leech", 920);

Employee steve = new PartTimeEmployee("Steve Jones", 800);
Employee magda = new PartTimeEmployee("Magda Iovan", 920);

return Arrays.asList(anna, billy, steve, magda);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oops.SOLID.openClosePrinciple.after.taxes;

import oops.SOLID.openClosePrinciple.after.employees.Employee;

public interface TaxCalculator {
double calculate(Employee employee);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package oops.SOLID.openClosePrinciple.after.taxes;

import oops.SOLID.openClosePrinciple.after.employees.Employee;

public class TaxCalculatorFullTimeEmp implements TaxCalculator{

private final static int INCOME_TAX_PERCENTAGE = 30;
private final static int PROFESSIONAL_TAX_PERCENTAGE = 2;
private final static int EDUCATION_CESS_PERCENTAGE = 1;

@Override
public double calculate(Employee employee) {
return
(employee.getMonthlyIncome()* 12 * PROFESSIONAL_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome()* 12 * INCOME_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome()* 12 * EDUCATION_CESS_PERCENTAGE)/100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package oops.SOLID.openClosePrinciple.after.taxes;

import oops.SOLID.openClosePrinciple.after.employees.Employee;

public class TaxCalculatorIntern implements TaxCalculator{
private final static int TAX_FREE = 300000;
private final static int TAX_PERCENTAGE = 15;

@Override
public double calculate(Employee employee) {
if(employee.getMonthlyIncome() * 12 <= TAX_FREE)
return 0D;

return (employee.getMonthlyIncome()*12*TAX_PERCENTAGE)/100;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package oops.SOLID.openClosePrinciple.after.taxes;

import oops.SOLID.openClosePrinciple.after.employees.Employee;

public class TaxCalculatorPartTimeEmp implements TaxCalculator{
private final static int INCOME_TAX_PERCENTAGE = 20;
private final static int PROFESSIONAL_TAX_PERCENTAGE = 3;
private final static int EDUCATION_CESS_PERCENTAGE = 1;

@Override
public double calculate(Employee employee) {
return
(employee.getMonthlyIncome()* 12 * PROFESSIONAL_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome()* 12 * INCOME_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome()* 12 * EDUCATION_CESS_PERCENTAGE)/100;
}
}