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
5 changes: 5 additions & 0 deletions Anna_Smith.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Anna Smith
POSITION: oops.SOLID.singleResponsibilityPrinciple.before.FullTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 2000
5 changes: 5 additions & 0 deletions Billy_Leech.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Billy Leech
POSITION: oops.SOLID.singleResponsibilityPrinciple.before.FullTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 920
5 changes: 5 additions & 0 deletions Magda_Iovan.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Magda Iovan
POSITION: oops.SOLID.singleResponsibilityPrinciple.before.PartTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 920
5 changes: 5 additions & 0 deletions Steve_Jones.rec
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### EMPLOYEE RECORD ####
NAME: Steve Jones
POSITION: oops.SOLID.singleResponsibilityPrinciple.before.PartTimeEmployee
EMAIL: [email protected]
MONTHLY WAGE: 800
1 change: 1 addition & 0 deletions bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/oops/
51 changes: 28 additions & 23 deletions src/oops/SOLID/lsp/stack/before/StackWrong.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
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<Integer>{
private int topPointer = 0;

import java.io.IOException;

public class StackWrong {
ArrayList<Integer> st = new ArrayList<Integer>();
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);
st.push(2);
System.out.println(st.top());
st.pop();
System.out.println(st.top());
st.clear();
st.pop();
System.out.println(st.top());
st.pop();
}
}
160 changes: 75 additions & 85 deletions src/oops/SOLID/singleResponsibilityPrinciple/before/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}