From 9a6688f638db1d94de0447ea15e5215f4e654330 Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Fri, 16 Nov 2018 14:45:51 -0500 Subject: [PATCH 1/6] initial code for implementation code --- hexagonal-architecture/.gitignore | 1 + hexagonal-architecture/pom.xml | 14 ++++ .../com/baeldung/hexagonal/Application.java | 27 +++++++ .../baeldung/hexagonal/domain/Employee.java | 70 +++++++++++++++++++ .../hexagonal/domain/EmployeeService.java | 26 +++++++ .../hexagonal/output/EmployeeCsvWriter.java | 64 +++++++++++++++++ .../hexagonal/output/EmployeeLogger.java | 17 +++++ .../hexagonal/output/EmployeeOutput.java | 9 +++ .../hexagonal/storage/EmployeeRepository.java | 11 +++ .../storage/InMemoryEmployeeRepository.java | 31 ++++++++ .../ui/EmployeeConsoleInputImpl.java | 53 ++++++++++++++ .../baeldung/hexagonal/ui/EmployeeInput.java | 7 ++ 12 files changed, 330 insertions(+) create mode 100644 hexagonal-architecture/.gitignore create mode 100644 hexagonal-architecture/pom.xml create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java diff --git a/hexagonal-architecture/.gitignore b/hexagonal-architecture/.gitignore new file mode 100644 index 0000000000..ae3c172604 --- /dev/null +++ b/hexagonal-architecture/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml new file mode 100644 index 0000000000..c8eceae21a --- /dev/null +++ b/hexagonal-architecture/pom.xml @@ -0,0 +1,14 @@ + + 4.0.0 + com.baeldung.hexagonal + hexagonal-architecture + jar + hexagonal-architecture + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../parent-java + + \ No newline at end of file diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java new file mode 100644 index 0000000000..4691d498f4 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java @@ -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(); + + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java new file mode 100644 index 0000000000..91b22d0d11 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java @@ -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 + "]"; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java new file mode 100644 index 0000000000..12b2028035 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java @@ -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 employees = employeeRepository.findAll(); + employeeOutput.writeAll(employees); + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java new file mode 100644 index 0000000000..c434d19525 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java @@ -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 employees) { + BufferedWriter writer = null; + + try { + writer = new BufferedWriter(new FileWriter(outputFile)); + for (Iterator 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 + } + } + } + + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java new file mode 100644 index 0000000000..681e631012 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java @@ -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 employees) { + employees.forEach(employee -> LOG.info(employee.toString())); + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java new file mode 100644 index 0000000000..979c06e1f4 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java @@ -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 employees); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java new file mode 100644 index 0000000000..f12584f8e1 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java @@ -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 findAll(); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java new file mode 100644 index 0000000000..e860c16462 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java @@ -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 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 findAll() { + List all = new ArrayList(employees.values()); + return all; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java new file mode 100644 index 0000000000..5f613f7bf9 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java @@ -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")); + } + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java new file mode 100644 index 0000000000..62156fc6d9 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java @@ -0,0 +1,7 @@ +package com.baeldung.hexagonal.ui; + +import com.baeldung.hexagonal.domain.EmployeeService; + +public interface EmployeeInput { + void collectData(EmployeeService service); +} From 35db28ccfb884d8f277c6bdb3adc4e5fa5f0bb1c Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Fri, 16 Nov 2018 16:40:46 -0500 Subject: [PATCH 2/6] formatting changes --- .../com/baeldung/hexagonal/Application.java | 1 - .../baeldung/hexagonal/domain/Employee.java | 2 +- .../hexagonal/domain/EmployeeService.java | 33 +++++++------- .../hexagonal/output/EmployeeCsvWriter.java | 16 ++++--- .../hexagonal/output/EmployeeLogger.java | 2 +- .../hexagonal/storage/EmployeeRepository.java | 8 ++-- .../ui/EmployeeConsoleInputImpl.java | 43 ++++++++++--------- .../baeldung/hexagonal/ui/EmployeeInput.java | 2 +- 8 files changed, 55 insertions(+), 52 deletions(-) diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java index 4691d498f4..237b664708 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java @@ -21,7 +21,6 @@ public class Application { EmployeeInput ui = new EmployeeConsoleInputImpl(); ui.collectData(service); service.generateOutput(); - } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java index 91b22d0d11..b785da1494 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java @@ -8,7 +8,7 @@ public class Employee { private String lastName; private String employeeId; private BigDecimal salary; - + public Employee( Long id, String firstName, diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java index 12b2028035..2ab3ce9712 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java @@ -1,26 +1,25 @@ 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 employees = employeeRepository.findAll(); - employeeOutput.writeAll(employees); - } + 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 employees = employeeRepository.findAll(); + employeeOutput.writeAll(employees); + } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java index c434d19525..6d364dd11b 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java @@ -11,24 +11,26 @@ 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 += "/"; - + if (!path.endsWith("/")) { + path += "/"; + } + outputFile = new File(path, fileName); } @Override public void writeAll(List employees) { BufferedWriter writer = null; - + try { writer = new BufferedWriter(new FileWriter(outputFile)); for (Iterator it = employees.iterator(); it.hasNext();) { @@ -48,13 +50,13 @@ public class EmployeeCsvWriter implements EmployeeOutput { } writer.flush(); } catch (IOException ioe) { - //handle the exception + // handle the exception } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { - //handle the exception + // handle the exception } } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java index 681e631012..5272d18961 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java @@ -11,7 +11,7 @@ public class EmployeeLogger implements EmployeeOutput { @Override public void writeAll(List employees) { - employees.forEach(employee -> LOG.info(employee.toString())); + employees.forEach(employee -> LOG.info(employee.toString())); } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java index f12584f8e1..2787601998 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java @@ -5,7 +5,9 @@ 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 findAll(); + public Long save(Employee employee); + + public Employee findById(Long id); + + public List findAll(); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java index 5f613f7bf9..4960dfd254 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java @@ -13,26 +13,26 @@ 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); - + + 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 @@ -45,7 +45,8 @@ public class EmployeeConsoleInputImpl implements EmployeeInput { 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); + if (keepGoing.length() > 1) + keepGoing = keepGoing.substring(0, 1); } while (keepGoing.equalsIgnoreCase("Y")); } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java index 62156fc6d9..adec2852cd 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java @@ -3,5 +3,5 @@ package com.baeldung.hexagonal.ui; import com.baeldung.hexagonal.domain.EmployeeService; public interface EmployeeInput { - void collectData(EmployeeService service); + void collectData(EmployeeService service); } From f1c3029c59c71eabfcd302956b4f93069d3cea02 Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Sun, 18 Nov 2018 09:57:39 -0500 Subject: [PATCH 3/6] trim down Exmployee fields --- .../baeldung/hexagonal/domain/Employee.java | 80 ++++++++----------- .../hexagonal/output/EmployeeCsvWriter.java | 9 +-- .../ui/EmployeeConsoleInputImpl.java | 21 ++--- 3 files changed, 41 insertions(+), 69 deletions(-) diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java index b785da1494..0d883995b6 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java @@ -1,25 +1,12 @@ 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; + private String name; - public Employee( - Long id, - String firstName, - String lastName, - String employeeId, - BigDecimal salary) { + public Employee(Long id, String name) { this.id = id; - this.firstName = firstName; - this.lastName = lastName; - this.employeeId = employeeId; - this.salary = salary; + this.name = name; } public Long getId() { @@ -30,41 +17,44 @@ public class Employee { this.id = id; } - public String getFirstName() { - return firstName; + public String getName() { + return name; } - 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; + public void setName(String name) { + this.name = name; } @Override public String toString() { - return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", employeeId=" + employeeId + ", salary=" + salary + "]"; + return "Employee [id=" + id + ", name=" + name + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Employee other = (Employee) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + + return true; } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java index 6d364dd11b..799fbc28ba 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java @@ -38,14 +38,7 @@ public class EmployeeCsvWriter implements EmployeeOutput { 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()); + empLine.append(emp.getName()); writer.newLine(); } writer.flush(); diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java index 4960dfd254..77453da48f 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java @@ -1,6 +1,5 @@ package com.baeldung.hexagonal.ui; -import java.math.BigDecimal; import java.util.Scanner; import org.slf4j.Logger; @@ -13,26 +12,15 @@ 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: "); + LOG.info("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(); + String name = scanner.next(); - Employee employee = new Employee(id, firstName, lastName, employeeId, salary); + Employee employee = new Employee(id, name); service.add(employee); - } @Override @@ -45,8 +33,9 @@ public class EmployeeConsoleInputImpl implements EmployeeInput { LOG.info("Do you want to enter another employee? (Y/N)"); System.out.print("> "); keepGoing = scanner.next(); - if (keepGoing.length() > 1) + if (keepGoing.length() > 1) { keepGoing = keepGoing.substring(0, 1); + } } while (keepGoing.equalsIgnoreCase("Y")); } } From 09e7119f89208eb42e535b3b4e1c9c85aa4cf108 Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Sun, 18 Nov 2018 10:07:05 -0500 Subject: [PATCH 4/6] add new line to pom.xml --- hexagonal-architecture/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml index c8eceae21a..d184576705 100644 --- a/hexagonal-architecture/pom.xml +++ b/hexagonal-architecture/pom.xml @@ -11,4 +11,4 @@ 0.0.1-SNAPSHOT ../parent-java - \ No newline at end of file + From 2e6a5a35106f15c17187f886c8db2b58f0e260c4 Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Wed, 12 Dec 2018 06:32:57 -0500 Subject: [PATCH 5/6] removed eval article code --- hexagonal-architecture/.gitignore | 1 - hexagonal-architecture/pom.xml | 14 ----- .../com/baeldung/hexagonal/Application.java | 26 -------- .../baeldung/hexagonal/domain/Employee.java | 60 ------------------- .../hexagonal/domain/EmployeeService.java | 25 -------- .../hexagonal/output/EmployeeCsvWriter.java | 59 ------------------ .../hexagonal/output/EmployeeLogger.java | 17 ------ .../hexagonal/output/EmployeeOutput.java | 9 --- .../hexagonal/storage/EmployeeRepository.java | 13 ---- .../storage/InMemoryEmployeeRepository.java | 31 ---------- .../ui/EmployeeConsoleInputImpl.java | 43 ------------- .../baeldung/hexagonal/ui/EmployeeInput.java | 7 --- 12 files changed, 305 deletions(-) delete mode 100644 hexagonal-architecture/.gitignore delete mode 100644 hexagonal-architecture/pom.xml delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java diff --git a/hexagonal-architecture/.gitignore b/hexagonal-architecture/.gitignore deleted file mode 100644 index ae3c172604..0000000000 --- a/hexagonal-architecture/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/bin/ diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml deleted file mode 100644 index d184576705..0000000000 --- a/hexagonal-architecture/pom.xml +++ /dev/null @@ -1,14 +0,0 @@ - - 4.0.0 - com.baeldung.hexagonal - hexagonal-architecture - jar - hexagonal-architecture - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../parent-java - - diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java deleted file mode 100644 index 237b664708..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/Application.java +++ /dev/null @@ -1,26 +0,0 @@ -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(); - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java deleted file mode 100644 index 0d883995b6..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/Employee.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.baeldung.hexagonal.domain; - -public class Employee { - private Long id; - private String name; - - public Employee(Long id, String name) { - this.id = id; - this.name = name; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "Employee [id=" + id + ", name=" + name + "]"; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((id == null) ? 0 : id.hashCode()); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Employee other = (Employee) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - - return true; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java deleted file mode 100644 index 2ab3ce9712..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/domain/EmployeeService.java +++ /dev/null @@ -1,25 +0,0 @@ -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 employees = employeeRepository.findAll(); - employeeOutput.writeAll(employees); - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java deleted file mode 100644 index 799fbc28ba..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeCsvWriter.java +++ /dev/null @@ -1,59 +0,0 @@ -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 employees) { - BufferedWriter writer = null; - - try { - writer = new BufferedWriter(new FileWriter(outputFile)); - for (Iterator it = employees.iterator(); it.hasNext();) { - Employee emp = it.next(); - StringBuffer empLine = new StringBuffer(); - empLine.append(emp.getId()); - empLine.append(","); - empLine.append(emp.getName()); - writer.newLine(); - } - writer.flush(); - } catch (IOException ioe) { - // handle the exception - } finally { - if (writer != null) { - try { - writer.close(); - } catch (IOException e) { - // handle the exception - } - } - } - - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java deleted file mode 100644 index 5272d18961..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeLogger.java +++ /dev/null @@ -1,17 +0,0 @@ -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 employees) { - employees.forEach(employee -> LOG.info(employee.toString())); - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java deleted file mode 100644 index 979c06e1f4..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/output/EmployeeOutput.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.hexagonal.output; - -import java.util.List; - -import com.baeldung.hexagonal.domain.Employee; - -public interface EmployeeOutput { - public void writeAll(List employees); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java deleted file mode 100644 index 2787601998..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/EmployeeRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -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 findAll(); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java deleted file mode 100644 index e860c16462..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/storage/InMemoryEmployeeRepository.java +++ /dev/null @@ -1,31 +0,0 @@ -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 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 findAll() { - List all = new ArrayList(employees.values()); - return all; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java deleted file mode 100644 index 77453da48f..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeConsoleInputImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.hexagonal.ui; - -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("Name: "); - System.out.print("> "); - String name = scanner.next(); - - Employee employee = new Employee(id, name); - 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")); - } - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java deleted file mode 100644 index adec2852cd..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/ui/EmployeeInput.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.hexagonal.ui; - -import com.baeldung.hexagonal.domain.EmployeeService; - -public interface EmployeeInput { - void collectData(EmployeeService service); -} From 1e29af79ffaf70c65fad2652d4f5da1dec7a95e5 Mon Sep 17 00:00:00 2001 From: amdegregorio Date: Sun, 16 Dec 2018 06:58:45 -0500 Subject: [PATCH 6/6] Example code for Guide to Packages --- .../java/com/baeldung/packages/TodoApp.java | 22 +++++++++++ .../java/com/baeldung/packages/TodoList.java | 27 +++++++++++++ .../baeldung/packages/domain/TodoItem.java | 39 +++++++++++++++++++ .../baeldung/packages/PackagesUnitTest.java | 23 +++++++++++ 4 files changed, 111 insertions(+) create mode 100644 core-java-lang/src/main/java/com/baeldung/packages/TodoApp.java create mode 100644 core-java-lang/src/main/java/com/baeldung/packages/TodoList.java create mode 100644 core-java-lang/src/main/java/com/baeldung/packages/domain/TodoItem.java create mode 100644 core-java-lang/src/test/java/com/baeldung/packages/PackagesUnitTest.java diff --git a/core-java-lang/src/main/java/com/baeldung/packages/TodoApp.java b/core-java-lang/src/main/java/com/baeldung/packages/TodoApp.java new file mode 100644 index 0000000000..0f4a56f708 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/packages/TodoApp.java @@ -0,0 +1,22 @@ +package com.baeldung.packages; + +import java.time.LocalDate; + +import com.baeldung.packages.domain.TodoItem; + +public class TodoApp { + + public static void main(String[] args) { + TodoList todoList = new TodoList(); + for (int i = 0; i < 3; i++) { + TodoItem item = new TodoItem(); + item.setId(Long.valueOf((long)i)); + item.setDescription("Todo item " + (i + 1)); + item.setDueDate(LocalDate.now().plusDays((long)(i + 1))); + todoList.addTodoItem(item); + } + + todoList.getTodoItems().forEach((TodoItem todoItem) -> System.out.println(todoItem.toString())); + } + +} diff --git a/core-java-lang/src/main/java/com/baeldung/packages/TodoList.java b/core-java-lang/src/main/java/com/baeldung/packages/TodoList.java new file mode 100644 index 0000000000..6ed6cd4ec1 --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/packages/TodoList.java @@ -0,0 +1,27 @@ +package com.baeldung.packages; + +import java.util.ArrayList; +import java.util.List; + +import com.baeldung.packages.domain.TodoItem; + +public class TodoList { + private List todoItems; + + public void addTodoItem(TodoItem todoItem) { + if (todoItems == null) { + todoItems = new ArrayList(); + } + + todoItems.add(todoItem); + } + + public List getTodoItems() { + return todoItems; + } + + public void setTodoItems(List todoItems) { + this.todoItems = todoItems; + } + +} diff --git a/core-java-lang/src/main/java/com/baeldung/packages/domain/TodoItem.java b/core-java-lang/src/main/java/com/baeldung/packages/domain/TodoItem.java new file mode 100644 index 0000000000..972e574a7f --- /dev/null +++ b/core-java-lang/src/main/java/com/baeldung/packages/domain/TodoItem.java @@ -0,0 +1,39 @@ +package com.baeldung.packages.domain; + +import java.time.LocalDate; + +public class TodoItem { + private Long id; + private String description; + private LocalDate dueDate; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public LocalDate getDueDate() { + return dueDate; + } + + public void setDueDate(LocalDate dueDate) { + this.dueDate = dueDate; + } + + @Override + public String toString() { + return "TodoItem [id=" + id + ", description=" + description + ", dueDate=" + dueDate + "]"; + } + +} diff --git a/core-java-lang/src/test/java/com/baeldung/packages/PackagesUnitTest.java b/core-java-lang/src/test/java/com/baeldung/packages/PackagesUnitTest.java new file mode 100644 index 0000000000..212fb7b3c7 --- /dev/null +++ b/core-java-lang/src/test/java/com/baeldung/packages/PackagesUnitTest.java @@ -0,0 +1,23 @@ +package com.baeldung.packages; + +import static org.junit.Assert.assertEquals; + +import java.time.LocalDate; + +import org.junit.Test; + +import com.baeldung.packages.domain.TodoItem; + +public class PackagesUnitTest { + + @Test + public void whenTodoItemAdded_ThenSizeIncreases() { + TodoItem todoItem = new TodoItem(); + todoItem.setId(1L); + todoItem.setDescription("Test the Todo List"); + todoItem.setDueDate(LocalDate.now()); + TodoList todoList = new TodoList(); + todoList.addTodoItem(todoItem); + assertEquals(1, todoList.getTodoItems().size()); + } +}