Java实现购物车模块模拟-架构搭建(面向对象编程实现)

需求
    模拟购物车模块功能,需要实现添加商品到购物车中去,同时需要提供
    修改商品的购买数量,结算商品价格功能(请用面向对象编程实现)
分析
    购物车中的每个商品都是一个对象,需要定义一个商品类
    购物车本身也是个对象:可以使用数组对象代表
    完成界面架构,让用户选择操作功能

public class Goods {
    int id;
    String name;
    double price;
    int numGoods;
}
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //1、定义商品类,用于创建商品对象
        //2、定义购物车对象:使用数组对象来表示
        Goods[] shopCar = new Goods[100];//[null,null,...]//数组对象
        //3、搭建构架
        OUT:
        while (true) {
            System.out.println("请您选择以下指令进行操作:");
            System.out.println("添加商品到购物车:add");
            System.out.println("查询购物车的商品:query");
            System.out.println("修改购买商品数量:update");
            System.out.println("结算购物商品价格:pay");
            System.out.println("退出购物车:out");

            System.out.println("请您输入指令:");
            Scanner sc = new Scanner(System.in);
            String command = sc.next();
            switch (command) {
                case "add":
                    addGoods(shopCar, sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar, sc);
                    break;
                case "pay":
                    payGoods(shopCar);
                    break;
                case "out":
                    break OUT;
                default:
                    System.out.println("您输入的指令有误!");
                    break;
            }
        }
    }

    //结算购物车商品购买价格
    //商品价格 * 商品数量
    private static void payGoods(Goods[] shopCar) {
        queryGoods(shopCar);
        double prices = 0;
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] != null) {
                prices += shopCar[i].price * shopCar[i].numGoods;
            } else {
                break;
            }
        }
        System.out.println("结算金额:" + prices);
    }

    //修改购物车商品的数量
    //分析:按照商品的编码去查找商品,将这个查找商品的过程使用方法实现
    private static void updateGoods(Goods[] shopCar, Scanner sc) {
        while (true) {
            System.out.println("请输入您要修改商品数量的编号:");
            int numGoods = sc.nextInt();
            Goods g = getGoods(shopCar, numGoods);//查找需要修改的数组对象
            if (g == null) {
                System.out.println("购物车没有此商品");
            } else {
                System.out.println("请输入" + g.name + "商品修改的数量:");
                g.numGoods = sc.nextInt();
                System.out.println("修改完成!");
                queryGoods(shopCar);
                break;
            }
        }
    }

    //查询购物车
    private static void queryGoods(Goods[] shopCar) {
        //shopCar[i]=[g1,g2,...];
        System.out.println("---------------购物车商品---------------");
        System.out.println("编号" + "\t\t\t" + "名称" + "\t\t\t" + "价格" + "\t\t\t" + "数量");
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] != null) {
                System.out.println(shopCar[i].id + "\t\t\t" + shopCar[i].name + "\t\t\t" + shopCar[i].price + "\t\t\t" + shopCar[i].numGoods);
            } else {
                break;
            }
        }
    }

    //添加商品到购物车
    private static void addGoods(Goods[] shopCar, Scanner sc) {
        Goods g = new Goods();
        //录入商品信息
        System.out.println("请输入添加商品的编号:");
        g.id = sc.nextInt();
        System.out.println("请输入添加商品的名称:");
        g.name = sc.next();
        System.out.println("请输入添加商品的价格:");
        g.price = sc.nextDouble();
        System.out.println("请输入添加商品的数量:");
        g.numGoods = sc.nextInt();

        //将这个商品信息封装成一个商品对象放进数组对象
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                shopCar[i] = g;//将这个商品对象的地址给数组
                break;
            }
        }
        System.out.println("您的商品" + g.name + "成功添加进购物车!");
    }

    //根据编号查找商品信息
    public static Goods getGoods(Goods[] shopCar, int num) {
        for (int i = 0; i < shopCar.length; i++) {
            //Goods g = shopCar[i];
            if (shopCar[i] != null) {
                if (num == shopCar[i].id) {
                    return shopCar[i];
                }
            } else {
                return null;
            }
        }
        return null;
    }
}


使用JavaBean改造购物车

