diff --git a/src/main/java/com/napier/sem/App.java b/src/main/java/com/napier/sem/App.java index 42a4b7d..a6f5435 100644 --- a/src/main/java/com/napier/sem/App.java +++ b/src/main/java/com/napier/sem/App.java @@ -71,129 +71,6 @@ public void disconnect() } } - public List getEmployeeWithTitile(String title) - { - List employees = new ArrayList(); - - try - { - // Create an SQL prepared statement to prevent SQL injection - String strSelect = - "SELECT employees.emp_no, employees.first_name, employees.last_name, salaries.salary " + - "FROM employees, salaries, titles " + - "WHERE employees.emp_no = salaries.emp_no " + - "AND employees.emp_no = titles.emp_no " + - "AND salaries.to_date = '9999-01-01' " + - "AND titles.to_date = '9999-01-01' " + - "AND titles.title = ? " + // Use a placeholder for title - "ORDER BY employees.emp_no ASC"; - - // Prepare statement - PreparedStatement pstmt = con.prepareStatement(strSelect); - // Set the title parameter - pstmt.setString(1, title); - - // Execute SQL statement - ResultSet rset = pstmt.executeQuery(); - - // Return new employee if valid. - while (rset.next()) - { - Employee emp = new Employee(); - emp.emp_no = rset.getInt("emp_no"); - emp.first_name = rset.getString("first_name"); - emp.last_name = rset.getString("last_name"); - emp.salary = rset.getInt("salary"); - employees.add(emp); - } - return employees; - } - catch (Exception e) - { - System.out.println(e.getMessage()); - System.out.println("Failed to get employee details"); - return null; - } - } - - - /** - * Gets all the current employees and salaries. - * @return A list of all employees and salaries, or null if there is an error. - */ - public ArrayList getAllSalaries() - { - try - { - // Create an SQL statement - Statement stmt = con.createStatement(); - // Create string for SQL statement - String strSelect = - "SELECT employees.emp_no, employees.first_name, employees.last_name, salaries.salary " - + "FROM employees, salaries " - + "WHERE employees.emp_no = salaries.emp_no AND salaries.to_date = '9999-01-01' " - + "ORDER BY employees.emp_no ASC"; - // Execute SQL statement - ResultSet rset = stmt.executeQuery(strSelect); - // Extract employee information - ArrayList employees = new ArrayList(); - while (rset.next()) - { - Employee emp = new Employee(); - emp.emp_no = rset.getInt("employees.emp_no"); - emp.first_name = rset.getString("employees.first_name"); - emp.last_name = rset.getString("employees.last_name"); - emp.salary = rset.getInt("salaries.salary"); - employees.add(emp); - } - return employees; - } - catch (Exception e) - { - System.out.println(e.getMessage()); - System.out.println("Failed to get salary details"); - return null; - } - } - - /** - * Prints a list of employees. - * @param employees The list of employees to print. - */ - public void printSalaries(ArrayList employees) - { - if (employees == null){ - System.out.println("No employees"); - return; - } - // Print header - System.out.println(String.format("%-10s %-15s %-20s %-8s", "Emp No", "First Name", "Last Name", "Salary")); - // Loop over all employees in the list - for (Employee emp : employees) - { - if (emp == null) - continue; - String emp_string = - String.format("%-10s %-15s %-20s %-8s", - emp.emp_no, emp.first_name, emp.last_name, emp.salary); - System.out.println(emp_string); - } - } - - public void displayEmployeeWithTitle(List emp) - { - if (emp != null && !emp.isEmpty()) - { - System.out.println(String.format("%-10s %-15s %-20s %-8s", "Emp No", "First Name", "Last Name", "Title" ,"Salary")); - for (Employee employee : emp) { - String displayString = - String.format("%-10s %-15s %-20s %-8s", - employee.emp_no, employee.first_name, employee.last_name, " Engineer", employee.salary ); - System.out.println(displayString); - } - } - } - public static void main(String[] args) { // Create new Application @@ -202,15 +79,18 @@ public static void main(String[] args) // Connect to database a.connect(); + EmployeeSQL employeeSQL = new EmployeeSQL(a.con); + EmployeeDisplay employeeDisplay = new EmployeeDisplay(); + // get Employee who are Engineers database - List emp = a.getEmployeeWithTitile("Engineer"); - a.displayEmployeeWithTitle(emp); + List emp = employeeSQL.getEmployeeWithTitle("Engineer"); + // display employee with title + employeeDisplay.displayEmployeeWithTitle(emp); - ArrayList emp1 = a.getAllSalaries(); - a.printSalaries(emp1); + ArrayList emp1 = employeeSQL.getAllSalaries(); + employeeDisplay.printSalaries(emp1); // Disconnect from database a.disconnect(); - } } \ No newline at end of file diff --git a/src/main/java/com/napier/sem/EmployeeDisplay.java b/src/main/java/com/napier/sem/EmployeeDisplay.java new file mode 100644 index 0000000..91cefc2 --- /dev/null +++ b/src/main/java/com/napier/sem/EmployeeDisplay.java @@ -0,0 +1,46 @@ +package com.napier.sem; + +import java.util.ArrayList; +import java.util.List; + +public class EmployeeDisplay { + + /** + * Prints a list of employees. + * @param employees The list of employees to print. + */ + public void printSalaries(ArrayList employees) + { + if (employees == null){ + System.out.println("No employees"); + return; + } + // Print header + System.out.println(String.format("%-10s %-15s %-20s %-8s", "Emp No", "First Name", "Last Name", "Salary")); + // Loop over all employees in the list + for (Employee emp : employees) + { + if (emp == null) + continue; + String emp_string = + String.format("%-10s %-15s %-20s %-8s", + emp.emp_no, emp.first_name, emp.last_name, emp.salary); + System.out.println(emp_string); + } + } + + public void displayEmployeeWithTitle(List emp) + { + if (emp != null && !emp.isEmpty()) + { + System.out.println(String.format("%-10s %-15s %-20s %-8s", "Emp No", "First Name", "Last Name", "Title" ,"Salary")); + for (Employee employee : emp) { + String displayString = + String.format("%-10s %-15s %-20s %-8s", + employee.emp_no, employee.first_name, employee.last_name, " Engineer", employee.salary ); + System.out.println(displayString); + } + } + } + +} diff --git a/src/main/java/com/napier/sem/EmployeeSQL.java b/src/main/java/com/napier/sem/EmployeeSQL.java new file mode 100644 index 0000000..fead920 --- /dev/null +++ b/src/main/java/com/napier/sem/EmployeeSQL.java @@ -0,0 +1,84 @@ +package com.napier.sem; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +public class EmployeeSQL { + + private Connection con; + + public EmployeeSQL(Connection con) { + this.con = con; + } + + public List getEmployeeWithTitle(String title) { + List employees = new ArrayList<>(); + + try { + String strSelect = + "SELECT employees.emp_no, employees.first_name, employees.last_name, salaries.salary " + + "FROM employees " + + "JOIN salaries ON employees.emp_no = salaries.emp_no " + + "JOIN titles ON employees.emp_no = titles.emp_no " + + "WHERE salaries.to_date = '9999-01-01' " + + "AND titles.to_date = '9999-01-01' " + + "AND titles.title = ? " + + "ORDER BY employees.emp_no ASC"; + + PreparedStatement pstmt = con.prepareStatement(strSelect); + pstmt.setString(1, title); + ResultSet rset = pstmt.executeQuery(); + + while (rset.next()) { + Employee emp = new Employee(); + emp.emp_no = rset.getInt("emp_no"); + emp.first_name = rset.getString("first_name"); + emp.last_name = rset.getString("last_name"); + emp.salary = rset.getInt("salary"); + employees.add(emp); + } + rset.close(); + pstmt.close(); + return employees; + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Failed to get employee details"); + return null; + } + } + + public ArrayList getAllSalaries() { + ArrayList employees = new ArrayList<>(); + try { + String strSelect = + "SELECT employees.emp_no, employees.first_name, employees.last_name, salaries.salary " + + "FROM employees " + + "JOIN salaries ON employees.emp_no = salaries.emp_no " + + "WHERE salaries.to_date = '9999-01-01' " + + "ORDER BY employees.emp_no ASC"; + + Statement stmt = con.createStatement(); + ResultSet rset = stmt.executeQuery(strSelect); + + while (rset.next()) { + Employee emp = new Employee(); + emp.emp_no = rset.getInt("emp_no"); + emp.first_name = rset.getString("first_name"); + emp.last_name = rset.getString("last_name"); + emp.salary = rset.getInt("salary"); + employees.add(emp); + } + rset.close(); + stmt.close(); + return employees; + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Failed to get salary details"); + return null; + } + } +} diff --git a/src/test/java/com/napier/devops/AppTest.java b/src/test/java/com/napier/devops/AppTest.java index 02bb214..daa2caf 100644 --- a/src/test/java/com/napier/devops/AppTest.java +++ b/src/test/java/com/napier/devops/AppTest.java @@ -2,6 +2,7 @@ import com.napier.sem.App; import com.napier.sem.Employee; +import com.napier.sem.EmployeeDisplay; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -9,12 +10,12 @@ public class AppTest { - static App app; + static EmployeeDisplay app; @BeforeAll static void init() { - app = new App(); + app = new EmployeeDisplay(); } // null diff --git a/target/surefire-reports/TEST-com.napier.devops.AppTest.xml b/target/surefire-reports/TEST-com.napier.devops.AppTest.xml index 16c975d..876327e 100644 --- a/target/surefire-reports/TEST-com.napier.devops.AppTest.xml +++ b/target/surefire-reports/TEST-com.napier.devops.AppTest.xml @@ -1,5 +1,5 @@ - + @@ -64,7 +64,7 @@ - + diff --git a/target/surefire-reports/com.napier.devops.AppTest.txt b/target/surefire-reports/com.napier.devops.AppTest.txt index 3a60294..b92d02b 100644 --- a/target/surefire-reports/com.napier.devops.AppTest.txt +++ b/target/surefire-reports/com.napier.devops.AppTest.txt @@ -1,4 +1,4 @@ ------------------------------------------------------------------------------- Test set: com.napier.devops.AppTest ------------------------------------------------------------------------------- -Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.009 sec - in com.napier.devops.AppTest +Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec - in com.napier.devops.AppTest