Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
eieihtwesan committed Oct 7, 2024
1 parent bfbb82f commit 4a14821
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 133 deletions.
136 changes: 8 additions & 128 deletions src/main/java/com/napier/sem/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,129 +71,6 @@ public void disconnect()
}
}

public List<Employee> getEmployeeWithTitile(String title)
{
List<Employee> employees = new ArrayList<Employee>();

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<Employee> 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<Employee> employees = new ArrayList<Employee>();
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<Employee> 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<Employee> 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
Expand All @@ -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<Employee> emp = a.getEmployeeWithTitile("Engineer");
a.displayEmployeeWithTitle(emp);
List<Employee> emp = employeeSQL.getEmployeeWithTitle("Engineer");
// display employee with title
employeeDisplay.displayEmployeeWithTitle(emp);

ArrayList<Employee> emp1 = a.getAllSalaries();
a.printSalaries(emp1);
ArrayList<Employee> emp1 = employeeSQL.getAllSalaries();
employeeDisplay.printSalaries(emp1);

// Disconnect from database
a.disconnect();

}
}
46 changes: 46 additions & 0 deletions src/main/java/com/napier/sem/EmployeeDisplay.java
Original file line number Diff line number Diff line change
@@ -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<Employee> 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<Employee> 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);
}
}
}

}
84 changes: 84 additions & 0 deletions src/main/java/com/napier/sem/EmployeeSQL.java
Original file line number Diff line number Diff line change
@@ -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<Employee> getEmployeeWithTitle(String title) {
List<Employee> 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<Employee> getAllSalaries() {
ArrayList<Employee> 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;
}
}
}
5 changes: 3 additions & 2 deletions src/test/java/com/napier/devops/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

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;

import java.util.ArrayList;

public class AppTest
{
static App app;
static EmployeeDisplay app;

@BeforeAll
static void init()
{
app = new App();
app = new EmployeeDisplay();
}

// null
Expand Down
4 changes: 2 additions & 2 deletions target/surefire-reports/TEST-com.napier.devops.AppTest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.napier.devops.AppTest" time="0.009" tests="4" errors="0" skipped="0" failures="0">
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="com.napier.devops.AppTest" time="0.016" tests="4" errors="0" skipped="0" failures="0">
<properties>
<property name="idea.version" value="2024.2.2"/>
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
Expand Down Expand Up @@ -64,7 +64,7 @@
<property name="sun.stdout.encoding" value="UTF-8"/>
<property name="sun.cpu.isalist" value="amd64"/>
</properties>
<testcase name="printSalariesTestContainsNull" classname="com.napier.devops.AppTest" time="0.005"/>
<testcase name="printSalariesTestContainsNull" classname="com.napier.devops.AppTest" time="0.009"/>
<testcase name="printSalariesTestEmpty" classname="com.napier.devops.AppTest" time="0"/>
<testcase name="printSalariesTestNull" classname="com.napier.devops.AppTest" time="0"/>
<testcase name="printSalaries" classname="com.napier.devops.AppTest" time="0.001"/>
Expand Down
2 changes: 1 addition & 1 deletion target/surefire-reports/com.napier.devops.AppTest.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4a14821

Please sign in to comment.