public class Goods {
    private int id;
    private String name;
    private double price;
    private int numGoods;

    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 getNumGoods() {
        return numGoods;
    }

    public void setNumGoods(int numGoods) {
        this.numGoods = numGoods;
    }
}
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        //1、定义商品类,用于创建商品对象
        //2、定义购物车对象:使用数组对象来表示
        Goods[] shopCar = new Goods[100];//[null,null,...]//数组对象
        //3、搭建构架
        OUT:
        while (true) {
            System.out.println("请您选择以下指令进行操作:");
            System.out.println("添加商品到购物车:add");
            System.out.println("查询购物车的商品:query");
            System.out.println("修改购买商品数量:update");
            System.out.println("结算购物商品价格:pay");
            System.out.println("退出购物车:out");

            System.out.println("请您输入指令:");
            Scanner sc = new Scanner(System.in);
            String command = sc.next();
            switch (command) {
                case "add":
                    addGoods(shopCar, sc);
                    break;
                case "query":
                    queryGoods(shopCar);
                    break;
                case "update":
                    updateGoods(shopCar, sc);
                    break;
                case "pay":
                    payGoods(shopCar);
                    break;
                case "out":
                    break OUT;
                default:
                    System.out.println("您输入的指令有误!");
                    break;
            }
        }
    }

    //结算购物车商品购买价格
    //商品价格 * 商品数量
    private static void payGoods(Goods[] shopCar) {
        queryGoods(shopCar);
        double prices = 0;
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] != null) {
                prices += shopCar[i].getPrice() * shopCar[i].getNumGoods();
            } else {
                break;
            }
        }
        System.out.println("结算金额:" + prices);
    }

    //修改购物车商品的数量
    //分析:按照商品的编码去查找商品,将这个查找商品的过程使用方法实现
    private static void updateGoods(Goods[] shopCar, Scanner sc) {
        while (true) {
            System.out.println("请输入您要修改商品数量的编号:");
            int numGoods = sc.nextInt();
            Goods g = getGoods(shopCar, numGoods);//查找需要修改的数组对象
            if (g == null) {
                System.out.println("购物车没有此商品");
            } else {
                System.out.println("请输入" + g.getName() + "商品修改的数量:");
                g.setNumGoods(sc.nextInt());
                System.out.println("修改完成!");
                queryGoods(shopCar);
                break;
            }
        }
    }

    //查询购物车
    private static void queryGoods(Goods[] shopCar) {
        //shopCar[i]=[g1,g2,...];
        System.out.println("---------------购物车商品---------------");
        System.out.println("编号" + "\t\t\t" + "名称" + "\t\t\t" + "价格" + "\t\t\t" + "数量");
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] != null) {
                System.out.println(shopCar[i].getId() + "\t\t\t" + shopCar[i].getName() + "\t\t\t" + shopCar[i].getPrice() + "\t\t\t" + shopCar[i].getNumGoods());
            } else {
                break;
            }
        }
    }

    //添加商品到购物车
    private static void addGoods(Goods[] shopCar, Scanner sc) {
        Goods g = new Goods();
        //录入商品信息
        System.out.println("请输入添加商品的编号:");
        g.setId(sc.nextInt());
        System.out.println("请输入添加商品的名称:");
        g.setName(sc.next());
        System.out.println("请输入添加商品的价格:");
        g.setPrice(sc.nextDouble());
        System.out.println("请输入添加商品的数量:");
        g.setNumGoods(sc.nextInt());

        //将这个商品信息封装成一个商品对象放进数组对象
        for (int i = 0; i < shopCar.length; i++) {
            if (shopCar[i] == null) {
                shopCar[i] = g;//将这个商品对象的地址给数组
                break;
            }
        }
        System.out.println("您的商品" + g.getName() + "成功添加进购物车!");
    }

    //根据编号查找商品信息
    public static Goods getGoods(Goods[] shopCar, int num) {
        for (int i = 0; i < shopCar.length; i++) {
            //Goods g = shopCar[i];
            if (shopCar[i] != null) {
                if (num == shopCar[i].getId()) {
                    return shopCar[i];
                }
            } else {
                return null;
            }
        }
        return null;
    }
}

你可能感兴趣的:(Java学习,java,架构,开发语言)