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
19 changes: 15 additions & 4 deletions src/oops/SOLID/lsp/stack/before/StackWrong.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,30 @@
* so objects of ArrayList are not fully replaceable by the objects of stack.
*
*/
public class StackWrong extends ArrayList<Integer>{
public class StackWrong{
private int topPointer = 0;
private ArrayList<Integer> list = new ArrayList<>();

public void push(Integer a) {
add(topPointer, a);
list.add(topPointer, a);
topPointer++;
}
public void pop() {
remove(topPointer-1);
if(topPointer == 0)
return;
list.remove(topPointer-1);
topPointer--;
}

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

public Integer top() {
return get(topPointer-1);
if(topPointer == 0)
return -1;
return list.get(topPointer-1);
}

public static void main(String[] args) {
Expand Down
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.taxCalculate();
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,7 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) {
public String getFullName(){
return this.firstName + " " + this.lastName;
}

public abstract double taxCalculate();
}

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

import oops.SOLID.openClosePrinciple.before.taxes.FullTimeEmpTaxCalculate;
import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculator;

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

@Override
public double taxCalculate() {
// TODO Auto-generated method stub
TaxCalculator calculator = new FullTimeEmpTaxCalculate();
return calculator.calculate(this); // Employee emp = new FullTimeEmployee()
}
}
10 changes: 10 additions & 0 deletions src/oops/SOLID/openClosePrinciple/before/employees/Intern.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package oops.SOLID.openClosePrinciple.before.employees;

import oops.SOLID.openClosePrinciple.before.taxes.InternTaxCalculator;
import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculator;

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

@Override
public double taxCalculate() {
// TODO Auto-generated method stub
TaxCalculator calculator = new InternTaxCalculator();
return calculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package oops.SOLID.openClosePrinciple.before.employees;

import oops.SOLID.openClosePrinciple.before.taxes.PartTimeEmployeeTaxCalculator;
import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculator;

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

@Override
public double taxCalculate() {
// TODO Auto-generated method stub
TaxCalculator calculator = new PartTimeEmployeeTaxCalculator();
return calculator.calculate(this);
}
}
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;

public class FullTimeEmpTaxCalculate implements TaxCalculator{

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

@Override
public double calculate(Employee emp) {
// TODO Auto-generated method stub
return
(emp.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE * 12) / 100 +
(emp.getMonthlyIncome() * INCOME_TAX_PERCENTAGE * 12) / 100 +
(emp.getMonthlyIncome() * EDUCATION_CESS * 12) / 100;
}

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

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

public class InternTaxCalculator implements TaxCalculator{

@Override
public double calculate(Employee emp) {
int income = emp.getMonthlyIncome();
if(income * 12 < 300000)
return 0;
else
return (income * 12 * 15)/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;

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_CESS = 1;

@Override
public double calculate(Employee emp) {
// TODO Auto-generated method stub
return
(emp.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE * 12) / 100 +
(emp.getMonthlyIncome() * INCOME_TAX_PERCENTAGE * 12) / 100 +
(emp.getMonthlyIncome() * EDUCATION_CESS * 12) / 100;
}

}
13 changes: 3 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,8 @@

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 emp);

}
23 changes: 6 additions & 17 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 @@ -53,7 +48,7 @@ public int getNbHoursPerWeek() {

public void setNbHoursPerWeek(int nbHoursPerWeek) {
if(nbHoursPerWeek <= 0){
throw new IllegalArgumentException("Income must be positive");
throw new IllegalArgumentException("Number of working hours per week must be positive");
}

this.nbHoursPerWeek = nbHoursPerWeek;
Expand All @@ -63,9 +58,9 @@ public String getFullName(){
return this.firstName + " " + this.lastName;
}

public void save(){
try {
Employee employee =this;
public String serializeData(){

Employee employee = this;
StringBuilder sb = new StringBuilder();
sb.append("### EMPLOYEE RECORD ####");
sb.append(System.lineSeparator());
Expand All @@ -82,13 +77,7 @@ public void save(){
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);
}

return sb.toString();
}
}
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 @@ -15,5 +19,19 @@ public List<Employee> findAll(){
Employee magda = new PartTimeEmployee("Magda Iovan", 920);

return Arrays.asList(anna, billy, steve, magda);

}

public void save(Employee emp) {
try {
String serializedData = emp.serializeData();
Path path = Paths.get(emp.getFullName()
.replace(" ","_") + ".rec");
Files.write(path, serializedData.getBytes());

}catch (IOException e){
System.out.println("ERROR: Could not save employee. " + e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static void main(String[] args) {

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