Java学习(88)Java集合——案例:商品信息管理(HashMap增删改查)
创始人
2024-05-12 04:31:24
0

Java集合——案例:商品信息管理(HashMap增删改查)

  • 需求分析
  • 分析商品信息类(属性、方法)
  • 商品类的属性、构造方法和重写toString()方法
  • 商品信息添加,输出商品信息
  • 商品信息添加优化(判断商品编号id是否存在)、数据类型异常处理
  • 商品信息管理完整代码

需求分析

(1) 使用HashMap对商品信息进行管理,其中key为商品编号,value为商品对象
(2) 对HashMap中的商品信息进行增、删、改、查操作

分析商品信息类(属性、方法)

(1) 属性
a. 商品编号:id
b. 商品名称:name
c. 商品价格:price
(2) 方法
a. 构造方法
b. 获取和设置属性值的方法
c. 其他方法

商品类的属性、构造方法和重写toString()方法

public class Goods {private String id;//商品编号private String name;//商品名称private double price;//商品价格//构造方法public Goods(String id,String name,double price){this.id=id;this.name=name;this.price=price;}//getter和setter方法public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public String toString(){return "商品编号:"+id+",商品名称:"+name+",商品价格:"+price;}
}

注:Scanner在java.util包里。

商品信息添加,输出商品信息

		Scanner console = new Scanner(System.in);// 定义HashMap对象Map goodsMap = new HashMap();System.out.println("请输入三条商品信息:");int i = 0;while (i < 3) {System.out.println("请输入第" + (i + 1) + "条商品信息:");System.out.println("请输入商品编号:");String goodsId = console.next();// 判断商品编号id是否存在if (goodsMap.containsKey(goodsId)) {System.out.println("该商品编号已经存在!请重新输入!");continue;}System.out.println("请输入商品名称:");String goodsName = console.next();System.out.println("请输入商品价格:");Goods goods = new Goods(goodsId, goodsName, goodsPrice);// 将商品信息添加到HashMap中goodsMap.put(goodsId, goods);i++;}// 遍历Map,输出商品信息System.out.println("商品的全部信息为:");Iterator itGoods = goodsMap.values().iterator();while (itGoods.hasNext()) {System.out.println(itGoods.next());}}

商品信息添加优化(判断商品编号id是否存在)、数据类型异常处理

map.containsKey(key)

		// 定义HashMap对象Map goodsMap = new HashMap();System.out.println("请输入三条商品信息:");int i = 0;while (i < 3) {System.out.println("请输入第" + (i + 1) + "条商品信息:");System.out.println("请输入商品编号:");String goodsId = console.next();// 判断商品编号id是否存在if (goodsMap.containsKey(goodsId)) {System.out.println("该商品编号已经存在!请重新输入!");continue;}System.out.println("请输入商品名称:");String goodsName = console.next();System.out.println("请输入商品价格:");double goodsPrice = 0;try {goodsPrice = console.nextDouble();} catch (java.util.InputMismatchException e) {System.out.println("商品价格的格式不正确,请输入数值型数据!");console.next();continue;}Goods goods = new Goods(goodsId, goodsName, goodsPrice);

商品信息管理完整代码

package com.study.set;import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;public class GoodsTest {public static void main(String[] args) {Scanner console = new Scanner(System.in);// 定义HashMap对象Map goodsMap = new HashMap();System.out.println("请输入三条商品信息:");int i = 0;while (i < 3) {System.out.println("请输入第" + (i + 1) + "条商品信息:");System.out.println("请输入商品编号:");String goodsId = console.next();// 判断商品编号id是否存在if (goodsMap.containsKey(goodsId)) {System.out.println("该商品编号已经存在!请重新输入!");continue;}System.out.println("请输入商品名称:");String goodsName = console.next();System.out.println("请输入商品价格:");double goodsPrice = 0;try {goodsPrice = console.nextDouble();} catch (java.util.InputMismatchException e) {System.out.println("商品价格的格式不正确,请输入数值型数据!");console.next();continue;}Goods goods = new Goods(goodsId, goodsName, goodsPrice);// 将商品信息添加到HashMap中goodsMap.put(goodsId, goods);i++;}// 遍历Map,输出商品信息System.out.println("商品的全部信息为:");Iterator itGoods = goodsMap.values().iterator();while (itGoods.hasNext()) {System.out.println(itGoods.next());}}
}

运行结果:

请输入三条商品信息:
请输入第1条商品信息:
请输入商品编号:
s00001
请输入商品名称:
冰箱
请输入商品价格:
sss
商品价格的格式不正确,请输入数值型数据!
请输入第1条商品信息:
请输入商品编号:
s00001
请输入商品名称:
冰箱
请输入商品价格:
3000
请输入第2条商品信息:
请输入商品编号:
s00002
请输入商品名称:
手机
请输入商品价格:
2000
请输入第3条商品信息:
请输入商品编号:
s00003
请输入商品名称:
电视机
请输入商品价格:
5000
商品的全部信息为:
商品编号:s00001,商品名称:冰箱,商品价格:3000.0
商品编号:s00002,商品名称:手机,商品价格:2000.0
商品编号:s00003,商品名称:电视机,商品价格:5000.0

相关内容

热门资讯

福建省药监局发布2025年服务... 转自:海峡消费报6月30日上午,福建省药监局召开新闻发布会,向社会各界通报近年省药监局服务医药产业发...
关于“支持创新药高质量发展的若... 转自:氨基观察2025年7月1日上午,为进一步完善全链条支持创新药发展举措,推动创新药高质量发展,更...
北京住房公积金管理中心:公积金... 人民财讯7月1日电,北京住房公积金管理中心公众号消息,2024—2025年度(2024年7月1日—2...
燃擎岚岛 2025平潭国际赛车... 来源:中国网6月27日-29日,2025平潭国际赛车嘉年华将在平潭如意湖国际城市赛道正式启幕,作为平...
七旬老人花175万投资“洛阳古... 来源:案件聚焦 中央领导都关心的“洛阳隋唐城遗址”开发项目,有投资保障的政府债券,年化收益10.5%...
众生药业:获得环孢素滴眼液(I... 转自:财联社【众生药业:获得环孢素滴眼液(III)及复方托吡卡胺滴眼液《药品注册证书》】财联社7月1...
物产金轮(002722.SZ)... 格隆汇7月1日丨物产金轮(002722.SZ)公布,2025年6月,公司未进行回购。截至2025年6...
沪皖协作探新路:从“单向帮扶”... 转自:上观新闻近日,安徽赛富乐斯半导体科技有限公司R系列芯片产线正式投产,国内量产Micro-LED...
“三大纪律八项注意”——始终同... 转自:中央纪委国家监委网站1935年10月,红一方面军的战士征得老乡同意后,在延安吴起县倒水湾村驻扎...
亚普股份:累计回购10万股 亚普股份(SH 603013,收盘价:17.29元)7月1日晚间发布公告称,截至2025年6月底,公...