综合实战:超市购物车

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

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;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;}Bag  bag = (Bag ) obj;return this.name.equals(bag.name) && this.price == bag.price;}public String toString() {return "【背包信息】名称:" + this.name + "、价格:" + this.price;}
}

6、进行代码测试编写

public class ShopCarDemo {public static void main(String[] args) {IShopCar car = new ShopCarImpl();car.add(new Book("Java开发", 79.8));car.add(new Book("数据结构", 89.8));car.add(new Bag("小强背包", 879.8));Cashier cas = new Cashier(car);System.out.println("总价格:" + cas.allPrice() + "、购买总数量:" + cas.allCount());}
}

整体的代码都是基于链表的功能实现的。

完整代码:

interface ILink2<E>{//设置泛型避免安全隐患public void add(E e);//增加数据public int size(); //获取数据的个数public boolean isEmpty();//判断是否空集合public Object [] toArray() ;//将集合元素以数组的形式返回public E get(int index) ; //根据索引获取数据public void set(int index,E data);//修改索引数据public boolean contains (E data) ;//判断数据是否存在public void remove(E e);//数据删除public void clean() ;//清空集合
}
class LinkImpl2<E> implements ILink2<E>{private class Node {//保存节点的数据关系private E data;//保存数据private Node next;//保存下一个引用public Node(E data) {//有数据的情况下才有意义this.data = data;}//第一次调用:this =LinkImpl.root;//第二次调用:this =LinkImpl.root.next;//第三次调用: this = LinkImpl.root.next.next;public void addNode(Node newNode) { //保存新的Node数据if (this.next == null){//当前节点的下一个节点为null:this.next =newNode ; //保存当前节点}else {this.next.addNode (newNode) ;}}//第一次调用:this =LinkImpl.root//第二次调用:this =LinkImp .root.next//第三次调用: this =LinkImp.root.next.nextpublic void toArrayNode () {LinkImpl2.this.returnData [LinkImpl2.this.foot ++]= this.data;if (this.next !=null){//还有下一个数据this.next.toArrayNode() ;}}public E getNode (int index){if(LinkImpl2.this.foot ++ ==index) {//索引相同return this .data ; //返回当前数据}else {return this .next.getNode (index);}}public void setNode(int index,E data) {if(LinkImpl2.this.foot ++ == index) {//索引相同this .data = data ; //修改数据}else {this.next.setNode(index,data);}}public boolean containsNode (E data){//if(this.data.equals(data)){//对象比较if(data.equals(this.data)){//对象比较return true ;}else {if (this.next ==null) {//没有后续节点了return false ; //找不到}else {return this.next.containsNode (data) ; //向后继续判断}}}public void removeNode (Node previous,E data){if (this.data.equals (data) ){previous.next = this.next ; //空出当前节点}else {if (this.next != null) {//有后续节点this.next.removeNode(this,data) ; //向后继续茏}}}}//-------------- 以下为Link类中定义的成员 --------------private Node root;//保存根元素private int count;//保存数据个数private int foot ; //描述的是操作数组的脚标private Object [] returnData ; //返回的数据保存//-------------- 以下为Link类中定义的方法 --------------public void add(E e) {if (e == null) {//保存的数据为空return;//方法调用直接结束}//数据本身是不具有关联特性的,只有Node类有,那么要想实现关联处理就必须将数据包装在Node之中Node newNode = new Node (e) ; //创建一个新的节点if (this.root == null ){//现在没有根节点this.root = newNode ; //第一个节点作为根节点}else {//根节点存在this.root.addNode (newNode) ;//将新节点存在合适位置}this.count++;}public int size() {return this.count;}public boolean isEmpty() {//return this.root == null;return this.count == 0;}public Object[] toArray(){if(this.isEmpty()){//空集合return null ; //现在没有数据}this.foot = 0 ; //脚标清零this.returnData = new Object [this.count] ;//根据已有的长度开辟数组this.root. toArrayNode ();//公利用Node类进行递归数据获取return this.returnData ;}public E get(int index){if (index >=this.count) {//索引应该在指定的范围之内return null ;}//索引数据的获取应该由Node类完成this.foot =0 ; //重置索引的下标return this.root.getNode(index);}public void set(int index,E data) {if (index >= this.count) {//索引应该在指定的范围之内return ;//方法结束}//索引数据的获取应该由Node类完成this.foot =0 ; //重置索引的下标this.root.setNode(index,data);//修改数据}public boolean contains(E data) {if (data ==null){return false ; //没有数据}return this.root.containsNode (data) ;//交给Node天判断}public void remove(E data) { if (this.contains(data)) {//判断数据是否存在if( this.root.data.equals(data)) {//根节点为要删除的节点this.root = this.root.next;//根的下一个节点}else {//交由Node类负责删除this .root.next.removeNode (this.root,data) ;}this.count --;}}public void clean() {this.root = null ;//后续的所有节点都没了this.count = 0; //个数清零}
}
interface IGoods{//定义商品标准public String getName();public double getPrice();
}
interface IShopCar{//购物车public void add(IGoods goods);//添加商品信息public void delete(IGoods goods);//删除商品public Object [] getAll();//获得购物车中的全部商品信息}
class ShopCarImpl implements IShopCar{//购物车private  ILink2<IGoods> allGoodses = new LinkImpl2<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();}
}
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;}
}
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;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;}Bag  bag = (Bag ) obj;return this.name.equals(bag.name) && this.price == bag.price;}public String toString() {return "【背包信息】名称:" + this.name + "、价格:" + this.price;}
}
public class ShopCarDemo {public static void main(String[] args) {IShopCar car = new ShopCarImpl();car.add(new Book("Java开发", 79.8));car.add(new Book("数据结构", 89.8));car.add(new Bag("小强背包", 879.8));Cashier cas = new Cashier(car);System.out.println("总价格:" + cas.allPrice() + "、购买总数量:" + cas.allCount());}
}

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

  1. 基于python的知识融合_独家干货2,融合多个技巧的Python文件-综合实战应用

    这是菜鸟学python的第30篇原创文章 阅读本文大概需要6分钟 最近有几个好学的小伙伴比如'supercalifragilisticexpiadocious',还有广东汕头的同学,还有侠客行,不断的 ...

  2. 小白都能学会的Python基础 第七讲:综合实战3 - 文字识别、人脸识别实战

    1.华小智系列 - Python基础(案例版) <Python基础>目录 第七讲:综合实战3 - 文字识别.人脸识别实战 1.Python图片文字识别(OCR) 2.Python人脸识别( ...

  3. MapReduce数据分析(10)综合实战

    十.MapReduce综合实战 综合实战:环境大数据 案列目的 1.学会分析环境数据文件: 2.学会编写解析环境数据文件并进行统计的代码: 3.学会进行递归MapReduce. 案例要求 要求实验结束 ...

  4. (王道408考研数据结构)第二章线性表-第三节2:双链表的定义及其操作(插入和删除)

    文章目录 一:双链表的定义 二:双链表代码描述 三:双链表的初始化 四:双链表的插入 五:双链表的删除 一:双链表的定义 双链表:双链表在单链表的基础上再增加一个指针域,用于指向它的前驱结点 二:双链 ...

  5. (王道408考研数据结构)第二章线性表-第三节1:单链表的定义及其操作(插入和删除,建立之尾插和头插)

    文章目录 一:单链表相关 (1)单链表的定义 (2)头指针与头结点 二:单链表代码描述 三:单链表的初始化 四:单链表的插入 五:单链表的删除 六:单链表查找 (1)按位查找 (2)按值查找 七:单链 ...

  6. 单链表的定义、特点、结构及其一些基本操作

    一,基本运算 1,单链表,双链表的定义: 设计链式存储结构时,每个逻辑节点存储单独存储. 2,单链表的基本结构:  头节点在前,首节点在后. 3,顺序表与链表间存储密度的差异: 顺序表的存储密度为1, ...

  7. 链表的特点,单链表的定义、存储结构,单链表的基本操作(判断链表是否为空、销毁链表、清空链表、求链表表长、查找、插入、删除,建立单链表)

    目录 一.链表(链式存储结构)的特点 二.单链表的定义和表示 1.带头结点的单链表 2.单链表的存储结构 三.单链表基本操作的实现 1.单链表的初始化(带头结点的单链表) 2.补充单链表的几个常用简单 ...

  8. Java入门第三季-综合实战:简易扑克牌游戏

    一.概述 第三季的综合实战部分是写一个简易扑克牌游戏,功能实现要求如下: 1.创建一副扑克牌 包括四种花色: 黑桃 红桃 梅花 方片 十三种点数 2-10,JQKA,不考虑大小王 2.创建两名玩家 玩 ...

  9. Golang语言快速上手到综合实战(Go语言、Beego框架、高并发聊天室、豆瓣电影爬虫) 下载

    下载Golang语言快速上手到综合实战(Go语言.Beego框架.高并发聊天室.豆瓣电影爬虫) 下载地址:请加QQ:397245854 Go是Google开发的一种编译型,可并行化,并具有垃圾回收功能 ...

最新文章

  1. Numpy入门教程:12. 线性代数
  2. 阿里巴巴首次提出智慧建筑,技术与创新的新巅峰
  3. Axure中推动拉动元件不生效_单线图标元件库SVG矢量图标 纯净无干扰的Axure元件库...
  4. 【转载保存】Selenium Webdriver元素定位的八种常用方式
  5. php fpm core,在php-fpm下,服务器间歇出现core dump 追踪到php代码是include一个php文件...
  6. Firefly是什么?有什么特点?
  7. pytorch Sampler
  8. 杭电1863+1879
  9. 获取西刺代理IP构建代理池
  10. 通过Keytool 生成 Android 签名文件
  11. 怎么使用小爱同学音响_小爱同学语音唤醒功能怎么设置,小爱同学音箱的优缺点是什么...
  12. 如何释放计算机的ip地址,电脑执行释放DNS和更新IP的命令是什么?怎么执行
  13. 王者荣耀以鸿蒙起网名,王者荣耀
  14. 【Kay】Java多线程
  15. 技术人员谈管理之帕累托法则(80/20法则)
  16. 中文乱码
  17. 怎么挖掘长尾关键词 SEO关键词挖掘方法教程
  18. 软件测试需要学什么?测试小白入门必看!
  19. Berkeley DB 使用
  20. PDF文件破解打开密码

热门文章

  1. 21个为您的网站和博客提供的免费视频播放器[转载]
  2. Python数据分析与机器学习7-Seaborn之调色版
  3. 为什么内马尔要离开巴萨去巴黎?
  4. 什么是性能测试?性能测试目的?性能测试的主要分类以及性能测试的常用指标?
  5. qmail 相关问题
  6. pap认证失败_pap chap认证配置
  7. !!非null,非undefined,非空,非空格的字符串验证 。
  8. 笨鸟Linux学习连载之 - 万事开头难
  9. 中国消化保健食品和饮料市场趋势报告、技术动态创新及市场预测
  10. 技嘉B560M VCCIO2电压设计缺陷