A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

这是我利用空余时间写的一个购物的小程序,大部分主要功能已经写在第一个注释上了,但是还是有写小细节没写首先要说的是这个程序还是
比较严谨的,各个方面都考虑到,当然有些地方暂时没想到,如果你看到
请给我留言,还有很多地方需要优化的,也请大家指点一二,还有一点就是
关于这个背景的,不是因为我娘,而是这个背景就这个看起来整洁,让人看
的清楚,需要黑马,后期也改一下,不然这样看代码太累了

/*
         *
         * 首页
         * ------------------欢迎光顾小店------------------
         * 1,查看购物车
         * 2,购买商品
         * 3,打印小票
         * 4,买单
         * 5,退货
         * 6,退出程序
         * 请输入你的选择
         *
         * 功能注释:
         *         1,查看购物车
         *                 查看购物车内是否有商品,
         *                         有,显示商品信息,结束方法
         *                         无,给出提示,结束方法
         *                 当没有购买商品时查看不了
         *
         *         2,购买商品
         *          显示商品  名称--价格--库存
         *          当已经买单进不来
         *         
         *         3,买单
         *                 显示已经购买车内的商品及其,数量,总额
         *                 当已经买单过进不来
         *                 当没有购买商品进不来
         *
         *         4,退货
         *                 显示购物车内商品信息
         *                 当购物车内没有商品时进不来
         *                 当已经买单后进不来
         *                 当退货时,商品退往库存
         *                
         *         5,打印小票
         *                 显示商品信息,单项商品购买金额,总金额
         *                 当没有买单进不来
         *                 当购物车内没商品时进不来
         *                 当成功打印小票时,购物车商品清空,进行下一轮购买
         *                
         *         6,退出程序
         *                 当购物车内有东西时
         *                 提供两种方式,1返回买单,2逃单
         *                 当买单后进来,提示退出,清空购物车数据,退出程序
         *        
         */
        public static void main(String[] args) throws IOException {
                // 创建键盘录入对象
                Scanner sc = new Scanner(System.in);
                // 存储总计金额,实付,找零
                double[] arr = new double[3];

                while (true) {
                        System.out.println("---------------欢迎光顾小店---------------");
                        System.out.println("1,查看购物车");
                        System.out.println("2,购买商品");
                        System.out.println("3,买单");
                        System.out.println("4,退货");
                        System.out.println("5,打印小票");
                        System.out.println("6,退出小店");
                        System.out.println("请输入你的选择!");
                        int xuanze = sc.nextInt();

                        switch (xuanze) {
                        case 1:
                                // 查看购物车
                                listlook();
                                break;
                        case 2:
                                // 购买商品
                                listadd(sc, arr);
                                break;
                        case 3:
                                // 买单
                                listpay(sc, arr);
                                break;
                        case 4:
                                // 退货
                                listSales(sc, arr);
                                break;
                        case 5:
                                // 打印小票
                                listPrint(arr);
                                System.out.println("\r\n欢迎下次光临小店!");
                                return;
                        case 6:
                                // 退出程序
                                listreturn(sc,arr);
                                break;
                        default:
                                System.out.println("你输入的有误");
                                break;
                        }
                }
        }// main

        /***
         * 购买商品
         *
         * @param list
         * @param sc
         * @throws IOException
         */
        public static void listadd(Scanner sc, double[] arr) throws IOException {
                //判断是否已经买单
                String site1 = "你已经买单,请先打印小票后再购买";
                panDuan(arr, site1);

                // 创建购物车集合,存储每次购买的商品
                ArrayList<Commodity> gouwuch = new ArrayList<Commodity>();
                // 创建商品集合对象
                ArrayList<Commodity> kucun = new ArrayList<Commodity>();

                // 将库存路径封装成字符串
                String site = "kucun.txt";
                // 调用读入流对象,将库存读入
                reader(kucun, site);
                //调用读入流,读取购物车数据
                String site2 = "gouwuch.txt";
                reader(gouwuch, site2);

                while (true) {
                        System.out.println("------------------欢迎光顾小店------------------");
                        System.out.println("商品名称\t商品价格\t商品库存");
                        // 输出库存详细
                        for (int i = 0; i < kucun.size(); i++) {
                                Commodity c = kucun.get(i);

                                System.out.println(c.getName() + "\t" + c.getValue() + "\t" + c.getNumber());
                        }

                        // 客户录入购买商品名称
                        System.out.println("\r\n请输入你要购买的商品名称(退出购买-1)");
                        String name = sc.next();

                        // 当输入-1时退出购买程序
                        if (name.equals("-1")) {
                                System.out.println("退出购买程序");
                                return;
                        }

                        // 购买的商品价格
                        double value = 0;
                        // 库存中的商品数量
                        int number1 = 0;
                        // 存储该商品的索引
                        int index = 0;
                        // 判断集合中是否有该商品
                        int count = 0;
                        for (int i = 0; i < kucun.size(); i++) {
                                Commodity c = kucun.get(i);

                                if (c.getName().equals(name)) {
                                        // 将该商品的价钱存储起来
                                        value = c.getValue();
                                        // 将该商品的数量存储起来
                                        number1 = c.getNumber();
                                        // 将该商品的索引存储起来
                                        index = i;
                                        // 找到对应的商品没比要运行多余的代码,退出循环
                                        break;
                                } else {
                                        // 统计整个集合里是否全没有该商品
                                        count++;
                                }
                        }
                        // 没有该商品,给出提示,跳出此次循环,重新录入
                        if (count == kucun.size()) {
                                System.out.println("你输入的商品不存在");
                                continue;
                        }

                        // 客户输入购买的数量
                        System.out.println("请输入购买的数量");
                        int number = sc.nextInt();

                        // 判断客户录入的数量是否合法
                        if (number <= 0 || number > number1) {
                                System.out.println("你输入的数量有误!");
                                continue;
                        }

                        // 将库存的减去购买的了
                        Commodity m = kucun.get(index);
                        m.setNumber(m.getNumber() - number);

                        // 统计变量,
                        int count2 = 0;
                        for (int i = 0; i < gouwuch.size(); i++) {
                                Commodity cc = gouwuch.get(i);
                                // 如果已经有过该商品,就在原有的基础上加上本次够买的
                                if (name.equals(cc.getName())) {
                                        cc.setNumber(cc.getNumber() + number);
                                } else {
                                        // 如果该商品不存在就加1
                                        count2++;
                                }
                        }
                        // 如果集合中都没有该商品,就将该商品加入集合
                        if (count2 == gouwuch.size()) {
                                // 将够买的商品信息放入对象
                                Commodity c = new Commodity(name, value, number);
                                // 将对象放入集合
                                gouwuch.add(c);
                        }
                        // 加入后给出提示
                        System.out.println("已将该商品放入购物车中");
                        break;

                }
                // 将修改过的gouwuch集合(购物车)写入文本
                String site3 = "gouwuch.txt";
                writer(gouwuch, site3);

                // 将修改过的list集合(库存)写入文本
                String site4 = "kucun.txt";
                writer(kucun, site4);

        }

        /***
         * 查看购物车内商品信息
         *
         * @throws IOException
         */
        public static void listlook() throws IOException {
                // 创建购物车集合,存储每次购买的商品
                ArrayList<Commodity> gouwuch = new ArrayList<Commodity>();
                // 将路径封装成字符串
                String site = "gouwuch.txt";
                reader(gouwuch, site);

                // 判断购物车内有无商品
                if (gouwuch.size() == 0) {
                        System.out.println("购物车中没有商品,请先购买后查看\r\n");
                        return;
                }

                // 展示购物车商品
                System.out.println("商品名称\t商品价格\t商品数量");
                for (int i = 0; i < gouwuch.size(); i++) {
                        Commodity c = gouwuch.get(i);
                        System.out.println(c.getName() + "\t" + c.getValue() + "\t" + c.getNumber());
                }
                // 换行,格式好看点
                System.out.println();
        }

        /***
         * 买单
         *
         * @param sc
         * @param arr
         * @throws IOException
         */
        public static void listpay(Scanner sc, double[] arr) throws IOException {
                // 判断是否已经买单
                String line = "你已经买单了不需要再次买单";
                panDuan(arr, line);

                // 创建购物车集合,存储每次购买的商品
                ArrayList<Commodity> gouwuch = new ArrayList<Commodity>();
                // 将路径封装成字符串
                String site = "gouwuch.txt";
                // 调用读入方法
                reader(gouwuch, site);

                if (gouwuch.size() == 0) {
                        System.out.println("购物车中没有商品,不需要支付");
                        return;
                }

                // 展示购物车商品
                System.out.println("商品名称\t商品价格\t商品数量");
                for (int i = 0; i < gouwuch.size(); i++) {
                        Commodity c = gouwuch.get(i);
                        System.out.println(c.getName() + "\t" + c.getValue() + "\t" + c.getNumber());
                }

                // 获得本次购买商品需要支付的金额
                double sum = 0;
                for (int i = 0; i < gouwuch.size(); i++) {
                        Commodity c = gouwuch.get(i);
                        // 将每个元素的价钱和数量相乘
                        sum += c.getValue() * c.getNumber();
                }

                while (true) {
                        // 你需要支付的金额
                        System.out.println("\r\n你需要支付" + sum + "元钱");
                        // 你正在支付的金额
                        System.out.println("请输入你要支付的金额");
                        double jinEr = sc.nextDouble();

                        // 客户录入小于0时
                        if (jinEr < 0) {
                                System.out.println("请输入正确的金额");
                                // 客户录入小于需要付的钱时
                        } else if (jinEr < sum) {
                                System.out.println("支付不成功,需要支付的是" + sum + "元");
                                return;
                                // 客户录入大于或等于需要付的钱时
                        } else if (jinEr >= sum) {
                                double yuEr = (jinEr - sum);
                                // 总计
                                arr[0] = sum;
                                // 实付
                                arr[1] = jinEr;
                                // 找零
                                arr[2] = yuEr;
                                System.out.println("成功买单!找回" + yuEr + "元");
                                return;
                        }
                } // while

        }

        /***
         * 退货
         *
         * @param sc
         * @throws IOException
         */
        public static void listSales(Scanner sc, double[] arr) throws IOException {
                // 判断是否已经买单了
                String line = "你已经买单不能退了";
                panDuan(arr, line);

                // 创建购物车集合,存储每次购买的商品
                ArrayList<Commodity> gouwuch = new ArrayList<Commodity>();
                // 将路径封装成字符串
                String site = "gouwuch.txt";
                // 调用读入方法
                reader(gouwuch, site);

                //判断购物车内是否有商品存在
                if(gouwuch.size() == 0){
                        System.out.println("你的购物车内没有商品可退");
                        return;
                }

                // 创建商品集合对象
                ArrayList<Commodity> kucun = new ArrayList<Commodity>();
                // 将路径封装成字符串
                String site1 = "kucun.txt";
                //读入库存数据
                reader(kucun, site1);

                // 展示购物车商品
                System.out.println("商品名称\t商品价格\t商品数量");
                for (int i = 0; i < gouwuch.size(); i++) {
                        Commodity c = gouwuch.get(i);
                        System.out.println(c.getName() + "\t" + c.getValue() + "\t" + c.getNumber());
                }

                String name = null;
                int number = 0;
                while (true) {
                        System.out.println("\r\n请输入你要退回的商品");
                        name = sc.next();

                        // 用来存储需要退货的索引
                        int index = -1;
                        // 存储该元素的数量
                        int numberConut = 0;
                        // 判断该商品是否存在
                        int count = 0;
                        // 判断
                        for (int i = 0; i < gouwuch.size(); i++) {
                                Commodity c = gouwuch.get(i);

                                if (c.getName().equals(name)) {
                                        index = i;
                                        numberConut = c.getNumber();
                                        break;
                                } else {
                                        count++;
                                }
                        }
                        if (count == gouwuch.size()) {
                                System.out.println("购物车中没有该商品");
                                continue;
                        }

                        System.out.println("请输入你要退回的数量");
                        number = sc.nextInt();

                        // 如果要退的数量大于本有的
                        if (number > numberConut) {
                                System.out.println("你的购物车中没有这么多商品");
                                continue;

                                // 如果输入的是0或者负数,直接跳过循环
                        } else if (number <= 0) {
                                System.out.println("请输入正确的数量");
                                continue;

                                // 如果要退的数量等于本有的直接删除该对象
                        } else if (number == numberConut) {
                                gouwuch.remove(index);
                                System.out.println("已经将商品全部退回");

                                // 退出while循环去执行写出语句
                                break;

                                // 如果要退的数量小与本有的,将本有的减去要退的
                        } else if (number < numberConut) {
                                Commodity cc = gouwuch.get(index);
                                cc.setNumber(cc.getNumber() - number);
                                System.out.println("已经退还" + number + "个" + name);

                                // 退出while循环去执行写出语句
                                break;
                        }
                } // while

                //退回的商品,加到库存中
                for (int i = 0; i < kucun.size(); i++) {
                        Commodity c = kucun.get(i);
                        if(c.getName().equals(name)){
                                c.setNumber(c.getNumber()+number);
                        }
                }

                // 将数据写回购物车
                String site2 = "gouwuch.txt";
                writer(gouwuch, site2);
                //将数据写回库存
                String site3 = "kucun.txt";
                writer(kucun, site3);

        }

        /***
         * 打印小票
         *
         * @param arr
         * @throws IOException
         */
        public static void listPrint(double[] arr) throws IOException {
                // 创建购物车集合,存储每次购买的商品
                ArrayList<Commodity> gouwuch = new ArrayList<Commodity>();
                // 将路径封装成字符串
                String site = "gouwuch.txt";
                // 调用读入方法
                reader(gouwuch, site);

                //判断购物车内有无商品
                if(gouwuch.size() == 0){
                        System.out.println("购物车内没有商品,请先将商品加入购物车");
                        return;
                }

                // 判断判是否已经买单
                int count2 = 0;
                for (int i = 0; i < arr.length; i++) {
                        if (arr == 0.0) {
                                count2++;
                        }
                }
                if (count2 == 3) {
                        System.out.println("请先买单后打印小票");
                        return;
                }

                System.out.println("\r\n欢\t迎\t光\t临");
                System.out.println("商品名称\t商品价格\t商品数量\t小计金额");
                System.out.println("----------------------------------");

                int count = 0;
                double maxNumber = 0;
                for (int i = 0; i < gouwuch.size(); i++) {
                        Commodity c = gouwuch.get(i);
                        // 购买了多少项商品
                        count++;
                        // 小计金额
                        double sum = c.getValue() * c.getNumber();
                        // 累加购买了多少件商品
                        maxNumber += c.getNumber();
                        System.out.println(c.getName() + "\t" + c.getValue() + "\t" + c.getNumber() + "\t" + sum);
                }
                System.out.println();

                System.out.println(count + "项商品" + "\t" + "共计:" + maxNumber + "件");
                System.out.println("总计:" + arr[0]);
                System.out.print("实付:" + arr[1]);
                System.out.println("找零:" + arr[2]);
                System.out.println("凭此小票换取发票");

                // 打印小票后清空集合
                gouwuch.clear();
                // 写出空白集合清空购物车
                String site2 = "gouwuch.txt";
                writer(gouwuch, site2);
                return;

        }

        /***退出小店
         *
         * @param sc
         * @param arr
         * @throws IOException
         */
        public static void listreturn(Scanner sc,double[] arr) throws IOException{
                int count = 0;
                for (int i = 0; i < arr.length; i++) {
                        if(arr > 0.0){
                                count++;
                        }
                }
                if(count < 2){
                        System.out.println("1前往买单,2赶紧逃单");
                        int xuanZhe = sc.nextInt();

                        switch(xuanZhe){
                        case 1:
                                System.out.println("前往买单!");
                                // 买单
                                return;
                        case 2:
                                //直接退出虚拟机
                                System.out.println("你已经被打死在厕所!");
                                System.exit(0);
                        default:
                                System.out.println("你输入的数据有误");
                        }
                }else if(count >= 2){
                        System.out.println("欢迎下次光顾!");
                        //清空购物车
                        ArrayList<Commodity> gouwuch = new ArrayList<Commodity> ();
                        String site = "gouwuch.txt";
                        writer(gouwuch,site);

                        System.exit(0);
                }
        }

        /***
         * 写出
         *
         * @param list
         * @param site
         * @throws IOException
         */
        public static void writer(ArrayList<Commodity> list, String site) throws IOException {
                // 加入的商品写入到购物车文本里
                BufferedWriter bw = new BufferedWriter(new FileWriter(site));

                for (int i = 0; i < list.size(); i++) {
                        Commodity c = list.get(i);

                        bw.write(c.getName() + "," + c.getValue() + "," + c.getNumber());
                        bw.newLine();
                }
                bw.close();
        }

        /***
         * 读入
         *
         * @param list
         * @param site
         * @throws IOException
         */
        public static void reader(ArrayList<Commodity> list, String site) throws IOException {
                // 将购物车内的商品信息读入
                BufferedReader br = new BufferedReader(new FileReader(site));

                String line;

                while ((line = br.readLine()) != null) {
                        String[] s = line.split(",");
                        // 将String类型转为double
                        double d = Double.parseDouble(s[1]);
                        // 将String类型转为int
                        int i = Integer.parseInt(s[2]);
                        // 将元素加入对象
                        Commodity c = new Commodity(s[0], d, i);
                        // 将对象加入集合
                        list.add(c);
                }
        }

        /***
         * 判断判断是否已经买单
         *
         * @param arr
         * @param line
         */
        public static void panDuan(double[] arr, String line) {
                // 判断判断是否已经买单
                int count2 = 0;
                for (int i = 0; i < arr.length; i++) {
                        if (arr > 0.0) {
                                count2++;
                        }
                }
                if (count2 >= 2) {
                        System.out.println(line);
                        return;
                }
        }
}


1 个回复

倒序浏览
我的格式呢?我的背景呢?
来自宇宙超级黑马专属安卓客户端来自宇宙超级黑马专属安卓客户端
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马