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
5 changes: 5 additions & 0 deletions Anna_Smith.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Anna Smith
POSITION: oops.SOLID.singleResponsibilityPrinciple.after.FullTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 2000
5 changes: 5 additions & 0 deletions Billy_Leech.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Billy Leech
POSITION: oops.SOLID.singleResponsibilityPrinciple.after.FullTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 920
5 changes: 5 additions & 0 deletions Magda_Iovan.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Magda Iovan
POSITION: oops.SOLID.singleResponsibilityPrinciple.after.PartTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 920
5 changes: 5 additions & 0 deletions Steve_Jones.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Steve Jones
POSITION: oops.SOLID.singleResponsibilityPrinciple.after.PartTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 800
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/oops/
49 changes: 49 additions & 0 deletions src/oops/SOLID/lsp/stack/after/StackWrong.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package oops.SOLID.lsp.stack.after;


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 StackWrong {
private int topPointer = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
public void push(Integer a) {
list.add(topPointer, a);
topPointer++;
}
public void pop() {
list.remove(topPointer-1);
topPointer--;
}
public Integer top() {
return list.get(topPointer-1);
}

public void clear() {
list.clear();
topPointer =0;
}

public static void main(String[] args) {
StackWrong st = new StackWrong();
st.push(1);
st.push(2);
System.out.println(st.top());
st.pop();
System.out.println(st.top());
st.clear();
System.out.println("stack is empty");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package oops.SOLID.openClosePrinciple.after.client;

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

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


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){
System.out.println(employee.toString());
// compute individual tax
double tax = employee.calculateTax(employee);
String formattedTax = currencyFormatter.format(tax);
System.out.println(formattedTax);
// add to company total taxes
totalTaxes += employee.calculateTax(employee);

}
}
}
65 changes: 65 additions & 0 deletions src/oops/SOLID/openClosePrinciple/after/employees/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package oops.SOLID.openClosePrinciple.after.employees;

import oops.SOLID.openClosePrinciple.after.taxes.iTaxCalculator;

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

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 calculateTax(Employee employee);
}

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.FullTimeTaxCalculator;

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

@Override
public double calculateTax(Employee employee) {
FullTimeTaxCalculator fullTimeTaxCalculator = new FullTimeTaxCalculator();
return fullTimeTaxCalculator.calculateTax(employee);
}
}
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.InternTaxCalculator;

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

@Override
public double calculateTax(Employee employee) {
InternTaxCalculator internTaxCalculator = new InternTaxCalculator();
return internTaxCalculator.calculateTax(employee);
}
}
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.PartTimeTaxCalculator;

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

@Override
public double calculateTax(Employee employee) {
PartTimeTaxCalculator partItemTaxCalculator = new PartTimeTaxCalculator();
return partItemTaxCalculator.calculateTax(employee);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package oops.SOLID.openClosePrinciple.after.persistence;

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

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

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,21 @@
package oops.SOLID.openClosePrinciple.after.taxes;

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

public class FullTimeTaxCalculator implements iTaxCalculator{

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

@Override
public double calculateTax(Employee employee) {


return ((employee.getMonthlyIncome() /100)*
(INCOME_TAX_PERCENTAGE +
PROFESSIONAL_TAX_PERCENTAGE +
EDUCATIONAL_TAX_PERCENTAGE));
}

}
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 InternTaxCalculator implements iTaxCalculator {
private final static int INCOME_TAX_PERCENTAGE = 15;

@Override
public double calculateTax(Employee employee) {
if(employee.getMonthlyIncome()*12 < 300000)
return 0;
else
return ((employee.getMonthlyIncome() / 100)
* (INCOME_TAX_PERCENTAGE));
}

}
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 PartTimeTaxCalculator implements iTaxCalculator {
private final static int INCOME_TAX_PERCENTAGE = 20;
private final static int PROFESSIONAL_TAX_PERCENTAGE = 3;
private final static int EDUCATIONAL_TAX_PERCENTAGE = 1;

@Override
public double calculateTax(Employee employee) {

return ((employee.getMonthlyIncome() / 100)
* (INCOME_TAX_PERCENTAGE + PROFESSIONAL_TAX_PERCENTAGE + EDUCATIONAL_TAX_PERCENTAGE));
}

}
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 iTaxCalculator {
public double calculateTax(Employee employee);
}
Loading