救命呀老师!我想把购物车类里的《商品信息》和《购买数量》添加不进去购物车的信息类里
package com.imooc.shoop; public class Goods { //商品类 //属性:编号,名称,价格,描述 private String goodsId; private String goodsName; private double goodsPrice; private String goodsDesp; public Goods() { } public Goods(String goodsId,String goodsName,double goodsPrice,String goodsDesp) { setGoodsId(goodsId); setGoodsName(goodsName); setGoodsPrice(goodsPrice); setGoodsDesp(goodsDesp); } 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 getGoodsPrice() { return goodsPrice; } public void setGoodsPrice(double goodsPrice) { this.goodsPrice=goodsPrice; } public String getGoodsDesp() { return goodsDesp; } public void setGoodsDesp(String goodsDesp) { this.goodsDesp=goodsDesp; } public String toString() { return "商品信息[商品编号:"+goodsId+",商品名称:"+goodsName+",商品价格"+goodsPrice+",商品描述:"+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(goodsPrice); 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 goods=(Goods) obj; return goods.getGoodsId().equals(goodsId)&&goods.getGoodsName().equals(goodsName)&& goods.getGoodsPrice()==goodsPrice&&goods.getGoodsDesp().equals(goodsDesp); } return true; } } ================================================================================================ package com.imooc.shoop; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.omg.PortableServer.ImplicitActivationPolicyOperations; public class GoodsManage { // 商品管理类 // 属性:存放商品的容器goodsSet(HashSet集合,Set类型) private Set<Goods> goodsSet = new HashSet(); // 构造方法 public GoodsManage() { } public Set getGoodsSet() { return goodsSet; } public void setGoodsSet(Set goodsSet) { this.goodsSet = goodsSet; } // 商品信息导入 public void importGoods() { Goods goods1 = new Goods("goods001", "水杯", 56, "不锈钢水杯"); Goods goods2 = new Goods("goods002", "饮水机", 299, "带净化功能的饮水机"); Goods goods3 = new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本"); Goods goods4 = new Goods("goods004", "手机", 2300, "android手机"); goodsSet.add(goods1); goodsSet.add(goods2); goodsSet.add(goods3); goodsSet.add(goods4); System.out.println("商品信息导入"); System.out.println("导入成功!"); } // 显示所有商品信息 public void displayAllGoods() { System.out.println("所有商品信息为:"); Iterator<Goods> it = goodsSet.iterator(); while (it.hasNext()) { System.out.println(it.next()); } } public static void mian(String[] args) { } } ============================================================================================== package com.imooc.shoop; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; public class GoodsInCart { // 购物车中信息类(这里打算用HashMap集合来完成对象和购买数量的Key-Value来存储多条信息) // -商品信息(goods):Goods类型 -商品数量(num):int类型 private Goods goods; private int num; Map<Goods, Integer> goodsMap = new HashMap<Goods, Integer>(); public GoodsInCart() { } public GoodsInCart(Goods goods, int num) { goodsMap.put(goods, 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; } // 显示购物车中的所有信息 public void show() { System.out.println("显示购物车中的所有商品信息"); if (goodsMap.isEmpty()) { System.out.println("你的购物车空空如也,赶紧去添加商品吧!"); } else { Set<Entry<Goods, Integer>> set = goodsMap.entrySet(); for (Entry<Goods, Integer> entry : set) { System.out.println("商品名称:" + entry.getKey().getGoodsName() + ",商品价格:" + entry.getKey().getGoodsPrice() + ",商品描述:" + entry.getKey().getGoodsDesp() + ",数量:" + entry.getValue()); } } } // 修改购物车中的商品数量 public void modify() { if (goodsMap.isEmpty()) { System.out.println("您的购物车空空如也,赶紧去添加商品吧!"); } else { System.out.println("修改购物车中的商品数量"); System.out.println("请输入要修改的商品编号"); Scanner console = new Scanner(System.in); String strId = console.next(); Set<Entry<Goods, Integer>> set = goodsMap.entrySet(); for (Entry<Goods, Integer> entry : set) { if (entry.getKey().getGoodsId().equals(strId)) { System.out.println("请输入新的商品数量:"); int intNum; try { intNum = console.nextInt(); } catch (InputMismatchException e) { System.out.println("你输入的不是正整数请重新输入!"); return; } if (intNum > 0) { int i = entry.getValue(); i = intNum; break; } else if (intNum == 0) { goodsMap.remove(entry); break; } } else { System.out.println("你输入的商品编号没在你的购物车中,请核对后在输入!"); } } } } // 结算:这里想用goodsMap集合的把Integer对象转换成int然后调用来求值 } =========================================================================================== package com.imooc.shoop; import java.util.HashMap; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Map; import java.util.Scanner; public class ShoppingCart { // 购物车类 // 属性:购物车(shoppingCart):Map类型 /* * -Key为String类型,商品编号 -Value为GoodsInCart类型,购物车中的信息 */ private Map shoppingCart = new HashMap(); int i; public ShoppingCart() { } public ShoppingCart(Map shoppingCart) { } public Map getShoppingCart() { return shoppingCart; } public void setShoppingCart(Map shoppingCart) { this.shoppingCart = shoppingCart; } Scanner console = new Scanner(System.in); public void addGoodsToCart(GoodsManage gm) { Goods goods = null; boolean flag = false; int i = 0; GoodsInCart goodsInCart = new GoodsInCart(); for (;;) { System.out.println("添加商品到购物车"); gm.importGoods(); gm.displayAllGoods(); System.out.println("请输入要添加的商品编号:"); String stId = console.next(); Iterator<Goods> it = gm.getGoodsSet().iterator(); while (it.hasNext()) { goods = it.next(); flag = goods.getGoodsId().equals(stId); if (flag) { break; } } if (flag) { shoppingCart.put(stId, goods); System.out.println("请输入要添加的商品数量:"); try { i = console.nextInt(); // 这里数据数据添加不进去GoodInCart类里的goodsMap (HashMap)集合里面去 goodsInCart.goodsMap.put(goods, i); break; } catch (InputMismatchException e) { System.out.println("你输入的格式不正确!不是整数!请重新输入!"); console.next(); continue; } } else { System.out.println("暂时没有你提供商品编号!请重新输入!"); } } } } =============================================================================================== package com.imooc.shoop; import java.util.InputMismatchException; import java.util.Scanner; public class TestDemo { static GoodsManage goodsManage = new GoodsManage(); public static void main(String[] args) { // 根据需求分析实现主流程(单独定义一个方法把要完成的逻辑操作放单独定义的方法里面在里面) Scanner console = new Scanner(System.in); for (;;) { System.out.println("******************************************************"); System.out.println(" *****主菜单*****"); System.out.println(" 1--商品管理"); System.out.println(" 2--购物车"); System.out.println(" 0--退出"); System.out.println("******************************************************"); System.out.println("请输入对应数字进行操作:"); int i = 0; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println("输入格式不正确,请输入主菜单对应的选项数字!请重新输入!"); console.next(); continue; } if (i == 1) { commodityManagement(); } else if (i == 2) { shoppingCart(); } else if (i == 0) { break; } else { System.out.println("输入有误,请输入主菜单对应的选项的数字!请重新输入!"); } } } // 商品管理 public static void commodityManagement() { Scanner console = new Scanner(System.in); for (;;) { System.out.println("******************************************************"); System.out.println(" *****商品管理*****"); System.out.println(" 1--商品信息导入"); System.out.println(" 2--显示所有商品信息"); System.out.println(" 9--返回上一级菜单"); System.out.println("******************************************************"); System.out.println("请输入对应数字进行操作:"); int i = 0; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println("输入格式不正确,请输入主菜单对应的选项数字!请重新输入!"); console.next(); continue; } if (i == 1) { goodsManage.importGoods(); } else if (i == 2) { } else if (i == 9) { break; } else { System.out.println("输入有误,请输入主菜单对应的选项的数字!请重新输入!"); } } } // 购物车管理 public static void shoppingCart() { Scanner console = new Scanner(System.in); for (;;) { 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("请输入对应数字进行操作:"); int i = 0; try { i = console.nextInt(); } catch (InputMismatchException e) { System.out.println("输入格式不正确,请输入主菜单对应的选项数字!请重新输入!"); console.next(); continue; } ShoppingCart shoppingCart = new ShoppingCart(); GoodsInCart goodsInCart = new GoodsInCart(); if (i == 1) { shoppingCart.addGoodsToCart(goodsManage); } else if (i == 2) { goodsInCart.modify(); } else if (i == 3) { goodsInCart.show(); } else if (i == 4) { goodsManage.displayAllGoods(); } else if (i == 9) { break; } else { System.out.println("输入有误,请输入主菜单对应的选项的数字!请重新输入!"); } } } }
正在回答
请参见http://class.imooc.com/course/qadetail/63509
package com.imooc.shoop;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.Map.Entry;
public class ShoppingCart {
// 购物车类
// 属性:购物车(shoppingCart):Map类型
/*???????????????????????????????????????
* 在ShoppingCart里的添加商品方法里shoopingCart集合是有数据的可是到了修改购
* 物车中的商品数量和显示购物车中所有商品信息那里判断集合又是空的
*???????????????????????????????????????
* -Key为String类型,商品编号 -Value为GoodsInCart类型,购物车中的信息
*/
private Map<String, GoodsInCart> shoppingCart = new HashMap<String, GoodsInCart>();
int i;
public ShoppingCart() {
}
public ShoppingCart(Map shoppingCart) {
setShoppingCart(shoppingCart);
}
public Map getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(Map shoppingCart) {
this.shoppingCart = shoppingCart;
}
Scanner console = new Scanner(System.in);
// 添加商品到购物车
public void addGoodsToCart(GoodsManage gm) {
Goods goods = null;
boolean flag = false;
int i = 0;
for (;;) {
System.out.println("添加商品到购物车");
gm.importGoods();
gm.displayAllGoods();
System.out.println("请输入要添加的商品编号:");
String stId = console.next();
Iterator<Goods> it = gm.getGoodsSet().iterator();
while (it.hasNext()) {
goods = it.next();
flag = goods.getGoodsId().equals(stId);
if (flag) {
break;
}
}
if (flag) {
System.out.println("请输入要添加的商品数量:");
try {
i = console.nextInt();
// 这里数据数据添不进去shoppingCart (HashMap)集合里面去,可后面检测集合是空的
GoodsInCart gi = new GoodsInCart(goods, i);
shoppingCart.put(stId, gi);
System.out.println("成功添加商品到购物车!!");
Set<Entry<String,GoodsInCart>>s=shoppingCart.entrySet();
for(Entry<String,GoodsInCart> en:s) {
System.out.println(en);
}
break;
} catch (InputMismatchException e) {
System.out.println("你输入的格式不正确!不是整数!请重新输入!");
console.next();
continue;
}
} else {
System.out.println("暂时没有你提供商品编号!请重新输入!");
}
}
}
// 修改购物车中的商品数量
public void modify() {
//检测集合为空的。
if (shoppingCart.isEmpty()) {
System.out.println("您的购物车空空如也,赶紧去添加商品吧!");
} else {
System.out.println("修改购物车中的商品数量");
System.out.println("请输入要修改的商品编号");
Scanner console = new Scanner(System.in);
String strId = console.next();
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry<String, GoodsInCart> entry : set) {
if (entry.getKey().equals(strId)) {
System.out.println("请输入新的商品数量:");
try {
i = console.nextInt();
} catch (InputMismatchException e) {
System.out.println("你输入的不是正整数请重新输入!");
return;
}
if (i > 0) {
entry.getValue().setNum(i);
break;
// 判断用户输入的是否为0 为0时候删除该商品
} else if (i == 0) {
shoppingCart.remove(entry);
break;
}
} else {
System.out.println("你输入的商品编号没在你的购物车中,请核对后在输入!");
}
}
}
}
// 显示购物车中的所有信息
public void show() {
System.out.println("显示购物车中的所有商品信息");
System.out.println(shoppingCart.isEmpty());
//这里也检测出集合是空的
if (shoppingCart.isEmpty()) {
System.out.println("你的购物车空空如也,赶紧去添加商品吧!");
} else {
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry entry : set) {
System.out.println(entry.getValue());
}
}
}
//结算
public void Settlement() {
System.out.println("结算购物车中的所有商品:");
System.out.println(shoppingCart.isEmpty());
double sum=0;
//这里也检测出集合是空的
if (shoppingCart.isEmpty()) {
System.out.println("你的购物车里没有商品.........");
} else {
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry <String,GoodsInCart>entry : set) {
double d1=entry.getValue().getNum()*entry.getValue().getGoods().getGoodsPrice();
sum=sum+d1;
}
}
show();
System.out.println("你需要付款" + sum + "元");
shoppingCart.clear();
}
}
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星