5.2作业,显示购物车中所有商品的方法

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("您的输入有误,请重新输入!");

}

}

}

}


}

http://img1.sycdn.imooc.com//climg/5abb2a3a000131d505900324.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
irista23 2018-03-28 14:20:12

代码有如下几个问题:1)Goods类中的equals()方法只判断商品编号和名称相等即可;2)settleAccounts()方法中,结算后没有清空购物车,没有判断购物车为空的情况。也可以把作业提交上来,每个作业有两次提交机会,助教老师会有更细致的针对性的建议。

  • 提问者 心予明月 #1
    老师,麻烦您帮我看一下,为什么显示不了购物车里所有的商品信息
    2018-03-28 14:21:46
  • irista23 回复 提问者 心予明月 #2
    你应该先通过gc.keySet()取得Set<String>集合。然后遍历这个集合,根据gc.get(String);取出GoodsInCart对象,根据GoodsInCart对象.getGoods()取出商品对象
    2018-03-28 14:57:18
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师