diff --git a/Anna_Smith.rec b/Anna_Smith.rec new file mode 100644 index 00000000..65436c72 --- /dev/null +++ b/Anna_Smith.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Anna Smith +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.FullTimeEmployee +EMAIL: Anna.Smith@globomanticshr.com +MONTHLY WAGE: 2000 diff --git a/Billy_Leech.rec b/Billy_Leech.rec new file mode 100644 index 00000000..2ec8176f --- /dev/null +++ b/Billy_Leech.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Billy Leech +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.FullTimeEmployee +EMAIL: Billy.Leech@globomanticshr.com +MONTHLY WAGE: 920 diff --git a/Magda_Iovan.rec b/Magda_Iovan.rec new file mode 100644 index 00000000..8f673ff6 --- /dev/null +++ b/Magda_Iovan.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Magda Iovan +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.PartTimeEmployee +EMAIL: Magda.Iovan@globomanticshr.com +MONTHLY WAGE: 920 diff --git a/Steve_Jones.rec b/Steve_Jones.rec new file mode 100644 index 00000000..1bf39ad7 --- /dev/null +++ b/Steve_Jones.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Steve Jones +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.PartTimeEmployee +EMAIL: Steve.Jones@globomanticshr.com +MONTHLY WAGE: 800 diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 00000000..8af4d073 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1 @@ +/oops/ diff --git a/src/oops/SOLID/lsp/stack/before/StackBehavior.java b/src/oops/SOLID/lsp/stack/before/StackBehavior.java new file mode 100644 index 00000000..5186c025 --- /dev/null +++ b/src/oops/SOLID/lsp/stack/before/StackBehavior.java @@ -0,0 +1,11 @@ +package oops.SOLID.lsp.stack.before; + +public interface StackBehavior { + + public void push(T a); + + public void pop(); + + public T top(); + +} \ No newline at end of file diff --git a/src/oops/SOLID/lsp/stack/before/StackWrong.java b/src/oops/SOLID/lsp/stack/before/StackWrong.java index 1170e573..578ea1d6 100644 --- a/src/oops/SOLID/lsp/stack/before/StackWrong.java +++ b/src/oops/SOLID/lsp/stack/before/StackWrong.java @@ -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? @@ -14,29 +16,42 @@ * so objects of ArrayList are not fully replaceable by the objects of stack. * */ -public class StackWrong extends ArrayList{ +public class StackWrong implements StackBehavior { private int topPointer = 0; - public void push(Integer a) { - add(topPointer, a); + private List stack; + + public StackWrong() { + stack = new ArrayList(); + } + + 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 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()); } } diff --git a/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java b/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java index 1651161c..419f28e8 100644 --- a/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java +++ b/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java @@ -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); } } diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/EducationalTax.java b/src/oops/SOLID/openClosePrinciple/before/employees/EducationalTax.java new file mode 100644 index 00000000..f7a4c37f --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/employees/EducationalTax.java @@ -0,0 +1,6 @@ +package oops.SOLID.openClosePrinciple.before.employees; + +public interface EducationalTax { + + int getEducationalTax(); +} diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java b/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java index a84b72a3..65e2a0d0 100644 --- a/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java +++ b/src/oops/SOLID/openClosePrinciple/before/employees/Employee.java @@ -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); @@ -57,5 +59,6 @@ public void setNbHoursPerWeek(int nbHoursPerWeek) { public String getFullName(){ return this.firstName + " " + this.lastName; } + } diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/FullTimeEmployee.java b/src/oops/SOLID/openClosePrinciple/before/employees/FullTimeEmployee.java index cd9e9384..aa566b3e 100644 --- a/src/oops/SOLID/openClosePrinciple/before/employees/FullTimeEmployee.java +++ b/src/oops/SOLID/openClosePrinciple/before/employees/FullTimeEmployee.java @@ -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; + } } diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/IncomeTax.java b/src/oops/SOLID/openClosePrinciple/before/employees/IncomeTax.java new file mode 100644 index 00000000..e4b30da4 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/employees/IncomeTax.java @@ -0,0 +1,6 @@ +package oops.SOLID.openClosePrinciple.before.employees; + +public interface IncomeTax { + + int getIncomeTax(); +} diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/Intern.java b/src/oops/SOLID/openClosePrinciple/before/employees/Intern.java index 80191de2..4917ed1f 100644 --- a/src/oops/SOLID/openClosePrinciple/before/employees/Intern.java +++ b/src/oops/SOLID/openClosePrinciple/before/employees/Intern.java @@ -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; + } } diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/PartTimeEmployee.java b/src/oops/SOLID/openClosePrinciple/before/employees/PartTimeEmployee.java index 5c491175..803148fe 100644 --- a/src/oops/SOLID/openClosePrinciple/before/employees/PartTimeEmployee.java +++ b/src/oops/SOLID/openClosePrinciple/before/employees/PartTimeEmployee.java @@ -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; + } } \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/employees/ProfessionalTax.java b/src/oops/SOLID/openClosePrinciple/before/employees/ProfessionalTax.java new file mode 100644 index 00000000..5d7ce6b0 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/employees/ProfessionalTax.java @@ -0,0 +1,7 @@ +package oops.SOLID.openClosePrinciple.before.employees; + +public interface ProfessionalTax { + + int getProfessionalTax(); + +} diff --git a/src/oops/SOLID/openClosePrinciple/before/persistence/EmployeeRepository.java b/src/oops/SOLID/openClosePrinciple/before/persistence/EmployeeRepository.java index 6d7a4ac9..cfa10ac1 100644 --- a/src/oops/SOLID/openClosePrinciple/before/persistence/EmployeeRepository.java +++ b/src/oops/SOLID/openClosePrinciple/before/persistence/EmployeeRepository.java @@ -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 findAll(){ - - // Employees are kept in memory for simplicity - Employee anna = new FullTimeEmployee("Anna Smith", 2000); - Employee billy = new FullTimeEmployee("Billy Leech", 920); + List employees = new ArrayList(); + 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 findAll(){ + return employees; } } \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java index ab0300be..9e2efbbf 100644 --- a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java @@ -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(); } + } \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java index a742fac0..67bddba4 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java @@ -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 */ @@ -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(); + } + + } diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java index b76f4589..a477bb56 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java @@ -1,19 +1,52 @@ 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.ArrayList; import java.util.Arrays; import java.util.List; public class EmployeeRepository { - - public List findAll(){ - - // Employees are kept in memory for simplicity + + List employees = new ArrayList(); + + + 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); + addEmployee(anna, billy, steve, magda); + } + + public void addEmployee(Employee... employees) { + this.employees.addAll(Arrays.asList(employees)); + } + + public List findAll(){ + return employees; + } + + public void save(Employee... employees){ + try { + + for (Employee employee : employees) { + + + Path path = Paths.get(employee.getFullName() + .replace(" ","_") + ".rec"); + Files.write(path, employee.employeeFormat().getBytes()); - return Arrays.asList(anna, billy, steve, magda); + System.out.println("Saved employee " + employee.toString()); + } + + } catch (IOException e){ + System.out.println("ERROR: Could not save employee. " + e); + } } } \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java index 3e30e5e9..fa68d115 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java @@ -10,7 +10,7 @@ public static void main(String[] args) { // Save all for (Employee e : employees){ - e.save(); + repository.save(e); } } } \ No newline at end of file