查看“Java任务:用集合和接口实现商品库”的源代码
←
Java任务:用集合和接口实现商品库
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==实训目的== 用HashMap重新仓库类Store,实现增删查改功能。 ===map接口常用方法=== <nowiki>方法名 说 明 Object put(Object key,Object value) 插入新的对象,并用key作为其键字 void putAll(Map t) 将另一个Map中的所有对象复制进来 Set entrySet() 返回映射中的关键字-值对的集合 Set keySet() 返回映射中所有关键字的集合 Collection values() 返回映射中所有值的集合 Object remove(Object key) 删除指定的对象 Object get(Object key) 获取与key相联系的对象 boolean containsKey(Object key) 判断是否包含指定的键值 Boolean containsValue(Object value) 判断是否包含指定的对象</nowiki> ==分步代码== ===构造Store类=== <nowiki> public class Store { private HashMap<String,Goods> gmap; private int num ; public Store() { num = 3; gmap = new HashMap<>(); }</nowiki> ===编写数量方法=== <nowiki>public int getNum() { return this.gmap.size(); }</nowiki> ===编写添加功能=== <nowiki>public void add(String id,Goods g) { this.gmap.put(id, g); }</nowiki> ===编写删除功能=== <nowiki>public void delete(String id) { //补充你的代码 }</nowiki> ===编写查询功能=== <nowiki> public String find(String id) {//查询某一商品 //补充你的代码 }</nowiki> ===编写查询所有功能=== <nowiki>public String findAll() {//查询所有商品 String total=""; Set<String> keys = this.gmap.keySet(); System.out.println("编号\t餐品名"); System.out.println("***********************"); //遍历keyset,通过map.get(key)方法获得value的值 for (String key : keys) { total +=(key + "\t" + this.gmap.get(key)+"\n"); } return total; }</nowiki> ===编写修改商品功能=== <nowiki>public void change(String id,Goods g) { this.gmap.put(id, g); }</nowiki> ===编写调用代码=== main方法中: <nowiki> public static void main(String[] args) { // TODO Auto-generated method stub Store s = new Store(); Goods g1= new Goods("山西刀削面",105.0f,30); Goods g2= new Noodles("兰州牛肉面",15.0f,20); Goods g3= new Rice("黄焖鸡米饭",12.0f,30,false); //添加元素 System.out.println("执行添加餐品操作..."); s.add("01", g1); s.add("02", g2); s.add("03", g3); System.out.println("仓库的商品总数为:"+s.getNum()); System.out.println("以下为商品详情信息:"); System.out.println(s.toString()); //删除元素 System.out.println("执行删除餐品操作..."); s.delete("02"); //查找所有商品 String d1 = s.findAll(); System.out.println("删除后商品信息为:"); System.out.println(d1); //查找某一商品 String id="02"; String d2= s.find(id); System.out.println(id+"号商品详情为:"); System.out.println(d2); }</nowiki> ==全部代码== <nowiki> package entity; import java.util.*; public class Store { private HashMap<String,Goods> gmap; private int num ; public Store() { num = 3; gmap = new HashMap<>(); } public int getNum() { return this.gmap.size(); } public void add(String id,Goods g) { this.gmap.put(id, g); } public void delete(String id) { } public String find(String id) {//查询某一商品 } public String findAll() {//查询所有商品 String total=""; Set<String> keys = this.gmap.keySet(); System.out.println("编号\t餐品名"); System.out.println("***********************"); //遍历keyset,通过map.get(key)方法获得value的值 for (String key : keys) { total +=(key + "\t" + this.gmap.get(key)+"\n"); } return total; } public void change(String id,Goods g) { this.gmap.put(id, g); } @Override public String toString() { String total=""; Set<String> keys = this.gmap.keySet(); System.out.println("编号\t餐品名"); System.out.println("***********************"); //遍历keyset,通过map.get(key)方法获得value的值 for (String key : keys) { total +=(key + "\t" + this.gmap.get(key)+"\n"); } return total; } public static void main(String[] args) { // TODO Auto-generated method stub Store s = new Store(); Goods g1= new Goods("山西刀削面",105.0f,30); Goods g2= new Noodles("兰州牛肉面",15.0f,20); Goods g3= new Rice("黄焖鸡米饭",12.0f,30,false); //添加元素 System.out.println("执行添加餐品操作..."); s.add("01", g1); s.add("02", g2); s.add("03", g3); System.out.println("仓库的商品总数为:"+s.getNum()); System.out.println("以下为商品详情信息:"); System.out.println(s.toString()); //删除元素 System.out.println("执行删除餐品操作..."); s.delete("02"); //查找所有商品 String d1 = s.findAll(); System.out.println("删除后商品信息为:"); System.out.println(d1); //查找某一商品 String id="02"; String d2= s.find(id); System.out.println(id+"号商品详情为:"); System.out.println(d2); } } </nowiki>
返回至
Java任务:用集合和接口实现商品库
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息