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
117 changes: 39 additions & 78 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package oops.SOLID.singleResponsibilityPrinciple.before;

public interface PersistData {

void save();
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

public class SaveEmployeesMain {
public static void main(String[] args) {
// Grab employees
EmployeeRepository repository = new EmployeeRepository();
List<Employee> employees = repository.findAll();

// Save all
PersistData persist;
for (Employee e : employees){
e.save();
persist = new PersistEmployeeData(e);
persist.save();
}
}
}