用链表实现简单购物操作

本文演示了如何使用链表数据结构实现一个简单的购物系统,包括商品、购物车和收银台的功能。商品接口IGoods定义了获取名称和价格的方法,购物车接口IShopCar实现了添加、删除商品和获取所有商品信息的接口。ShopCarImpl类实现了购物车功能,使用链表存储商品。Cashier类用于计算购物车中所有商品的总价和数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//购物:商品->购物车->收银台
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()) ;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值