formatting changes
This commit is contained in:
parent
9a6688f638
commit
35db28ccfb
|
@ -21,7 +21,6 @@ public class Application {
|
|||
EmployeeInput ui = new EmployeeConsoleInputImpl();
|
||||
ui.collectData(service);
|
||||
service.generateOutput();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@ public class Employee {
|
|||
private String lastName;
|
||||
private String employeeId;
|
||||
private BigDecimal salary;
|
||||
|
||||
|
||||
public Employee(
|
||||
Long id,
|
||||
String firstName,
|
||||
|
|
|
@ -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<Employee> 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<Employee> employees = employeeRepository.findAll();
|
||||
employeeOutput.writeAll(employees);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Employee> employees) {
|
||||
BufferedWriter writer = null;
|
||||
|
||||
|
||||
try {
|
||||
writer = new BufferedWriter(new FileWriter(outputFile));
|
||||
for (Iterator<Employee> 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class EmployeeLogger implements EmployeeOutput {
|
|||
|
||||
@Override
|
||||
public void writeAll(List<Employee> employees) {
|
||||
employees.forEach(employee -> LOG.info(employee.toString()));
|
||||
employees.forEach(employee -> LOG.info(employee.toString()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<Employee> findAll();
|
||||
public Long save(Employee employee);
|
||||
|
||||
public Employee findById(Long id);
|
||||
|
||||
public List<Employee> findAll();
|
||||
}
|
||||
|
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue