部门最新成员工资
在这个问题中,您必须修改一个现有的 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");
}
}
}