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.before.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.before.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.before.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.before.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/
11 changes: 11 additions & 0 deletions src/oops/SOLID/lsp/stack/before/StackBehavior.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oops.SOLID.lsp.stack.before;

public interface StackBehavior<T> {

public void push(T a);

public void pop();

public T top();

}
31 changes: 23 additions & 8 deletions src/oops/SOLID/lsp/stack/before/StackWrong.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package oops.SOLID.lsp.stack.before;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
/*
* Stack is-a list with push() pop() methods.
* So can we create a stack by extending an ArrayList class?
Expand All @@ -14,29 +16,42 @@
* so objects of ArrayList are not fully replaceable by the objects of stack.
*
*/
public class StackWrong extends ArrayList<Integer>{
public class StackWrong<T> implements StackBehavior<T> {
private int topPointer = 0;

public void push(Integer a) {
add(topPointer, a);
private List<T> stack;

public StackWrong() {
stack = new ArrayList<T>();
}

public void push(T a) {
stack.add(topPointer, a);
topPointer++;
}
public void pop() {
remove(topPointer-1);
if(topPointer == 0) {
throw new EmptyStackException();
}
stack.remove(topPointer-1);
topPointer--;
}
public Integer top() {
return get(topPointer-1);

public T top() {
if(topPointer == 0) {
throw new EmptyStackException();
}
return stack.get(topPointer-1);
}

public static void main(String[] args) {
StackWrong st = new StackWrong();
StackWrong<Integer> st = new StackWrong<>();
st.push(1);
st.push(2);
System.out.println(st.top());
st.pop();
System.out.println(st.top());
st.clear();
// st.clear(); // not supported
System.out.println(st.top());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public static void main(String[] args) {

// compute individual tax
double tax = TaxCalculator.calculate(employee);
String formattedTax = currencyFormatter.format(tax);
// add to company total taxes
totalTaxes += TaxCalculator.calculate(employee);
totalTaxes += tax;
}
String formattedTax = currencyFormatter.format(totalTaxes);


System.out.println("totalTaxes : \t" + formattedTax);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package oops.SOLID.openClosePrinciple.before.employees;

public interface EducationalTax {

int getEducationalTax();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public abstract class Employee {
private String lastName;
private int monthlyIncome;
private int nbHoursPerWeek;

public abstract double calculate();

public Employee(String fullName, int monthlyIncome){
setMonthlyIncome(monthlyIncome);
Expand Down Expand Up @@ -57,5 +59,6 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) {
public String getFullName(){
return this.firstName + " " + this.lastName;
}

}

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

public class FullTimeEmployee extends Employee {
public FullTimeEmployee(String fullName, int monthlyIncome) {
public class FullTimeEmployee extends Employee implements IncomeTax , ProfessionalTax,EducationalTax {

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

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

@Override
public double calculate() {
return (this.getMonthlyIncome() * getIncomeTax()) / 100 + (this.getMonthlyIncome() * getProfessionalTax()) / 100
+ (this.getMonthlyIncome() * getEducationalTax()) / 100;
}

@Override
public int getEducationalTax() {
return EDUCATIONAL_TAX_PERCENTAGE;
}

@Override
public int getProfessionalTax() {
return PROFESSIONAL_TAX_PERCENTAGE;
}

@Override
public int getIncomeTax() {
return INCOME_TAX_PERCENTAGE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package oops.SOLID.openClosePrinciple.before.employees;

public interface IncomeTax {

int getIncomeTax();
}
23 changes: 21 additions & 2 deletions src/oops/SOLID/openClosePrinciple/before/employees/Intern.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package oops.SOLID.openClosePrinciple.before.employees;

public class Intern extends Employee {
public Intern(String fullName, int monthlyIncome, int nbHours) {
public class Intern extends Employee implements IncomeTax{

private final static int INCOME_TAX_PERCENTAGE = 15;

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

@Override
public double calculate() {
Double tax = 0.0;

if ((this.getMonthlyIncome() * 12) > 30000) {
tax = (getIncomeTax() * getIncomeTax()) / 100.0;
}
return tax;
}


@Override
public int getIncomeTax() {
return INCOME_TAX_PERCENTAGE;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
package oops.SOLID.openClosePrinciple.before.employees;

public class PartTimeEmployee extends Employee {
public PartTimeEmployee(String fullName, int monthlyIncome) {
public class PartTimeEmployee extends Employee implements IncomeTax , ProfessionalTax,EducationalTax{

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

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

@Override
public
double calculate() {
return (this.getMonthlyIncome() * getIncomeTax()) / 100 + (this.getMonthlyIncome() * getProfessionalTax()) / 100
+ (this.getMonthlyIncome() * getEducationalTax()) / 100;
}


@Override
public int getEducationalTax() {
return EDUCATIONAL_TAX_PERCENTAGE;
}

@Override
public int getProfessionalTax() {
return PROFESSIONAL_TAX_PERCENTAGE;
}

@Override
public int getIncomeTax() {
return INCOME_TAX_PERCENTAGE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oops.SOLID.openClosePrinciple.before.employees;

public interface ProfessionalTax {

int getProfessionalTax();

}
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package oops.SOLID.openClosePrinciple.before.persistence;

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

import oops.SOLID.openClosePrinciple.before.employees.Employee;
import oops.SOLID.openClosePrinciple.before.employees.FullTimeEmployee;
import oops.SOLID.openClosePrinciple.before.employees.Intern;
import oops.SOLID.openClosePrinciple.before.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);
List<Employee> employees = new ArrayList<Employee>();
private final static int NB_HOURS_PER_WEEK= 10;

public EmployeeRepository(){

// 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);
Employee steve = new PartTimeEmployee("Steve Jones", 800);
Employee magda = new PartTimeEmployee("Magda Iovan", 920);

Employee intern1 = new Intern("ravi kumar", 800,NB_HOURS_PER_WEEK);
Employee intern2 = new Intern("syam kumar", 920,NB_HOURS_PER_WEEK);

addEmployee(anna, billy, steve, magda,intern1,intern2);
}

public void addEmployee(Employee... employees) {
this.employees.addAll(Arrays.asList(employees));
}

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

public List<Employee> findAll(){
return employees;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@
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;

return employee.calculate();
}

}
56 changes: 22 additions & 34 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 @@ -62,33 +57,26 @@ 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);
}
}


public String employeeFormat() {
StringBuilder sb = new StringBuilder();
sb.append("### EMPLOYEE RECORD ####");
sb.append(System.lineSeparator());
sb.append("NAME: ");
sb.append(this.getFullName());
sb.append(System.lineSeparator());
sb.append("POSITION: ");
sb.append(this.getClass().getTypeName());
sb.append(System.lineSeparator());
sb.append("EMAIL: ");
sb.append(this.getEmail());
sb.append(System.lineSeparator());
sb.append("MONTHLY WAGE: ");
sb.append(this.getMonthlyIncome());
sb.append(System.lineSeparator());
return sb.toString();
}


}
Loading