天天看点

用链表实现简单购物操作

//购物:商品->购物车->收银台

package com.demo;

//商品标准

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>() ;

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

    }

    //要删除商品信息必须覆写equals()方法

    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;

    }

    //要删除商品信息必须覆写equals()方法

    public boolean equals(Object obj) {

        if(obj == null) {

            return false ;

        }

        if(this == obj) {

            return true ;

        }

        if(!(obj instanceof Bag)) {

            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 Counter {

    public static void main(String[] args) {

        IShopCar car = new ShopCarImpl() ;

        car.add(new Book("Java开发", 79.8)); //向购物车添加商品

        car.add(new Bag("背包", 888.86));

        Cashier cas = new Cashier(car) ; //将购物车送到收银台

        System.out.println("总量:" + cas.allCount() + ";总价:" + cas.allPrice()) ;

    }

}

继续阅读