8、管理手机库存,请彭彭老师检查,感谢!
来源:14-17 测评作业

mottoyin
2025-02-20
测试类
package com.imooc.chapter14test;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class PhoneStock {
public static void main(String[] args) {
//创建华为手机库存Map
Map<String,Integer> hphoneStock = new HashMap<>();
hphoneStock.put("Pura70",0);
hphoneStock.put("Mate6070",0);
hphoneStock.put("Mate60Pro",0);
//创建小米手机库存Map
Map<String,Integer> xphoneStock = new HashMap<>();
xphoneStock.put("mi5s",0);
xphoneStock.put("xiaomi13",65);
xphoneStock.put("Redmi Note13",27);
//创建品牌与手机库存Map
Map<String, Map<String, Integer>> brandMap = new HashMap<>();
brandMap.put("华为",hphoneStock);
brandMap.put("小米",xphoneStock);
//清空库存全为0的品牌,等到有库存时再使用
Set<Map.Entry<String, Map<String, Integer>>> entrySet = brandMap.entrySet();
for (Map.Entry<String, Map<String, Integer>> item:
entrySet) {
if(item.getKey().equals("华为")){
//清空华为库存
item.getValue().clear();
}else if(item.getKey().equals("小米")){
//找到mi5s的key进行删除
if(item.getValue().containsKey("mi5s")){
item.getValue().remove("mi5s");
}
}
//打印最后手机库存
System.out.println(item);
}
}
}
我理解这题考察我们试用Map内嵌Map的使用
写回答
1回答
-
彭彭老师
2025-02-20
没有问题
00
相似问题
7、图书管理,请彭彭老师检查,万分感谢!
回答 1
3、枚举类的应用,请彭彭老师检查,感谢!
回答 1