第4次任务:迭代改进购物车2(购物车需求变更:使用继承、接口与多态)

任务 姓名
编码规范、前期调查与功能设计、博客制作、UML类图 黄帅
面向对象设计、博客制作、功能设计 韩龙飞

1.前期调查与功能设计

我们以京东商城为例来进行探讨,首先,进入商城主界面时,我们可以看到许多商城中的物品,而上方的搜索栏可以帮助我们搜索想要的物品。

点击物品图标,我们可以看到物品的详细信息,还可以执行“加入购物车“这一操作。

进入购物车时,我们能够看到自己购买了什么东西、购买多少个,以及预估的结算价格,而且我们可以在购物车中执行删除操作,能够删除任意商品、任意数量。

从以上观察的情况来看,购物车程序所需要实现的功能大致有:

  • 需要有商品的搜索功能(可能需要一个用户类来识别并执行用户操作)
  • 需要有购物车中物品的添加功能和删除功能(需要一个购物车类来存放购买物品的数量和总价)
  • 在购物车中需要有显示结算总价的程序(需要一个账单类来打印支付凭条)
  • 由于个人设计的程序中的商品不会像网络商场一样多的数不过来,所以可以考虑做出一个可以显示商城中所有商品的商品清单功能。(需要一个商品类来存储商品的基本信息,还可能有全部商品信息输出的功能)

2.面向对象设计


在上述的故事中,用红色字代表可能的对象(名词)或属性,用蓝色字代表可能的方法(动词)。
这样,通过讲故事的方法,我们就可以思路清晰的找出一个程序所需要的类和其中的方法。

3.系统功能结构图

4.功能设计

5.UML图

6.关键代码

商品类父类

package shop;

import java.math.BigDecimal;

public class Commodity {
	private int id;
	private String name;
	private BigDecimal price;
	private String description;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}

商品类子类

package shop;

import java.math.BigDecimal;

public class Food extends Commodity {

	private String shelfLife;// 保质期
	private String date = "见包装";

	public Food() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Food(Integer id, String name, BigDecimal price, String shelfLife) {
		this.setId(id);
		this.setName(name);
		this.setPrice(price);
		this.setDescription("Food");
		this.shelfLife = shelfLife;

	}

	public String getShelfLife() {
		return shelfLife;
	}

	public void setShelfLife(String shelfLife) {
		this.shelfLife = shelfLife;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return "Food [id: " + this.getId() + ", name: " + this.getName() + ", price: " + this.getPrice()
				+ ", shelfLife: " + shelfLife + ", date: " + date;
	}

}

package shop;

import java.math.BigDecimal;

public class Clothes extends Commodity {
	private String size;
	private String brand;

	public Clothes() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Clothes(Integer id, String name, BigDecimal price, String size, String brand) {
		this.setId(id);
		this.setName(name);
		this.setPrice(price);
		this.setDescription("Clothes");
		this.size = size;
		this.brand = brand;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	@Override
	public String toString() {
		return "Clothes [id: " + this.getId() + ", name: " + this.getName() + ", price: " + this.getPrice() + ", size: "
				+ size + ", brand: " + brand;
	}
}
package shop;

import java.math.BigDecimal;

public class Book extends Commodity {
	@Override
	public String toString() {
		return "Book [id: " + this.getId() + ", name: " + this.getName() + ", price: " + this.getPrice() + ", author: "
				+ author + ", press: " + press;
	}

	private String author;
	private String press;

	public Book(Integer id, String name, BigDecimal price, String author, String press) {
		this.setId(id);
		this.setName(name);
		this.setPrice(price);
		this.setDescription("Book");
		this.author = author;
		this.press = press;
	}

	public Book() {
		super();
		// TODO Auto-generated constructor stub
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getPress() {
		return press;
	}

	public void setPress(String press) {
		this.press = press;
	}
}

购物车条目类

package shop;

public class cartItem {
	public cartItem(Commodity item, int amount) {
		super();
		this.item = item;
		this.amount = amount;
	}

	Commodity item;
	int amount;

	public Commodity getItem() {
		return item;
	}

