创建字符流过滤流输入流读取

package co.hp.lx.gtIo;

import co.hp.lx.gtIo.GoodsLx;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class CounterLx {
    int num;
    private GoodsLx[] goods = new GoodsLx[10];

    //展示柜台所有的商品(不能输出null)
    private void show() throws IOException {
        System.out.println("-->商品货架信息");
        //创建字符流过滤输入流读入
        FileReader fr = new FileReader("E:\\goods.txt");
        BufferedReader br = new BufferedReader(fr);//封装为过滤流
        //开始读取
        int i = 0;
        String line = null;
        //readLine()一次读取一行的字符粗
        while ((line = br.readLine()) != null){
            String[] data = line.split(",");
            //System.out.println(data);
            //System.out.println(data[0]+","+data[1]+","+data[2]+","+data[3]);
            //System.out.println(data[0]);
            //System.out.println(Integer.getInteger(data[0])+data[1]+Integer.getInteger(data[2])+data[3]);
            this.goods[i] = new GoodsLx(data[0],data[1],data[2],data[3]);
            i++;
            num ++;
        }
        //创建字符流过滤输出流读取
        FileWriter fw = new FileWriter("E:\\goods.txt");
        BufferedWriter bw = new BufferedWriter(fw);
//        //开始写入
        for (int j = 0; j < this.goods.length; j++) {
            if (this.goods[j]!=null){
                bw.write(this.goods[j].getId()+","+this.goods[j].getGoodsName()+","+this.goods[j].getPrice()+","+this.goods[j].getDesc());
                bw.newLine();//newLine() 写行分隔符。
                System.out.println(goods[j]);
            }
        }
        br.close();
        fr.close();
        bw.close();
        fw.close();
        System.out.println("货架目前商品数量"+num);
        return;
    }
    //修改商品价格
//    private void updata() {
//        //System.out.println("updata");
//        show();
//        System.out.println("请输入要修改的商品编号:");
//        int id = 0;
//        try {
//            id = new Scanner(System.in).nextInt();
//        } catch (Exception e) {
//            //e.printStackTrace();
//            System.out.println("请输入正确的格式如:1001...");
//            updata();
//            return;
//        }
//        for (int i = 0; i < this.lxgoods.length; i++) {
//            if(this.lxgoods[i] != null && this.lxgoods[i].getId() == id) {
//                System.out.println("商品的原价为" + this.lxgoods[i].getPrice());
//                System.out.println("请输入要修改的商品价格:");
//                double price = 0;
//                try {
//                    price = new Scanner(System.in).nextDouble();
//                } catch (Exception e) {
//                    //e.printStackTrace();
//                    System.out.println("请输入正确的格式如:10.1...");
//                    price = new Scanner(System.in).nextDouble();
//                }
//                //把改的价格初始化到原来的价格中
//                GoodsLx lxgoods = new GoodsLx(this.lxgoods[i].getId(), this.lxgoods[i].getGoodsName(), price, this.lxgoods[i].getDesc());

//                //最后再把新的商品信息放回到原来的位置
//                this.lxgoods[i] = lxgoods;
//                show();
//                return;
//            }
//        }
//    }
//    //上架商品
//    private void add() {
//        // System.out.println("add");
//        //判断货架是否还有空位,有空位才可以添加
//        if (this.lxgoods.length == num) {
//            System.out.println("糟糕!商品没位置放了");
//            return;
//        }
//        System.out.println("请输入要添加的商品编号:");
//        int id = 0;
//        try {
//            id = new Scanner(System.in).nextInt();
//        } catch (Exception e) {
//            //e.printStackTrace();
//            System.out.println("请输入正确的格式如:1001...");
//            add();
//            return;
//        }
//        for (int i = 0; i < this.lxgoods.length; i++) {
//            //判断商品编号是否存在
//            if (this.lxgoods[i] != null && this.lxgoods[i].getId() == id) {
//                System.out.println("商品编号已存在");
//                add();
//            }
//        }
//        System.out.println("请输入要添加的商品名称:");
//        String name = new Scanner(System.in).next();
//        System.out.println("请输入要添加的商品价格:");
//        double price = 0;
//        try {
//            price = new Scanner(System.in).nextDouble();
//        } catch (Exception e) {
//            //e.printStackTrace();
//            System.out.println("请输入正确的格式如:10.1...");
//            price = new Scanner(System.in).nextDouble();
//        }
//        System.out.println("请输入要添加的商品描述:");
//        String desc = new Scanner(System.in).next();
//        //调用构造器,初始化商品信息
//        GoodsLx lxgoods = new GoodsLx(id, name, price, desc);
//        for (int i = 0; i < this.lxgoods.length; i++) {
//            //把初始化的商品放入null的位置
//            if (this.lxgoods[i] == null) {
//                this.lxgoods[i] = lxgoods;
//                System.out.println("商品添加成功");
//                num++;
//                show();
//                return;
//            }
//        }
//    }
//    //下架商品
//    private void delete() {
//        System.out.println("请输入要删除的商品编号:");
//        int id = 0;
//        try {
//            id = new Scanner(System.in).nextInt();
//        } catch (Exception e) {
//            //e.printStackTrace();
//            System.out.println("请输入正确的格式如:1001...");
//        }
//        for (int i = 0; i < this.lxgoods.length; i++) {
//                if(this.lxgoods[i] != null && this.lxgoods[i].getId() == id) {
//                    this.lxgoods[i] = null;
//                    num--;
//                    //System.out.println("货架目前商品数量" + num);
//                    show();
//                    return;
//                }
//            }
//                System.out.println("商品不存在");
//                delete();
//        }
    public void main() throws IOException {
        while (true){
            System.out.println("-->欢迎使用商品管理系统");
            System.out.println("-->输入1显示所有商品信息");
            System.out.println("-->输入2下架商品");
            System.out.println("-->输入3上架商品");
            System.out.println("-->输入4修改商品价格");
            System.out.println("-->输入0退出商品货架");
            System.out.println("-->请输入热键:");
            int i = new Scanner(System.in).nextInt();
            switch (i){
                case 1:show();
                    break;
//                case 2:delete();
//                    break;
//                case 3:add();
//                    break;
//                case 4:updata();
//                    break;
                case 0:System.exit(0);//退出jvm
            }
        }
    }
}

 

-->欢迎使用商品管理系统
-->输入1显示所有商品信息
-->输入2下架商品
-->输入3上架商品
-->输入4修改商品价格
-->输入0退出商品货架
-->请输入热键:
1
-->商品货架信息
GoodsLx{id=1001, goodsName='肠粉', price=6, desc='真好吃啊'}
GoodsLx{id=1002, goodsName='辣条', price=2, desc='真好吃啊'}
GoodsLx{id=1003, goodsName='巧克力', price=36, desc='真好吃啊'}
货架目前商品数量3

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值