run as时只有run configurations
package com.homework;
public class Goods {
private String goodsId;
private String goodsNamde;
private double price;
private String goodsDesp; //描述
//构造方法
public Goods() {
}
public Goods(String goodsId, String goodsNamde, double price, String goodsDesp) {
super();
this.goodsId = goodsId;
this.goodsNamde = goodsNamde;
this.price = price;
this.goodsDesp = goodsDesp;
}
// get set
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsNamde() {
return goodsNamde;
}
public void setGoodsNamde(String goodsNamde) {
this.goodsNamde = goodsNamde;
}
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;
}
// hashCode
@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 + ((goodsNamde == null) ? 0 : goodsNamde.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 (goodsDesp == null) {
if (other.goodsDesp != null)
return false;
} else if (!goodsDesp.equals(other.goodsDesp))
return false;
if (goodsId == null) {
if (other.goodsId != null)
return false;
} else if (!goodsId.equals(other.goodsId))
return false;
if (goodsNamde == null) {
if (other.goodsNamde != null)
return false;
} else if (!goodsNamde.equals(other.goodsNamde))
return false;
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
return false;
return true;
}
//toString
@Override
public String toString() {
return "商品信息 [编号=" + goodsId + ", 名称=" + goodsNamde + ", 价格=" + price + ", 描述="
+ goodsDesp + "]";
}
}
package com.homework;
public class GoodsInCart {
private Goods goods;
private int num;
public GoodsInCart(Goods goods, int num) {
super();
this.goods = goods;
this.num = num;
}
public GoodsInCart() {
}
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.homework;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GoodsManage {
private Set goodsSet; //容器
public GoodsManage() {
}
public GoodsManage(Set goodsSet) {
this.goodsSet = goodsSet;
}
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.homework;
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;
}
public Map<String, GoodsInCart> getShoppingCart() {
return shoppingCart;
}
public void setShoppingCart(Map<String, GoodsInCart> shoppingCart) {
this.shoppingCart = shoppingCart;
}
//添加商品到购物车
public void addGoodsToCart(GoodsManage gm) {
Scanner console=new Scanner(System.in);
System.out.println("请输入要添加商品的编号:");
String goodsId=console.next();
System.out.println("请输入要添加商品的数量:");
int num=console.nextInt();
//构建迭代器查询与待添加商品编号相同的商品
Iterator<Goods> it=gm.getGoodsSet().iterator();//容器放入迭代器中
while(it.hasNext()) {
if(it.next().getGoodsId().equals(goodsId)) {
GoodsInCart goodsInCart=new GoodsInCart(it.next(),num);//添加到购物车中
shoppingCart.put(goodsId, goodsInCart); //添加都购物车的map集合
break;
}
}
}
//修改购物车的商品数量
public void updateNumInCart() {
Scanner console=new Scanner(System.in);
System.out.println("请输入要修改的商品编号:");
String goodsId=console.next();
System.out.println("请输入新的商品数量");
int newNum=console.nextInt();
Set <Entry<String,GoodsInCart>>entry=shoppingCart.entrySet();
Iterator <Entry<String,GoodsInCart>>it=entry.iterator();
while(it.hasNext()) {
if(it.next().getValue().getGoods().getGoodsId().equals(goodsId)) {
if(newNum==0) {
System.out.println("因为商品数量为0,该商品已经从购物车上移除!");
}
it.next().getValue().setNum(newNum);
}
}
}
//显示购物车中的所有商品
public void displayAllInCart() {
if(shoppingCart!=null) {
Iterator <Entry<String,GoodsInCart>>it=shoppingCart.entrySet().iterator();
while(it.hasNext()) {
System.out.println(it.next().getValue().getGoods()+",数量:"+it.next().getValue().getNum());
}
}else {
System.out.println("购物车是空的");
}
}
//结算
public void settleAccounts() {
System.out.println("结算");
double sum=0.0;
Set<Entry<String,GoodsInCart>> entrySet=shoppingCart.entrySet();
for(Entry<String,GoodsInCart> entry:entrySet) {
sum+=entry.getValue().getNum()*entry.getValue().getGoods().getPrice();
}
System.out.println("商品的总价为"+sum);
displayAllInCart();
entrySet.removeAll(entrySet);
}
}
package com.homework;
import java.util.Scanner;
public class TestDemo {
public void mianMenu() {
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 goodsManageMenu() {
/*
* 商品管理菜单
*/
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 shoppingCartManagementMenu() {
/*
* 购物车管理菜单
*/
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 mian(String[]args) {
System.out.println("000");
Scanner sc = new Scanner(System.in);
boolean flag = true;
TestDemo test = new TestDemo();
while (flag) {
test.mianMenu();
int num = sc.nextInt();
if (num == 1) {
boolean f1 = true;
while (f1) {
test.mianMenu();
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.shoppingCartManagementMenu();
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("输入错误,请重新输入!");
}
}
}
}
正在回答 回答被采纳积分+1
你好同学,如下图所示,在TestDemo类中,你的main单词写错了哦,改完之后就可以运行了,祝学习愉快~
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星