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

Java学习大作业-购物车系统(一)

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

Java学习大作业-购物车系统(一)

团队介绍
成员姓名负责任务
连正代码
徐宁阳前期调查,流程图,UML类图
Git地址
Gitee地址 前期调查

需要的类有商品、商品数量、商品单价、单商品总消费、所有商品总数量及总价。


系统功能结构图


系统描述

商家可以添加商品及单价
用户可以查看、添加到购物车,然后在购物车中添加商品数量或移除商品,亦或者清空购物车,可查看已购买商品总价及总数。

UML类图

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

商品的名称、单价等属性是购物车中商品的基本属性,在程序的运行过程中保持不变,因此,可对其进行封装。面向对象的封装性能够把信息封装,保证数据的完整和安全,使数据不会发生变化。
采用private进行修饰,可以防止其他操作改变其值,使用时只能通过getter和setter方法进行访问。

实现截图

本次实现的关键代码
放入商品
public int putin(goods g)
    {
        shopping s=new shopping(g);
        int flag=0;
        int index=-1;
        if(this.list.size()==0)
        {
            this.list.add(s);
            totalquantity++;
            totalprice+=g.getPrice();
        }
        else
        {
            for(int i=0;i 
shopping类 
public class shopping {
    private goods g;
    private int count = 0;
    private double totalprice=0;
    public goods getG()
    {
        return g;
    }
    public void setG(goods g)
    {
        this.g=g;
    }
    public int getCount()
    {
        return count;
    }
    public void setCount(int count)
    {
        this.count=count;
    }
    public double getTotalprice()
    {
        return totalprice;
    }
  public void setTotalprice(double totalprice)
  {
      this.totalprice=totalprice;
  }
  public shopping(goods g) {
      this.g = g;
      this.count = 1;
      this.totalprice = g.getPrice();
  }
  public void add()
  {
      this.count++;
      this.totalprice+=g.getPrice();
  }
  public void reduce()
  {
      this.count--;
      this.totalprice-=g.getPrice();
  }

    @Override
    public String toString() {
        return "shopping{" +
                "g=" + g +
                ", count=" + count +
                ", totalprice=" + totalprice +
                '}';
    }
}

商品类
public class goods {
    private String name;
    private double price;
    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 goods(String name,double price)
    {
        this.name=name;
        this.price=price;
    }
    @Override
    public boolean equals(Object obj)
    {
     if (this == obj)
     {
        return true;
    }
        if (obj == null)
        {
        return false;
    }
        if (getClass() != obj.getClass())
        {
        return false;
    }
        goods other = (goods) obj;
        if (name == null)
        {
        if (other.name != null)
        {
            return false;
        }
        }
        else if (!name.equals(other.name))
        {
        return false;
         }
        if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
        {
        return false;
        }
        return true;
}
    public String toString() {
        return "goods{" +
                "name='" + name + ''' +
                ", price=" + price +
                '}';
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/295191.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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