initial code for implementation code

This commit is contained in:
amdegregorio 2018-11-16 14:45:51 -05:00
parent 60697a321c
commit 9a6688f638
12 changed files with 330 additions and 0 deletions

1
hexagonal-architecture/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/bin/

View File

@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.hexagonal</groupId>
<artifactId>hexagonal-architecture</artifactId>
<packaging>jar</packaging>
<name>hexagonal-architecture</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
</project>

View File

@ -0,0 +1,27 @@
package com.baeldung.hexagonal;
import java.io.File;
import com.baeldung.hexagonal.domain.EmployeeService;
import com.baeldung.hexagonal.output.EmployeeCsvWriter;
import com.baeldung.hexagonal.output.EmployeeLogger;
import com.baeldung.hexagonal.output.EmployeeOutput;
import com.baeldung.hexagonal.storage.EmployeeRepository;
import com.baeldung.hexagonal.storage.InMemoryEmployeeRepository;
import com.baeldung.hexagonal.ui.EmployeeConsoleInputImpl;
import com.baeldung.hexagonal.ui.EmployeeInput;
public class Application {
public static void main(String[] args) {
EmployeeRepository repository = new InMemoryEmployeeRepository();
EmployeeOutput output = new EmployeeLogger();
EmployeeOutput csvOutput = new EmployeeCsvWriter(new File(".").getAbsolutePath(), "output.csv");
EmployeeService service = new EmployeeService(repository, csvOutput);
EmployeeInput ui = new EmployeeConsoleInputImpl();
ui.collectData(service);
service.generateOutput();
}
}

View File

@ -0,0 +1,70 @@
package com.baeldung.hexagonal.domain;
import java.math.BigDecimal;
public class Employee {
private Long id;
private String firstName;
private String lastName;
private String employeeId;
private BigDecimal salary;
public Employee(
Long id,
String firstName,
String lastName,
String employeeId,
BigDecimal salary) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.employeeId = employeeId;
this.salary = salary;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public BigDecimal getSalary() {
return salary;
}
public void setSalary(BigDecimal salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", employeeId=" + employeeId + ", salary=" + salary + "]";
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.hexagonal.domain;
import java.util.List;
import com.baeldung.hexagonal.output.EmployeeOutput;
import com.baeldung.hexagonal.storage.EmployeeRepository;
public class EmployeeService {
private EmployeeRepository employeeRepository;
private EmployeeOutput employeeOutput;
public EmployeeService(EmployeeRepository employeeRepository, EmployeeOutput employeeOutput) {
this.employeeRepository = employeeRepository;
this.employeeOutput = employeeOutput;
}
public Long add(Employee employee) {
return employeeRepository.save(employee);
}
public void generateOutput() {
List<Employee> employees = employeeRepository.findAll();
employeeOutput.writeAll(employees);
}
}

View File

@ -0,0 +1,64 @@
package com.baeldung.hexagonal.output;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import com.baeldung.hexagonal.domain.Employee;
public class EmployeeCsvWriter implements EmployeeOutput {
private File outputFile;
public EmployeeCsvWriter(String path, String fileName) throws IllegalArgumentException {
if (fileName == null || path == null || fileName.length() == 0 || path.length() == 0) {
throw new IllegalArgumentException("Path and FileName are required");
} else if (!fileName.endsWith(".csv")) {
throw new IllegalArgumentException("File name must be a .csv file");
}
System.out.println(path);
if (!path.endsWith("/")) path += "/";
outputFile = new File(path, fileName);
}
@Override
public void writeAll(List<Employee> employees) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(outputFile));
for (Iterator<Employee> it = employees.iterator(); it.hasNext();) {
Employee emp = it.next();
StringBuffer empLine = new StringBuffer();
empLine.append(emp.getId());
empLine.append(",");
empLine.append(emp.getFirstName());
empLine.append(",");
empLine.append(emp.getLastName());
empLine.append(",");
empLine.append(emp.getEmployeeId());
empLine.append(",");
empLine.append(emp.getSalary());
writer.write(empLine.toString());
writer.newLine();
}
writer.flush();
} catch (IOException ioe) {
//handle the exception
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
//handle the exception
}
}
}
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.hexagonal.output;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.hexagonal.domain.Employee;
public class EmployeeLogger implements EmployeeOutput {
private static final Logger LOG = LoggerFactory.getLogger(EmployeeLogger.class);
@Override
public void writeAll(List<Employee> employees) {
employees.forEach(employee -> LOG.info(employee.toString()));
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.hexagonal.output;
import java.util.List;
import com.baeldung.hexagonal.domain.Employee;
public interface EmployeeOutput {
public void writeAll(List<Employee> employees);
}

View File

@ -0,0 +1,11 @@
package com.baeldung.hexagonal.storage;
import java.util.List;
import com.baeldung.hexagonal.domain.Employee;
public interface EmployeeRepository {
public Long save(Employee employee);
public Employee findById(Long id);
public List<Employee> findAll();
}

View File

@ -0,0 +1,31 @@
package com.baeldung.hexagonal.storage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baeldung.hexagonal.domain.Employee;
public class InMemoryEmployeeRepository implements EmployeeRepository {
private static Map<Long, Employee> employees = new HashMap<>();
@Override
public Long save(Employee employee) {
Long id = employee.getId();
employees.put(id, employee);
return id;
}
@Override
public Employee findById(Long id) {
return employees.get(id);
}
@Override
public List<Employee> findAll() {
List<Employee> all = new ArrayList<Employee>(employees.values());
return all;
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.hexagonal.ui;
import java.math.BigDecimal;
import java.util.Scanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.hexagonal.domain.Employee;
import com.baeldung.hexagonal.domain.EmployeeService;
public class EmployeeConsoleInputImpl implements EmployeeInput {
private static final Logger LOG = LoggerFactory.getLogger(EmployeeConsoleInputImpl.class);
public void enterEmployee(EmployeeService service, Scanner scanner) {
LOG.info("ID: ");
System.out.print("> ");
Long id = scanner.nextLong();
LOG.info("First Name: ");
System.out.print("> ");
String firstName = scanner.next();
LOG.info("Last Name: ");
System.out.print("> ");
String lastName = scanner.next();
LOG.info("Employee ID: ");
System.out.print("> ");
String employeeId = scanner.next();
LOG.info("Salary: " );
System.out.print("> ");
BigDecimal salary = scanner.nextBigDecimal();
Employee employee = new Employee(id, firstName, lastName, employeeId, salary);
service.add(employee);
}
@Override
public void collectData(EmployeeService service) {
try (final Scanner scanner = new Scanner(System.in)) {
String keepGoing = "Y";
do {
LOG.info("Enter information for an employee");
enterEmployee(service, scanner);
LOG.info("Do you want to enter another employee? (Y/N)");
System.out.print("> ");
keepGoing = scanner.next();
if (keepGoing.length() > 1) keepGoing = keepGoing.substring(0, 1);
} while (keepGoing.equalsIgnoreCase("Y"));
}
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.hexagonal.ui;
import com.baeldung.hexagonal.domain.EmployeeService;
public interface EmployeeInput {
void collectData(EmployeeService service);
}