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
14 changes: 9 additions & 5 deletions src/oops/SOLID/lsp/stack/before/StackWrong.java
Original file line number Diff line number Diff line change
@@ -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?
Expand All @@ -14,16 +16,18 @@
* so objects of ArrayList are not fully replaceable by the objects of stack.
*
*/
public class StackWrong extends ArrayList<Integer>{
public class StackWrong extends Vector<Integer> {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
15 changes: 5 additions & 10 deletions src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
52 changes: 21 additions & 31 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,4 +20,20 @@ public List<Employee> 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<Employee> employees) {
for (Employee e : employees){
save(e.generateKey(), e.serialize());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public static void main(String[] args) {
List<Employee> employees = repository.findAll();

// Save all
for (Employee e : employees){
e.save();
}
repository.saveAll(employees);
}
}