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
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/oops/
17 changes: 13 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,28 @@
* 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> arrayList = new ArrayList<>();

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

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

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

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

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

public double calculateTax() {
TaxCalculator calculator = new TaxCalculatorFulltime();
return calculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package oops.SOLID.openClosePrinciple.before.employees;

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

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

public double calculateTax() {
TaxCalculator calculator = new TaxCalculatorIntern();
return calculator.calculate(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
package oops.SOLID.openClosePrinciple.before.employees;

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

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

public double calculateTax() {
TaxCalculator calculator = new TaxCalculatorPartTime();
return calculator.calculate(this);
}
}
13 changes: 2 additions & 11 deletions src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@

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 {
double calculate(Employee employee);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package oops.SOLID.openClosePrinciple.before.taxes;

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

public class TaxCalculatorFulltime implements TaxCalculator {

public double calculate(Employee employee) {
final int INCOME_TAX_PERCENTAGE = 30;
final int PROFESSIONAL_TAX_PERCENTAGE = 2;
final int EDUCATIONAL_TAX_PERCENTAGE = 1;

return
(employee.getMonthlyIncome() * PROFESSIONAL_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * INCOME_TAX_PERCENTAGE) / 100 +
(employee.getMonthlyIncome() * EDUCATIONAL_TAX_PERCENTAGE) / 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 TaxCalculatorIntern implements TaxCalculator {

public double calculate(Employee employee) {
final int INCOME_TAX_PERCENTAGE = 15;

if(employee.getMonthlyIncome() < 300000)
return 0;
else
return (employee.getMonthlyIncome() * INCOME_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 TaxCalculatorPartTime implements TaxCalculator {

public double calculate(Employee employee) {
final int INCOME_TAX_PERCENTAGE = 20;
final int PROFESSIONAL_TAX_PERCENTAGE = 3;
final int EDUCATIONAL_TAX_PERCENTAGE = 1;

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

}
48 changes: 20 additions & 28 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,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 must be positive");
}

this.nbHoursPerWeek = nbHoursPerWeek;
Expand All @@ -63,32 +63,24 @@ 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 getDetails(){
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());

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 @@ -16,4 +20,17 @@ public List<Employee> findAll(){

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

public void save(Employee employee) {
try {
String employeeDetails = employee.getDetails();
Path path = Paths.get(employee.getFullName().replace(" ","_") + ".rec");
Files.write(path, employeeDetails.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
Expand Up @@ -10,7 +10,7 @@ public static void main(String[] args) {

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