diff --git a/Anna_Smith.rec b/Anna_Smith.rec new file mode 100644 index 00000000..4b4624ae --- /dev/null +++ b/Anna_Smith.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Anna Smith +POSITION: oops.SOLID.singleResponsibilityPrinciple.after.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..37596533 --- /dev/null +++ b/Billy_Leech.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Billy Leech +POSITION: oops.SOLID.singleResponsibilityPrinciple.after.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..bc0ff54c --- /dev/null +++ b/Magda_Iovan.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Magda Iovan +POSITION: oops.SOLID.singleResponsibilityPrinciple.after.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..4dece543 --- /dev/null +++ b/Steve_Jones.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Steve Jones +POSITION: oops.SOLID.singleResponsibilityPrinciple.after.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/singleResponsibilityPrinciple/after/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/after/Employee.java new file mode 100644 index 00000000..0cc32596 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/after/Employee.java @@ -0,0 +1,99 @@ +package oops.SOLID.singleResponsibilityPrinciple.after; + +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]; + } + + public String getEmail() { + return this.firstName + "." + this.lastName + "@globomanticshr.com"; + } + + @Override + public String toString() { + return this.firstName + " " + this.lastName + " - " + this.monthlyIncome; + } + + public int getMonthlyIncome() { + return monthlyIncome; + } + + public void setMonthlyIncome(int monthlyIncome) { + if (monthlyIncome < 0) { + throw new IllegalArgumentException("Income must be positive"); + } + + this.monthlyIncome = monthlyIncome; + } + + public int getNbHoursPerWeek() { + return nbHoursPerWeek; + } + + public void setNbHoursPerWeek(int nbHoursPerWeek) { + if (nbHoursPerWeek <= 0) { + throw new IllegalArgumentException("Income must be positive"); + } + + this.nbHoursPerWeek = nbHoursPerWeek; + } + + public String getFullName() { + return this.firstName + " " + this.lastName; + } + + public byte[] getData() { + 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().getBytes(); + } + + public String getFileName() { + Employee employee = this; + String fileName = employee.getFullName().replace(" ", "_") + ".rec"; + return fileName; + + } + + public void save() { + try { + Employee employee = this; + ReadWriteToFile.readWriteToFile(getFileName(), getData()); + + 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/after/EmployeeRepository.java b/src/oops/SOLID/singleResponsibilityPrinciple/after/EmployeeRepository.java new file mode 100644 index 00000000..b9e73600 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/after/EmployeeRepository.java @@ -0,0 +1,19 @@ +package oops.SOLID.singleResponsibilityPrinciple.after; + +import java.util.Arrays; +import java.util.List; + +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); + + Employee steve = new PartTimeEmployee("Steve Jones", 800); + Employee magda = new PartTimeEmployee("Magda Iovan", 920); + + return Arrays.asList(anna, billy, steve, magda); + } +} \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/after/FullTimeEmployee.java b/src/oops/SOLID/singleResponsibilityPrinciple/after/FullTimeEmployee.java new file mode 100644 index 00000000..48f2b614 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/after/FullTimeEmployee.java @@ -0,0 +1,8 @@ +package oops.SOLID.singleResponsibilityPrinciple.after; + +public class FullTimeEmployee extends Employee { + public FullTimeEmployee(String fullName, int monthlyIncome) { + super(fullName, monthlyIncome); + this.setNbHoursPerWeek(40); + } +} \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/after/PartTimeEmployee.java b/src/oops/SOLID/singleResponsibilityPrinciple/after/PartTimeEmployee.java new file mode 100644 index 00000000..bf19e167 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/after/PartTimeEmployee.java @@ -0,0 +1,8 @@ +package oops.SOLID.singleResponsibilityPrinciple.after; + +public class PartTimeEmployee extends Employee { + public PartTimeEmployee(String fullName, int monthlyIncome) { + super(fullName, monthlyIncome); + this.setNbHoursPerWeek(20); + } +} \ No newline at end of file diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/after/ReadWriteToFile.java b/src/oops/SOLID/singleResponsibilityPrinciple/after/ReadWriteToFile.java new file mode 100644 index 00000000..cc55fa26 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/after/ReadWriteToFile.java @@ -0,0 +1,17 @@ +package oops.SOLID.singleResponsibilityPrinciple.after; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class ReadWriteToFile { + public static void readWriteToFile( String fileName , byte[] data) throws IOException { + Path path = Paths.get(fileName); + // System.out.println(path); + Files.write(path, data); + + } + + +} diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/after/SaveEmployeesMain.java b/src/oops/SOLID/singleResponsibilityPrinciple/after/SaveEmployeesMain.java new file mode 100644 index 00000000..6bf2dd66 --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/after/SaveEmployeesMain.java @@ -0,0 +1,16 @@ +package oops.SOLID.singleResponsibilityPrinciple.after; + +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