Skip to content
Open

srp #123

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package oops.SOLID.singleResponsibilityPrinciple.before;

import java.nio.file.Path;

public Inteface DatabaseContract{
public static void save(Path path,StringBuilder sb);
}
59 changes: 33 additions & 26 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import EmployeeRepository

/*
Models an employee form a business perspective
Expand Down Expand Up @@ -63,32 +64,38 @@ public String getFullName(){
return this.firstName + " " + this.lastName;
}

public string getEmployeeStringRepresentation(){
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
}

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);
}
StringBuilder sb = getEmployeeStringRepresentation()
Path path = getPath()
EmployeeRepository repository = new EmployeeRepository();
repository.save(path,sb)

}

public Path getPath(){
Employee employee =this;
Path path = Paths.get(employee.getFullName()
.replace(" ","_") + ".rec");
return path
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.List;
import java.nio.file.Path;

public class EmployeeRepository {

Expand All @@ -16,4 +17,8 @@ public List<Employee> findAll(){

return Arrays.asList(anna, billy, steve, magda);
}

public void saveEmployee(Path path,StringBuilder sb){
filedb.save(path, sb.toString().getBytes())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package oops.SOLID.singleResponsibilityPrinciple.before;
import java.nio.file.Path;

public class FileDataBase implements DatabaseContract{
Employee employee =this;
public static void save(Path path,StringBuilder sb){
try {
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);
}
}

}