主方法里导入Set后 后方法里调用显示为空为什么?

主方法里导入Set后 后方法里调用显示为空为什么?

package goods;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class GoodsManage extends Goods {
    private Set<Goods> goodsSet = new HashSet<Goods>();
    public GoodsManage() {
    }
    public Set getGoodsSet() {
        return goodsSet;
    }
    public void setGoodsSet(Set goodsSet) {
        this.goodsSet = goodsSet;
    }
    //商品导入方法
    public void importGoods() {
        System.out.println("商品信息导入:");
        Goods g1 = new Goods("goods004", "手机", 2300, "android手机");
        Goods g2 = new Goods("goods002", "饮水机", 299, "带净化功能的饮水机");
        Goods g3 = new Goods("goods003", "笔记本电脑", 4999, "15寸笔记本");
        Goods g4 = new Goods("goods001", "水杯", 56, "不锈钢水杯");
        goodsSet.add(g1);
        goodsSet.add(g2);
        goodsSet.add(g3);
        goodsSet.add(g4);
        this.setGoodsSet(goodsSet);
        System.out.println("导入成功!");
    } 

 //显示所有商品信息
    public void displayAllGoods() {
        if (goodsSet.isEmpty()) {
            System.out.println("还没有商品,请导入商品");
        }
        Iterator it=goodsSet.iterator();
        while (it.hasNext()){
            System.out.println("所有的商品信息为:");
            System.out.println(it.next());
        }
    }
}
//主方法
import goods.GoodsManage;
import goods.Info;
import goods.ShoppingCart;
import java.util.InputMismatchException;
import java.util.Scanner;
public class TestDemo {
    public static void main(String[] args) {
        Info info = new Info();
        Scanner sc = new Scanner(System.in);
        GoodsManage goodsManage = new GoodsManage();
        ShoppingCart shoppingCart = new ShoppingCart();
        int a, b, c;
        while (true) {
            boolean f = true;
            info.mainMenu();
            try {
                a = sc.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("输入错误请重新输入!");
                sc.next();
                continue;
            }
            switch (a) {
                case 1:
                    while (f) {
                        info.manage();
                        System.out.println("请输入对应的数字进行商品信息的管理:");
                        try {
                            b = sc.nextInt();
                        } catch (InputMismatchException e) {
                            System.out.println("输入错误请重新输入!");
                            sc.next();
                            continue;
                        }
                        switch (b) {
                            case 1:
                                goodsManage.importGoods();
                                break;
                            case 2:
                                goodsManage.displayAllGoods();
                                break;
                            case 9:
                                f = false;
                                break;
                            default:
                                System.out.println("请输入对应的数字!");
                                break;
                        }
                    }
                    break;
                case 2:
                    while (f) {
                        info.shoppoing();
                        System.out.println("请输入对应的数字对购物车进行管理:");
                        try {
                            c = sc.nextInt();
                        } catch (InputMismatchException e) {
                            System.out.println("输入错误请重新输入!");
                            sc.next();
                            continue;
                        }
                        switch (c) {
                            case 1:
//addGoodsToCart方法里无法调用 显示Set为空
public void addGoodsToCart() {

        goodsManage.displayAllGoods();

        System.out.println("请输入要添加的商品编号:");

        String s = sc.next();

        System.out.println("请输入想要添加的数量:");

        int i = sc.nextInt();

        Iterator itGoods = goodsManage.getGoodsSet().iterator();

        while (itGoods.hasNext()) {

            if (goodsManage.getGoodsSet().contains(s)) {

                gi.setGoods((Goods) itGoods.next());

                gi.setNum(i);

                shoppingCart.put(s, gi);

            }else

                System.out.println("没有这个商品!");

        }

    }//
                                shoppingCart.addGoodsToCart();
                                break;
                            case 2:
                                shoppingCart.updateNumInCart();
                                break;
                            case 3:
                                shoppingCart.displayAllInCart();
                                break;
                            case 4:
                                shoppingCart.settleAccounts();
                                break;
                            case 9:
                                f = false;
                                break;
                            default:
                                System.out.println("请重新输入!");
                        }
                    }
                    break;
                case 0:
                    System.out.println("退出程序");
                    return;
                default:
                    System.out.println("请输入对应的数字!");
            }
        }
    }
}

正在回答

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

3回答

没明白你的意思,1)”导入Set“是什么意思?是”import java.util.Set;“吗?2)”后方法里调用显示为空“是什么意思?你是说输出结果为空?具体哪一行?

very_well 2017-10-06 20:25:50

public void addGoodsToCart()这里需要设置参数,把之前已经导入过商品的goodsManage传进来,不然在方法体内的 第一句  goodsManage.displayAllGoods() 的goodsManage 实际只是一个局部变量(新的对象,跟主方法定义的goodsManage对象无关),所以建议方法体改为 public void addGoodsToCart(GoodsManage goodsManage),将主方法调用的代码改为 shoppingCart.addGoodsToCart(goodsManage)

提问者 静水流深3932456 2017-08-25 14:17:14
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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