脚本宝典收集整理的这篇文章主要介绍了面向对象大作业-第一次-购物车,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
任务 | 姓名 |
---|---|
前期调查与功能设计、博客制作 | 郑文华 |
代码编写 | 任媛 |
学会使用讲故事的方式找到系统中可能包含的类与属性 理解封装
当今,网上购物早已成为一种常态,网上购物给人们提供了很大的便利。下面,以淘宝为例,来看看购物车系统都有些什么功能。首先,在商品的界面,我们清楚的可以看见商品的名称以及单价,还有一些其他商品信息介绍。我们可以对商品将添加入购物车。
在购物车里,可以增加商品数量,减少商品数量,删除商品,查找商品等。
勾选商品,进行结算。
将选定的商品放入购物车,在购物车内可以存放多种类型的商品,用户还可以删除选中商品、清空购物车、增加或减少商品数量,计算购物车内所有商品的数量、价格,用户可以在购物车内进行结算,生成订单。
public interface shopDAO {
public boolean addproduct(Product e,int count);//添加商品
public boolean reduceproduct(Integer productId);//减少商品数量
public boolean increase(Integer productId);//增加商品数量
public boolean delete(Integer productId);//删除商品
public int findById(Integer productId);//查找商品
public void diplayproduct();//展示商品
public double checkout();//结算
}
package Shoppingcart;
public class Product {
private Integer productId;// 商品编号
private String productName;// 商品名称
private double price;// 单价
public Product(Integer productId, String productName,double price) {
this.productId = productId;
this.productName = productName;
this.price = price;
}
public Integer getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [productId=" + productId + ", productName=" + productName + ", price=" + price + "]";
}
}
package Shoppingcar;
import java.util.Scanner;
public class ShopAarrayDAO implements ShopDAO{
Scanner sc = new Scanner(System.in);
private CommodityItem[] itemList = new CommodityItem[20];
private int size=0;
public boolean addproduct(Commodity e,int count) //添加商品
{
int index = findById(e.getId());
if (index == -1) {//购物车没有此商品
itemList[size++] = new CommodityItem(e,count);
} else {
itemList[index].increase(count);
}
return true;
}
public boolean reduceproduct(Integer id) //减少商品数量
{
int index = findById(id);
int a = sc.nextInt();
CommodityItem entry = itemList[index];
if (entry.getCount()-a <= 0) {// 商品数量为0,删除
delete(index);
size--;
} else {
entry.decrease(a);
}
return true;
}
private void delete(int i) {
for (int j = i; j < size; j++) {
itemList[j] = itemList[j+1];
}
}
public boolean increase(Integer id) //增加商品数量
{
int index = findById(id);
int a = sc.nextInt();
CommodityItem entry = itemList[index];
entry.increase(a);
return true;
}
public boolean delete(Integer id) //删除商品
{
int index = findById(id);
delete(index);
size--;
return true;
}
public void diplayproduct()//展示商品
{
for (int i = 0; i < size; i++) {
System.out.println(itemList[i].getProduct().toString()+" 数量:"+itemList[i].getCount()+" 总价:"+itemList[i].total());
}
}
public int findById(Integer id) //查找商品
{
for (int i = 0; i < size; i++) {
if (itemList[i].getProduct().getId().equals(id))
return i;
}
return -1;
}
public double checkout() //结算
{
double x = 0;
for (int i = 0; i < size; i++) {
x = x + itemList[i].total();
}
return x;
}
}
以上是脚本宝典为你收集整理的面向对象大作业-第一次-购物车全部内容,希望文章能够帮你解决面向对象大作业-第一次-购物车所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。