	public void increase(int number) {
		amount = amount + number;
	}

	public void decrease(int number) {
		amount = amount - number;
		if (amount < 0)
			amount = 0;
	}

	public void setItem(Commodity item) {
		this.item = item;
	}

	public int getAmount() {
		return amount;
	}

	public int getId() {
		return item.getId();
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}

	@Override
	public String toString() {
		return "cartItem [item=" + item + ", amount=" + amount + "]";
	}

}

货架DAO

package shop;

public interface ShelfDao {
	public void showCommodity();

	public void addCommodity(Commodity e);

	public void subCommodity(Commodity e);

	public Commodity idSearch(int id);// 根据id找商品
	// 根据关键词找商品

	public boolean nameSearch(String name);
}

package shop;

import java.util.ArrayList;

public class ShelfDaoImpl implements ShelfDao {
	private ArrayList<Commodity> shelfList;

	/**
	 * 
	 */
	public ShelfDaoImpl() {
		shelfList = new ArrayList<>();
	}

	public void showCommodity() {
		System.out.println("商品ID	商品名	商品价格");
		for (Commodity x : shelfList) {
			System.out.println(x.getId() + "	" + x.getName() + "	" + x.getPrice());
		}

	}

	public void addCommodity(Commodity e) {
		if (e == null) {
			return;
		}
		for (int i = 0; i < shelfList.size(); i++) {
			if (shelfList.get(i).getId() == e.getId()) {
				System.out.println("该商品已存在");
				return;
			}
		}
		shelfList.add(e);
	}

	public void subCommodity(Commodity e) {
		for (int i = 0; i < shelfList.size(); i++) {
			if (shelfList.get(i).getId() == e.getId()) {
				shelfList.remove(i);
				break;
			}
		}

	}

	public Commodity idSearch(int id) {
		for (int i = 0; i < shelfList.size(); i++) {
			if (shelfList.get(i).getId() == id) {
				return shelfList.get(i);
			}
		}
		return null;
	}

	public boolean nameSearch(String name) {
		int count = 0;
		for (int i = 0; i < shelfList.size(); i++) {
			if (shelfList.get(i).getName().indexOf(name) != -1) {
				count++;
				if (count == 1) {
					System.out.println("查找结果如下 : ");
					System.out.println();
					System.out.println("商品ID	商品名	商品价格");
				}
				System.out.println(shelfList.get(i).getId() + "	" + shelfList.get(i).getName() + "	"
						+ shelfList.get(i).getPrice());
			}
		}
		if (count == 0)
			return false;
		else
			return true;
	}
}

购物车DAO

package shop;

public interface ShoppingCartDao {
	public boolean cartAdd(Commodity e, int number);// 加入购物车

	public boolean cartsub(int id, int number);

	public void showCart();

	public void cleanCart();
}

package shop;

import java.util.ArrayList;

public class ShoppingCartDaoImpl implements ShoppingCartDao {
	private ArrayList<cartItem> itemList;

	public ShoppingCartDaoImpl() {
		itemList = new ArrayList<>();
	}

	public boolean cartAdd(Commodity e, int number) {
		if (e == null)
			return false;
		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getId() == e.getId()) {
				itemList.get(i).increase(number);
			}
		}
		itemList.add(new cartItem(e, number));
		return true;
	}

	public boolean cartsub(int id, int number) {

		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getId() == id) {
				itemList.get(i).decrease(number);
				return true;
			}
		}

		return false;
	}

	public void showCart() {
		for (cartItem e : itemList) {
			System.out.println(e);
		}
	}

	public void cleanCart() {
		itemList.clear();
	}
}

主函数

package shopping;

import java.math.BigDecimal;
import java.util.Scanner;

import display.Ui;
import shop.Book;
import shop.Clothes;
import shop.Commodity;
import shop.Food;
import shop.Mall;
import shop.ShelfDao;
import shop.ShelfDaoImpl;
import shop.ShoppingCartDao;
import shop.ShoppingCartDaoImpl;

