| 成员姓名 | 负责任务 |
|---|---|
| 连正 | 代码 |
| 徐宁阳 | 前期调查,流程图,UML类图 |
Gitee地址 前期调查
需要的类有商品、商品数量、商品单价、单商品总消费、所有商品总数量及总价。
系统功能结构图
系统描述
商家可以添加商品及单价
用户可以查看、添加到购物车,然后在购物车中添加商品数量或移除商品,亦或者清空购物车,可查看已购买商品总价及总数。
本系统体现的面向对象的封装性
商品的名称、单价等属性是购物车中商品的基本属性,在程序的运行过程中保持不变,因此,可对其进行封装。面向对象的封装性能够把信息封装,保证数据的完整和安全,使数据不会发生变化。
采用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 +
'}';
}
}



