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
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package oops.SOLID.DependencyInversionPrinciple.before.main;

import oops.SOLID.DependencyInversionPrinciple.before.serielizer.EmployeeFileSerializer;
import oops.SOLID.DependencyInversionPrinciple.before.payments.PaymentProcessor;

public class PayEmployeesMain {

public static void main(String[] args) {
PaymentProcessor paymentProcessor = new PaymentProcessor();
EmployeeFileSerializer serializer = new EmployeeFileSerializer();
PaymentProcessor paymentProcessor = new PaymentProcessor(serializer);
int totalPayments = paymentProcessor.sendPayments();
System.out.println("Total payments " + totalPayments);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import oops.SOLID.DependencyInversionPrinciple.before.serielizer.EmployeeFileSerializer;

public class PaymentProcessor {
private EmployeeFileSerializer serializer;

public PaymentProcessor(EmployeeFileSerializer serializer) {
this.serializer = serializer;
}

public int sendPayments(){
EmployeeFileSerializer serializer =
new EmployeeFileSerializer();

EmployeeFileRepository employeeRepository =
new EmployeeFileRepository(serializer);
Expand Down
15 changes: 15 additions & 0 deletions src/oops/SOLID/lsp/stack/before/ClientMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package oops.SOLID.lsp.stack.before;

class ClientMain{

public static void main(String[] args) {
Stack<Integer> st = new Stack<>();
st.push(1);
st.push(2);
System.out.println(st.top());
st.pop();
System.out.println(st.top());
st.clear();
System.out.println(st.top());
}
}
26 changes: 26 additions & 0 deletions src/oops/SOLID/lsp/stack/before/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package oops.SOLID.lsp.stack.before;

import java.util.ArrayList;

public class Stack<T> {
private int topPointer;
private List<T> arr;
public StackWrong(){
this.topPointer=0;
this.arr=new ArrayList<>();
}
public void push(T a) {
arr.add(topPointer, a);
topPointer++;
}
public void pop() {
arr.remove(topPointer-1);
topPointer--;
}
public void clear() {
arr.removeAll(arr);
topPointer=0;
}
public T top() {
return arr.get(topPointer-1);
}
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
Expand Up @@ -26,10 +26,10 @@ public static void main(String[] args) {
for (Employee employee: employees){

// compute individual tax
double tax = TaxCalculator.calculate(employee);
double tax = employee.getTax();
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += TaxCalculator.calculate(employee);
totalTaxes += tax;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,8 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) {
public String getFullName(){
return this.firstName + " " + this.lastName;
}

public abstract double getTax();

}

Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package oops.SOLID.openClosePrinciple.before.employees;
package oops.SOLID.openClosePrinciple.before.taxes;

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

public double getTax(){
TaxCalculator tc =new FullTimeEmployeeTaxCalculator();
return tc.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package oops.SOLID.openClosePrinciple.before.employees;
package oops.SOLID.openClosePrinciple.before.taxes;

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

public double getTax(){
TaxCalculator tc =new InternTaxCalculator();
return tc.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package oops.SOLID.openClosePrinciple.before.employees;
package oops.SOLID.openClosePrinciple.before.taxes;

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

public double getTax(){
TaxCalculator tc =new PartTimeEmployeeTaxCalculator();
return tc.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

class FullTimeEmployeeTaxCalculator implements TaxCalculator{

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


public double calculate(Employee employee) {
return
(employee.getMonthlyIncome() * INCOME_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * EDUCATIONAL_CESS) / 100;

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

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

class InternTaxCalculator implements TaxCalculator{

private final static int INCOME_TAX_PERCENTAGE = 15;


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


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

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

class PartTimeEmployeeTaxCalculator implements TaxCalculator{

private final static int INCOME_TAX_PERCENTAGE = 20;
private final static int PROFESSIONAL_TAX_PERCENTAGE = 3;
private final static int EDUCATIONAL_CESS = 1;


public double calculate(Employee employee) {
return
(employee.getMonthlyIncome() * INCOME_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * EDUCATIONAL_CESS) / 100;

}
}
14 changes: 2 additions & 12 deletions src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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 static double calculate(Employee employee) {
return
(employee.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * INCOME_TAX_PERCENTAGE) / 100;

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

import java.nio.file.Paths;
class CreatePath{

public Path getPath(Employee employee){
return Paths.get(employee.getFullName()
.replace(" ","_") + ".rec");
}
}
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 @@ -62,33 +57,5 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) {
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);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package oops.SOLID.singleResponsibilityPrinciple.before;

import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class SaveEmployeesMain {
public static void main(String[] args) {
Expand All @@ -10,7 +13,21 @@ public static void main(String[] args) {

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

public void save(Employee employee){
try {
SerializeData serialData=new SerializeData();
String sb=serialData.getSerializeData(employee);
CreatePath cp=new getSerializeData();
Path path = cp.getPath(employee);
Files.write(path, sb.getBytes());

System.out.println("Saved employee " + employee.toString());
} catch (IOException e){
System.out.println("ERROR: Could not save employee. " + e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package oops.SOLID.singleResponsibilityPrinciple.before;

class SerializeData{


public String getSerializeData(Employee employee){
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();
}

}
Loading