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
Binary file added src/oops/SOLID/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions src/oops/SOLID/lsp/stack/before/StackInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package oops.SOLID.lsp.stack.before;

public interface StackInterface<T> {
void push(T a);
T pop();
T top();
void clear();
}
71 changes: 71 additions & 0 deletions src/oops/SOLID/lsp/stack/before/StackRight.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package oops.SOLID.lsp.stack.before;

import java.util.ArrayList;
import java.util.List;

/*
* Stack is-a list with push() pop() methods.
* So can we create a stack by extending an ArrayList class?
*
* No! This implementation violates the Liskov Substitution Principle.
* Which states that
* "Objects in a program should be replaceable with instances of their subtypes
* without altering the correctness of that program."
*
* In this case ArrayList has multiple methods which stack is not supposed to have (ex clear(), get(i) etc)
* so objects of ArrayList are not fully replaceable by the objects of stack.
*
*/
public class StackRight<T> implements StackInterface<T>{
private int topPointer;
private List<T> elementData;

public StackRight() {
this.topPointer = -1;
this.elementData = new ArrayList<>();
}

@Override
public void push(T a) {
topPointer++;
elementData.add(a);
}

public T pop() {
T popped = null;
if (topPointer == -1) {
System.out.println("Stack is Empty");
} else {
topPointer--;
popped = elementData.remove(elementData.size() - 1);
}
return popped;
}

public T top() {
T topEle = null;
if (topPointer == -1) {
System.out.println("Stack is Empty");
} else {
topEle = elementData.get(topPointer);
}
return topEle;
}

public void clear() {
this.elementData.clear();
this.topPointer = -1;
}

public static void main(String[] args) {
StackInterface<Integer> st = new StackRight<>();
st.push(1);
st.push(2);
System.out.println(st.top());
st.pop();
System.out.println(st.top());
st.clear();
System.out.println(st.pop());
}

}
42 changes: 0 additions & 42 deletions src/oops/SOLID/lsp/stack/before/StackWrong.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package oops.SOLID.openClosePrinciple.before.client;

import oops.SOLID.openClosePrinciple.before.employees.Employee;
import oops.SOLID.openClosePrinciple.before.persistence.EmployeeRepository;
import oops.SOLID.openClosePrinciple.before.taxes.FullTimeEmployeeTaxCalculator;
import oops.SOLID.openClosePrinciple.before.taxes.InternEmployeeTaxCalculator;
import oops.SOLID.openClosePrinciple.before.taxes.PartTimeEmployeeTaxCalculator;
import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculatorInterface;

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

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


public class CalculateTaxesClient {
public static void main(String[] args) {
Expand All @@ -20,16 +23,30 @@ public static void main(String[] args) {
// Calculate taxes
Locale locale = new Locale("en", "US");
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale);


double totalTaxes = 0;
for (Employee employee: employees){
TaxCalculatorInterface fullTimeEmployee = new FullTimeEmployeeTaxCalculator(30, 2, 1);
TaxCalculatorInterface partTimeEmployee = new PartTimeEmployeeTaxCalculator(20, 3, 1);
TaxCalculatorInterface internEmployee = new InternEmployeeTaxCalculator(15);

double totalTaxes = 0;
for (Employee employee: employees) {
// compute individual tax
double tax = TaxCalculator.calculate(employee);
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += TaxCalculator.calculate(employee);
if (employee.getNbHoursPerWeek() == 40) {
double tax = fullTimeEmployee.calculate(employee);
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += tax;
} else if (employee.getNbHoursPerWeek() == 20) {
double tax = partTimeEmployee.calculate(employee);
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += tax;
} else {
double tax = internEmployee.calculate(employee);
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += tax;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

public class FullTimeEmployeeTaxCalculator implements TaxCalculatorInterface {
private final int incomeTaxPercentage;
private final int professionalTaxPercentage;
private final int educationCess;

public FullTimeEmployeeTaxCalculator(int incomeTaxPercentage, int professionalTaxPercentage, int educationCess) {
this.incomeTaxPercentage = incomeTaxPercentage;
this.professionalTaxPercentage = professionalTaxPercentage;
this.educationCess = educationCess;
}

public double calculate(Employee employee) {
int salary = employee.getMonthlyIncome();
return
(salary * professionalTaxPercentage) / 100d +
(salary * incomeTaxPercentage) / 100d +
(salary * educationCess) / 100d ;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

public class InternEmployeeTaxCalculator implements TaxCalculatorInterface {

private final int incomeTaxPercentage;
private final int professionalTaxPercentage;
private final int educationCess;

public InternEmployeeTaxCalculator(int incomeTaxPercentage) {
this(incomeTaxPercentage, 0,0);
}

public InternEmployeeTaxCalculator(int incomeTaxPercentage, int professionalTaxPercentage, int educationCess) {
this.incomeTaxPercentage = incomeTaxPercentage;
this.professionalTaxPercentage = professionalTaxPercentage;
this.educationCess = educationCess;
}

public double calculate(Employee employee) {
int salary = employee.getMonthlyIncome();
if (salary > 3_00_000) {
return
(salary * incomeTaxPercentage) / 100d +
(salary * professionalTaxPercentage) / 100d +
(salary * educationCess) / 100d;
}
return 0d;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

public class PartTimeEmployeeTaxCalculator implements TaxCalculatorInterface {
private final int incomeTaxPercentage;
private final int professionalTaxPercentage;
private final int educationCess;

public PartTimeEmployeeTaxCalculator(int incomeTaxPercentage, int professionalTaxPercentage, int educationCess) {
this.incomeTaxPercentage = incomeTaxPercentage;
this.professionalTaxPercentage = professionalTaxPercentage;
this.educationCess = educationCess;
}

public double calculate(Employee employee) {
int salary = employee.getMonthlyIncome();
return
(salary * incomeTaxPercentage) / 100d +
(salary * professionalTaxPercentage) / 100d +
(salary * educationCess) / 100d ;

}
}
16 changes: 0 additions & 16 deletions src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

public interface TaxCalculatorInterface {
double calculate(Employee employee);
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package oops.SOLID.singleResponsibilityPrinciple.before;

import java.util.List;

public class ClientMain {
public static void main(String[] args) {
EmployeeRepository repository = new EmployeeRepository();
// Add employees
repository.addEmployee(new FullTimeEmployee("Anna Smith", 2000));
repository.addEmployee(new FullTimeEmployee("Billy Leech", 920));
repository.addEmployee(new PartTimeEmployee("Steve Jones", 800));
repository.addEmployee(new PartTimeEmployee("Magda Iovan", 920));

// Grab and save employees
List<Employee> employees = repository.findAll();
for (Employee e : employees){
repository.save(e);
}
}
}
31 changes: 4 additions & 27 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Models an employee form a business perspective
*/
public abstract class Employee {
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private int monthlyIncome;
Expand Down Expand Up @@ -63,32 +64,8 @@ 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());

System.out.println("Saved employee " + employee.toString());
} catch (IOException e){
System.out.println("ERROR: Could not save employee. " + e);
}
public String serialize() {
return EmployeeSerializer.serialize(this);
}

}
Loading