Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package org.sample;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.util.*;
  7.  
  8. public class MainAttempt {
  9.     static String csvFile = "data.csv";
  10.     static String line = "";
  11.     static String cvsSplitBy = ",";
  12.     static String[] attributes = new String[0];
  13.     static ArrayList<Employee> employees = new ArrayList<Employee>();
  14.  
  15.     public static void main(String[] args) {
  16.         String csvFile = "data.csv";
  17.         String line = "";
  18.         String cvsSplitBy = ",";
  19.         String[] attributes = new String[0];
  20.  
  21.         try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
  22.             //rename this so it's not obvious you copied lol
  23.             String headerLine = br.readLine(); //to remove header line which contains string identifiers
  24.             while ((line = br.readLine()) != null) {
  25.                 // use comma as separator
  26.                 attributes = line.split(cvsSplitBy);
  27.                 System.out.println("Country [code= " + attributes[4] + " , name=" + attributes[5] + "]");
  28.                 //        id,first_name,last_name,email,DepartmentId,Salary,ipAddress,Address
  29.                 employees.add(new Employee(Integer.parseInt(attributes[0]), attributes[1], attributes[2], attributes[3], Integer.parseInt(attributes[4]), Double.parseDouble(attributes[5]), attributes[6], attributes[7]));
  30.  
  31.             }
  32.         } catch (IOException e) {
  33.             e.printStackTrace();
  34.         }
  35.  
  36.         System.out.println("getEmployee");
  37.         System.out.println(getEmployee(1));
  38.         System.out.println("getTopNSalaries");
  39.         System.out.println(getTopNSalaries(3));
  40.         System.out.println("getDepartmentCost");
  41.         System.out.println(getDepartmentCost(71));
  42.  
  43.     }
  44.     //    Your application will provide a method to return details of an Employee chosen via the employee ID number.
  45.     private static String getEmployee(int input){
  46.         String employee = "E1"+input;
  47.         //        your code
  48.         for (Employee employee1 : employees) {
  49.             if (employee1.getEmployeeId() == input) {
  50.                 employee = employee1.toString();
  51.                 System.out.println(employee1);
  52.             }
  53.         }
  54.  
  55.         return employee;
  56.     }
  57.     //    Your application will provide a method to return the top n salaries paid by the company.
  58.     public static int[] getTopNSalaries(int n){
  59.         int[] salaries = new int[n];
  60.         //        your code
  61.         employees.sort(Comparator.comparing(Employee::getSalary));
  62.  
  63.         while(n > 0){
  64.             salaries[n -1] = ((int) employees.get(employees.size() - n).getSalary());
  65.             System.out.println(salaries[n - 1]);
  66.  
  67.             n--;
  68.         }
  69.         return salaries;
  70.     }
  71.     //    Your application will provide a method to return the sum of salaries paid by a given department.
  72.     public static int getDepartmentCost(int input){
  73.         int departmentCost = 0;
  74.         double realPrice = 0;
  75. //        //        your code
  76.         int x = employees.size() -1;
  77.         while(x > -1){
  78.                 if (employees.get(x).getDepartmentId() == input) {
  79.                     realPrice += employees.get(x).getSalary();
  80.  
  81.             }
  82.  
  83.             x--;
  84.         }
  85.         departmentCost = (int) realPrice;
  86.         return departmentCost;
  87.     }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement