diff --git a/src/oops/SOLID/openClosePrinciple/after/client/CalculateTaxesClient.java b/src/oops/SOLID/openClosePrinciple/after/client/CalculateTaxesClient.java new file mode 100644 index 00000000..c6767696 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/client/CalculateTaxesClient.java @@ -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 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; + } + } +} diff --git a/src/oops/SOLID/openClosePrinciple/after/employees/Employee.java b/src/oops/SOLID/openClosePrinciple/after/employees/Employee.java new file mode 100644 index 00000000..d32cfaf5 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/employees/Employee.java @@ -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(); +} + diff --git a/src/oops/SOLID/openClosePrinciple/after/employees/FullTimeEmployee.java b/src/oops/SOLID/openClosePrinciple/after/employees/FullTimeEmployee.java new file mode 100644 index 00000000..9a2cd547 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/employees/FullTimeEmployee.java @@ -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); + } +} diff --git a/src/oops/SOLID/openClosePrinciple/after/employees/Intern.java b/src/oops/SOLID/openClosePrinciple/after/employees/Intern.java new file mode 100644 index 00000000..06e25bf0 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/employees/Intern.java @@ -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); + } +} diff --git a/src/oops/SOLID/openClosePrinciple/after/employees/PartTimeEmployee.java b/src/oops/SOLID/openClosePrinciple/after/employees/PartTimeEmployee.java new file mode 100644 index 00000000..b0cc3077 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/employees/PartTimeEmployee.java @@ -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); + } +} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/after/persistence/EmployeeRepository.java b/src/oops/SOLID/openClosePrinciple/after/persistence/EmployeeRepository.java new file mode 100644 index 00000000..ed700484 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/persistence/EmployeeRepository.java @@ -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 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); + } +} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculator.java b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculator.java new file mode 100644 index 00000000..f88d6691 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculator.java @@ -0,0 +1,7 @@ +package oops.SOLID.openClosePrinciple.after.taxes; + +import oops.SOLID.openClosePrinciple.after.employees.Employee; + +public interface TaxCalculator { + double calculate(Employee employee); +} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorFullTimeEmp.java b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorFullTimeEmp.java new file mode 100644 index 00000000..1fa1f657 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorFullTimeEmp.java @@ -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; + } +} diff --git a/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorIntern.java b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorIntern.java new file mode 100644 index 00000000..577fa7b9 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorIntern.java @@ -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; + } +} diff --git a/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorPartTimeEmp.java b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorPartTimeEmp.java new file mode 100644 index 00000000..e2714f4a --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/after/taxes/TaxCalculatorPartTimeEmp.java @@ -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; + } +}