题目
1.有一组医院科室物品用量数据(代码中getItemList()方法返回的结果),每条数据为某个科室的某种物品用量。
2.要求对这组数据进行数据转换和计算处理,转换成科室物品用量列表(格式如下表),每条记录里面有4种物品用量和总用量,并在此基础上计算全院的平均用量。
3. 要求表格按总用量从高到低排序,最后用控制台输出即可。
科室 A物品 B物品 C物品 D物品 总用量
全科 89 78 88 87 342
儿科 87 89 66 95 337
…
平均用量 79 72 69 77 299
需要注意的是,最终完成代码的代码质量(逻辑是否清晰,变量/方法命名是否合理等),将作为考察标准的一部分
import java.util.*;
public class ShowMeBug {
// 请按你的实际需求修改参数
public static void main(String[] args) {
new ShowMeBug().test();
}
public void test(){
List<ItemVo> itemList = getItemList();
Map<String,DepartmentVo> map = new HashMap<>();
// TODO 请编写代码实现题目要求
for (ItemVo itemVo : itemList) {
DepartmentVo departmentVo = null;
if (map.get(itemVo.departmentName) == null){
departmentVo = new DepartmentVo();
departmentVo.setName(itemVo.departmentName);
}else {
departmentVo = map.get(itemVo.departmentName);
}
switch (itemVo.itemName){
case "A物品" : {
departmentVo.setItemAConsumption(itemVo.consumption);
break;
}
case "B物品" : {
departmentVo.setItemBConsumption(itemVo.consumption);
break;
}
case "C物品" : {
departmentVo.setItemCConsumption(itemVo.consumption);
break;
}
case "D物品" : {
departmentVo.setItemDConsumption(itemVo.consumption);
break;
}
}
departmentVo.setTotalConsumption(departmentVo.getItemAConsumption()+departmentVo.getItemBConsumption()+departmentVo.getItemCConsumption()+departmentVo.getItemDConsumption());
map.put(itemVo.departmentName,departmentVo);
}
List<DepartmentVo> departmentVos = new ArrayList(map.values());
Double averageA = departmentVos.stream().mapToDouble(DepartmentVo::getItemAConsumption).average().getAsDouble();
Double averageB = departmentVos.stream().mapToDouble(DepartmentVo::getItemBConsumption).average().getAsDouble();
Double averageC = departmentVos.stream().mapToDouble(DepartmentVo::getItemCConsumption).average().getAsDouble();
Double averageD = departmentVos.stream().mapToDouble(DepartmentVo::getItemDConsumption).average().getAsDouble();
Double averageTotal = departmentVos.stream().mapToDouble(DepartmentVo::getTotalConsumption).average().getAsDouble();
System.out.println("科室"+"\t"+"物品A"+"\t"+"物品B"+"\t"+"物品C"+"\t"+"物品D"+"\t"+"总用量");
for (DepartmentVo vo : departmentVos) {
System.out.println(vo.name+"\t"+vo.getItemAConsumption()+"\t\t"+vo.getItemBConsumption()+"\t\t"+vo.getItemCConsumption()+"\t\t"+vo.getItemDConsumption()+"\t\t"+vo.getTotalConsumption());
}
System.out.println("平均用量"+"\t"+averageA+"\t"+averageB+"\t"+averageC+"\t"+averageD+"\t"+averageTotal);
}
public List<ItemVo> getItemList() {
List<ItemVo> itemList = new ArrayList<ItemVo>();
itemList.add(new ItemVo("全科", "B物品", 80));
itemList.add(new ItemVo("全科", "D物品", 76));
itemList.add(new ItemVo("儿科", "B物品", 78));
itemList.add(new ItemVo("牙科", "A物品", 65));
itemList.add(new ItemVo("儿科", "C物品", 88));
itemList.add(new ItemVo("儿科", "D物品", 87));
itemList.add(new ItemVo("骨科", "B物品", 67));
itemList.add(new ItemVo("全科", "C物品", 76));
itemList.add(new ItemVo("儿科", "A物品", 89));
itemList.add(new ItemVo("骨科", "C物品", 65));
itemList.add(new ItemVo("眼科", "D物品", 95));
itemList.add(new ItemVo("骨科", "A物品", 78));
itemList.add(new ItemVo("骨科", "D物品", 65));
itemList.add(new ItemVo("眼科", "B物品", 89));
itemList.add(new ItemVo("眼科", "A物品", 87));
itemList.add(new ItemVo("外科", "B物品", 78));
itemList.add(new ItemVo("外科", "C物品", 65));
itemList.add(new ItemVo("产科", "A物品", 87));
itemList.add(new ItemVo("全科", "A物品", 56));
itemList.add(new ItemVo("外科", "D物品", 76));
itemList.add(new ItemVo("产科", "C物品", 89));
itemList.add(new ItemVo("外科", "A物品", 98));
itemList.add(new ItemVo("眼科", "C物品", 66));
itemList.add(new ItemVo("产科", "B物品", 56));
itemList.add(new ItemVo("产科", "D物品", 76));
itemList.add(new ItemVo("内科", "B物品", 88));
itemList.add(new ItemVo("内科", "C物品", 67));
itemList.add(new ItemVo("牙科", "C物品", 43));
itemList.add(new ItemVo("内科", "A物品", 75));
itemList.add(new ItemVo("牙科", "B物品", 45));
itemList.add(new ItemVo("牙科", "D物品", 56));
itemList.add(new ItemVo("内科", "D物品", 88));
return itemList;
}
class DepartmentVo {
private String name;
private int itemAConsumption;
private int itemBConsumption;
private int itemCConsumption;
private int itemDConsumption;
private int totalConsumption;
public DepartmentVo(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getItemAConsumption() {
return itemAConsumption;
}
public void setItemAConsumption(int itemAConsumption) {
this.itemAConsumption = itemAConsumption;
}
public int getItemBConsumption() {
return itemBConsumption;
}
public void setItemBConsumption(int itemBConsumption) {
this.itemBConsumption = itemBConsumption;
}
public int getItemCConsumption() {
return itemCConsumption;
}
public void setItemCConsumption(int itemCConsumption) {
this.itemCConsumption = itemCConsumption;
}
public int getItemDConsumption() {
return itemDConsumption;
}
public void setItemDConsumption(int itemDConsumption) {
this.itemDConsumption = itemDConsumption;
}
public int getTotalConsumption() {
return totalConsumption;
}
public void setTotalConsumption(int totalConsumption) {
this.totalConsumption = totalConsumption;
}
}
class ItemVo {
public String departmentName;
public String itemName;
public int consumption;
public ItemVo(String departmentName, String itemName, int consumption){
this.departmentName = departmentName;
this.itemName = itemName;
this.consumption = consumption;
}
public String toString(){
return this.departmentName + "\t" + this.itemName + "\t" + this.consumption;
}
}
}