Java 初学 购物车案例 字符缓冲输入/输出流

学了一点点$java$,于是写了下面这个小案例复习复习。

本案例只在终端运行

本案例旨在模拟购物车,具有增删改查的功能,可以一键清空购物车,结算购物车。
利用字符缓冲流提高输入输出效率。

商品类

package ShoppingCart;

/**
 * 商品类
 */
public class Goods {
    int id;//编号
    String name;//姓名
    Double price;//价格
    int boughtnums;//购买数量

    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 int getBoughtnums() {
        return boughtnums;
    }

    public void setBoughtnums(int boughtnums) {
        this.boughtnums = boughtnums;
    }
}

购物车内商品文件

仅作测试。

1 手表 199.9 1
2 耳机 899.0 2
3 13 13.0 13
4 5 6.0 7
6 6 6.0 6
10 10 10.0 10

Main

package ShoppingCart;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

/**
 * 需求
 * 模拟购物车模块的功能,需要实现添加商品到购物车中去,
 * 同时需要提供修改商品的购买数量,结算商品价格功能(请使用面向对象编程来解决)。
 * 分析
 * 购物车中的每个商品都是一个对象,需要定义一个商品类。
 * 购物车本身也是一个对象:可以使用数组对象代表它。
 * 完成界面架构,让用户选择操作的功能。
 * <p>
 * 添加 查看 修改 结算
 * add query update pay
 */
public class TestShopCart {
    static HashMap<Integer, Boolean> vim = new HashMap<>();
    static Vector<Goods> goods = new Vector<>();

    public static void main(String[] args) {
        Init();
//        for (Goods good : goods) {
//            System.out.println(good.id + " " + good.name + " " + good.price + " " + good.boughtnums);
//        }
        while (true) {
            System.out.println("你可以进行如下操作:");
            System.out.println("添加商品到购物车: add");
            System.out.println("查看已选择商品: query");
            System.out.println("修改已选择商品数量: update");
            System.out.println("结算购物车中商品: pay");
            System.out.println("清空购物车: clear");
            Scanner sc = new Scanner(System.in);
            System.out.println("请您输入指令:");
            String command = sc.next();

            switch (command) {
                case "add":
                    addGoods(goods, sc);
                    break;
                case "query":
                    queryGoods(goods);
                    break;
                case "update":
                    updateGoods(goods, sc);
                    break;
                case "pay":
                    payGoods(goods);
                    break;
                case "clear":
                    clearGoods();
                    break;
                case "end":
                    break;
                default:
                    System.out.println("没有该操作");
            }

            if (command.equals("end")) {
                try(
                        BufferedWriter bw = new BufferedWriter(new FileWriter("src/ShoppingCart/GoodsList.txt"));
                        ){
                    Collections.sort(goods, new Comparator<Goods>() {
                        @Override
                        public int compare(Goods o1, Goods o2) {
                            return o1.id - o2.id;
                        }
                    });
                    for (Goods good : goods) {
                        String str = good.id+" "+good.name+" "+good.price+" "+good.boughtnums;
                        bw.write(str);
                        bw.newLine();
                    }
                    bw.close();
                }catch(Exception e){
                   e.printStackTrace();
                }
                break;
            }

        }

    }

    /**
     * 清空购物车
     */
    private static void clearGoods() {
        goods.clear();
    }

    private static void Init() {
        try (
                BufferedReader br = new BufferedReader(new FileReader("src/ShoppingCart/GoodsList.txt"));
//                BufferedWriter bw = new BufferedWriter(new FileWriter("src/ShoppingCart/GoodsList.txt"));
        ) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] str = line.split(" ");
                Goods gs = new Goods();
                int i = 0;
                for (String s : str) {
                    if(i==0){
                        gs.id = new BigDecimal(s).intValue();
                    }
                    else if(i == 1){
                        gs.name = s;
                    }
                    else if(i == 2){
                        gs.price = new BigDecimal(s).doubleValue();
                    }
                    else{
                        gs.boughtnums = new BigDecimal(s).intValue();
                    }
                    i++;
                }
                goods.add(gs);
//                System.out.println(line);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 完成添加商品到购物车的功能
     *
     * @param goods
     * @param sc
     */
    private static void addGoods(Vector<Goods> goods, Scanner sc) {
        System.out.println("请您输入要购买的商品的编号:");
        int id = sc.nextInt();
        System.out.println("请您输入购买商品的名称:");
        String name = sc.next();
        System.out.println("请您输入购买的商品单价:");
        Double price = sc.nextDouble();
        System.out.println("请您输入购买的商品数量:");
        int nums = sc.nextInt();

        vim.put(id, true);

        Goods g = new Goods();
        g.setId(id);
        g.setName(name);
        g.setPrice(price);
        g.setBoughtnums(nums);

        goods.add(g);
        System.out.println(goods.size());
        System.out.println("您的商品: " + g.name + "添加成功");
        String str = g.id+" "+g.name+" "+g.price+" "+g.boughtnums;
        try(
                BufferedWriter bw = new BufferedWriter(new FileWriter("src/ShoppingCart/GoodsList.txt",true));
                ){
            bw.write(str);
            bw.newLine();
            bw.close();


        }catch (Exception e){
            e.printStackTrace();
        }


    }

    /**
     * 查询购物车中的商品信息
     *
     * @param goods
     */
    private static void queryGoods(Vector<Goods> goods) {
        System.out.println("=====购物车商品信息如下=====");
        System.out.println("编号\t\t姓名\t\t价格\t\t购买数量");
        for (Goods it : goods) {
            System.out.println(it.id + "\t\t" + it.name + "\t\t" + it.price + "\t\t" + it.boughtnums);
        }
    }

    /**
     * 修改编号对应商品信息
     *
     * @param goods
     * @param sc
     */
    private static void updateGoods(Vector<Goods> goods, Scanner sc) {
        int id = sc.nextInt();
        if (vim.get(id) == null) {
            System.out.println("该编号对应商品不在购物车中");
        } else if (vim.get(id) == true) {
            for (Goods it : goods) {
                if (it.id == id) {
                    System.out.println("请输入你要修改的商品购买数量");
                    int nums = sc.nextInt();
                    it.setBoughtnums(nums);
                    break;
                }
            }
        }
    }

    private static void payGoods(Vector<Goods> goods) {
        double Sum = 0.0;
        for (Goods it : goods) {
            Sum += it.boughtnums * it.price;
        }
        System.out.println("总计 " + Sum + " 元");
        System.out.println("您需要支付" + Sum + "元");
    }
}

如有不足 还望指教

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值