Java课程设计 购物车系统(个人博客)

1. 团队课程设计博客链接

课程设计

2. 个人负责模块或任务说明

  • 编写ShoppingCart类,连接数据库
  • 编写updateCart类,从数据库中获取商品信息,获取指定编号的商品信息
  • 编写Users类,从数据库中获取商品用户信息,获取指定用户名的商品信息.

3. 自己的代码提交记录截图

4. 自己负责模块或任务详细说明

  • 1.基本类,商品类name和操作类Item,beand类中属性有:商品编号num,商品名称name,商品价格price,商品数量amount,商品图片picture.其中基本的set/get方法外,还有商品数目增删方法;Operate类中属性有:静态数组arr,购物车总价total.其中还包含根据商品编号进行增加商品addGoods(),删除商品removeGoods(),以及计算购物车总价格totalPrice()等方法.
  • 2.基本类,商品类Users和与数据库建立连接类DBConnection,Users类中属性有:编号num,用户名name,密码password.其中含有基本的set/get方法;DBConnection类中负责与数据库建立连接.
  • 3.编写一个商品的获取信息类Goods其中用getAllGoods()方法获得所有商品的信息,以及getItByNum(int num)方法根据商品编号num获得商品信息;编写Users类,含有从数据库中获取商品用户信息,获取指定用户名的商品信息.

主要代码

public class ShoppingCart 
{ 
	private int itemCount;      // 商品数量
	private double totalPrice;  // 购物车总价格
	private int capacity;       // 数组大小 
	private Item[] cart;


	// ----------------------------------------------------------- 
	//  Creates an empty shopping cart with a capacity of 5 items. 
	// ----------------------------------------------------------- 
	public ShoppingCart() 
	{ 
		capacity = 5; 
		itemCount = 0; 
		totalPrice = 0.0; 
		cart = new Item[capacity];
	} 

	/**
	 * 
	 * 添加功能
	 */
	public void buy(Item item) 
	{
		if(itemCount == capacity){
			increaseSize();
		}
		cart[itemCount] = item;
		totalPrice += cart[itemCount].getPrice();
		itemCount++;
		return;
	}
	
	/**
	 * 删除功能
	 */
	public void deleteCart(int No) {
		int i;
		for (i = 0; i < cart.length; i++) {
			if(cart[i].getNo() == No){
				totalPrice -= cart[i].getPrice();
				for (int j = i; j < cart.length-1; j++) {
					cart[j] = cart[j+1];
				}
				itemCount--;
				break;
			}
		}
		if(i == cart.length){
			System.out.println("无此商品,无法删除。");
		}
		return;
	}
	/**
	 * 修改功能
	 */
	public void updateCart(int No) {//修改编号
		int i;
		
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		
		for (i = 0; i < cart.length; i++) {
			if(cart[i].getNo() == No){
				totalPrice -= cart[i].getPrice();
				System.out.println("需要修改的信息如下:");
				System.out.println(cart[i]);
				System.out.println("请输入新的产品信息:");
				cart[i].setNo(scan.nextInt());
				cart[i].setName(scan.next());
				cart[i].setBrand(scan.next());
				cart[i].setPrice(scan.nextDouble());
				totalPrice += cart[i].getPrice();
				break;
			}
		}
		if(i == cart.length){
			System.out.println("无此商品");
		}
		return;
	}

	/**
	 * 增加购物车容量
	 */
	private void increaseSize() 
	{ 
		Item[] temp = new Item[capacity + 3];
		for (int i = 0; i < cart.length; i++) {
			temp[i] = cart[i];
		}
		cart = temp;
	} 
	
	/**
	 *获取商品总值
	 */
	public double getTotalPrice() {
		return totalPrice;
	}

	public Item[] getCart() {
		return cart;
	}

	public void setCart(Item[] cart) {
		this.cart = cart;
	}

	/**
	 * 输出购物车信息
	 */
	public String toString() 
	{ 
		String contents = "您的购物车信息如下:"; 
		contents += "
no		name		brand		price
"; 
		
		for (int i = 0; i < itemCount; i++) 
			contents += cart[i].toString() + "
"; 
		
		return contents; 
	} 
}

运行过程






5. 课程设计感想

购物车系统涉及到了web的相关知识,通过查资料收获到了很多东西,如解决了界面的设计,网页间的跳转以及web与数据库相连接等问题。通过不断借鉴与学习反思弥补自己知识储备上的不足,能够将知识运用到实践中,和小伙伴完成一项任务,感到很充实。

原文地址:https://www.cnblogs.com/fx8023/p/7067521.html