zoukankan      html  css  js  c++  java
  • Java面向对象设计——购物车·

    人员分工

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

    前期调查

    购物流程

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

    购物车

    1.系统功能流程图

    2.系统业务流程图

    3.UML类图

    代码实现

    Mall

    package shopping;
    
    import java.util.ArrayList;
    import java.util.List;
    public class Mall {
    	private List<Commodity> commodities = new ArrayList<>();
        {//初始化商品
    //    	Commodity a1=new Commodity("苹果",1, 3.2);
    //    	int a2=5;
            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 void show() {
    		for(Commodity e:commodities)
    		{
    			System.out.println("商品名称:"+e.getName()+"  商品编号:"+e.getId()+"商品单价:"+e.getPrice());
    		}
    	}
    
    	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;
        }
    
    	public List<Commodity> getCommodities() {
    		return commodities;
    	}
    
    	public void setCommodities(List<Commodity> commodities) {
    		this.commodities = commodities;
    	}
        
    }
    
    
    功能

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

    Commodity

    package shopping;
    
    public class Commodity {
    	 private String name;//名称
    	    private Integer id;//编号
    	    private double price;//价格
    	    //private int number;//商品数量
    	    
    	    public Commodity(String name,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;
    //		}
    
    }
    
    
    功能:

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

    MyCart

    package shopping;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MyCart {
    	  private ArrayList<Item> List = new ArrayList<Item>();
    	  
    	  public void Add(Item e)//加购
    	  {
    		  int index = findById(e.getItem().getId());
    	        if (index == -1) {
    	            List.add(e);//将此商品添加到购物车
    
    	        } else {//购物车内已经这个物品
    	        	Item exiItem=List.get(index);
    	            exiItem.setNumber(e.count+exiItem.getNumber());//原有加上后来
    	        }
    		   
    		
    	  }
    	 
    	public boolean DeleteCommodity(Integer id)//删除
    	  {
    		 if (id == null)
    	            return false;
    		 if(List.size()<=0)
    		 {
    			 System.out.println("购物车为空!不允许此操作");
    			 return false;
    		 }
    	        int index = findById(id);
    	        if (index == -1) {// 没找到
    	            return false;
    	        }
    	        else
    	        {
    	            Item entry = this.List.get(index);
    	            this.List.remove(index);//在购物车了删除这个商品
    	        }
    	        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.count*x.item.getPrice();
                  }
              }
    		  
    		return sum;//结账
    		}
    	  
    	public void show() {
    		for(Item e:List)
    		{
    			System.out.println("商品名称:"+e.getItem().getName()+"  商品编号:"+e.getItem().getId()+"商品单价:"+e.getItem().getPrice()+" 商品数量:"+e.getNumber());
    		}
    	}
    	 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;
    	}
    
    	public ArrayList<Item> getList() {
    		return List;
    	}
    
    	public void setList(ArrayList<Item> list) {
    		List = list;
    	}
    	 
    	 
    }
    
    
    
    
    MyCart的内部类Item
    package shopping;
    
    public class Item {
    	
            Commodity item;
            Integer count;
    
            public Item(Commodity item,Integer count) {
                this.item = item;
                this.count=count;
            }
            
            public String toString() {
                return "商品 [item=" + item + ", piece=" + count + "]";
            }
    
            public Commodity getItem() {
    			return item;
    		}
    
    		public void setItem(Commodity item) {
    			this.item = item;
    		}
    
    		public Integer getNumber() {
    			return count;
    		}
    
    		public void setNumber(Integer number) {
    			this.count = number;
    		}
    
    
        }
    
    
    
    
    功能:
    1. 购物车添加商品
    2. 购物车删除商品
    3. 展示购物车所有商品
    4. 结算购物车
    购物车添加商品流程实现

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

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

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

  • 相关阅读:
    linux 命令
    linux 后门防范
    linux date
    shell 常用参数
    linux rule策略
    vlan对服务器要注意的事情
    STL vector——c++
    蛇形矩阵
    简单a+b
    小 X 与数字(ten)
  • 原文地址:https://www.cnblogs.com/yang123789/p/15366641.html
Copyright © 2011-2022 走看看