public class Main {
	public static void main(String[] args) {
		// Mall.showList();
		Commodity x1 = new Food(0, "苹果", new BigDecimal("13"), "30days");
		Commodity x2 = new Book(1, "小王子", new BigDecimal("130"), "Unknown", "Xinhua");
		Commodity x3 = new Clothes(2, "衬衫", new BigDecimal("30"), "XL", "Nike");
		Commodity x4 = new Food(1, "苹果", new BigDecimal("13"), "30days");// 累加
		Commodity x5 = new Food(3, "奶茶", new BigDecimal("15"), "2h");
		Commodity x6 = new Book(4, "《眠狂四郎》", new BigDecimal("100"), "Leo Tolstoy", "Xinhua");
		Commodity x7 = new Food(5, "牛奶", new BigDecimal("3"), "20min");
		ShoppingCartDao cart = new ShoppingCartDaoImpl();
		ShelfDao shelf = new ShelfDaoImpl();
		// 加入货架
		shelf.addCommodity(x1);
		shelf.addCommodity(x2);
		shelf.addCommodity(x3);
		shelf.addCommodity(x4);
		shelf.addCommodity(x5);
		shelf.addCommodity(x6);
		shelf.addCommodity(x7);
		Scanner choose = new Scanner(System.in);
		Scanner search = new Scanner(System.in);
		Scanner goodsId = new Scanner(System.in);
		Scanner pause = new Scanner(System.in);// 暂停参数
		String Pause = null;
		access: while (true) {
			Ui.mainUi();
			System.out.println("请输入选项:");
			int Choose = choose.nextInt();

			switch (Choose) {
			case 1:
				shelf.showCommodity();// 显示商品
				System.out.println("是否进行购买?[是(1)/否(0)]");
				Choose = choose.nextInt();
				if (Choose == 1) {
					Mall.buyGoods(goodsId);
				}
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 2:
				// 商品查询ID
				int flag = 1;
				while (flag > 0) {
					System.out.println("请输入商品ID:");
					int id = search.nextInt();
					if (shelf.idSearch(id) == null) {
						System.out.println("没有找到该商品!");
						System.out.println("是否继续查询?");
						flag = search.nextInt();
					} else {
						System.out.print("是否添加到购物车?[是(1)/否(0)] :");
						int chioce = search.nextInt();
						if (chioce == 1) {
							System.out.print("添加数量?:");
							int number = search.nextInt();
							if (cart.cartAdd(shelf.idSearch(id), number))
								System.out.println("添加成功!");// 在购物车单中添加商品
							else
								System.out.println("添加失败!");
						}
						System.out.println("是否继续查询?[是(1)/否(0)] :");
						flag = search.nextInt();
					}
				}
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 3:
				// 商品查询Name
				flag = 1;
				while (flag > 0) {
					System.out.println("请输入关键词:");
					String name = search.next();

					if (!shelf.nameSearch(name)) {
						System.out.println("没有找到与" + name + "相关的商品");
						System.out.println("是否继续查询?[是(1)/否(0)] :");
						flag = search.nextInt();
					} else {

						System.out.println("是否继续查询?[是(1)/否(0)] :");
						flag = search.nextInt();
					}
				}
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 4:
				// 商品删除
				System.out.println("请输入您需要删除的商品的对应ID:");
				int choiceGoodsId = goodsId.nextInt();
				System.out.println("请输入您需要减少的商品的数量:");
				int choiceSubNumber = goodsId.nextInt();
				if (cart.cartsub(choiceGoodsId, choiceSubNumber))
					System.out.println("删除商品成功!");// 在购物车单中添加商品
				else
					System.out.println("删除商品失败!");
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 5:
				// 查看购物车
				cart.showCart();
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 6:
				// 购物车清空
				cart.cleanCart();
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 7:
				// 购物车预结算
				cart.showCart();
				System.out.println("<按回车返回主界面>");
				Pause = pause.nextLine();
				break;
			case 0:
				break access;

			}
			for (int i = 0; i < 25; i++)
				System.out.println();
		}
		choose.close();
		search.close();
		goodsId.close();
		pause.close();
		Ui.endUi();
	}
}

posted @ 2021-10-25 23:44  黄帅2011  阅读(110)  评论(0编辑  收藏  举报