第1次任务:购物车程序的面向对象设计

本文详细介绍了京东购物车系统的实现过程,包括人员分工、系统功能结构图、类图展示,重点讲解了如何运用面向对象的封装性、继承与多态。通过实例展示了`Cart`、`Commodity`和`Store`类的设计与操作,以及如何确保数据的安全性和可维护性。
摘要由CSDN通过智能技术生成

一、人员分工

任务姓名
编码规范,面向对象设计王奥运
前期调查与功能设计,PPT制作或博客制作陈伟盛

二、前期调查

以京东为例
购物车界面:其中展示的项目有,商品名称,商品单价,商品数量,商品总价。其中商品数量可选,商品可删除。
在这里插入图片描述
结算界面:进入结算前,可选多种商品。结算时有一次确认订单,可返回购物车修改。
在这里插入图片描述

三、系统功能结构图

在这里插入图片描述

四、系统描述:一段用来描述系统功能与流程的文字,用红色字代表可能的对象(名词)或属性,用蓝色字代表可能的方法(动词)

在这里插入图片描述

五、UML类图:类的关键属性与方法、类与类之间的关系。每个类的功能描述。可使用ProcessOn绘制。

请添加图片描述
请添加图片描述
请添加图片描述

六、本系统哪里体现了面向对象的封装性。可选:哪里体现了继承与多态。

例如商类中,商品的名称单价数量总价等属性不可直接修改不可访问,只能通过对外接口如查询商品信息,修改数量,删除商品等。

七、项目包结构与关键代码:项目的包结构(为什么要这样设计包结构),主要功能(如网购物车添加、删除商品)的流程图与关键代码。

购物车类

public class Cart {
    private BigDecimal totalPrice = new BigDecimal("1");
    public Map<Commodity, Integer> cart = new HashMap<>();
    public Cart() {

    }

    @Override
    public String toString() {
        return cart + "";

    }
    public void updateCart() {
        System.out.print("请输入要修改的商品的名称:");
        Scanner Sc = new Scanner(System.in);
        String name = Sc.nextLine();
        Commodity commodity = findCommodity(name);
        if (commodity != null) {
            System.out.print("请输入您要修改的数量:");
            int number = Integer.valueOf(Sc.nextLine());
            int addnumber = cart.get(commodity) - number;
            if (!updateNumber(commodity, number)) {
                System.out.println("您输入的数量有误,请重新输入");
                updateCart();
            }
//            else {
//                heixin.commoditys.put(commodity, heixin.commoditys.get(commodity) + addnumber);
//            }
        } else {
            System.out.println("购物车内没有该商品,请重新输入");
            updateCart();
        }
    }
    public boolean updateNumber(Commodity commodity, int number) {
        if (cart.get(commodity) < number) {
            return false;
        } else if (number == 0) {
            cart.remove(commodity);
            return true;
        }
        cart.put(commodity, number);
        return true;
    }

    public void add(Commodity goods, int number) {
        if(cart.containsKey(goods)){
            cart.put(goods, cart.get(goods)+number);
        }else {
            cart.put(goods, number);
        }
    }



    public BigDecimal price() {
        if (cart.isEmpty()) {
            return new BigDecimal("0");
        }
        totalPrice = new BigDecimal("0");
        cart.forEach((K, V) -> {
            BigDecimal k = new BigDecimal(K.getPrice() + "");
            BigDecimal v = new BigDecimal(V + "");
            BigDecimal temp1 = k.multiply(v);
            totalPrice = totalPrice.add(temp1);
            //System.out.println(totalPrice);
        });
        return totalPrice;
    }

    public Commodity findCommodity(String name) {
        for (Commodity commodity : cart.keySet()) {
            if (commodity.getName().equals(name)) {
                return commodity;
            }
        }
        return null;
    }
    public  void showCart() {
        System.out.println("===============您的购物车=============");
        System.out.println("ID\t\t\tname\t\t\t\tprice\t\t\t\tnumber");
        cart.forEach((K,V)-> System.out.println(K+"\t"+V));
    }
    public void submitOrder() throws IOException {
        Writer fw =new FileWriter("src/com/jmu/shoppingcart/order.txt");
        cart.forEach((K,V)->{
            try {
                fw.write(K.toString()+V+"\n");
            } catch (IOException e) {
                System.out.println(e);
            }
        });
        fw.flush();
    }

    public Commodity getCommdityByName(String name) {
        for (Commodity commodity : cart.keySet()) {
            if(commodity.getName().equals(name)){
                return commodity;
            }
        }
        return null;
    }

    public void delete(Commodity commodity) {
        cart.remove(commodity);
    }
}


商品类

public class Commodity {
    private String name;
    private double price;
    private int  ID;

    public Commodity(String name, double price, int ID) {
        this.name = name;
        this.price = price;
        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 getID() {
        return ID;
    }

    public void setID(int  ID) {
        this.ID = ID;
    }
    @Override
    public String toString() {
        return ID+"\t\t\t"+name+"\t\t\t\t"+price+"\t\t\t\t";
    }

}

商店类

public class Store {
    private static int ID=0;
    Map<Commodity,Integer> commoditys= new LinkedHashMap<>();
    public void add(String name,double price,int number){
        Commodity commodity=new Commodity(name,price,ID++);
        commoditys.put(commodity,number);
    }

    public void showStory(){
        System.out.println("ID\t\t\tname\t\t\t\tprice\t\t\t\tnumber");
        commoditys.forEach((K,V)->{
            System.out.println(K+"\t"+V);
        });
    }
    public Commodity findCommodity(String name) {
        for (Commodity commodity : commoditys.keySet()) {
            if(commodity.getName().equals(name)){
                return commodity;
            }
        }
        return null;
    }

    public int getRepertory(Commodity commodity) {
        return commoditys.get(commodity);
    }

    public void update(Commodity commodity,int number) {
        commoditys.put(commodity,commoditys.get(commodity)-number);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值