柜台管理系统(三)

前言

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

如:goods.txt,参考数据

	1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
	1002,辣条,2,真好吃啊,那些吃不到的人真可怜..
	1003,巧克力,36,真好吃啊,那些吃不到的人真可怜..

定义柜台类和商品类

柜台类

public class Counter {
    private Goods goods[] = new Goods[10];
    private int num;


    public Counter() throws IOException {
        this.goods[0]=new Goods(1001,"巧克力",25,"美味可口,恋爱必备!");
        this.goods[1]=new Goods(1002,"卫龙辣条",1,"隔壁小孩馋哭了!");
        num=2;//相当于两个商品
        FileWriter out = new FileWriter("goods.txt");
        BufferedWriter bw = new BufferedWriter(out);
        for (int i = 0; i < goods.length; i++) {
            if (goods[i] != null){
                String data = goods[i].getId() + "," + goods[i].getGoodsName() + "," + goods[i].getPrice() + "," + goods[i].getDesc();
                bw.write(data);
                bw.newLine();
            }
        }
        bw.close();
        out.close();
    }
   }

定义商品类

public class Goods {
    private int id; //商品编号
    private String goodsName; //商品名字
    private double price; //商品价格
    private String desc; //商品描述

    public Goods() {

    }

    public Goods(int id, String goodsName, double price, String desc) {
        this.id = id;
        this.goodsName = goodsName;
        this.price = price;
        this.desc = desc;
    }

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

    public int getId() {
        return id;
    }

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

    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 getDesc() {
        return desc;
    }

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

定义所需要用到的工具类

输入工具类

public class InputUtils {
    /**
     *  获取我们指定范围的序号
     */
    public static int getNum() {
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("-->>输入非数字数据");
            n = getNum();
        }
        if (n <0 || n > 4){
            System.out.println("输入非法命令");
            n = getNum();
        }
        return n;
    }

    /**
     * 获取商品id
     */
    public static int getGoodsId() {
        int n = 0;
        try {
            n = new Scanner(System.in).nextInt();
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("-->>输入非法数字");
            n = getGoodsId(); //递归
        }
        return n;
    }


    /**
     * 获取修改后的价格
     */
    public static double getPrice() {
        double n = 0;
        try {
            n = new Scanner(System.in).nextDouble();
        } catch (Exception e) {
//            e.printStackTrace();
            System.out.println("-->>输入非法数字");
            n = getPrice(); //递归
        }
        return n;
    }

}

文件工具类

public class FileUtils {
    /**
     * 删除指定一行
     * @param delete
     * @throws IOException
     */
    public static void deleteOfOne(String delete) throws IOException {
        FileReader in = new FileReader("goods.txt");
        BufferedReader br = new BufferedReader(in);
        String line = null;
        List<String> list = new ArrayList<>();
        while((line = br.readLine()) != null){
            list.add(line);
        }
        list.remove(delete);
        //关闭资源
        br.close();
        in.close();
        //覆盖源文件
        FileWriter out = new FileWriter("goods.txt");
        BufferedWriter bw = new BufferedWriter(out);
        for (String s :
                list) {
            bw.write(s);
            bw.newLine();
        }
        //关闭资源
        bw.close();
        out.close();
    }

    /**
     * 更改某一行的内容
     * @param data
     * @param update
     * @throws IOException
     */
    public static void update(String data, String update) throws IOException {
        FileReader in = new FileReader("goods.txt");
        BufferedReader br = new BufferedReader(in);
        String line = null;
        List<String> list = new ArrayList<>();
        while((line = br.readLine()) != null){
            list.add(line);
        }
        if (list.indexOf(data) > -1){
            list.set(list.indexOf(data),update);
        }
        //关闭资源
        br.close();
        in.close();
        
        //覆盖源文件
        FileWriter out = new FileWriter("goods.txt");
        BufferedWriter bw = new BufferedWriter(out);
        for (String s :
                list) {
            bw.write(s);
            bw.newLine();
        }
        //关闭资源
        bw.close();
        out.close();
    }
}

在柜台类中编写各种功能方法

菜单、上架、删除、修改

