链表的定义及使用 综合实战:超市购物车

综合实战:超市购物车使用面向对象的概念表示出下面的生活场景:巧明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。1、定义出一个商品的标准:interface IGoods{//定义商品标准 public String getNmae(); public double getPrice();}2、定义购物车处理标准interface IShopCar{//购物车 public void add(IGoods goods);//添加商品信息 pu
摘要由CSDN通过智能技术生成

综合实战:超市购物车

使用面向对象的概念表示出下面的生活场景:巧明去超市买东西,所有买到的东西都放在了购物车之中,最后到收银台一起结账。

1、定义出一个商品的标准:

interface IGoods{
   //定义商品标准
	public String getNmae();
	public double getPrice();
}

2、定义购物车处理标准

interface IShopCar{
   //购物车
	public void add(IGoods goods);//添加商品信息
	public void delete(IGoods goods);//删除商品
	public Object getAll();//获得购物车中的全部商品信息
	
}

3、定义购物车的一个实现类

class ShopCarImpl implements IShopCar{
   //购物车
	private  ILink<IGoods> allGoodses = new ILinkImpl<IGoods>();
	public void add(IGoods goods) {
		this.allGoodses.add(goods);
	}
	public void delete(IGoods goods) {
		this.allGoodses.remove(goods);
	}	
	public Object [] getAll() {
		return this.allGoodses.toArray();
	}
}

4、定义收银台

class Cashier{
   //收银台
	private IShopCar shopCar;
	public Cashier(IShopCar shopCar) {
		this.shopCar = shopCar;
	}
	public double allPrice() {
   //计算总价
		double all = 0.0;
		Object result [] = this.shopCar.getAll();
		for(Object obj : result) {
			IGoods goods = (IGoods) obj;
			all += goods.getPrice();
		}
		return all;
	}
	public int allCount() {
   //商品数量
		return this.shopCar.getAll().length;
	}
}

5、定义商品信息:

图书:

class Book implements IGoods{
	private String name;
	private double price;
	public Book(String name, double price) {
		this.name = name;
		this.price = price;
	}
	public String getName() {
		return this.name;
	}
	public double getPrice() {
		return this.price;
	}
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		if (!(obj instanceof Book)) {
			return false;
		}
		Book book = (Book) obj;
		return this.name.equals(book.name) && this.price == book.price;
	}
	public String toString() {
		return "【图书信息】名称:" + this.name + "、价格:" + this.price;
	}
}

书包:

class Bag implements IGoods{
	private String name;
	private double price;
	public Bag (String name, double price) {
		this.name = name;
		
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值