5-2添加商品的方法不会写了。。。
package com.imooc.shopping;
import java.util.*;
public class ShoppingCart {
private Map<String, GoodsInCart> shoppingCart;
public ShoppingCart(){
}
public ShoppingCart(Map<String, GoodsInCart> shoppingCart) {
this.setShoppingCart(shoppingCart);
}
//添加商品到购物车
public void addGoodsToCart(GoodsManage gm) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入要购买的商品编号:");
String input=scan.next();
Goods goods1=new Goods();
Set<Goods> goods = gm.getGoodsSet();
boolean flag = false;
for(Goods gInfo:goods) {
if(gInfo.getGoodsId().equals(input)) {
flag = true;
if(flag) {
System.out.println("请输入购买商品的数量:");
int num = scan.nextInt();
GoodsInCart gic = new GoodsInCart(goods1, num);
shoppingCart.put(input, gic);
}else {
System.out.println("无此商品编号!请重新输入");
}
break;
}
}
}
//修改购物车中的商品数量
public void updateNumInCart() {
}
//显示购物车中的商品数量
public void displayAllInCart() {
Set<Map.Entry<String, GoodsInCart>> set=shoppingCart.entrySet();
for (Map.Entry<String, GoodsInCart>g:set){
if (set.size()==0){
System.out.println("购物车为空!");
}
System.out.println(g.getValue().getGoods());
}
}
//结算
public void settleAccounts() {
}
public Map<String, GoodsInCart> getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) {
this.shoppingCart = shoppingCart;
}
}
-----------------------------------------------------------------------
package com.imooc.shopping;
public class Goods {
private String goodsId;// 商品编号
private String goodsName;// 商品名称
private double price;// 商品价格
private String goodsDesp;// 商品描述
// 无参构造方法
public Goods() {
}
// 赋值构造方法
public Goods(String goodsId, String goodsName, double price, String goodsDesp) {
super();
this.setGoodsId(goodsId);
this.setGoodsName(goodsName);
this.setPrice(price);
this.setGoodsDesp(goodsDesp);
}
// 公共getter、setter方法
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
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 String toString() {
return "商品信息 [编号=" + goodsId + ", 名称=" + goodsName + ", 价格=" + price + ", 描述=" + goodsDesp + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((goodsDesp == null) ? 0 : goodsDesp.hashCode());
result = prime * result + ((goodsId == null) ? 0 : goodsId.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.getClass() == Goods.class) {
Goods good = (Goods) obj;
return (good.getGoodsId() == goodsId) && (good.getGoodsName().equals(goodsName))
&& (good.getPrice()) == price && (good.getGoodsDesp().equals(goodsDesp));
}
return false;
}
}
--------------------------------------------------------------
package com.imooc.shopping;
public class GoodsInCart {
private Goods goods;
private int num;
public GoodsInCart(Goods goods, int num) {
this.setGoods(goods);
this.setNum(num);
}
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;
}
}
---------------------------------------------------------
package com.imooc.shopping;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GoodsManage {
Set<Goods> goodsSet = new HashSet<Goods>();
public GoodsManage() {
}
public GoodsManage(Set<Goods> goodsSet) {
this.setGoodsSet(goodsSet);
}
public Set<Goods> getGoodsSet() {
return goodsSet;
}
public void setGoodsSet(Set<Goods> goodsSet) {
this.goodsSet = goodsSet;
}
// 商品信息导入
public void importGoods() {
Goods good1 = new Goods("goods004", "手机", 2300, "android手机");
Goods good2 = new Goods("goods002", "饮水机", 299, "带净化功能的饮水机");
Goods good3 = new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本电脑");
Goods good4 = new Goods("goods001", "水杯", 56, "不锈钢水杯");
goodsSet.add(good1);
goodsSet.add(good2);
goodsSet.add(good3);
goodsSet.add(good4);
}
// 显示所有商品信息
public void displayAllGoods() {
Iterator<Goods> it = goodsSet.iterator();
if (goodsSet.size() == 0) {
System.out.println("还没有商品,请先导入商品信息!");
} else {
System.out.println("显示所有商品信息:");
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
}
--------------------------------------------------------
package com.imooc.shopping;
import java.util.*;
public class ShoppingCart {
private Map<String, GoodsInCart> shoppingCart;
public ShoppingCart(){
}
public ShoppingCart(Map<String, GoodsInCart> shoppingCart) {
this.setShoppingCart(shoppingCart);
}
//添加商品到购物车
public void addGoodsToCart(GoodsManage gm) {
Scanner scan=new Scanner(System.in);
System.out.println("请输入要购买的商品编号:");
String input=scan.next();
Goods goods1=new Goods();
Set<Goods> goods = gm.getGoodsSet();
boolean flag = false;
for(Goods gInfo:goods) {
if(gInfo.getGoodsId().equals(input)) {
flag = true;
if(flag) {
System.out.println("请输入购买商品的数量:");
int num = scan.nextInt();
GoodsInCart gic = new GoodsInCart(goods1, num);
shoppingCart.put(input, gic);
}else {
System.out.println("无此商品编号!请重新输入");
}
break;
}
}
}
//修改购物车中的商品数量
public void updateNumInCart() {
}
//显示购物车中的商品数量
public void displayAllInCart() {
Set<Map.Entry<String, GoodsInCart>> set=shoppingCart.entrySet();
for (Map.Entry<String, GoodsInCart>g:set){
if (set.size()==0){
System.out.println("购物车为空!");
}
System.out.println(g.getValue().getGoods());
}
}
//结算
public void settleAccounts() {
}
public Map<String, GoodsInCart> getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) {
this.shoppingCart = shoppingCart;
}
}
------------------------------------------------------
package com.imooc.shopping;
public class Show {
public void Menu1() {
System.out.println("*********************************");
System.out.println("**主菜单**");
System.out.println("1--商品管理");
System.out.println("2--购物车");
System.out.println("0--退出");
System.out.println("*********************************");
System.out.println("请输入对应的数字进行操作:");
}
public void Menu2() {
System.out.println("*********************************");
System.out.println("**商品管理**");
System.out.println("1--商品信息导入");
System.out.println("2--显示所有商品信息");
System.out.println("9--返回上一级菜单");
System.out.println("*********************************");
System.out.println("请输入对应的数字对商品进行管理:");
}
public void Menu3() {
System.out.println("*********************************");
System.out.println("**购物车管理**");
System.out.println("1--添加商品到购物车");
System.out.println("2--修改购物车中的商品数量");
System.out.println("3--显示购物车中的所有商品信息");
System.out.println("4--结算");
System.out.println("9--返回上一级菜单");
System.out.println("**********************************");
System.out.println("请输入对应的数字对购物车进行管理");
}
}
正在回答
同学你好,添加商品到购物车的方法可参考如下思路:
在addGoodsToCart()方法中,先使用gm.getGoodsSet().isEmpty()判断是否导入商品,如果没有,则提示还没导入商品信息。如果导入了商品信息,则提示请输入要添加的商品编号。输入商品编号后定义Goods类对象goods并使其为null,定义boolean变量flag赋值为false;然后循环遍历goosSet,判断商品编号是否存在,如果存在则将flag赋值true,并将查到的商品赋值给goods,使用break调出循环。在循环外判断flag,如果为true则表示商品编号存在,则提示输入商品数量,并将其添加到购物车中,如果不存在,则提示商品编号不存在,请重新输入。代码可参考如下:
如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星