Skip to content

Commit

Permalink
Create OOP for department
Browse files Browse the repository at this point in the history
  • Loading branch information
eieihtwesan committed Oct 10, 2024
1 parent 822ceda commit e11dca1
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/napier/sem/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public static void main(String[] args)
ArrayList<Employee> emp1 = employeeSQL.getAllSalaries();
employeeDisplay.printSalaries(emp1);

DepartmentSQL departmentSQL = new DepartmentSQL(a.con);


// Disconnect from database
a.disconnect();
}
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/napier/sem/Department.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.napier.sem;

public class Department {
public String dept_name;
public String dept_no;
public Employee manager;

public Department(String dept_name, String dept_no, Employee manager) {
this.dept_name = dept_name;
this.dept_no = dept_no;
this.manager = manager;
}

public String getDept_name() {
return dept_name;
}

public void setDept_name(String dept_name) {
this.dept_name = dept_name;
}

public String getDept_no() {
return dept_no;
}

public void setDept_no(String dept_no) {
this.dept_no = dept_no;
}

public Employee getManager() {
return manager;
}

public void setManager(Employee manager) {
this.manager = manager;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/napier/sem/DepartmentSQL.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.napier.sem;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class DepartmentSQL {

private Connection con;

public DepartmentSQL(Connection conn) {

this.con = conn;
}

public Department getDepartment(String dept_name) {

try {
// Prepare SQL statement to prevent SQL injection
String strSelect = "SELECT dept_no, dept_name FROM departments WHERE dept_name = ?";
PreparedStatement pstmt = con.prepareStatement(strSelect);
pstmt.setString(1, dept_name);
ResultSet rset = pstmt.executeQuery();


if (rset.next()) {
// Here, we fetch department number as a string (like "d005")
String dept_no = rset.getString("dept_no");
// Assume that fetching the manager is handled separately
Employee manager = null; // Placeholder for manager if needed
String dep_name = rset.getString("dept_name");
return new Department(dep_name, dept_no, manager);
} else {
return null;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
System.out.println("Failed to get department details");
return null;
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/com/napier/sem/EmployeeSQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,37 @@ public ArrayList<Employee> getAllSalaries() {
return null;
}
}

public ArrayList<Employee> getSalariesByDepartment(Department dept) {
ArrayList<Employee> employees = new ArrayList<>();
try{
String strSelect = "SELECT employees.emp_no, employees.first_name, employees.last_name, salaries.salary " +
"FROM employees, salaries, dept_emp, departments " +
"WHERE employees.emp_no = salaries.emp_no " +
"AND employees.emp_no = dept_emp.emp_no " +
"AND dept_emp.dept_no = departments.dept_no " +
"AND salaries.to_date = '9999-01-01' " +
"AND departments.dept_no = '" + dept.getDept_no() + "' " + // Dept no is treated as string
"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;
}
}
}

0 comments on commit e11dca1

Please sign in to comment.