Java面向对象设计——购物车·

2021/10/5 1:40:50

本文主要是介绍Java面向对象设计——购物车·,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 人员分工
  • 前期调查
    • 购物流程
    • 购物车
      • 1.系统功能流程图
      • 2.系统业务流程图
      • 3.UML类图
  • 代码实现
    • Mall
      • 功能
    • Commodity
      • 功能:
    • ShoppingCart
      • 功能:
      • 购物车添加商品流程实现
      • 删除购物车中商品流程实现
    • 本系统哪里体现了面向对象的封装性

人员分工

任务 组员
编码规范,面向对象设计 王鑫
前期调查,功能设计 杨佳琴

前期调查

购物流程

首先, 用户 在商城(Mall)中挑选自己需要的 商品(Commodity) ,将它们 加入
购物车(ShoppingCart) 中,在页面中 结算(CheckOut) ,支付完成。

购物车

1.系统功能流程图

2.系统业务流程图

3.UML类图

代码实现

Mall

package ShoppingCart;

import java.util.ArrayList;
import java.util.List;
public class Mall {
	private List<Commodity> commodities = new ArrayList<>();
    {//初始化商品
        commodities.add(new Commodity("苹果",1, 3.2));
        commodities.add(new Commodity("香蕉",2, 1.2));
        commodities.add(new Commodity("猕猴桃",3, 3.5));
        commodities.add(new Commodity("西瓜",4, 15.8));
        commodities.add(new Commodity("甘蔗",5, 2.3));
        commodities.add(new Commodity("梨子",6, 2.6));
        commodities.add(new Commodity("西柚",7, 5.7));
        commodities.add(new Commodity("柠檬",8, 1.5));
        commodities.add(new Commodity("芒果",9, 3.4));
        commodities.add(new Commodity("石榴",10, 4.8));
    }
	 

    public List<Commodity> show() {// 展示商品
        return this.commodities;
     }
	
    public Commodity searchGoodById(int id) {// 按编号搜索商品
        int i = 0;
        for (i = 0; i < commodities.size(); i++) {
            if (commodities.get(i).getId() == (id)) {
                return commodities.get(i);
            }
        }
       //找不到
            System.out.println("对不起,您要找的商品我们没有");
            return null;
    }
}
功能

1.初始化商品
2. 展示商城中的商品
3. 按编号搜索商品,若正确,输出对应商品;反之,则无法找到该商品。

Commodity

package ShoppingCart;

public class Commodity {
	 private String name;//名称
	    private Integer id;//编号
	    private double price;//价格
	    //private int number;//商品数量
	    
	    
	    public Commodity(String nae,Integer id,double price)
	    {
	    	this.name=name;
	    	this.price=price;
	    	this.id=id;
	    	//this.number=number;
	    }


		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 Integer getId() {
			return id;
		}


		public void setId(Integer id) {
			this.id = id;
		}


//		public int getNumber() {
//			return number;
//		}
//
//
//		public void setNumber(int number) {
//			this.number = number;
//		}

}
功能:

展示商品信息:名称,编号,价格,商品数量等。

ShoppingCart

package ShoppingCart;

import java.util.ArrayList;
import java.util.List;

public class MyCart {
	  private ArrayList<Item> List = new ArrayList<Item>();
	  
	  public void Add(Commodity e)//加购
	  {
		  int index = findById(e.getId());
	        if (index == -1) {
	            List.add(new Item(e));//将此商品添加到购物车

	        } else {
	            List.get(index).increase();
	        }
		   
		  
		
	  }
	 
	public boolean DeleteCommodity(Integer id)//删除
	  {
		 if (id == null)
	            return false;
	        int index = findById(id);
	        if (index == -1) {// 未找到
	            return false;
	        } else {
	            Item entry = this.List.get(index);
	            if (entry.getNumber() <= 1) {
	                this.List.remove(index);//小于1,把这个商品从购物车里删除
	            } else {
	                entry.decrease();
	            }
	        }
	        return true;
	  }
	  public double CheckOut(){
		  double sum=0;
		  for (int i = 0; i < List.size(); i++) {
			 Item x= List.get(i);
              if(x.getNumber()!=0)
              {
            	  sum+=x.number*x.item.getPrice();
              }
          }
		  
		return sum;//结账
		}
	  
	@Override
	public String toString() {//展示
		return "购物车 [List=" + List + ", CheckOut=" + CheckOut() + "]";
	}
	  
	 private int findById(Integer id) {//找商品的在数组的下标
		  int index = -1;
	        if (List.size() > 0) {
	            for (int i = 0; i < List.size(); i++) {
	                if (List.get(i).getItem().getId() == id)
	                    index = i;
	            }
	        }
	        return index;
	}
}

功能:
  1. 购物车添加商品
  2. 购物车删除商品
  3. 展示购物车所有商品
  4. 清空购物车
购物车添加商品流程实现

删除购物车中商品流程实现

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

在Community中,把商品的展示和按编号搜索商品开作为两个类中的方法。就可以创建这个类的时候,使用这个类的时候就能够直接调用这个函数。给人以整体的感觉。
而MyCart也是,其中的add、CheckOut和delete也是作为购物车中会拥有的功能来实现,这样使用起来直观的像是一个购物车的功能。



这篇关于Java面向对象设计——购物车·的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程