Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package employees;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.HashMap;
- public class Employee extends Person
- {
- static HashMap<String, Employee> employees = new HashMap<String, Employee>();
- static final double MIN_SALARY = 0.0;
- double monthly_salary;
- String title;
- int id;
- ArrayList<String> language_skills = new ArrayList<String>();
- public Employee() {}
- public Employee(int id_emp, String first_name_param, String last_name_param, int born_in, String title_param, double salary_param)
- {
- super(first_name_param, last_name_param, born_in);
- id = id_emp;
- title = title_param;
- monthly_salary = salary_param;
- }
- public boolean canSpeak(String language)
- {
- return language_skills.contains(language);
- }
- public void addLanguage(String new_language)
- {
- language_skills.add(new_language);
- }
- public double getIncome()
- {
- return 12 * monthly_salary;
- }
- public String toString()
- {
- return this.getFirstName() + " " + this.getLastName() + ", " + this.title;
- }
- public static void whoKnows(String language)
- {
- for (Employee employee: employees.values())
- {
- if (employee.canSpeak(language))
- {
- System.out.println(employee);
- }
- }
- }
- public static void printTopIncome(double low_limit)
- {
- for (Employee employee: employees.values())
- {
- if (employee.getIncome() > low_limit)
- {
- System.out.println(employee);
- }
- }
- }
- public static Employee getEmployee(String first_name, String last_name)
- {
- System.out.println(employees.get(first_name + " " + last_name));
- return employees.get(first_name + " " + last_name);
- }
- public String getTitle ()
- {
- return this.title;
- }
- public ArrayList<String> getLanguages()
- {
- return language_skills;
- }
- public String getFullName()
- {
- return this.getFirstName() + " " + this.getLastName();
- }
- public static void loadFromDB()
- {
- String jdbcUrl = "jdbc:postgresql://localhost/postgres";
- String username = "postgres";
- String password = "postgres";
- Connection conn = null;
- Statement stmt = null;
- Statement stmt2 = null;
- ResultSet rs = null;
- ResultSet rs_l = null;
- try
- {
- conn = DriverManager.getConnection(jdbcUrl, username, password);
- stmt = conn.createStatement();
- stmt2 = conn.createStatement();
- rs = stmt.executeQuery("select id, first_name, last_name, year_of_birth,title, monthly_salary from employees where id not in (select emp_id from managers) and id not in (select emp_id from developers)");
- while (rs.next())
- {
- Employee e = new Employee(rs.getInt("id"),rs.getString("first_name"),rs.getString("last_name"), rs.getInt("year_of_birth"), rs.getString("title"), rs.getInt("monthly_salary"));
- rs_l = stmt2.executeQuery("select emp_language from emp_languages where emp_id = " + e.id);
- while (rs_l.next())
- {
- e.addLanguage(rs_l.getString("emp_language"));
- System.out.println(e.getFirstName() + " " + e.getLastName() + " " + rs_l.getString("emp_language"));
- }
- employees.put(e.getFullName(), e);
- }
- } catch (SQLException e)
- {
- e.printStackTrace();
- }
- finally
- {
- try {
- if (stmt != null) {
- stmt.close();
- }
- if (rs != null) {
- rs.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement