停车场收费简单模拟

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 ICar{//标准
    public String getName();
    public double getPrice();
}
interface IPark{//标准
    public void add(ICar car);
    public void delete(ICar car);
    public Object [] getAll();
}
class Park implements IPark{//车场
    private ILink<ICar> allCars = new ILinkImpl<ICar>();
    public void add(ICar car){
        this.allCars.add(car);
    }
    public void delete(ICar car){
        this.allCars.remove(car);
    }
    public Object [] getAll(){
        Object result [] = this.allCars.toArray();
        return result;
    }
}
class Cashier{//收费员
    private IPark park;
    public Cashier(IPark park){
        this.park = park;
    }
    public int getAllCount(){
        return this.park.getAll().length;
    }
    public double getIncome(){
        double all = 0.0;
        Object result [] = this.park.getAll();
        for(Object obj : result){
            ICar car = (ICar) obj;
            all += car.getPrice();
        }
        return all;
    }
}
class BMW implements ICar{
    private String name;
    private double price;
    public BMW(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 BMW)){
            return false;
        }
        BMW bmw = (BMW) obj;
        return this.name.equals(bmw.getName()) && this.price == bmw.getPrice();
    }
    public String toString(){
        return "【停车信息】名称:" + this.name + "、收费:" + this.price;
    }
}
class Benz implements ICar{
    private String name;
    private double price;
    public Benz(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 Benz)){
            return false;
        }
        Benz benz = (Benz) obj;
        return this.name.equals(benz.getName()) && this.price == benz.getPrice();
    }
    public String toString(){
        return "【停车信息】名称:" + this.name + "、收费:" + this.price;
    }
}
public class JavaDemo{
    public static void main(String args []){
        IPark park = new Park();
        park.add(new BMW("宝马X3",12.00));
        park.add(new BMW("宝马X5",20.00));
        park.add(new Benz("奔驰Q5",8.00));
        park.add(new Benz("奔驰A4",11.00));
        Cashier cas = new Cashier(park);
        System.out.println(cas.getAllCount());
        System.out.println(cas.getIncome());
    }
}

猜你喜欢

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