5.2作业,显示购物车中所有商品的方法
package com.imooc.model;
/**
* 商品类(Goods)
* @author XHJ
*
*/
public class Goods {
// 成员属性:商品编号(goodsId)、商品名称(goodsName)、商品价格(price)、商品描述(goodsDesp)
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;
}
// 重写hashCode()、equals()、toString()方法
@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(goodsId) && goods.getGoodsName().equals(goodsName)
&& (goods.getPrice() == price) && goods.getGoodsDesp().equals(goodsDesp);
}
return false;
}
public String toString() {
return "商品信息[编号:" + goodsId + ",名称:" + goodsName + ",价格:" + price + ",描述:" + goodsDesp + "]";
}
}
package com.imooc.model;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* 商品管理类(GoodsManage)
* @author XHJ
*
*/
public class GoodsManage {
//成员属性:存放商品的容器(goodsSet)
public Set<Goods> goodsSet;
//无参构造方法
public GoodsManage() {
goodsSet=new HashSet<Goods>();
}
//有参构造方法
public GoodsManage(Set<Goods> goodsSet) {
super();
this.setGoodsSet(goodsSet);
}
//构建getter、setter方法
public Set<Goods> getGoodsSet() {
return goodsSet;
}
public void setGoodsSet(Set<Goods> goodsSet) {
this.goodsSet = goodsSet;
}
//商品信息导入方法
public void importGoods() {
//将商品对象导入HashSet
Goods one=new Goods("goods001","水杯",56,"不锈钢水杯");
Goods two=new Goods("goods002","饮水机",299,"带净化功能的饮水机");
Goods three=new Goods("goods003","笔记本电脑",4999,"15寸笔记本电脑");
Goods four=new Goods("goods004","手机",2300,"android手机");
goodsSet.add(one);
goodsSet.add(two);
goodsSet.add(three);
goodsSet.add(four);
System.out.println("商品信息导入成功");
}
//显示所有商品信息的方法
public void displayAllGoods() {
if(goodsSet.size()==0) {
System.out.println("商品信息为空,请导入");
}else {
Iterator<Goods> it=goodsSet.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}
}
}
}
package com.imooc.model;
/**
* 购物车中商品管理类(GoodsInCart)
* @author XHJ
*
*/
public class GoodsInCart {
// 成员属性:商品信息(goods)、商品数量(num)
private Goods goods;
private int num;
//无参构造方法
public GoodsInCart() {
}
// 双参构造方法
public GoodsInCart(Goods goods, int num) {
super();
this.setGoods(goods);
this.setNum(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;
}
}
package com.imooc.model;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
/**
* 购物车类(ShoppingCart)
* @author XHJ
*
*/
public class ShoppingCart {
// 成员属性:购物车(shoppingCart)
public Map<String, GoodsInCart> shoppingCart;
// 无参构造方法
public ShoppingCart() {
}
// 有参构造方法
public ShoppingCart(Map<String, GoodsInCart> shoppingCart) {
super();
this.setShoppingCart(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);
Map<String, GoodsInCart> gc = new HashMap<String, GoodsInCart>();
// 添加商品到购物车的方法
public void addGoodsToCart(GoodsManage gm) {
boolean flag = false;
String str = null;
int n = 0;
if (gm.getGoodsSet().size() == 0) {
System.out.println("商品列表为空,请按提示导入!");
} else {
displayAllInCart();
System.out.println("请输入购买的商品编号");
str = sc.next();
Set<Goods> goods = gm.getGoodsSet();
for (Goods g : goods) {
if (g.getGoodsId().equals(str)) {
flag = true;
break;
}
}
}
if (flag) {
System.out.println("请输入购买数量:");
n = sc.nextInt();
GoodsInCart go = new GoodsInCart(new Goods(), n);
gc.put(str, go);
} else {
System.out.println("没找到该商品!");
}
}
// 修改购物车中商品数量的方法
public void updateNumInCart() {
Set<String> set = gc.keySet();
String str = null;
int n = 0;
boolean flag = false;
System.out.println("请输入要修改的商品编号:");
str = sc.next();
for (String s : set) {
if (s.equals(str)) {
flag = true;
break;
}
}
if (flag) {
System.out.println("请输入要修改的数量");
n = sc.nextInt();
if (n == 0) {
gc.remove(str);
System.out.println("数量为零,已移除该商品");
} else {
gc.get(str).setNum(n);
displayAllInCart();
}
} else {
System.out.println("购物车中没有该商品");
}
}
// 显示购物车中的所有商品的方法
public void displayAllInCart() {
System.out.println("显示购物车中的所有商品信息");
if (gc.size() == 0) {
System.out.println("购物车空空如也,赶紧把它装满吧~~~");
} else {
Iterator<GoodsInCart> it = gc.values().iterator();
while (it.hasNext()) {
GoodsInCart gic = it.next();
Goods g = gic.getGoods();
System.out.println("商品名称:" + g.getGoodsName() + ",商品价格:" + g.getPrice() + ",商品描述:" + g.getGoodsDesp()
+ ",数量" + gic.getNum());
}
}
}
// 结算的方法
public void settleAccounts() {
double sum = 0;
Set<Entry<String, GoodsInCart>> entrySet = shoppingCart.entrySet();
for (Entry<String, GoodsInCart> entry : entrySet) {
sum += (entry.getValue().getGoods().getPrice()) * entry.getValue().getNum();
}
System.out.println("总金额为:" + sum);
}
}
package com.imooc.test;
import java.util.Scanner;
import com.imooc.model.GoodsManage;
import com.imooc.model.ShoppingCart;
/**
* 测试类(DemoTest)
* @author XHJ
*
*/
public class DemoTest {
//封装导航方法
public void nav() {
System.out.println("********************");
System.out.println(" 主菜单 ");
System.out.println(" 1-商品管理 ");
System.out.println(" 2-购物车 ");
System.out.println(" 0-退出 ");
System.out.println("********************");
}
public void nav1() {
System.out.println("********************");
System.out.println(" 商品管理 ");
System.out.println(" 1-商品信息导入 ");
System.out.println(" 2-显示所有商品信息 ");
System.out.println(" 9-返回上一级菜单 ");
System.out.println("********************");
}
public void nav2() {
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("********************");
}
//主方法
public static void main(String[] args) {
//定义对象、全局变量
Scanner sc=new Scanner(System.in);
DemoTest test=new DemoTest();
GoodsManage gm=new GoodsManage();
ShoppingCart cart=new ShoppingCart();
boolean flag=true;
//实现流程
while(flag) {
test.nav();
int n=sc.nextInt();
switch(n) {
case 0:{
flag=false;
System.out.println("欢迎您下次光临");
}break;
case 1:{
boolean flag1=true;
while(flag1) {
test.nav1();
int m=sc.nextInt();
switch(m) {
case 1:
gm.importGoods();
break;
case 2:
gm.displayAllGoods();
break;
case 9:
flag1=false;
break;
default:
System.out.println("您的输入有误,请重新输入!");
}
}
}break;
case 2:{
boolean flag2=true;
while(flag2) {
test.nav2();
int k=sc.nextInt();
switch(k) {
case 1:
cart.addGoodsToCart(gm);
break;
case 2:
cart.updateNumInCart();
break;
case 3:
cart.displayAllInCart();
break;
case 4:
cart.settleAccounts();
break;
case 9:
flag2=false;
break;
default:
System.out.println("您的输入有误,请重新输入!");
}
}
}break;
default:{
System.out.println("您的输入有误,请重新输入!");
}
}
}
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 7317 份
- 解答问题 14452 个
想要入门学编程?多年一直活跃在编程语言排行版前列的Java是一个很好的选择。本路径将从Java基础语法、面向对象、常用工具类三部分,为你开启软件开发的大门!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星