diff --git a/Anna_Smith.rec b/Anna_Smith.rec new file mode 100644 index 00000000..65436c72 --- /dev/null +++ b/Anna_Smith.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Anna Smith +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.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..2ec8176f --- /dev/null +++ b/Billy_Leech.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Billy Leech +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.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..8f673ff6 --- /dev/null +++ b/Magda_Iovan.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Magda Iovan +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.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..1bf39ad7 --- /dev/null +++ b/Steve_Jones.rec @@ -0,0 +1,5 @@ +### EMPLOYEE RECORD #### +NAME: Steve Jones +POSITION: oops.SOLID.singleResponsibilityPrinciple.before.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/lsp/stack/before/StackWrong.java b/src/oops/SOLID/lsp/stack/before/StackWrong.java index 1170e573..274be2f2 100644 --- a/src/oops/SOLID/lsp/stack/before/StackWrong.java +++ b/src/oops/SOLID/lsp/stack/before/StackWrong.java @@ -1,34 +1,38 @@ 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; - +import java.io.IOException; + +public class StackWrong { + ArrayList st = new ArrayList(); + private int topPointer = -1; + public void push(Integer a) { - add(topPointer, a); + st.add(a); topPointer++; - } + } public void pop() { - remove(topPointer-1); - topPointer--; + if( topPointer >= 0 ){ + st.remove(topPointer); + topPointer--; + return ; + } + else{ + System.out.println("stack is empty, cannot pop"); + return; + } } public Integer top() { - return get(topPointer-1); + if( topPointer >= 0 ) + { + return st.get(topPointer); + } + else + { + System.out.println("cannot return top because stack is empty"); + return -1; + } } - public static void main(String[] args) { StackWrong st = new StackWrong(); st.push(1); @@ -36,7 +40,8 @@ public static void main(String[] args) { System.out.println(st.top()); st.pop(); System.out.println(st.top()); - st.clear(); + st.pop(); System.out.println(st.top()); + st.pop(); } } diff --git a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java index a742fac0..2ceb6e1e 100644 --- a/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java @@ -5,90 +5,80 @@ 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 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); - } - } + 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 String getFirstName() + { + return this.firstName; + } + + public String getLastName() + { + return this.lastName; + } + public void save(){ + try { + Employee employee =this; + Path path = Paths.get(employee.getFullName() + .replace(" ","_") + ".rec"); + String serializedEmployee = serializeEmployee.getSerializedEmployee(this); + Files.write(path, serializedEmployee.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/serializeEmployee.java b/src/oops/SOLID/singleResponsibilityPrinciple/before/serializeEmployee.java new file mode 100644 index 00000000..d0d703ca --- /dev/null +++ b/src/oops/SOLID/singleResponsibilityPrinciple/before/serializeEmployee.java @@ -0,0 +1,26 @@ +package oops.SOLID.singleResponsibilityPrinciple.before; + +public class serializeEmployee { + + public static String getSerializedEmployee(Employee e) + { + Employee employee = e; + StringBuilder sb = new StringBuilder(); + sb.append("### EMPLOYEE RECORD ####"); + sb.append(System.lineSeparator()); + sb.append("NAME: "); + sb.append(employee.getFirstName() + " " + employee.getLastName()); + 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(); + } +}