课时144:综合实战:超市购物车
摘要:本案例旨在运用面向对象编程的概念,模拟小明去超市购物的生活场景。
1.场景概述
2.代码实现步骤
3.代码优缺点分析
01.场景概述
具体流程为:小明作为客户端,将购买的商品放入购物车,最后到收银台进行统一结账。购物车主要用于存放商品,我们关注商品的名称、价格等信息;收银台的主要功能是对购物车中的商品进行结算。
02.代码实现步骤
2.1定义商品标准
首先,我们需要定义一个商品的标准,用于规范商品的基本属性。以下是示例代码:
interface IGoods { // 定义商品标准 public String getName(); public double getPrice(); }
在这个接口中,我们定义了两个方法getName()
和getPrice()
,分别用于获取商品的名称和价格。
2.2定义购物车标准
为了避免类与类之间的强耦合,我们需要定义一个购物车的标准。购物车应具备添加商品、删除商品以及返回所有商品信息的功能。以下是示例代码:
interface IShopCar { // 定义购物车 public void add (IGoods goods); //添加商品信息 public void delete (IGoods goods); // 删除商品 public Object[ ] getAll() ; // 获得购物车中的全部商品信息 }
2.3定义一个购物车的实现类
接下来,我们需要实现购物车接口,创建一个具体的购物车类。这里我们使用链表来存储商品信息,以避免数组内容频繁修改带来的麻烦。以下是示例代码:
class ShopCar implements IShopCar { // 定义购物车 private ILink<IGoods> Allgoodses = new LinkeImpl<IGoods>(); public void add (IGoods goods) { this.allGoodses.add(goods) } public void delete (IGoods goods) { this.allGoodses.remove (goods); } public Object[ ] getAll () { return this.allGoodses.toArray(); } }
2.4定义收银台类
收银台类负责对购物车中的商品进行结算,计算商品的总价和总数量。以下是示例代码:
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; } }
2.5定义具体商品类
我们定义两个具体的商品类:图书类和背包类,它们都需要实现IGoods接口。同时,为了能够正确删除商品,我们需要重写equals()方法。以下是图书类的示例代码:
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; } public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (!(obj instanceof Book)) { return fales ; 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; } public boolean equals(Object obj) { if (obj == null) { return false; } if (this == obj) { return true; } if (!(obj instanceof Bag)) { return fales ; Bag bag = (Bag) obj; return this.name.equals(bag.name) && this.price == bag.price; } Public String toString() { return "【背包信息】名称:" + this.name + "、价格" + this.price; } }
2.6代码测试
最后,我们编写测试代码,模拟小明购物和结账的过程。以下是示例代码:
public class JavaDemo { public static void main(String[] args) { IShopCar car = new ShopCar(); car.addGoods(new Book("Java开发", 79.8)); car.addGoods(new Book("Oracle开发", 89.8)); car.addGoods(new Bag("小强背包", 889.8)); Cashier cas = new Cashier(car); System.out.println("总价格:" + cas.allPrice() + "、购买总数量:" + cas.allCount()); } }
03.代码优缺点分析
3.1优点
通过使用链表存储商品信息,避免了数组内容频繁修改带来的麻烦,提高了代码的灵活性和可维护性。同时,采用面向对象的编程思想,将不同的功能模块封装在不同的类中,降低了代码的耦合度。
3.2缺点
当前代码存在一定的局限性,例如不能存储商品的购买数量。若要实现该功能,仅依靠链表是不够的,需要学习更加复杂的数据结构。