请老师帮看一下。
public class Goods { private String goodId;//编号 private String goodsName;//名称 private double price;//价格 private String goodsDesp;//描述 //构造方法 public Goods(String goodId, String goodsName, double price, String goodsDesp) { super(); this.goodId = goodId; this.goodsName = goodsName; this.price = price; this.goodsDesp = goodsDesp; } //getter和setter方法 public String getGoodId() { return goodId; } public void setGoodId(String goodId) { this.goodId = goodId; } public String getGoodsName() { return goodsName; } public void setGoodsName(String goodsName) { this.goodsName = goodsName; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this.goodsDesp = goodsDesp; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((goodId == null) ? 0 : goodId.hashCode()); result = prime * result + ((goodsDesp == null) ? 0 : goodsDesp.hashCode()); result = prime * result + ((goodsName == null) ? 0 : goodsName.hashCode()); long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Goods other = (Goods) obj; if (goodId == null) { if (other.goodId != null) return false; } else if (!goodId.equals(other.goodId)) return false; if (goodsDesp == null) { if (other.goodsDesp != null) return false; } else if (!goodsDesp.equals(other.goodsDesp)) return false; if (goodsName == null) { if (other.goodsName != null) return false; } else if (!goodsName.equals(other.goodsName)) return false; if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) return false; return true; } @Override public String toString() { return "Goods [商品编号:" + goodId + ", 商品名称:" + goodsName + ", 商品价格:" + price + ", 商品描述:" + goodsDesp + "]"; } } public class GoodsInCart { private Goods goods;//商品信息 private int num; //商品数量 //构造方法 public GoodsInCart(Goods goods, int num) { super(); this.goods = goods; this.num = num; } //getter和setter方法 public Goods getGoods() { return goods; } public void setGoods(Goods goods) { this.goods = goods; } public int getNum() { return num; } public void setNum(int num) { this.num = num; } } public class GoodsManage { private Set<Goods> goodsSet = new HashSet<Goods>();// 存放商品的容器 // 构造方法 public GoodsManage() { } public GoodsManage(Set<Goods> goodsSet) { super(); this.goodsSet = goodsSet; } // getter和setter方法 public Set<Goods> getGoodsSet() { return goodsSet; } public void setGoodsSet(Set<Goods> goodsSet) { this.goodsSet = goodsSet; } // 商品信息导入 public void importGoods() { goodsSet.add(new Goods("goods004", "手机", 2300, "Android手机")); goodsSet.add(new Goods("goods002", "饮水机", 299, "带净化功能的饮水机")); goodsSet.add(new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本电脑")); goodsSet.add(new Goods("goods001", "水杯", 56, "不锈钢水杯")); this.setGoodsSet(goodsSet); System.out.println("商品讯息导入"); System.out.println("导入成功!!!"); } // 显示所有商品信息 public void displayAllGoods() { System.out.println("显示所有商品信息\n所有商品信息为:"); Iterator it=goodsSet.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } public class ShoppingCart { // 属性:购物车 Map<String, GoodsInCart> shoppingCart = new HashMap<String, GoodsInCart>(); // 构造方法 public ShoppingCart(Map<String, GoodsInCart> shoppingCart) { super(); this.shoppingCart = shoppingCart; } // getter和setter方法 public Map<String, GoodsInCart> getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) { this.shoppingCart = shoppingCart; } Scanner s=new Scanner(System.in); // 添加商品到购物车 public void addGoodsToCart(GoodsManage gm) { System.out.println("添加商品到购物车"); gm.displayAllGoods(); Iterator<Goods> it=gm.getGoodsSet().iterator(); while(true) { try { System.out.println("请输入要添加的商品编号:"); String id=s.next(); GoodsInCart gic=new GoodsInCart(); while(it.hasNext()) { Goods n=it.next(); if(id.equals(n.getGoodId())){ gic.setGoods(n); break; } } System.out.println("请输入要添加的商品数量:"); int num=s.nextInt(); gic.setNum(num); shoppingCart.put(id, gic); }catch(Exception e) { System.out.println("输入错误,请重新输入!"); continue; }break; } } // 修改购物车中的商品数量 public void updateNumInCart() { System.out.println("修改购物车中商品数量:"); Iterator<GoodsInCart> it=shoppingCart.values().iterator(); System.out.println("请输入要修改的商品编号:"); String id=s.next(); GoodsInCart gic=new GoodsInCart(); while(it.hasNext()) { if(id.equals(gic.getGoods().getGoodId())) { it.next(); break; } } System.out.println("请输入要修改的商品数量:"); int num=s.nextInt(); gic.setNum(num); shoppingCart.put(id, gic); } // 显示购物车中的所有商品 public void displayAllInCart() { System.out.println("显示购物车中所有商品信息"); if(shoppingCart.size() !=0) { Iterator<GoodsInCart> it=shoppingCart.values().iterator(); while(it.hasNext()) { GoodsInCart gic=it.next(); Goods g = gic.getGoods(); System.out.println("商品信息[编号:"+g.getGoodId()+",名称:"+g.getGoodsName()+",价格:" +g.getPrice()+",商品描述:"+g.getGoodsDesp()+",数量:"+gic.getNum()+"]"); } }else { System.out.println("购物车是空的哟,赶紧装满吧~"); } } // 结算 public void settleAccounts() { System.out.println("结算"); double sum=0; System.out.println("商品的总价为:"+sum); Iterator<GoodsInCart> it=shoppingCart.values().iterator(); while(it.hasNext()) { GoodsInCart gic = it.next(); Goods g=gic.getGoods(); System.out.println("商品信息[编号:"+g.getGoodId()+",名称:"+g.getGoodsName()+",价格:" +g.getPrice()+",商品描述:"+g.getGoodsDesp()+",数量:"+gic.getNum()+"]"); double sum1=g.getPrice()*gic.getNum(); sum=sum+sum1; } } } package com.imooc.cart; import java.util.Scanner; /** * 测试类 * @author xiaod * */ public class TestDemo { //商品管理 public void manageGoods() { System.out.println("======================"); System.out.println("*****商品管理*****"); System.out.println("1--商品信息导入"); System.out.println("2--显示所有商品讯息"); System.out.println("9--回上一层"); System.out.println("======================"); Scanner src = new Scanner(System.in); int num = src.nextInt(); GoodsManage goodsManage = new GoodsMaanage(); //根据商品编号执行操作 switch(num) { case 1: goodsManage.importGoods(); this.manageGoods(); break; case 2: goodsManage.displayAllGoods(); break; case 0: break; } } public static void main(String[] args) { TestDemo demo = new TestDemo(); //开始进入点 System.out.println("======================"); System.out.println("*****主菜单*****"); System.out.println("1--商品管理"); System.out.println("2--购物车"); System.out.println("0--退出"); System.out.println("======================"); System.out.println("*****请输入对应的数字操作*****"); Scanner src = new Scanner(System.in); int num = src.nextInt(); //根据编号执行操作 switch(num) { case 1: demo.manageGoods(); break; case 2: break; case 0: break; } } } 测试类和购物车类有错误呢。请老师详细解答,我仿写的。
131
收起
正在回答
3回答
ShoppingCart类缺少必要的包,
需要导入:
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.Scanner;
祝学习愉快~
相似问题
登录后可查看更多问答,登录/注册
Java零基础入门18
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星