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
16 changes: 11 additions & 5 deletions src/oops/SOLID/lsp/stack/before/StackWrong.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@
* 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;


ArrayList<Integer> arrayList;

public void push(Integer a) {
add(topPointer, a);
arrayList.add(topPointer, a);
topPointer++;
}
public void pop() {
remove(topPointer-1);
arrayList.remove(topPointer-1);
topPointer--;
}
public Integer top() {
return get(topPointer-1);
return arrayList.get(topPointer-1);
}

public StackWrong(){
arrayList = new ArrayList<Integer>();
}

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.getTaxableAmount();
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 @@ -3,7 +3,7 @@
/*
Models an employee form a business perspective
*/
public abstract class Employee {
public abstract class Employee extends ITaxable{
private String firstName;
private String lastName;
private int monthlyIncome;
Expand Down Expand Up @@ -57,5 +57,7 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) {
public String getFullName(){
return this.firstName + " " + this.lastName;
}

public abstract double getTaxableAmount();
}

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

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

@Override
public double getTaxableAmount(){
FullTimeEmployeeTaxCalculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package oops.SOLID.openClosePrinciple.before.employees;

public interface ITaxable{
public double getTaxableAmount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ public Intern(String fullName, int monthlyIncome, int nbHours) {
super(fullName, monthlyIncome);
setNbHoursPerWeek(nbHours);
}

@Override
public double getTaxableAmount(){
InternTaxCalculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ public PartTimeEmployee(String fullName, int monthlyIncome) {
super(fullName, monthlyIncome);
this.setNbHoursPerWeek(20);
}

@Override
public double getTaxableAmount(){
PartTimeEmployeeTaxCalculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

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

}
}
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;

public class InternTaxCalculator extends TaxCalculator {
private final static int INCOME_TAX_PERCENTAGE = 15;
private final static int TAX_FREE_LIMIT = 300000;


public static double calculate(Employee employee) {
if(employee.getMonthlyIncome() * 12 <= TAX_FREE_LIMIT){
return 0;
}

return
(employee.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE) / 100;

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

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

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

}
}
49 changes: 22 additions & 27 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/*
Models an employee form a business perspective
*/
public abstract class Employee {
public abstract class Employee implements ISavable {
private String firstName;
private String lastName;
private int monthlyIncome;
Expand Down Expand Up @@ -63,32 +63,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 getSavableData(){
return 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());
}

System.out.println("Saved employee " + employee.toString());
} catch (IOException e){
System.out.println("ERROR: Could not save employee. " + e);
}
public string getFileName(){
return employee.getFullName()
.replace(" ","_") + ".rec";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package oops.SOLID.singleResponsibilityPrinciple.before;

public interface ISavable{
string getSavableData();
string getFileName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ public static void main(String[] args) {
EmployeeRepository repository = new EmployeeRepository();
List<Employee> employees = repository.findAll();

public void save(ISavable savable){
try {
Path path = Paths.get(savable.getFileName());
Files.write(path, savable.getSavableData().getBytes());

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

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