Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
deepcloudlabs committed Jun 22, 2020
1 parent bef2a5b commit cc4cc2e
Show file tree
Hide file tree
Showing 30 changed files with 1,435 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/.metadata/
10 changes: 10 additions & 0 deletions hr-domain/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
</classpath>
17 changes: 17 additions & 0 deletions hr-domain/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>hr-domain</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
15 changes: 15 additions & 0 deletions hr-domain/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=14
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=14
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=enabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=14
31 changes: 31 additions & 0 deletions hr-domain/src/com/example/hr/application/EmployeeApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.hr.application;

import com.example.hr.domain.Employee;
import com.example.hr.events.EmployeeFiredEvent;
import com.example.hr.events.EmployeeHiredEvent;
import com.example.hr.infrastructure.EventPushlisher;
import com.example.hr.repository.EmployeeRepository;

public class EmployeeApplication {
private EmployeeRepository employeeRepository;
private EventPushlisher eventPushlisher;

public void setEmployeeRepository(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}

public void setEventPushlisher(EventPushlisher eventPushlisher) {
this.eventPushlisher = eventPushlisher;
}

public void hireEmployee(Employee employee) {
employeeRepository.save(employee);
eventPushlisher.publishEvent(new EmployeeHiredEvent("", "employees", employee));
}

public void fireEmployee(Employee employee) {
employeeRepository.remove(employee);
eventPushlisher.publishEvent(new EmployeeFiredEvent("", "employees", employee));
}

}
48 changes: 48 additions & 0 deletions hr-domain/src/com/example/hr/domain/BirthYear.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.example.hr.domain;

// Value Object, Immutable
public final class BirthYear {
private final int value;

private BirthYear(int value) {
this.value = value;
}

public static BirthYear of(int value) {
if (value > 2020)
throw new IllegalArgumentException("Birt Year cannot be from future");
return new BirthYear(value);
}

public int getValue() {
return value;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + value;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BirthYear other = (BirthYear) obj;
if (value != other.value)
return false;
return true;
}

@Override
public String toString() {
return "BirthYear [value=" + value + "]";
}

}
5 changes: 5 additions & 0 deletions hr-domain/src/com/example/hr/domain/Department.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.hr.domain;

public enum Department {
IT, SALES, HR, FINANCE
}
180 changes: 180 additions & 0 deletions hr-domain/src/com/example/hr/domain/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.example.hr.domain;

// DDD : Core Domain -> Entity Root -> Aggregate
// Ubiquitous Language: Entity, Entity Root/Aggregate/Value Object
// Bounded Context <--> Sub Domain
// Shortcuts (Eclipse) IntelliJ IDEA
// Ctrl + Shift + + (Zoom In) (Ctrl + Mouse Wheel)
// Ctrl + - (Zoom Out) (Ctrl + Mouse Wheel)
// Ctrl + 1 (Suggestion to remove errors) (Alt + Enter)
// Alt + Shift + S (Generate Source Code) (Alt + Insert)
// Ctrl + Shift + F (Format Source Code) (Ctrl + Alt + L)
public class Employee {
private static final Money MIN_SALARY = Money.of(3_000, MoneyCurrency.TL);
private final TcKimlikNo identityNo;
private FullName fullname;
private Money salary;
private Iban iban;
private final BirthYear birthYear;
private Photo photo;
private boolean fulltime;
private Department department;

public Employee(TcKimlikNo identityNo, FullName fullname, Money salary, Iban iban, BirthYear birthYear, Photo photo,
boolean fulltime, Department department) {
this.identityNo = identityNo;
this.fullname = fullname;
this.salary = salary;
this.iban = iban;
this.birthYear = birthYear;
this.photo = photo;
this.fulltime = fulltime;
this.department = department;
}

public Employee(Builder builder) {
this.identityNo = builder.identityNo;
this.fullname = builder.fullname;
this.salary = builder.salary;
this.iban = builder.iban;
this.birthYear = builder.birthYear;
this.photo = builder.photo;
this.fulltime = builder.fulltime;
this.department = builder.department;
}

public FullName getFullname() {
return fullname;
}

public void setFullname(FullName fullname) {
this.fullname = fullname;
}

public Iban getIban() {
return iban;
}

public void setIban(Iban iban) {
this.iban = iban;
}

public Photo getPhoto() {
return photo;
}

public void setPhoto(Photo photo) {
this.photo = photo;
}

public boolean isFulltime() {
return fulltime;
}

public void setFulltime(boolean fulltime) {
this.fulltime = fulltime;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

public TcKimlikNo getIdentityNo() {
return identityNo;
}

public Money getSalary() {
return salary;
}

public BirthYear getBirthYear() {
return birthYear;
}

public void increaseSalary(double percentage) {
if (percentage <= 0.)
throw new IllegalArgumentException("Percentage cannot be negative or zero");
this.salary = this.salary.multiply(1. + percentage);
}

public void decreaseSalary(double percentage) {
if (percentage <= 0.)
throw new IllegalArgumentException("Percentage cannot be negative or zero");
this.salary = this.salary.multiply(1. - percentage);
if (this.salary.lessThan(MIN_SALARY))
this.salary = MIN_SALARY;
}

public void hireToFulltime() {
if (this.fulltime)
throw new IllegalArgumentException("Already full-time");
this.fulltime = true;
this.increaseSalary(50.);
}

public void hireToParttime() {
if (!this.fulltime)
throw new IllegalArgumentException("Already part-time");
this.fulltime = false;
this.decreaseSalary(50.);
}

public static class Builder {
private final TcKimlikNo identityNo;
private FullName fullname;
private Money salary;
private Iban iban;
private BirthYear birthYear;
private Photo photo;
private boolean fulltime;
private Department department;

public Builder(String identityNo) {
this.identityNo = TcKimlikNo.of(identityNo);
}

public Builder fullname(String firstName, String lastName) {
this.fullname = new FullName(firstName, lastName);
return this;
}

public Builder salary(double value, MoneyCurrency currency) {
this.salary = Money.of(value, currency);
return this;
}

public Builder iban(String value) {
this.iban = Iban.of(value);
return this;
}

public Builder birthYear(int value) {
this.birthYear = BirthYear.of(value);
return this;
}

public Builder photo(byte[] value) {
this.photo = Photo.of(value);
return this;
}

public Builder fulltime(boolean fulltime) {
this.fulltime = fulltime;
return this;
}

public Builder department(Department department) {
this.department = department;
return this;
}

public Employee build() {
return new Employee(this);
}

}
}
57 changes: 57 additions & 0 deletions hr-domain/src/com/example/hr/domain/FullName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.hr.domain;

// Value Object, Immutable
public final class FullName {
private final String first;
private final String last;

public FullName(String first, String last) {
this.first = first;
this.last = last;
}

public String getFirst() {
return first;
}

public String getLast() {
return last;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((first == null) ? 0 : first.hashCode());
result = prime * result + ((last == null) ? 0 : last.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
FullName other = (FullName) obj;
if (first == null) {
if (other.first != null)
return false;
} else if (!first.equals(other.first))
return false;
if (last == null) {
if (other.last != null)
return false;
} else if (!last.equals(other.last))
return false;
return true;
}

@Override
public String toString() {
return "FullName [first=" + first + ", last=" + last + "]";
}

}
Loading

0 comments on commit cc4cc2e

Please sign in to comment.