0

部门最新成员工资

在这个问题中,您必须修改一个现有的 Java 程序,该程序读取一些员工数据,并对其进行一些处理。

输入是从名为 input.txt 的文件中读取的,格式如下:

22, Rajan Anand, Engineering, 1600000
23, Swati Patil, Testing, 800000
27, Vijay Chawda, Engineering, 800000
29, Basant Mahapatra, Engineering, 600000
32, Ajay Patel, Testing, 350000
34, Swaraj Birla, Testing, 350000

每行包含 4 个字段“员工 ID”、“姓名”、“部门”和“工资”。这里,“Employee ID”和“Salary”是整数,而“Name”和“Department”是不包含逗号或换行符的字符串。

目前,现有程序读取输入并从输入行创建一个字符串数组。然后它使用该数组调用方法 processData,并将返回的数据打印到输出文件。不幸的是,processData 目前没有做任何有用的事情——它只是返回一个空 Map。

您必须修改 processData 找到该部门中员工 ID 最高的员工的薪水(因为它代表加入该部门的最新员工)。具体来说,processData 应该返回一个 Map,其中每个键是部门的名称,值是该部门中员工 ID 最高的员工的薪水。

如果您的程序使用上面给出的输入运行,它应该产生以下输出:

Engineering: 600000
Testing: 350000

这是因为 Basant Mahapatra 在工程部门的员工 ID 最高(29),他的薪水为 600000,而 Swaraj Birla 在测试部门的员工 ID 最高(34),他的薪水为 350000。

请确保以下几点:

• 您的所有更改都必须在processData 方法中。不要对程序的其余部分进行任何更改

• 确保 processData 返回正确的值

• 不要添加包声明,也不要更改类的名称。

import java.util.*;
import java.io.*;

/* DO NOT CHANGE ANYTHING ABOVE THIS LINE */
/* You may add any imports here, if you wish, but only from the 
   standard library */

/* Do not add a namespace declaration */
public class Main {
    public static Map<String,Integer> processData(ArrayList<String> array) {
        /* 
         * Modify this method to process `array` as indicated
         * in the question. At the end, return a Map containing
         * the appropriate values
         *
         * Please create appropriate classes, and use appropriate
         * data structures as necessary.
         *
         * Do not print anything in this method.
         *
         * Submit this entire program (not just this method)
         * as your answer
         */
        Map<String,Integer> retVal = new Map<String,Integer>();
       return retVal;
    }

    public static void main (String[] args) {
        ArrayList<String> inputData = new ArrayList<String>();
        String line;
        try {
            Scanner in = new Scanner(new BufferedReader(new FileReader("input.txt")));
            while(in.hasNextLine())
                inputData.add(in.nextLine());
            Map<String,Integer> retVal = processData(inputData);
            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
            for(Map.Entry<String,Integer> e: retVal.entrySet())
                output.println(e.getKey() + ": " + e.getValue());
            output.close();
        } catch (IOException e) {
            System.out.println("IO error in input.txt or output.txt");
        }
    }
}
4

2 回答 2

1
import java.io.*;
import java.util.*;

/* DO NOT CHANGE ANYTHING ABOVE THIS LINE */
/* You may add any imports here, if you wish, but only from the 
   standard library */

/* Do not add a namespace declaration */
public class Main {
    public static Map<String,Integer> processData(ArrayList<String> array) {
        String str[];
        int count=0;
        Map<String,Integer> res= new HashMap<String,Integer>();
    int ar[]= new int[100];
    int ar1[]= new int[100];
    int s1=0,s2=0;

//遍历数组列表

            for(String s : array){
                str= s.split(",");

//将工程部的所有ID保存在ar(Array)中

                if(str[2].trim().equals("Engineering"))

{

                    ar[count]=(int)Integer.parseInt(str[0].trim());

//搜索ar(Array)中的最高ID

                    if(s1<=ar[count])
                        s1=ar[count];


                }

/*将所有测试部门的ID存储在ar1(Array)中*/

                if(str[2].trim().equals("Testing"))
                {


                    ar1[count]=(int)Integer.parseInt(str[0].trim());

        // Similarly Searching for the highest ID in the ar1(Array) 

                    if(s2<=ar1[count])
                        s2=ar1[count];

                }
        }

/* 再次遍历数组,将部门和薪水存储在我们搜索到的 ID 的 res(HashMap) 中 */

            for(String s : array){


                str= s.split(",");
                if(s1==(int)Integer.parseInt(str[0].trim())) {

                    Integer i=(Integer)Integer.parseInt(str[3].trim());
                    res.put("Engineering",i);

                }
                if(s2==(int)Integer.parseInt(str[0].trim())) {
                    Integer i=(Integer)Integer.parseInt(str[3].trim());
                    res.put("Testing",i);

                }

            }

            return res;
    }

//结尾

    public static void main (String[] args) {
        ArrayList<String> inputData = new ArrayList<String>();
        String line;
        try {
            Scanner in = new Scanner(new BufferedReader(new FileReader("C:\\Users\\LENOVO\\eclipse-workspace\\DesignPatterns\\src\\input.txt")));
            while(in.hasNextLine())
                inputData.add(in.nextLine());
            Map<String,Integer> retVal = processData(inputData);
            PrintWriter output = new PrintWriter(new BufferedWriter(new FileWriter("C:\\Users\\LENOVO\\eclipse-workspace\\DesignPatterns\\src\\output.txt")));
            for(Map.Entry<String,Integer> e: retVal.entrySet())
                output.println(e.getKey() + ": " + e.getValue());
            output.close();
        } catch (IOException e) {
            System.out.println("IO error in input.txt or output.txt");
        }
    }
}
于 2020-06-11T10:22:17.600 回答
1
public static Map<String, Integer> processData(ArrayList<String> array) {
    ArrayList<Employee> employees = new ArrayList<>();

    array.forEach(empString -> {
        String s[] = empString.split(",");
        Employee emp = new Employee(Integer.parseInt(s[0].trim()),
                Integer.parseInt(s[3].trim()), s[1], s[2]);
        employees.add(emp);
    });

    Map<String, Employee> retVal = employees.stream()
            .collect(groupingBy(
                    e -> e.getDepartment(),
                    collectingAndThen(maxBy(comparingInt(e -> e.getEmployeeId())), Optional::get)
            ));

    Map<String, Integer> map = new HashMap<>();
    retVal.entrySet().forEach(stringEmployeeEntry -> {
        map.put(stringEmployeeEntry.getKey(), stringEmployeeEntry.getValue().getSalary());
    });
    return map;
}
于 2020-10-04T11:02:17.010 回答