将“柜台商品管理”数据保存到本地文件中,原本的Goods[]数组作为柜台删除此行,现改为本地文件保存。


public class Good {
    private int id;
    private String name;
    private double price;
    private String desc;

    public Good(int goodsId, String name, int price, String desc) {
    }

    public void Good() {
    }

    public void Good(int id, String name, double price, String desc) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.desc = desc;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", desc='" + desc + '\'' +
                '}';
    }

    protected char getGoodsName() {
        return 0;
    }
}

public class Counter01 {
    //商品柜
    Good[] goodsArrays = new Good[9];
    int num = 0;
    private Inputil InputUtils;

    public Counter01(int goodsId, String name, int price, String desc) {
    }

    public Counter01() {

    }

    public void Counter() {
        this.goodsArrays[0] = new Good(1001, "巧克力", 25, "美味可口,恋爱必备!");
        this.goodsArrays[1] = new Good(1002, "卫龙辣条", 2, "隔壁小孩馋哭了!");
        this.goodsArrays[2] = new Good(1003, "蜜雪冰城", 7, "你爱我我爱你蜜雪冰城甜蜜蜜~~");
        this.goodsArrays[3] = new Good(1004, "盼盼小面包", 36, "面包还是盼盼好!");
        this.goodsArrays[4] = new Good(1005, "梅尼耶", 49, "干干脆脆,健康美味!");
        this.goodsArrays[5] = new Good(1006, "钟薛糕", 160, "雪糕刺客非常贵!!!");
        num = 6;//已占用6个位置
    }

    //展示商品
    private void show() {
        if (num == 0) {
            System.out.println("--柜台已售空,---");
            return;
        }
        for (int i = 0; i < goodsArrays.length; i++) {
            if (goodsArrays[i] != null) {
                System.out.println(goodsArrays[i]);
            }
        }
    }

    public void main() {
        while (true) {
            System.out.println("-----------------------------------");
            System.out.println("-----   1.展示商品   2.上架商品 ------");
            System.out.println("-----   3.下架商品   4.调整价格 ------");
            System.out.println("-----   0.退出               -------");
            System.out.println("-----------------------------------");
            //选择功能,控制台输出
            System.out.println("---请输入功能编号:");
            int key = InputUtils.getNum();
            switch (key) {
                case 1:
                    show();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    delete();
                    break;
                case 4:
                    update();
                    break;
                case 0:
                    System.exit(0);//推出JVM
            }
        }
    }

    private void add() {
        //商品柜是否还有空余位置


        System.out.println("请输入商品编号ID:");
        int goodsId = InputUtils.getGoodsId();
        //丰富功能:ID不能重复
        for (int i = 0; i < this.goodsArrays.length; i++) {
            if (this.goodsArrays[i] != null && this.goodsArrays[i].getId() == goodsId) {
                System.out.println("警告:当前编号已存在!!请重新输入");
                add();
                return;//由于重复了,代码结束;
            }
        }
        //程序能走到这里,说明编号ID没有错误重复
        System.out.println("提示:商品ID可以使用。。。");
        System.out.println("请输入商品名称:");
        String name = new Scanner(System.in).next();

        System.out.println("请输入商品价格:");
        double price = InputUtils.getPrice();

        System.out.println("请输入商品描述:");
        String desc = new Scanner(System.in).next();
        System.out.println("添加成功");
        //把输入的信息装在到Goods对象
        Good newGoods = new Good(goodsId, name, (int) price, desc);

        //把商品对象保存柜台数组为null的位置
        for (int i = 0; i < this.goodsArrays.length; i++) {
            //如果这个下标没有Goods商品对象,null,就把商品放进去
            if (this.goodsArrays[i] == null) {
                Good newGood = null;
                this.goodsArrays[i] = newGood;
                this.num++;

            }
        }
    }

    private void delete() {
        System.out.println("请输入要下架的商品编号:");
        int goodsId = InputUtils.getGoodsId();
        //先找到这个对应商品ID的商品对象
        for (int i = 0; i < this.goodsArrays.length; i++) {
            //查找对应ID的商品
            if (this.goodsArrays[i] != null && this.goodsArrays[i].getId() == goodsId) {
                //找到以后进行删除
                this.goodsArrays[i] = null;
                //商品数量减一
                this.num--;
                System.out.println("提示:下架成功!!当前商品数量为" + this.num);
                //是否需要继续循环
                return;//跳出方法(只有找到商品才执行)
            }
        }
        //如果没找到商品,就会执行这里
        System.out.println("警告:未找到该商品!!");
    }