public void main() throws IOException {
        while(true){
            System.out.println("--------------");
            System.out.println("--1.展示商品  2.上架商品--");
            System.out.println("--3.下架商品  4.调整价格--");
            System.out.println("---0.退出--------");
            //控制台输入
            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 show() throws IOException {
//        for (Goods good :
//                goods) {
//            if (good != null)
//                System.out.println(good);
//        }
//        System.out.println("-->>当前商品数量:"+ this.num);
        System.out.println("-------------  柜台商品信息  ---------------");
        //创建字符流过滤流输入流读取
        FileReader in = new FileReader("goods.txt");//字符流(提前创建goods.txt)
        BufferedReader br = new BufferedReader(in);//封装为过滤流

        //开始读取
        String line = null;//readLine()一次读取一行的字符粗
        while((line = br.readLine())!=null){
            System.out.println("line = " + line);
        }

        int i = 0;
        String line1 = null;//readLine()一次读取一行的字符粗
        while((line1 = br.readLine())!=null){
            //把读取的数据放到数组中
            String[] data = line1.split(",");//根据,拆分数据 1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
            //特别注意:数据都存到数组中了
            this.goods[i] = new Goods(Integer.parseInt(data[0]),data[1],Double.parseDouble(data[2]),data[3]);
            i++;
        }
        //关闭流,释放资源
        br.close();
        in.close();
        System.out.println("------------------------------------------");
    }

    /**
     * 上架商品
     */
    private void add() throws IOException {
        if (num >= goods.length){
            System.out.println("柜台已经满了,无法上架");
            return;
        }



        FileWriter out = new FileWriter("goods.txt", true);//字符流
        BufferedWriter bw = new BufferedWriter(out);//封装为过滤流


        System.out.println("-->>请输入商品Id");
        int goodsId = InputUtils.getGoodsId();
        for (int i = 0; i < goods.length; i ++){
            if (this.goods[i] != null && this.goods[i].getId() == goodsId){
                System.out.println("-->>当前ID已经存在,请重新输入");
                return; //判断生效后后边不用执行。
            }
            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();

            //把信息加载到一个新的Goods对象中
            Goods newGoods = new Goods(goodsId,name,price,desc);
//            Goods newGoods = new Goods(1, "1", 1, "1");
            //把对象放到柜台中去
            for (int j = 0; j < this.goods.length; j++){
                if(this.goods[j] != null){

                    this.goods[j] = newGoods;
                    this.num++;
                    bw.write(this.goods[j].getId()+","+this.goods[j].getGoodsName()+","+this.goods[j].getPrice()+","+this.goods[j].getDesc());//格式:1001,肠粉,6,真好吃啊,那些吃不到的人真可怜..
                    bw.newLine();
                    System.out.println("添加成功");
                    System.out.println("-->>当前商品数量" + this.num);
                    bw.close();
                    out.close();
                    return;
                }
            }
            //关闭流,释放资源
            bw.close();
            out.close();
            return;

//            for (int j = 0; j < this.goods.length; j++) {
//                if(this.goods[j]!=null){
//
//                    System.out.println("添加成功");
//                    return;
//                }
//            }

        }
    }

    /**
     *  下架商品
     */
    private void delete(){
        System.out.println("-->>请输入要下架的商品编号");
        int goodsId = InputUtils.getGoodsId();
        for (int i = 0; i < this.goods.length; i++){

            try {
                if (this.goods[i].getId() == goodsId){
                    //找到以后删除
                    String delete = this.goods[i].getId()+","+this.goods[i].getGoodsName()+","+this.goods[i].getPrice()+","+this.goods[i].getDesc();
                    this.goods[i] = null;
                    //商品数量减一
                    this.num--;
                    //还需不需要继续循环

                    FileUtils.deleteOfOne(delete);


                    System.out.println("-->>下架成功");
                    System.out.println("-->>当前柜台剩余商品数量:" + this.num);
                    break;//跳出循环
                }
            } catch (Exception e) {
//                e.printStackTrace();
                System.out.println("未找到当前商品编号,请重新输入");
                delete();
                return;
            }
        }

    }

    /**
     * 调整价格
     */
    private void update() throws IOException {
        System.out.println("-->>请输入要修改的商品编号");
        int goodsId = InputUtils.getGoodsId();
        for (int i = 0; i < this.goods.length; i++){
            if (this.goods[i] != null && this.goods[i].getId() == goodsId){
                String data = this.goods[i].getId()+","+this.goods[i].getGoodsName()+","+this.goods[i].getPrice()+","+this.goods[i].getDesc();
                System.out.println("请输入修改后的价格");
                Double price = InputUtils.getPrice();
                this.goods[i].setPrice(price);

                String update = this.goods[i].getId()+","+this.goods[i].getGoodsName()+","+price+","+this.goods[i].getDesc();
                FileUtils.update(data, update);
                System.out.println("-->>修改成功");
                return;
            }
        }
        System.out.println("-->>修改失败,请检查后重新修改");
        update();
        return;
    }

运行截图

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值