diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java index a742fac0..329bb8b7 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java @@ -1,94 +1,55 @@ 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 - */ public abstract class Employee { - private String firstName; - private String lastName; - private int monthlyIncome; - private int nbHoursPerWeek; - - public Employee(String fullName, int monthlyIncome){ - setMonthlyIncome(monthlyIncome); - - String[] names = fullName.split(" "); - this.firstName = names[0]; - this.lastName = names[1]; - } + private String firstName; + private String lastName; + private int monthlyIncome; + private int nbHoursPerWeek; - public String getEmail() { - return this.firstName + "." + - this.lastName + - "@globomanticshr.com"; - } + public Employee(String fullName, int monthlyIncome) { + setMonthlyIncome(monthlyIncome); - @Override - public String toString() { - return this.firstName + " " + - this.lastName + " - " + - this.monthlyIncome; - } + String[] names = fullName.split(" "); + this.firstName = names[0]; + this.lastName = names[1]; + } - public int getMonthlyIncome() { - return monthlyIncome; - } + public String getEmail() { + return this.firstName + "." + this.lastName + "@globomanticshr.com"; + } - public void setMonthlyIncome(int monthlyIncome) { - if(monthlyIncome < 0){ - throw new IllegalArgumentException("Income must be positive"); - } + @Override + public String toString() { + return this.firstName + " " + this.lastName + " - " + this.monthlyIncome; + } - this.monthlyIncome = monthlyIncome; - } + public int getMonthlyIncome() { + return monthlyIncome; + } - public int getNbHoursPerWeek() { - return nbHoursPerWeek; - } + public void setMonthlyIncome(int monthlyIncome) { + if (monthlyIncome < 0) { + throw new IllegalArgumentException("Income must be positive"); + } - public void setNbHoursPerWeek(int nbHoursPerWeek) { - if(nbHoursPerWeek <= 0){ - throw new IllegalArgumentException("Income must be positive"); - } + this.monthlyIncome = monthlyIncome; + } - this.nbHoursPerWeek = nbHoursPerWeek; - } + public int getNbHoursPerWeek() { + return nbHoursPerWeek; + } - public String getFullName(){ - return this.firstName + " " + this.lastName; - } + public void setNbHoursPerWeek(int nbHoursPerWeek) { + if (nbHoursPerWeek <= 0) { + throw new IllegalArgumentException("Income must be positive"); + } - 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()); + this.nbHoursPerWeek = nbHoursPerWeek; + } - Path path = Paths.get(employee.getFullName() - .replace(" ","_") + ".rec"); - Files.write(path, sb.toString().getBytes()); + public String getFullName() { + return this.firstName + " " + this.lastName; + } + - 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/PersistData.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/PersistData.java new file mode 100644 index 00000000..6a574827 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/PersistData.java @@ -0,0 +1,6 @@ +package oops.SOLID.singleResponsibilityPrinciple.before; + +public interface PersistData { + + void save(); +} diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/PersistEmployeeData.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/PersistEmployeeData.java new file mode 100644 index 00000000..a3da0901 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/PersistEmployeeData.java @@ -0,0 +1,49 @@ +package oops.SOLID.singleResponsibilityPrinciple.before; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class PersistEmployeeData implements PersistData { + + Employee employee; + + public PersistEmployeeData(Employee employee) { + this.employee = employee; + } + + @Override + public void save() { + try { + + Path path = Paths.get(this.employee.getFullName().replace(" ", "_") + ".rec"); + String employeeData = extractEmployeeDataToSave(); + Files.write(path, employeeData.getBytes()); + System.out.println("Saved employee " + this.employee.toString()); + + } catch (IOException e) { + System.out.println("ERROR: Could not save employee. " + e); + } + } + + public String extractEmployeeDataToSave() { + StringBuilder sb = new StringBuilder(); + sb.append("### EMPLOYEE RECORD ####"); + sb.append(System.lineSeparator()); + sb.append("NAME: "); + sb.append(employee.getFullName()); + 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.getMonthlyIncome()); + sb.append(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..816deeb6 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/SaveEmployeesMain.java @@ -4,13 +4,14 @@ public class SaveEmployeesMain { public static void main(String[] args) { - // Grab employees + EmployeeRepository repository = new EmployeeRepository(); List employees = repository.findAll(); - // Save all + PersistData persist; for (Employee e : employees){ - e.save(); + persist = new PersistEmployeeData(e); + persist.save(); } } } \ No newline at end of file