Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package employees;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.HashMap;
  10.  
  11. public class Employee extends Person
  12. {
  13.     static HashMap<String, Employee> employees = new HashMap<String, Employee>();
  14.     static final double MIN_SALARY = 0.0;
  15.  
  16.     double monthly_salary;
  17.     String title;
  18.     int id;
  19.     ArrayList<String> language_skills = new ArrayList<String>();
  20.  
  21.     public Employee() {}
  22.  
  23.     public Employee(int id_emp, String first_name_param, String last_name_param, int born_in, String title_param, double salary_param)
  24.     {
  25.         super(first_name_param, last_name_param, born_in);
  26.         id = id_emp;
  27.         title = title_param;
  28.         monthly_salary = salary_param;
  29.     }
  30.  
  31.     public boolean canSpeak(String language)
  32.     {
  33.         return language_skills.contains(language);
  34.     }
  35.  
  36.     public void addLanguage(String new_language)
  37.     {
  38.         language_skills.add(new_language);
  39.     }
  40.  
  41.     public double getIncome()
  42.     {
  43.         return 12 * monthly_salary;
  44.     }
  45.  
  46.     public String toString()
  47.     {
  48.         return this.getFirstName() + " " + this.getLastName() + ", " + this.title;
  49.     }
  50.  
  51.     public static void whoKnows(String language)
  52.     {
  53.  
  54.         for (Employee employee: employees.values())
  55.         {
  56.             if (employee.canSpeak(language))
  57.             {
  58.                 System.out.println(employee);
  59.             }
  60.         }
  61.     }
  62.  
  63.     public static void printTopIncome(double low_limit)
  64.     {
  65.         for (Employee employee: employees.values())
  66.         {
  67.             if (employee.getIncome() > low_limit)
  68.             {
  69.                 System.out.println(employee);
  70.             }
  71.         }
  72.     }
  73.  
  74.     public static Employee getEmployee(String first_name, String last_name)
  75.     {
  76.         System.out.println(employees.get(first_name + " " + last_name));
  77.         return employees.get(first_name + " " + last_name);
  78.     }
  79.  
  80.     public String getTitle ()
  81.     {
  82.         return this.title;
  83.     }
  84.  
  85.     public ArrayList<String> getLanguages()
  86.     {
  87.         return language_skills;
  88.     }
  89.    
  90.     public String getFullName()
  91.     {
  92.         return this.getFirstName() + " " + this.getLastName();
  93.     }
  94.    
  95.     public static void loadFromDB()
  96.     {
  97.         String jdbcUrl = "jdbc:postgresql://localhost/postgres";
  98.         String username = "postgres";
  99.         String password = "postgres";
  100.    
  101.         Connection conn = null;
  102.         Statement stmt = null;
  103.         Statement stmt2 = null;
  104.         ResultSet rs = null;
  105.         ResultSet rs_l = null;
  106.    
  107.         try
  108.         {
  109.             conn = DriverManager.getConnection(jdbcUrl, username, password);
  110.             stmt = conn.createStatement();
  111.             stmt2 = conn.createStatement();
  112.             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)");
  113.             while (rs.next())
  114.             {
  115.                 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"));
  116.                 rs_l = stmt2.executeQuery("select emp_language from emp_languages where emp_id = " + e.id);
  117.                 while (rs_l.next())
  118.                 {
  119.                     e.addLanguage(rs_l.getString("emp_language"));
  120.                     System.out.println(e.getFirstName() + " " + e.getLastName() + " " + rs_l.getString("emp_language"));
  121.                 }
  122.                 employees.put(e.getFullName(), e);
  123.             }
  124.         }    catch (SQLException e)
  125.         {
  126.             e.printStackTrace();
  127.         }
  128.         finally
  129.         {
  130.             try {
  131.                 if (stmt != null) {
  132.                     stmt.close();
  133.                 }
  134.                 if (rs != null) {
  135.                     rs.close();
  136.                 }
  137.                 if (conn != null) {
  138.                     conn.close();
  139.                 }
  140.             } catch (Exception e) {
  141.                 e.printStackTrace();
  142.             }
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement