栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

前期调查以京东为例,
购物车中展示的项目有:商品名称,商品单价,商品数量,商品总价。其中商品数量可选,商品可删除。

进入结算前,可选多种商品。结算时有一次确认订单,可返回购物车修改。

三、系统功能结构图

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

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



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

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

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

购物车类

package com.jmu.shoppingcart;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Cart {
    private BigDecimal totalPrice = new BigDecimal("1");
    Map 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("IDtttnamettttpricettttnumber");
        cart.forEach((K,V)->{
            System.out.println(K+"t"+V);
        });
    }
}

商品类

package com.jmu.shoppingcart;

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+"ttt"+name+"tttt"+price+"tttt";
    }

}

商店类

package com.jmu.shoppingcart;

import java.util.HashMap;
import java.util.linkedHashMap;
import java.util.Map;

public class Store {
    private static int ID=0;
    Map 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("IDtttnamettttpricettttnumber");
        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);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号