使用面向对象的概念表示出下面的生活场景:小明去超市买东西,所有买到的东西都放在购物车之中,最后到收银台一起结账。
- 定义一个商品的标准:
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 ILink<IGoods> allGoods = new LinkImpl<IGoods>(); //新建一个列表对象,存放数据为IGoods类型 public void add(IGoods goods){ //添加商品 this.allGoods.add(goods); } public void delete(IGoods goods){ //删除商品 this.allGoods.remove(goods); } public Object[] getAll(){ //获取全部商品 return this.allGoods.toArray(); } }
- 定义收银台
class Cashier { private IShopCar shopcar; public Cashier(IShopCar shopcar){ this.shopcar = shopcar; } public double allPrice(){ //计算总价 double all = 0.0; Object[] returngoods = this.shopcar.getAll(); for(Object obj : returngoods){ IGoods goods = (IGoods) obj; all = 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 + "\t价格:"+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 + "\t价格:"+this.price; } }
测试:
class JavaDemo { public static void main(String[] args){ IShopCar shopcar = new ShopCarImpl(); shopcar.add(new Book("HTML",12.9)); shopcar.add(new Book("CSS",25)); shopcar.add(new Bag("双肩包",33.5)); Cashier cashier = new Cashier(shopcar); Object[] data = shopcar.getAll(); for(Object obj : data){ System.out.println(obj); } System.out.println("\n总价格:"+cashier.allPrice()+ "\n商品数量:"+cashier.allCount()); } }
完整版:
-
//——————————————————————接口标准—————————————————————— interface ILink<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 data); //删除节点的方法 public void clean(); //清空链表的方法 } interface IGoods //定义商品标准 { public String getName(); public double getPrice(); } interface IShopCar //购物车 { public void add(IGoods goods); //添加商品信息 public void delete(IGoods goods); //删除商品 public Object[] getAll(); //获取购物车中全部商品信息 } //——————————————————————链表实现类—————————————————————— class LinkImpl<E> implements ILink<E> { private class Node //保存节点的数据关系 { private E data; //保存的数据 private Node next; //下一个节点 public Node(E data){ //有数据的情况下才有意义 this.data = data; } //第一次调用:LinkImpl.root.addNode(),this = LinkImpl.root //第二次调用:LinkImpl.root.next.addNode(),this = LinkImpl.root.next public void addNode(Node newNode){ //保存新的Node数据 if(this.next == null){ //root的下一个节点为空 this.next = newNode; }else{ //如果已经有节点了 this.next.addNode(newNode); //利用递归继续往后查询直到遇到空节点 } } //第一次调用:this = LinkImpl.root //第二次调用:this = LinkImpl.root.next public void toArrayNode(){ LinkImpl.this.returnData[LinkImpl.this.foot++] = this.data; if (this.next != null) //还有下一个数据 { this.next.toArrayNode(); } } //根据索引查找数据 public E getNode(int index){ if(LinkImpl.this.foot == index){ //索引相同 return this.data; //返回当前数据 }else{ return this.next.getNode(index); //递归调用 } } //根据索引修改数据 public void setNode(int index,E data){ if(LinkImpl.this.foot == index){ //索引相同 this.data = data; //修改数据 }else{ this.next.setNode(index,data); //递归调用 } } //判断数据是否存在 public boolean containsNode(E data){ if(this.data.equals(data)){ //数据与当前对象数据是否相等 return true; }else{ if(this.next == null){ //找不到 return false; } } 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); //向后继续寻找data数据相等的节点 } } } } //----------以下为LinkImpl类中定义的成员----------- private Node root; //保存根元素 private int count; //保存数据的个数 private int foot = 0; //操作数组的脚标 private Object[] returnData; //返回的数组 //----------以下为LinkImpl类中定义的方法----------- public void add(E e){ if(e == null){ //保存的数据为null return; //方法调用直接结束 } //数据本身是不具备关联性的,只有Node类有,那么想实现关联处理就必须将数据封装在Node类中 Node newNode = new Node(e); //创建一个新的节点 if(this.root == null){ //现在没有根节点 this.root = newNode;//第一个节点作为根节点 }else{ //根节点存在 this.root.addNode(newNode); //把创建的新节点交给Node类自行判断放在合适的位置 } this.count++;//每传入一个对象都会增加 } //获取长度的方法 public int size(){ return this.count; } public boolean isEmpty(){ return this.count == 0;//数组长度是否为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); } //删除节点的方法 public void remove(E data){ if(this.contains(data)){ //判断数据是否存在 if(this.root.data.equals(data)){ 根节点处理 this.root = this.root.next; //根引用根的下一个节点 }else{ //非根节点处理 this.root.next.removeNode(this.root,data); } this.count--; //数组长度减1 } } //清空链表的方法 public void clean(){ this.root =null; //后续的节点都没了 this.count = 0; //数组长度清零 } } //——————————————————————购物车实现类—————————————————————— class ShopCarImpl implements IShopCar { private ILink<IGoods> allGoods = new LinkImpl<IGoods>(); //新建一个列表对象,存放数据为IGoods类型 public void add(IGoods goods){ //添加商品 this.allGoods.add(goods); } public void delete(IGoods goods){ //删除商品 this.allGoods.remove(goods); } public Object[] getAll(){ //获取全部商品 return this.allGoods.toArray(); } } //——————————————————————收银台—————————————————————— class Cashier { private IShopCar shopcar; public Cashier(IShopCar shopcar){ this.shopcar = shopcar; } public double allPrice(){ //计算总价 double all = 0.0; Object[] returngoods = this.shopcar.getAll(); for(Object obj : returngoods){ IGoods goods = (IGoods) obj; all = 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 + "\t价格:"+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 + "\t价格:"+this.price; } } //——————————————————————主方法—————————————————————— class JavaDemo { public static void main(String[] args){ IShopCar shopcar = new ShopCarImpl(); shopcar.add(new Book("HTML",12.9)); shopcar.add(new Book("CSS",25)); shopcar.add(new Bag("双肩包",33.5)); Cashier cashier = new Cashier(shopcar); Object[] data = shopcar.getAll(); for(Object obj : data){ System.out.println(obj); } System.out.println("\n总价格:"+cashier.allPrice()+ "\n商品数量:"+cashier.allCount()); } }
结果:
【图书信息】名称:HTML 价格:12.9
【图书信息】名称:CSS 价格:25.0
【背包信息】名称:双肩包 价格:33.5总价格:71.4
商品数量:3
整体的代码都是自己与链表的功能实现的