Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sample;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.*;
- public class MainAttempt {
- static String csvFile = "data.csv";
- static String line = "";
- static String cvsSplitBy = ",";
- static String[] attributes = new String[0];
- static ArrayList<Employee> employees = new ArrayList<Employee>();
- public static void main(String[] args) {
- String csvFile = "data.csv";
- String line = "";
- String cvsSplitBy = ",";
- String[] attributes = new String[0];
- try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
- //rename this so it's not obvious you copied lol
- String headerLine = br.readLine(); //to remove header line which contains string identifiers
- while ((line = br.readLine()) != null) {
- // use comma as separator
- attributes = line.split(cvsSplitBy);
- System.out.println("Country [code= " + attributes[4] + " , name=" + attributes[5] + "]");
- // id,first_name,last_name,email,DepartmentId,Salary,ipAddress,Address
- 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]));
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println("getEmployee");
- System.out.println(getEmployee(1));
- System.out.println("getTopNSalaries");
- System.out.println(getTopNSalaries(3));
- System.out.println("getDepartmentCost");
- System.out.println(getDepartmentCost(71));
- }
- // Your application will provide a method to return details of an Employee chosen via the employee ID number.
- private static String getEmployee(int input){
- String employee = "E1"+input;
- // your code
- for (Employee employee1 : employees) {
- if (employee1.getEmployeeId() == input) {
- employee = employee1.toString();
- System.out.println(employee1);
- }
- }
- return employee;
- }
- // Your application will provide a method to return the top n salaries paid by the company.
- public static int[] getTopNSalaries(int n){
- int[] salaries = new int[n];
- // your code
- employees.sort(Comparator.comparing(Employee::getSalary));
- while(n > 0){
- salaries[n -1] = ((int) employees.get(employees.size() - n).getSalary());
- System.out.println(salaries[n - 1]);
- n--;
- }
- return salaries;
- }
- // Your application will provide a method to return the sum of salaries paid by a given department.
- public static int getDepartmentCost(int input){
- int departmentCost = 0;
- double realPrice = 0;
- // // your code
- int x = employees.size() -1;
- while(x > -1){
- if (employees.get(x).getDepartmentId() == input) {
- realPrice += employees.get(x).getSalary();
- }
- x--;
- }
- departmentCost = (int) realPrice;
- return departmentCost;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement