java链表的使用_java链表的基本使用 (购物车)代码

该博客介绍了使用链表数据结构实现购物车管理,包括添加、删除商品,获取购物车总价及数量。示例中定义了ILink接口,实现了LinkImpl类来操作链表,同时定义了IGoods和IShopCar接口,以及它们的实现类,用于商品和购物车的交互。最后,Cashier类用于计算购物车中商品的总价和数量。
摘要由CSDN通过智能技术生成

package test;

interface ILink{

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();//清空

}

class LinkImpl implements ILink{

private class Node{//保存节点的数据关系

private E data;

private Node next;//保存下一个引用

private Node(E data) {

this.data=data;

}

//第一次调用

public void addNode(Node newNode) {

//保存新的Node数据

if(this.next==null) {

this.next =newNode;

}else {

this.next.addNode(newNode);

}

}

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.getNode(index);

}

}

public boolean containsNode(E data) {

if(this.data.equals(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);

}

}

}

}

private Node root;

private int count;//增加数据

private int foot;

private Object[] returnData;

public void add(E e) {

if(e==null) { return ;

}else {

Node newNode = new Node(e);//创建新节点

if(this.root==null) {

this.root=newNode;

}

else {

this.root.addNode(newNode);

}

this.count++;

}

}

@Override

public int size() {

// TODO Auto-generated method stub

return this.count;

}

@Override

public boolean isEmpty() {

//this.count==0

return this.root==null;

}

@Override

public Object[] toArray() {

if(this.isEmpty()) {

return null;

}

this.foot=0;

this.returnData=new Object[this.count];

this.root.toArrayNode();

return this.returnData;

}

@Override

public E get(int index) {

if(index>this.count) {

return null;

}

this.foot=0;

return this.root.getNode(index);

}

@Override

public void set(int index, E data) {

if(index>=this.count) {

return ;

}

this.foot=0;

this.root.setNode(index, data);

}

@Override

public boolean contains(E data) {

if(data==null) {

return false;

}

return this.root.containsNode(data);

}

@Override

public void remove(E data) {

if(this.contains(data)) {

if(this.root.data==data) {

this.root=this.root.next;

}else {

this.root.next.removeNode(this.root, data);

}

this.count--;

}

}

@Override

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 ILink allGoodses =new LinkImpl();

@Override

public void add(IGoods goods) {

this.allGoodses.add(goods);

}

@Override

public void delete(IGoods goods) {

this.allGoodses.remove(goods);

}

@Override

public Object[] getAll() {

// TODO Auto-generated method stub

return this.allGoodses.toArray();

}

}

class Cashier{//收银台

public IShopCar shopcar;

public Cashier(IShopCar shopcar) {

this.shopcar=shopcar;

}

public double allPrice() {//计算总价

double sum=0.0;

Object[] all = this.shopcar.getAll();

for(Object obj:all) {

IGoods goods=(IGoods)obj;

sum+=goods.getPrice();

}

return sum;

}

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 name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = 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 "【图书信息】:name="+this.name

+"price="+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 name;

}

public void setName(String name) {

this.name = name;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = 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 "【背包信息】:name="+this.name

+"price="+this.price;

}

}

public class Test1 {

public static void main(String[] args) throws Exception {

IShopCar car = new shopCarImpl();

car.add(new Book("java开发", 40));

car.add(new Book("Oracle",24));

car.add(new Bag("小熊背包", 88));

Cashier cashier = new Cashier(car);

System.out.println(cashier.allPrice());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值