java购物车案例

任务 姓名
编码规范、前期调查与功能设计 林小强
面向对象设计、PPT制作或博客制作 陈泽役

1.前期调查、

  • 可以在此直接搜索商品

  • 选择对应商品加入购物车

  • 购物车面板

  • 删除商品

2.系统功能结构图

3.UML类图

4.本系统哪里体现了面向对象的封装性

package cart;

public class Goods {
    private int id; //商品id
    private String name;//商品名称
    private double 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;
	}
	@Override
	public String toString() {
		return "goods [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
	public Goods(String name,double price){
		this.name=name;
		this.price=price;
	}
}
  • 其中public、private都体现了面向对象的封装性,private属性私有化,通过使用private修饰符关键字进行修饰,外部无法直接访问,只能通过getters来访问。

5.项目包结构与关键代码

  • 商品类
package cart;

public class Goods {
    private int id; //商品id
    private String name;//商品名称
    private double 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;
	}
	@Override
	public String toString() {
		return "goods [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
	public Goods(String name,double price){
		this.name=name;
		this.price=price;
	}
}
package cart;

public class Entry {
    private Goods item;
    private int number;
    private double totalPrice;
    //private ArrayList<Hash>HashList=new ArrayList<>();//商品数组
	public Goods getItem() {
		return item;
	}
	public void setItem(Goods item) {
		this.item = item;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
		setTotalPrice();
	}
	public double getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice() {
		totalPrice = item.getPrice()*number;
	}
	@Override
	public String toString() {
		return "entry [item=" + item + ", number=" + number + ", totalprice=" + totalPrice + "]";
	}
	public Entry(Goods item,int number) {
		this.item=item;
		this.number=number;
		setTotalPrice();
	}
	
}


  • 购物车类
package cart;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
public class Cart {
	HashMap<Integer,Entry> mp=new HashMap<>();//id,entry
	private ArrayList<Hash>HashList=new ArrayList<>();//判断id
	public void addGoods(int key,Entry item) {
		if(mp.containsKey(key)){
			//存在则增加数量
			mp.get(key).setNumber(mp.get(key).getNumber()+item.getNumber());//原来的数量+新增的数量
		} else {
			HashList.add(new Hash(item.getItem().getName(),key));
			mp.put(key,item);//不存在则增加商品
			mp.get(key).getItem().setId(key);//置id
		}
	}
	public boolean modifyNumber(int key,int number){//修改商品的数量 后续调用数据存储统一id使用
		if(!mp.containsKey(key))
			return false;
		mp.get(key).setNumber(number);//修改数量
		return true;
	}
	public boolean deleteGoods(int number) {//删除某种商品 后续调用数据存储统一id使用
		if(mp.containsKey(number))
			mp.remove(number);
		else 
			return false;
		return true;
	}
	
	public void showAll(){//查看订单信息
	    Collection<Entry> goods = mp.values();//获得“值的集”
	    Iterator<Entry> iterator = goods.iterator();//迭代器
	    while(iterator.hasNext()){
	        Entry productItem = iterator.next();
	        Goods product = productItem.getItem();
	        System.out.println("商品ID:"+product.getId()+",商品名称:"+product.getName()+",单价:"+product.getPrice()
	        +",数量:"+productItem.getNumber()+",小计:"+productItem.getTotalPrice());
	        }
	 }
	public void getCartTotalPrice() {
		Collection<Entry> goods = mp.values();
        Iterator<Entry> iterator = goods.iterator();
        double total=0;
        while(iterator.hasNext()){
            Entry productItem = iterator.next();
            total+=productItem.getTotalPrice();
	        }
        System.out.println("当前购物车总价格为:"+total+"元");
        //return total;
	}
	public void clearCart() {//清空购物车
		mp.clear();
	}
	//@SuppressWarnings("unlikely-arg-type")
	public String getid(Entry pro){//判断是否出现过
		 for(int i=0;i<HashList.size();i++) {
			 if(HashList.get(i).name.equals(pro.getItem().getName()))
				 return String.valueOf(HashList.get(i).key);
		 }
		 StringBuffer now = new StringBuffer(new SimpleDateFormat("yyyyMMdd").format(new Date()));
		 int n = (int)(Math.random()*9000.0D+1000.0D);
		 return now.append(n).toString().substring(4);// "202110063548"
	}
	public String getUUID(){
		UUID uuid = UUID.randomUUID();
		String str = uuid.toString();
		String uuidStr = str.replace("-", "");
		return uuidStr;
	}
}

  • 添加商品

  • 删除商品

posted @ 2021-10-10 20:46  头秃了没  阅读(369)  评论(0编辑  收藏  举报