    private void update() {
        System.out.println("请输入要修改的商品编号:");
        Scanner sc = new Scanner(System.in);
        int goodsId = sc.nextInt();
        //先找到这个对应商品ID的商品对象
        for (int i = 0; i < this.goodsArrays.length; i++) {
            if (this.goodsArrays[i] != null && this.goodsArrays[i].getId() == goodsId) {
                //System.out.println("警告:当前编号已存在!!请重新输入");
                update();
                return;//由于重复了,代码结束;
            }
        }
        System.out.println("请输入要修改的商品名称:");
        String name = new Scanner(System.in).next();

        System.out.println("请输入要修改商品价格:");
        double price = InputUtils.getPrice();

        System.out.println("请输入要修改商品描述:");
        String desc = new Scanner(System.in).next();

        //把输入的信息装在到Goods对象
        Good newGoods = new Good(goodsId, name, (int) price, desc);

        //把商品对象保存柜台数组为null的位置
        for (int i = 0; i < this.goodsArrays.length; i++) {
            //如果这个下标没有Goods商品对象,null,就把商品放进去
            if (this.goodsArrays[i] == null) {
                Good newGood = null;
                this.goodsArrays[i] = newGood;
                this.num++;
            Inputil ip = new Inputil();
            if (Inputil.getGoodsId() == Inputil.getGoodsId()){
                System.out.println("请输入商品信息");
                int choose = sc.nextInt();
                switch(choose){
                  //  Inputil.getGoodsId()= sc.nextInt();
                }
            }
            }

            //查找对应ID的商品
        }
        //如果修改不成功,就会执行这里
        System.out.println("警告:价格修改失败!!");
    }
}

public class Inputil {
    /**
     * 获取我们的功能序号
     */
    public static int getGoodsId() {
        int n = 0;

        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //如果输入了非数字,就会抛出异常,被catch语句捕获,在这处理异常
            //e.printStackTrace();
            System.out.println("警告:输入非法数字!!请重新输入");
            n = getGoodsId(); //递归(调用自身)

        }
        return n;
    }

    public static double getPrice() {
        double n = 0;

        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
            //如果输入了非数字,就会抛出异常,被catch语句捕获,在这处理异常
            //e.printStackTrace();
            System.out.println("警告:输入非法数字!!请重新输入");
            n = getPrice(); //递归(调用自身)

        }
        return n;
    }

    public static int getNum(){
        int n = 0;

        try{
            n = new Scanner(System.in).nextInt();
        }catch (Exception e){
            //如果输入了非数字,就会抛出异常,被catch语句捕获,在这处理异常
            //e.printStackTrace();
            System.out.println("警告:输入非法数字!!请重新输入");
            n = getNum(); //递归(调用自身)
        }

        //必须是0-4
        if (n < 0 || n > 4) {
            System.out.println("警告:非法命令!!请重新输入");
            n = getNum();
        }
        return n;

    }
}

public class Write<Goodses, goodsArrays> extends Counter01 {
    int num;
    //private Goodses[] goods = new Goodses[9];
    private goodsArrays[] goodsArrays;
    private Good[] goods;

    public Write(int goodsId, String name, int price, String desc) {
        super(goodsId, name, price, desc);
    }

    //展示柜台所有的商品(不能输出null)
    private void show() throws IOException {
        System.out.println("-->商品货架信息");
        //创建字符流过滤输入流读入
        FileReader fr = new FileReader("E:/img/goods.txt");
        BufferedReader br = new BufferedReader(fr);//封装为过滤流
        //开始读取
        int i = 0;
        String line = null;
        //readLine()一次读取一行的字符粗
        while ((line = br.readLine()) != null) {
            String[] data = line.split(",");
            i++;
        }
        //创建字符流过滤输出流读取
        FileWriter fw = new FileWriter("E:/img/goods.txt");
        BufferedWriter bw = new BufferedWriter(fw);
//        //开始写入
        for (int i1 = 0; i1 < this.goodsArrays.length; i1++) {
            if (this.goodsArrays[i1] != null) {
                bw.write(this.goods[i1].getId() + "," + this.goods[i1].getGoodsName() + "," + this.goods[i1].getPrice() + "," + this.goods[i1].getDesc());
                bw.newLine();
                System.out.println(goods[i1]);


            }


        }


        br.close();
        fr.close();
        bw.close();
        fw.close();
        System.out.println("货架目前商品数量" + num);
        return;
    }
}

public class CounterTest01 {
    public static void main(String[] args) {
        //创建柜台对象
       Counter01 ct = new Counter01();
        //开启菜单
        ct.main();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值