老师,请帮我看一下,就是加入购物车后就出错了
package com.goodsdemo;//商品类
public class Goods {
//属性:商品编号goodsId、商品名称goodsName、商品价格price、商品描述goodsDesp
private String goodsId;
private String goodsName;
private double price;
private String goodsDesp;
public Goods(String goodsId, String goodsName, double price, String goodsDesp) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.price = price;
this.goodsDesp = 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 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 + ((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 goods=(Goods)obj;
return goods.getGoodsId().equals(getGoodsId())&&goods.getGoodsName().equals(getGoodsName());
}
return false;
}
@Override
public String toString() {
return "商品信息 [编号:" + goodsId + ", 名称:" + goodsName + ", 价格:" + price + ", 描述"
+ goodsDesp + "]";
}
}
*************************************************************************************************
package com.goodsdemo;//商品管理类
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class GoodsManage {
//属性:存放商品的容器goodsSet
private Set goodsSet;
//构造方法并对属性赋值
public GoodsManage() {
}
public GoodsManage(Set goodsSet) {
this.goodsSet = goodsSet;
}
//get/set方法
public Set getGoodsSet() {
return goodsSet;
}
public void setGoodsSet(Set goodsSet) {
this.goodsSet = goodsSet;
}
//商品信息导入方法
Set<Goods> set=new HashSet<Goods>();
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手机");
set.add(goods1);
set.add(goods2);
set.add(goods3);
set.add(goods4);
System.out.println("商品导入成功!");
}
//显示所有商品信息
public void displayAllGoods() {
Iterator<Goods> it=set.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
**************************************************************************************************
package com.goodsdemo;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class ShoppingCart {//购物车类
//成员属性:购物车
private Map<String,GoodsInCart> shoppingCart;
//构造方法
public ShoppingCart() {}
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 sc = new Scanner(System.in);
//添加商品到购物车
public void addGoodsToCart(GoodsManage gm) {
Set<Goods> goodsSet = gm.getGoodsSet();
boolean flag = false;
if (goodsSet.isEmpty()) {
System.out.println("还没有商品,记得导入商品信息哦");
} else {
System.out.println("可以导入的商品有:");
gm.displayAllGoods();
System.out.println("请输入要添加的商品");
String key = sc.next();
Iterator<Goods> itGoodsSet = gm.getGoodsSet().iterator();
int num = 0;
Goods addgoods=null;
while (itGoodsSet.hasNext()) {
Goods gs=itGoodsSet.next();
if (gs.getGoodsId().equals(key)) {
addgoods = gs;
flag = true;
System.out.println("请输入要添加商品的数量");
try {
num = sc.nextInt();
} catch (Exception e) {
System.out.println("输入有误!请输入非负整数");
e.printStackTrace();
continue;
}
GoodsInCart inCart = new GoodsInCart(addgoods, num);
this.getShoppingCart().put(key, inCart);
System.out.println("商品添加成功!");
}
}
if (flag == false) {
System.out.println("输入的商品信息有误!请重新输入!");
}
}
}
//修改购物车中的商品数量
public void updateNumInCart() {
Set<String> set=shoppingCart.keySet();
String str1 = null;
int num = 0;
System.out.println("请输入要修改的商品编号:");
str1 = sc.next();
for(String str:set) {
if(str1.equals(sc.next())) {
System.out.println("请输入要修改的数量");
num = sc.nextInt();
if(num == 0) {
set.remove(str1);
System.out.println("商品数量为零,删除该商品。");
}else {
shoppingCart.get(str1).setNum(num);
}
}else {
System.out.println("购物车中没有该商品!");
}
}
}
//显示购物车中的所有商品
public void displayAllInCart() {
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
if (set.size() == 0) {
System.out.println("您还没有添加任何商品");
} else {
for (Entry<String, GoodsInCart> entry : set) {
System.out.println("商品信息:"+entry.getValue().getGoods() +"数量:"+ entry.getValue().getNum() );
}
}
}
//结算
public void settleAccounts() {
int sum=0;
Set<Entry<String, GoodsInCart>> set = shoppingCart.entrySet();
for (Entry<String, GoodsInCart> entry : set) {
sum+=(entry.getValue().getGoods().getPrice()*entry.getValue().getNum());
}
System.out.println("总金额为:"+sum);
shoppingCart.clear();
}
}
*********************************************************************************************
package com.goodsdemo;//购物车中商品信息管理类
public class GoodsInCart {
//属性:商品信息goods、商品数量num
private Goods goods;
private int num;
public GoodsInCart(Goods goods, int num) {
this.goods = goods;
this.num = 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.goodsdemo;//测试类
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Scanner;
public class TestDemo {
public void mainInfo() {
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 info() {
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 shoppinginfo() {
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("请输入对应的数字对购物车进行管理:");
}
static GoodsManage gm = new GoodsManage();
static ShoppingCart cart = new ShoppingCart();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean flag = true;
TestDemo test = new TestDemo();
while (flag) {
// 显示主菜单
test.mainInfo();
// 输入对应数字进行操作
int num = sc.nextInt();
// 进入商品管理
if (num == 1) {
boolean f1 = true;
while (f1) {
test.info();
int m = sc.nextInt();
switch(m) {
case 1:
gm.importGoods();// 导入商品信息
continue;
case 2:
gm.displayAllGoods();// 显示所有商品信息
continue;
case 9:
f1 = false;
continue;
default:
System.out.println("输入错误数字,请重新出入!");
m = sc.nextInt();
continue;
}
}
}
// 进入购物车管理
if (num == 2) {
boolean f2 = true;
while (f2) {
test.shoppinginfo();
int n = sc.nextInt();
switch(n) {
case 1:
cart.addGoodsToCart(gm);
continue;
case 2:
cart.updateNumInCart();
continue;
case 3:
cart.displayAllInCart();
continue;
case 4:
cart.settleAccounts();
continue;
case 9:
f2 = false;
continue;
default:
System.out.println("输入错误数字,请重新出入!");
n = sc.nextInt();
continue;
}
}
}
if (num == 0) {
flag = false;
System.out.println("结束!");
break;
}else {
System.out.println("输入错误,请重新输入!");
continue;
}
}
}
}
正在回答 回答被采纳积分+1
public void updateNumInCart() {
System.out.println("请输入要修改的商品编号:");
String key = null;
key = sc.next();
Set<Entry<String, GoodsInCart>> cartset = this.getShoppingCart()
.entrySet();
try {
boolean floag=false;
for (Entry<String, GoodsInCart> entry : cartset) {
if (entry.getKey().contains(key)) {
System.out.println("请输入要修改的值");
int num = sc.nextInt();
System.out.println("数量修改成功!");
if (num == 0) {
this.getShoppingCart().remove(key);
System.out.println("商品移除成功!");
} else {
entry.getValue().setNum(num);
}
floag=true;
}
}
代码是这样的
老师,在修改购物车中数量时为什么修改成功后敲回车不会跳出菜单,而要输入数字再敲回车才会显示菜单是为什么
**********************************
**购物车管理**
1--添加商品到购物车
2--修改购物车中的商品数量
3--显示购物车中的所有商品信息
4--结算
9--返回上一级菜单
**********************************
请输入对应的数字对购物车进行管理:
2
请输入要修改的商品编号:
goods001
请输入要修改的值
1
数量修改成功!
1
**********************************
**购物车管理**
1--添加商品到购物车
2--修改购物车中的商品数量
3--显示购物车中的所有商品信息
4--结算
9--返回上一级菜单
**********************************
请输入对应的数字对购物车进行管理:
就像这样的,老师这是怎么回事呢
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星