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

0.人员分工。

任务 姓名
编码规范、前期调查与功能设计 傅肇杨
面向对象设计、博客制作 杨振鹏

1.前期调查。

主要类设想

1.product类

存放商品的编号
存放商品的名称
存放商品的价格

2.ProductItem

存放购物车里的商品
存放购物车里的商品数量

3.ShoppingCart

存放购物车中商品总价,所选中的商品条目,以及使用该购物车的用户
查看订单信息
添加,删除商品
展示购物车中所有商品
算出购物车中商品总价
清空购物车

4.test

存放测试用的数据

2.系统功能结构图。

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

4.UML类图:类的关键属性与方法、类与类之间的关系。每个类的功能描述。

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

ProductItem.java中:

public class ProductItem {
	private Product product;//购买的商品
	private int count;//商品数量

	public ProductItem() {
	}
 
	public ProductItem(Product product, int count) {
		this.product = product;
		this.count = count;
	}
 
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public double totalMoney(){//小计
		double price=product.getPrice();//获取商品单价
		return price*count;
	}


}

pubilc,private体现了面向对象的封装性

设置购买的商品和数量,以及小计等又可以体现面向对象的封装性

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

主要功能流程:

关键代码

product

用于存放商品的多种信息

package shopping;

public class Product {
	private int id;// 商品编号
	private String name;// 商品名称
	private double price;// 单价

	public Product(int id, String name,double price) {
		this.id = id;
		this.name = name;
		this.price = price;
	}

	@Override
	public String toString() {
		return "Product [id=" + id + ", name=" + name + ", price=" + price + "]";
	}

	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;
	}

	

}

productitem

存储购物车的商品信息

package shopping;

public class ProductItem {
	private Product product;//购买的商品
	private int count;//商品数量

	public ProductItem() {
	}
 
	public ProductItem(Product product, int count) {
		this.product = product;
		this.count = count;
	}
 
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public double totalMoney(){//小计
		double price=product.getPrice();//获取商品单价
		return price*count;
	}


}

Shopping Cart

对购物车功能的实现,删除,计算价格

package shopping;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
public class ShoppingCart {
private Map<Integer,ProductItem> map=new LinkedHashMap<Integer,ProductItem>();
	
	public void addProduct(Product p){//添加商品
		int productId=p.getId();
		if(map.containsKey(productId)){ //判断商品编号是否存在
			ProductItem productItem=map.get(productId);
			productItem.setCount(productItem.getCount()+1); //数量增加
		}else{
			map.put(productId, new ProductItem(p,1));//新增商品
		}
	}
	public void showAll(){//查看订单信息
		Collection<ProductItem> productItems = map.values();//获取键值
		Iterator<ProductItem> iterator = productItems.iterator();
		while(iterator.hasNext()){
			ProductItem productItem = iterator.next();
			Product product = productItem.getProduct();
			System.out.println("编号:"+product.getId()+",名称:"
			+product.getName()+",单价:"+product.getPrice()+",数量:"+productItem.getCount()
			+",小计:"+productItem.totalMoney());
		}
	}
	public boolean deleteProduct(int productId){//删除商品
		if(map.containsKey(productId)){
			map.remove(productId);
			return true;
		}
		return false;
	}
	public boolean modifyProduct(int productId,int count){//修改商品数量
		if(map.containsKey(productId)){
			if(count>=1){
				ProductItem productItem = map.get(productId);
				productItem.setCount(count);
				return true;
			}else if(count==0){//删除某商品
				deleteProduct(productId);
				return true;
			}	
		}
		return false;
	}
	
	public void clearCart(){//清空购物车
		map.clear();
	}
	
	public double totalAllMoney(){//商品总价值
		double total=0;
		Collection<ProductItem> productItems = map.values();
		Iterator<ProductItem> iterator = productItems.iterator();
		while(iterator.hasNext()){
			ProductItem productItem = iterator.next();
			double money=productItem.totalMoney();
			total+=money;
		}
		return total;
	}


}
posted @ 2021-10-07 22:09  写不下去了,淦  阅读(67)  评论(0编辑  收藏  举报