-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfbb82f
commit 4a14821
Showing
6 changed files
with
144 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |