From 30f189075bd64aa16852813b4efe2109fc18f6a0 Mon Sep 17 00:00:00 2001 From: achyuttibrewalla Date: Thu, 15 Apr 2021 00:10:22 -0400 Subject: [PATCH 1/4] Refactored for LSP --- .../lsp/stack/before/StackInterface.java | 8 +++ .../SOLID/lsp/stack/before/StackRight.java | 71 +++++++++++++++++++ .../SOLID/lsp/stack/before/StackWrong.java | 42 ----------- 3 files changed, 79 insertions(+), 42 deletions(-) create mode 100644 src/oops/SOLID/lsp/stack/before/StackInterface.java create mode 100644 src/oops/SOLID/lsp/stack/before/StackRight.java delete mode 100644 src/oops/SOLID/lsp/stack/before/StackWrong.java diff --git a/src/oops/SOLID/lsp/stack/before/StackInterface.java b/src/oops/SOLID/lsp/stack/before/StackInterface.java new file mode 100644 index 00000000..253b2029 --- /dev/null +++ b/src/oops/SOLID/lsp/stack/before/StackInterface.java @@ -0,0 +1,8 @@ +package oops.SOLID.lsp.stack.before; + +public interface StackInterface { + void push(T a); + T pop(); + T top(); + void clear(); +} diff --git a/src/oops/SOLID/lsp/stack/before/StackRight.java b/src/oops/SOLID/lsp/stack/before/StackRight.java new file mode 100644 index 00000000..31f7ed14 --- /dev/null +++ b/src/oops/SOLID/lsp/stack/before/StackRight.java @@ -0,0 +1,71 @@ +package oops.SOLID.lsp.stack.before; + +import java.util.ArrayList; +import java.util.List; + +/* + * Stack is-a list with push() pop() methods. + * So can we create a stack by extending an ArrayList class? + * + * No! This implementation violates the Liskov Substitution Principle. + * Which states that + * "Objects in a program should be replaceable with instances of their subtypes + * without altering the correctness of that program." + * + * In this case ArrayList has multiple methods which stack is not supposed to have (ex clear(), get(i) etc) + * so objects of ArrayList are not fully replaceable by the objects of stack. + * + */ +public class StackRight implements StackInterface{ + private int topPointer; + private List elementData; + + public StackRight() { + this.topPointer = -1; + this.elementData = new ArrayList<>(); + } + + @Override + public void push(T a) { + topPointer++; + elementData.add(a); + } + + public T pop() { + T popped = null; + if (topPointer == -1) { + System.out.println("Stack is Empty"); + } else { + topPointer--; + popped = elementData.remove(elementData.size() - 1); + } + return popped; + } + + public T top() { + T topEle = null; + if (topPointer == -1) { + System.out.println("Stack is Empty"); + } else { + topEle = elementData.get(topPointer); + } + return topEle; + } + + public void clear() { + this.elementData.clear(); + this.topPointer = -1; + } + + public static void main(String[] args) { + StackInterface st = new StackRight<>(); + st.push(1); + st.push(2); + System.out.println(st.top()); + st.pop(); + System.out.println(st.top()); + st.clear(); + System.out.println(st.pop()); + } + +} diff --git a/src/oops/SOLID/lsp/stack/before/StackWrong.java b/src/oops/SOLID/lsp/stack/before/StackWrong.java deleted file mode 100644 index 1170e573..00000000 --- a/src/oops/SOLID/lsp/stack/before/StackWrong.java +++ /dev/null @@ -1,42 +0,0 @@ -package oops.SOLID.lsp.stack.before; - -import java.util.ArrayList; -/* - * Stack is-a list with push() pop() methods. - * So can we create a stack by extending an ArrayList class? - * - * No! This implementation violates the Liskov Substitution Principle. - * Which states that - * "Objects in a program should be replaceable with instances of their subtypes - * without altering the correctness of that program." - * - * In this case ArrayList has multiple methods which stack is not supposed to have (ex clear(), get(i) etc) - * so objects of ArrayList are not fully replaceable by the objects of stack. - * - */ -public class StackWrong extends ArrayList{ - private int topPointer = 0; - - public void push(Integer a) { - add(topPointer, a); - topPointer++; - } - public void pop() { - remove(topPointer-1); - topPointer--; - } - public Integer top() { - return get(topPointer-1); - } - - public static void main(String[] args) { - StackWrong st = new StackWrong(); - st.push(1); - st.push(2); - System.out.println(st.top()); - st.pop(); - System.out.println(st.top()); - st.clear(); - System.out.println(st.top()); - } -} From bd0fbe4cff26d26e5706ab969835e9233d159f42 Mon Sep 17 00:00:00 2001 From: achyuttibrewalla Date: Thu, 15 Apr 2021 01:08:20 -0400 Subject: [PATCH 2/4] Changed Tax Code for BR 2 --- .../before/client/CalculateTaxesClient.java | 39 +++++++++++++------ .../taxes/FullTimeEmployeeTaxCalculator.java | 24 ++++++++++++ .../taxes/InternEmployeeTaxCalculator.java | 31 +++++++++++++++ .../taxes/PartTimeEmployeeTaxCalculator.java | 24 ++++++++++++ .../before/taxes/TaxCalculator.java | 16 -------- .../before/taxes/TaxCalculatorInterface.java | 7 ++++ 6 files changed, 114 insertions(+), 27 deletions(-) create mode 100644 src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java create mode 100644 src/oops/SOLID/openClosePrinciple/before/taxes/InternEmployeeTaxCalculator.java create mode 100644 src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java delete mode 100644 src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java create mode 100644 src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorInterface.java diff --git a/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java b/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java index 1651161c..089aee53 100644 --- a/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java +++ b/src/oops/SOLID/openClosePrinciple/before/client/CalculateTaxesClient.java @@ -1,13 +1,16 @@ package oops.SOLID.openClosePrinciple.before.client; +import oops.SOLID.openClosePrinciple.before.employees.Employee; +import oops.SOLID.openClosePrinciple.before.persistence.EmployeeRepository; +import oops.SOLID.openClosePrinciple.before.taxes.FullTimeEmployeeTaxCalculator; +import oops.SOLID.openClosePrinciple.before.taxes.InternEmployeeTaxCalculator; +import oops.SOLID.openClosePrinciple.before.taxes.PartTimeEmployeeTaxCalculator; +import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculatorInterface; + import java.text.NumberFormat; import java.util.List; import java.util.Locale; -import oops.SOLID.openClosePrinciple.before.employees.Employee; -import oops.SOLID.openClosePrinciple.before.persistence.EmployeeRepository; -import oops.SOLID.openClosePrinciple.before.taxes.TaxCalculator; - public class CalculateTaxesClient { public static void main(String[] args) { @@ -20,16 +23,30 @@ public static void main(String[] args) { // Calculate taxes Locale locale = new Locale("en", "US"); NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(locale); - - double totalTaxes = 0; - for (Employee employee: employees){ + TaxCalculatorInterface fullTimeEmployee = new FullTimeEmployeeTaxCalculator(30, 2, 1); + TaxCalculatorInterface partTimeEmployee = new PartTimeEmployeeTaxCalculator(20, 3, 1); + TaxCalculatorInterface internEmployee = new InternEmployeeTaxCalculator(15); + double totalTaxes = 0; + for (Employee employee: employees) { // compute individual tax - double tax = TaxCalculator.calculate(employee); - String formattedTax = currencyFormatter.format(tax); - // add to company total taxes - totalTaxes += TaxCalculator.calculate(employee); + if (employee.getNbHoursPerWeek() == 40) { + double tax = fullTimeEmployee.calculate(employee); + String formattedTax = currencyFormatter.format(tax); + // add to company total taxes + totalTaxes += tax; + } else if (employee.getNbHoursPerWeek() == 20) { + double tax = partTimeEmployee.calculate(employee); + String formattedTax = currencyFormatter.format(tax); + // add to company total taxes + totalTaxes += tax; + } else { + double tax = internEmployee.calculate(employee); + String formattedTax = currencyFormatter.format(tax); + // add to company total taxes + totalTaxes += tax; + } } } } diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java new file mode 100644 index 00000000..f7ffef66 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/FullTimeEmployeeTaxCalculator.java @@ -0,0 +1,24 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public class FullTimeEmployeeTaxCalculator implements TaxCalculatorInterface { + private final int incomeTaxPercentage; + private final int professionalTaxPercentage; + private final int educationCess; + + public FullTimeEmployeeTaxCalculator(int incomeTaxPercentage, int professionalTaxPercentage, int educationCess) { + this.incomeTaxPercentage = incomeTaxPercentage; + this.professionalTaxPercentage = professionalTaxPercentage; + this.educationCess = educationCess; + } + + public double calculate(Employee employee) { + int salary = employee.getMonthlyIncome(); + return + (salary * professionalTaxPercentage) / 100d + + (salary * incomeTaxPercentage) / 100d + + (salary * educationCess) / 100d ; + + } +} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/InternEmployeeTaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/InternEmployeeTaxCalculator.java new file mode 100644 index 00000000..806abb85 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/InternEmployeeTaxCalculator.java @@ -0,0 +1,31 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public class InternEmployeeTaxCalculator implements TaxCalculatorInterface { + + private final int incomeTaxPercentage; + private final int professionalTaxPercentage; + private final int educationCess; + + public InternEmployeeTaxCalculator(int incomeTaxPercentage) { + this(incomeTaxPercentage, 0,0); + } + + public InternEmployeeTaxCalculator(int incomeTaxPercentage, int professionalTaxPercentage, int educationCess) { + this.incomeTaxPercentage = incomeTaxPercentage; + this.professionalTaxPercentage = professionalTaxPercentage; + this.educationCess = educationCess; + } + + public double calculate(Employee employee) { + int salary = employee.getMonthlyIncome(); + if (salary > 3_00_000) { + return + (salary * incomeTaxPercentage) / 100d + + (salary * professionalTaxPercentage) / 100d + + (salary * educationCess) / 100d; + } + return 0d; + } +} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java b/src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java new file mode 100644 index 00000000..38317675 --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/PartTimeEmployeeTaxCalculator.java @@ -0,0 +1,24 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public class PartTimeEmployeeTaxCalculator implements TaxCalculatorInterface { + private final int incomeTaxPercentage; + private final int professionalTaxPercentage; + private final int educationCess; + + public PartTimeEmployeeTaxCalculator(int incomeTaxPercentage, int professionalTaxPercentage, int educationCess) { + this.incomeTaxPercentage = incomeTaxPercentage; + this.professionalTaxPercentage = professionalTaxPercentage; + this.educationCess = educationCess; + } + + public double calculate(Employee employee) { + int salary = employee.getMonthlyIncome(); + return + (salary * incomeTaxPercentage) / 100d + + (salary * professionalTaxPercentage) / 100d + + (salary * educationCess) / 100d ; + + } +} \ 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 deleted file mode 100644 index ab0300be..00000000 --- a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculator.java +++ /dev/null @@ -1,16 +0,0 @@ -package oops.SOLID.openClosePrinciple.before.taxes; - -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; - - } -} \ No newline at end of file diff --git a/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorInterface.java b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorInterface.java new file mode 100644 index 00000000..64b47f8c --- /dev/null +++ b/src/oops/SOLID/openClosePrinciple/before/taxes/TaxCalculatorInterface.java @@ -0,0 +1,7 @@ +package oops.SOLID.openClosePrinciple.before.taxes; + +import oops.SOLID.openClosePrinciple.before.employees.Employee; + +public interface TaxCalculatorInterface { + double calculate(Employee employee); +} From 062d73e78b7f16e447f91a43e6e9c3a128109ad4 Mon Sep 17 00:00:00 2001 From: achyuttibrewalla Date: Thu, 15 Apr 2021 01:46:41 -0400 Subject: [PATCH 3/4] Refactored for SRP --- src/oops/SOLID/.DS_Store | Bin 0 -> 6148 bytes .../singleResponsibilityPrinciple/.DS_Store | Bin 0 -> 6148 bytes .../before/ClientMain.java | 20 ++++++++++ .../before/Employee.java | 28 -------------- .../before/EmployeeRepository.java | 36 ++++++++++++++---- .../before/EmployeeSerializer.java | 27 +++++++++++++ .../before/SaveEmployeesMain.java | 11 ------ 7 files changed, 75 insertions(+), 47 deletions(-) create mode 100644 src/oops/SOLID/.DS_Store create mode 100644 src/oops/SOLID/singleResponsibilityPrinciple/.DS_Store create mode 100644 src/oops/SOLID/singleResponsibilityPrinciple/before/ClientMain.java create mode 100644 src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeSerializer.java diff --git a/src/oops/SOLID/.DS_Store b/src/oops/SOLID/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0a0732e48f716dd5028912742dc7b02312c770a3 GIT binary patch literal 6148 zcmeHK&2AGh5FR(7I6;UAA(aD__QI{8M1=zfq$Lf9+$t8q0Z_Z!U9~GMc9h*TDj{g^ zyZ}4}ufZGeIPi__fSQ1M07Vt@NaJs8&yV+uY{x_-`jfOr)FC1XWo)+5{6=`5bwxJZ z$R<#j9EwuPNYN3Ut$6zZ&wyv(vN6DW_YMuPYe#11{@TAo?Bi36jj5!ZGVb)r~#jnMoV5_mb`8vx2?Sge~pOjqDo?WDaW>1qoQM8 z6+MHZu30m$J%JX`3a8Qq-ylBzyUfY7965HrCZi%Rt8Vw3*l0AbY+jYJE!)@IZ=w@D zjjFhsl>K=8ihKKUb&@#yxz@!`J5FBY+4P`uYfq~x&Q&%x!Fig&<@L*4rFz=e6P1=G zPh=;gY{}L^XKOaQ*WC#oJ(%wVv);q*F6`ajeBP2pc4nv2vMa)2i4F%dz;hq@6hGRT%exbwKq75hE9v{M07Vd>2Om*ZB zj5~?YqHjF|o`LfWG;FuS_y5yBzyHq%`JQLMGw@$AAew_{(8njayLIK`_^u70hfo&w nt1X%kH10Y!24BUup++zlumKDm))p}W@qYvi4ZiUVtd)Tu_4lhH literal 0 HcmV?d00001 diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/.DS_Store b/src/oops/SOLID/singleResponsibilityPrinciple/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fadf4092b0fd2e21548b7b3eec25c75df9eeb4b4 GIT binary patch literal 6148 zcmeHK!A`?440W~v*$`p}BsliMZJdB3#Bu!t@>W-r@d~&@0`%nfuFVmYac2Vk8M9Vio*VRd1+TAsUIa_M<++E5JOIn zqok$!4b@9pT|JIxI;1Sh;(2*_Fj%WroUM&v#TnE#t97*Nwc)TRmse}s&5N7+hr#3U z=~>SL1nyTYixx-lg2u!g@4QYDtK=H}qnuGJBQwAZFav*;0ehaw`M){={%&S~8Td8^ z=zMTc30;esL49;!qe}q90=l)JO}~wl<5+YpW(KhbMVM4XlPYY9Axt{@9UJFb%nX`z z5VrUb_RYdpC_=v-$9Hr%2-hID%m6bm$pF=UC}q0;_rI?Hbr3hq05kAE84!g%Z?A!K zvv=#%Ea|RwP%ltPC@wQNPC-Lo#TZLh@d~OI^gCoAx)w8o=t1Eh0Zjup%)pN_@BtDr BUF`q> literal 0 HcmV?d00001 diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/ClientMain.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/ClientMain.java new file mode 100644 index 00000000..5d0cb331 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/ClientMain.java @@ -0,0 +1,20 @@ +package oops.SOLID.singleResponsibilityPrinciple.before; + +import java.util.List; + +public class ClientMain { + public static void main(String[] args) { + EmployeeRepository repository = new EmployeeRepository(); + // Add employees + repository.addEmployee(new FullTimeEmployee("Anna Smith", 2000)); + repository.addEmployee(new FullTimeEmployee("Billy Leech", 920)); + repository.addEmployee(new PartTimeEmployee("Steve Jones", 800)); + repository.addEmployee(new PartTimeEmployee("Magda Iovan", 920)); + + // Grab and save employees + List employees = repository.findAll(); + for (Employee e : employees){ + repository.save(e); + } + } +} diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java index a742fac0..22543006 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java @@ -63,32 +63,4 @@ 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); - } - } } diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java index b76f4589..91715092 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java @@ -1,19 +1,39 @@ package oops.SOLID.singleResponsibilityPrinciple.before; +import java.io.IOException; +import java.io.Serializable; +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 class EmployeeRepository implements Serializable { - public List findAll(){ + private final List employees; - // Employees are kept in memory for simplicity - Employee anna = new FullTimeEmployee("Anna Smith", 2000); - Employee billy = new FullTimeEmployee("Billy Leech", 920); + public EmployeeRepository() { + this.employees = new ArrayList<>(); + } + + public void save(Employee employee) { + try { + String serializedEmployeeObjectString = EmployeeSerializer.serialize(employee); + Path path = Paths.get(employee.getFullName() + .replace(" ","_") + ".rec"); + Files.write(path, serializedEmployeeObjectString.getBytes()); + System.out.println("Saved employee " + employee.toString()); + } catch (IOException e){ + System.out.println("ERROR: Could not save employee. " + e); + } + } - Employee steve = new PartTimeEmployee("Steve Jones", 800); - Employee magda = new PartTimeEmployee("Magda Iovan", 920); + public void addEmployee(Employee employee) { + this.employees.add(employee); + } - return Arrays.asList(anna, billy, steve, magda); + public List findAll() { + return employees; } } \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeSerializer.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeSerializer.java new file mode 100644 index 00000000..673f0451 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeSerializer.java @@ -0,0 +1,27 @@ +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.List; + +public class EmployeeSerializer { + public static String serialize(Employee employee) { + String sb = "### EMPLOYEE RECORD ####" + + System.lineSeparator() + + "NAME: " + + employee.getFullName() + + System.lineSeparator() + + "POSITION: " + + employee.getClass().getTypeName() + + System.lineSeparator() + + "EMAIL: " + + employee.getEmail() + + System.lineSeparator() + + "MONTHLY WAGE: " + + employee.getMonthlyIncome() + + System.lineSeparator(); + return sb.toString(); + } +} \ 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..fe763878 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java @@ -1,16 +1,5 @@ package oops.SOLID.singleResponsibilityPrinciple.before; -import java.util.List; - public class SaveEmployeesMain { - public static void main(String[] args) { - // Grab employees - EmployeeRepository repository = new EmployeeRepository(); - List employees = repository.findAll(); - // Save all - for (Employee e : employees){ - e.save(); - } - } } \ No newline at end of file From 4027ebe1f4b1f033c8235db7cf8ad0e12acd8d24 Mon Sep 17 00:00:00 2001 From: achyuttibrewalla Date: Thu, 15 Apr 2021 02:03:27 -0400 Subject: [PATCH 4/4] Minor fix --- .../SOLID/singleResponsibilityPrinciple/before/Employee.java | 5 +++++ .../before/EmployeeRepository.java | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java index 22543006..3c0d8570 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java @@ -9,6 +9,7 @@ Models an employee form a business perspective */ public abstract class Employee { + private static final long serialVersionUID = 1L; private String firstName; private String lastName; private int monthlyIncome; @@ -63,4 +64,8 @@ public String getFullName(){ return this.firstName + " " + this.lastName; } + public String serialize() { + return EmployeeSerializer.serialize(this); + } + } diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java index 91715092..d544c470 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/EmployeeRepository.java @@ -19,7 +19,7 @@ public EmployeeRepository() { public void save(Employee employee) { try { - String serializedEmployeeObjectString = EmployeeSerializer.serialize(employee); + String serializedEmployeeObjectString = employee.serialize(); Path path = Paths.get(employee.getFullName() .replace(" ","_") + ".rec"); Files.write(path, serializedEmployeeObjectString.getBytes());