diff --git a/src/oops/SOLID/lsp/stack/before/StackWrong.java b/src/oops/SOLID/lsp/stack/before/StackWrong.java index 1170e573..cb33a876 100644 --- a/src/oops/SOLID/lsp/stack/before/StackWrong.java +++ b/src/oops/SOLID/lsp/stack/before/StackWrong.java @@ -1,6 +1,8 @@ package oops.SOLID.lsp.stack.before; -import java.util.ArrayList; +import java.util.Stack; +import java.util.Vector; + /* * Stack is-a list with push() pop() methods. * So can we create a stack by extending an ArrayList class? @@ -14,16 +16,18 @@ * so objects of ArrayList are not fully replaceable by the objects of stack. * */ -public class StackWrong extends ArrayList{ +public class StackWrong extends Vector { private int topPointer = 0; - public void push(Integer a) { + public Integer push(Integer a) { add(topPointer, a); topPointer++; - } - public void pop() { + return a; + } + public Integer pop() { remove(topPointer-1); topPointer--; + return null; } public Integer top() { return get(topPointer-1); diff --git a/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java b/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java index 1651161c..96960d4f 100644 --- a/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java +++ b/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java @@ -7,6 +7,7 @@ import oops.SOLID.openClosePrinciple.before.employees.Employee; import oops.SOLID.openClosePrinciple.before.persistence.EmployeeRepository; import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculator; +import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculatorProvider; public class CalculateTaxesClient { @@ -24,12 +25,15 @@ public static void main(String[] args) { double totalTaxes = 0; for (Employee employee: employees){ + TaxCalculator taxCalculator = TaxCalculatorProvider.getTaxCalculator(employee); // compute individual tax - double tax = TaxCalculator.calculate(employee); + double tax = taxCalculator.calculate(employee); String formattedTax = currencyFormatter.format(tax); // add to company total taxes - totalTaxes += TaxCalculator.calculate(employee); + totalTaxes += taxCalculator.calculate(employee); + + System.out.println(employee+", TAX = " +formattedTax); } } } diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java b/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java index a84b72a3..45777729 100644 --- a/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java +++ b/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java @@ -57,5 +57,13 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) { public String getFullName(){ return this.firstName + " " + this.lastName; } + + public boolean incomeLessThan(int incomeCap){ + return this.monthlyIncome < incomeCap; + } + + public double getMonthlyIncomePercentage(int incomeTaxPercentage){ + return (this.monthlyIncome*incomeTaxPercentage)/100; + } } diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java new file mode 100644 index 00000000..8f94b9d8 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java @@ -0,0 +1,28 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public class FullTimeEmployeeTaxCalculator implements TaxCalculator{ + private final static int INCOME_TAX_PERCENTAGE = 30; + private final static int PROFESSIONAL_TAX_PERCENTAGE = 2; + private final static int EDUCATION_TAX_PERCENTAGE = 1; + + public double calculate(Employee employee) { + return calculateIncomeTax(employee) + calculateProfessionalTax(employee) +calculateEducationTax(employee); + } + + @Override + public double calculateIncomeTax(Employee employee) { + return employee.getMonthlyIncomePercentage(INCOME_TAX_PERCENTAGE); + } + + @Override + public double calculateProfessionalTax(Employee employee) { + return employee.getMonthlyIncomePercentage(PROFESSIONAL_TAX_PERCENTAGE); + } + + @Override + public double calculateEducationTax(Employee employee) { + return employee.getMonthlyIncomePercentage(EDUCATION_TAX_PERCENTAGE); + } +} diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/InternTaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/InternTaxCalculator.java new file mode 100644 index 00000000..b334ee60 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/InternTaxCalculator.java @@ -0,0 +1,30 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public class InternTaxCalculator implements TaxCalculator { + private final static int INCOME_TAX_PERCENTAGE = 15; + private final static int INCOME_CAP=300000; + private final static int PROFESSIONAL_TAX_PERCENTAGE = 0; + private final static int EDUCATION_TAX_PERCENTAGE = 0; + + + public double calculate(Employee employee) { + return calculateIncomeTax(employee) + calculateProfessionalTax(employee) +calculateEducationTax(employee); + } + + @Override + public double calculateIncomeTax(Employee employee) { + return employee.incomeLessThan(INCOME_CAP)?0:employee.getMonthlyIncomePercentage(INCOME_TAX_PERCENTAGE); + } + + @Override + public double calculateProfessionalTax(Employee employee) { + return employee.getMonthlyIncomePercentage(PROFESSIONAL_TAX_PERCENTAGE); + } + + @Override + public double calculateEducationTax(Employee employee) { + return employee.getMonthlyIncomePercentage(EDUCATION_TAX_PERCENTAGE); + } +} diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java new file mode 100644 index 00000000..d0f75d14 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java @@ -0,0 +1,28 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public class PartTimeEmployeeTaxCalculator implements TaxCalculator { + private final static int INCOME_TAX_PERCENTAGE = 20; + private final static int PROFESSIONAL_TAX_PERCENTAGE = 3; + private final static int EDUCATION_TAX_PERCENTAGE = 1; + + public double calculate(Employee employee) { + return calculateIncomeTax(employee) + calculateProfessionalTax(employee) +calculateEducationTax(employee); + } + + @Override + public double calculateIncomeTax(Employee employee) { + return employee.getMonthlyIncomePercentage(INCOME_TAX_PERCENTAGE); + } + + @Override + public double calculateProfessionalTax(Employee employee) { + return employee.getMonthlyIncomePercentage(PROFESSIONAL_TAX_PERCENTAGE); + } + + @Override + public double calculateEducationTax(Employee employee) { + return employee.getMonthlyIncomePercentage(EDUCATION_TAX_PERCENTAGE); + } +} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java index ab0300be..e0357ac8 100644 --- a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java @@ -2,15 +2,10 @@ import oops.SOLID.openClosePrinciple.before.employees.Employee; -public class TaxCalculator { - private final static int INCOME_TAX_PERCENTAGE = 20; - private final static int PROFESSIONAL_TAX_PERCENTAGE = 3; +public interface TaxCalculator { - - public static double calculate(Employee employee) { - return - (employee.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE) / 100 + - (employee.getMonthlyIncome() * INCOME_TAX_PERCENTAGE) / 100; - - } + double calculate(Employee employee); + double calculateIncomeTax(Employee employee); + double calculateProfessionalTax(Employee employee); + double calculateEducationTax(Employee employee); } \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorProvider.java b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorProvider.java new file mode 100644 index 00000000..95145b22 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorProvider.java @@ -0,0 +1,15 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.*; + +public class TaxCalculatorProvider { + private static final TaxCalculator FullTimeEmployeeTaxCalculator = new FullTimeEmployeeTaxCalculator(); + private static final TaxCalculator PartTimeEmployeeTaxCalculator = new PartTimeEmployeeTaxCalculator(); + private static final TaxCalculator InternTaxCalculator = new InternTaxCalculator(); + public static final TaxCalculator getTaxCalculator(Employee employee){ + if(employee instanceof FullTimeEmployee) return FullTimeEmployeeTaxCalculator; + if(employee instanceof PartTimeEmployee) return PartTimeEmployeeTaxCalculator; + if(employee instanceof Intern) return InternTaxCalculator; + return null; + } +} diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java index a742fac0..b2cf8de1 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java @@ -1,10 +1,5 @@ package oops.SOLID.singleResponsibilityPrinciple.before; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; - /* Models an employee form a business perspective */ @@ -63,32 +58,27 @@ public String getFullName(){ return this.firstName + " " + this.lastName; } - public void save(){ - try { - Employee employee =this; - StringBuilder sb = new StringBuilder(); - sb.append("### EMPLOYEE RECORD ####"); - sb.append(System.lineSeparator()); - sb.append("NAME: "); - sb.append(employee.firstName + " " + employee.lastName); - sb.append(System.lineSeparator()); - sb.append("POSITION: "); - sb.append(employee.getClass().getTypeName()); - sb.append(System.lineSeparator()); - sb.append("EMAIL: "); - sb.append(employee.getEmail()); - sb.append(System.lineSeparator()); - sb.append("MONTHLY WAGE: "); - sb.append(employee.monthlyIncome); - sb.append(System.lineSeparator()); - - Path path = Paths.get(employee.getFullName() - .replace(" ","_") + ".rec"); - Files.write(path, sb.toString().getBytes()); + public String serialize() { + Employee employee = this; + StringBuilder sb = new StringBuilder(); + sb.append("### EMPLOYEE RECORD ####"); + sb.append(System.lineSeparator()); + sb.append("NAME: "); + sb.append(employee.firstName + " " + employee.lastName); + sb.append(System.lineSeparator()); + sb.append("POSITION: "); + sb.append(employee.getClass().getTypeName()); + sb.append(System.lineSeparator()); + sb.append("EMAIL: "); + sb.append(employee.getEmail()); + sb.append(System.lineSeparator()); + sb.append("MONTHLY WAGE: "); + sb.append(employee.monthlyIncome); + sb.append(System.lineSeparator()); + return sb.toString(); + } - System.out.println("Saved employee " + employee.toString()); - } catch (IOException e){ - System.out.println("ERROR: Could not save employee. " + e); - } + public String generateKey(){ + return this.firstName+"_"+this.lastName; } } diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java index b76f4589..f3b7664b 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java @@ -1,5 +1,9 @@ package oops.SOLID.singleResponsibilityPrinciple.before; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Arrays; import java.util.List; @@ -16,4 +20,20 @@ public List findAll(){ return Arrays.asList(anna, billy, steve, magda); } + + public void save(String key, String serializedEmployee){ + Path path = Paths.get(key + ".rec"); + try { + Files.write(path, serializedEmployee.getBytes()); + System.out.println("Saved " + serializedEmployee); + } catch (IOException e) { + System.out.println("Error in saving " + e.getMessage()); + } + } + + public void saveAll(List employees) { + for (Employee e : employees){ + save(e.generateKey(), e.serialize()); + } + } } \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java index 3e30e5e9..b35bd6a3 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java @@ -9,8 +9,6 @@ public static void main(String[] args) { List employees = repository.findAll(); // Save all - for (Employee e : employees){ - e.save(); - } + repository.saveAll(employees); } } \ No newline at end of file