超市购物,链表

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();
}
class ILinkImpl<E> implements ILink<E>{
    private class Node{
        private Node next;
        private E data;
        public Node(E data){
            this.data = data;
        }
        public void addNode(Node newNode){
            if(this.next == null){
                this.next = newNode;
            }else{
                this.next.addNode(newNode);//保存节点
            }
        }
        public void toArrayNode(){
            ILinkImpl.this.returnData[ILinkImpl.this.foot ++] = this.data;
            if(this.next != null){
                this.next.toArrayNode();
            }
        }
        public E getNode(int index){
            if(ILinkImpl.this.foot ++ == index){
                return this.data;
            }else{
                return this.next.getNode(index);
            }
        }
        public void setNode(int index,E data){
            if(ILinkImpl.this.foot ++ == index){
                this.data = data;
            }else{
                this.next.setNode(index,data);
            }
        }
        public boolean containsNode(E data){
            if(data.equals(this.data)){
                return true;
            }else{
                if(this.next != null){
                    return this.next.containsNode(data);
                }else{
                    return false;
                }
            }
        }
        public void removeNode(Node previous,E data){
            if(this.data.equals(data)){
                previous = this.next;
            }else{
                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 ;
        }
        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;
    }
    public Object [] toArray(){
        if(this.root == null){
            return null;
        }
        this.foot = 0;
        this.returnData = new Object[this.count];
        this.root.toArrayNode();
        return this.returnData;
    }
    public E get(int index){
        if(index >= this.count){
            return null;
        }
        this.foot = 0;
        return this.root.getNode(index);
    }
    public void set(int index,E data){
        if(data == null){
            return ;
        }
        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.removeNode(this.root,data);
            }
            this.count --;
        }
    }
    public void clean(){
        this.count = 0;
        this.root = null;
    }
}
interface IGoods{//商品标准
    public String getName();
    public double getPrice();
}
interface IShopCar{
    public void add(IGoods goods);
    public void delete(IGoods goods);
    public Object [] getAll();
}
class ShopCar implements IShopCar{
    private ILink<IGoods> allGoods = new ILinkImpl<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 int getAllCount(){
        return this.shopCar.getAll().length;
    }
    public double getAllPrice(){
        double all = 0.0;
        Object result [] = this.shopCar.getAll();
        for(Object obj : result){
            IGoods goods = (IGoods) obj;
            all += goods.getPrice();
        }
        return all;
    }
}
class Ball implements IGoods{
    private String name;
    private double price;
    public Ball(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(obj == this){
            return true;
        }
        if(!(obj instanceof Ball)){
            return false;
        }
        Ball ball = (Ball) obj;
        return this.name.equals(ball.getName()) && this.price == ball.getPrice();
    }
    public String toString(){
        return "【商品信息:】名称:" + this.name + "、价格:" + this.price;
    }
}
class Shoes implements IGoods{
    private String name;
    private double price;
    public Shoes(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(obj == this){
            return true;
        }
        if(!(obj instanceof Shoes)){
            return false;
        }
        Shoes shoes = (Shoes) obj;
        return this.name.equals(shoes.getName()) && this.price == shoes.getPrice();
    }
    public String toString(){
        return "【商品信息:】名称:" + this.name + "、价格:" + this.price;
    }
}
public class JavaDemo{
    public static void main(String args []){
        IShopCar car = new ShopCar();
        car.add(new Ball("NBA篮球",298.00));
        car.add(new Ball("CBA篮球",99.8));
        car.add(new Shoes("NBA篮球鞋",498.00));
        car.add(new Shoes("李宁篮球鞋",198.00));
        Cashier cas = new Cashier(car);
        System.out.println("购买商品数量:" + cas.getAllCount());
        System.out.println("购买商品总价:" + cas.getAllPrice());
    }
}

思路:

1,商品标准;

2,购物车标准;

3,购物车将商品、前台、链表的关系关联起来。

猜你喜欢

转载自blog.csdn.net/qq_27347147/article/details/84659681