Java面向对象课程设计——购物车

一.前期调查

小组成员:

林双强:前期调查与功能设计,规范代码检查,Cart类,warehouse类,order类,图形展示。

黄霆塬:Main,Funciton,Commodity类,博客编写

客户在商城界面浏览或搜索商品,,在找到自己心仪的商品后将其加入购物车,然后选定所要购买的商品付款。

 二.图示

图表:

 功能结构图:

 UML图

 三.代码展示:

1.warehouse

package ShoppingCart;

import java.util.ArrayList;

public class warehouse {
	private static ArrayList<Commodity>  comhouse=new ArrayList<>();
	
	static {
		comhouse.add(new Commodity("烟台苹果",5, 2));
		comhouse.add(new Commodity("本地香蕉",3,2));
		comhouse.add(new Commodity("香梨",2.5,2));
		comhouse.add(new Commodity("阿迪王篮球鞋",200,2));
		comhouse.add(new Commodity("n1ke跑鞋",50,2));
		comhouse.add(new Commodity("anta跑鞋",500,2));
		comhouse.add(new Commodity("华硕电脑",5000,2));
		comhouse.add(new Commodity("惠普电脑",4500,2));
		comhouse.add(new Commodity("戴尔电脑",6000,2));
		comhouse.add(new Commodity("运动短袖",50,2));
		comhouse.add(new Commodity("宝姿短袖",2000,2));
		comhouse.add(new Commodity("普通短袖",50,2));
	}
	public static void displayall()//输出全部商品
	{
		System.out.println();
		for (Commodity e : comhouse) {
			System.out.println(e.toString());
		}
	}
	
	//在在里面完成增删改查
	public void addCom(Commodity p) {//增加
		for(Commodity e:comhouse)
		{
			if(e.getId()==p.getId())
				e.setStock(e.getStock()+p.getStock());
		}
		comhouse.add(p);
	}
	public boolean removeCom(int id) {//删除
		for (Commodity e : comhouse) {
			if(e.getId()==id)
			{
				int x=comhouse.indexOf(e);
				comhouse.remove(x);
				return true;
			}
		}
		return false;
	}
	public static boolean changeCom(int id,int num)//修改。。。
	{
		Commodity e=searchComByid(id);
		e.setStock(e.getStock()+num);
		return true;
	}
	public static Commodity  searchComByid(int id)//在现有的仓库或购物车中通过id查找
	{
		for (Commodity e : comhouse) {
			if(e.getId()==id)
				return e;
		}
		return null;
	}
	
	public static boolean addOrdelete(String name) {//新增和下架
		for (int i = 0; i < comhouse.size(); i++) {
			if(comhouse.get(i).getId().equals(name))
				comhouse.remove(i);
			return true;
		}
		comhouse.add(new Commodity(name));
		return true;
	}
	public static void fuzzySearch(String name)//模糊搜索
	{
		for (Commodity e : comhouse) {
			if(e.getName().contains(name)) {
				System.out.println(e.toString());
			}
		}
	}
}

 功能:

  • 默认开始对商城进行初始化
  • 展示商城所有商品
  • 对商品库存增删改查
  • 新增&下架商品
  • 模糊搜索商品

2. Main

package ShoppingCart;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.util.Scanner;

public class Main {
	static Scanner sc = new Scanner(System.in);
	static Cart cart = new Cart();
	static Commodity com = new Commodity();
	static order order1 = new order();

	public static void main(String[] args) throws AWTException {
		MultiLayout a = new MultiLayout();
		while (true) {
			Function.menu();
			int choice = sc.nextInt();
			switch (choice) {
			case 1: // 浏览商城
				warehouse.displayall();
				addFunction();
				break;
			case 2:// 搜索商品包括模糊搜索
				System.out.println("要退出搜索,请输入“返回”");
				System.out.print("输入您要搜索的物品名称:");
				while (sc.hasNext()) {
					String name = sc.next();
					if (name.equals("返回")) {
						System.out.println("搜索结束!");
						break;
					}
					warehouse.fuzzySearch(name);
					addFunctionTwo();
					System.out.println("请输入您要继续搜索的物品名称:");
				}
				break;
			case 3:
				Function.cartpage();
				cart.diplayAll();
				if(cart.getItemList().size()!=0) {
					int x = sc.nextInt();
					if (x == 0) {
						changeFunction();
					} else {
						payingFunction();
					}
				}
				break;
			case 4:
				order1.displayall();
				break;
			case 5:
				warehouse.displayall();
				Function.warehousepage();
				warehouseFunction();
				break;
			case 6:
				Function.backpage();
				return;
			default:
				System.out.println("输入错误!请重新输入。。。");
			}
		}
	}

	private static void addFunction() {
		Function.addToCartPage();
		while (sc.hasNextInt()) {
			int ComID = sc.nextInt();
			if (ComID == -1) {
				System.out.println("添加完毕!");
				break;
			}
			com = warehouse.searchComByid(ComID);
			if (com != null) {
				cart.add(com);
				System.out.println("\n已将" + com.getName() + "为您加入购物车!");
				System.out.println("请输入您所要继续添加的商品编号或输入-1进行退出");
			} else
				System.out.println("不好意思,不存在该编号的商品");
		}
	}
	private static void addFunctionTwo() {
		Function.addToCartPageTwo();
		while (sc.hasNextInt()) {
			int ComID = sc.nextInt();
			if (ComID == -1) {
				System.out.println("添加完毕!");
				break;
			}
			com = warehouse.searchComByid(ComID);
			if (com != null) {
				cart.add(com);
				System.out.println("\n已将" + com.getName() + "为您加入购物车!");
			} else
				System.out.println("不好意思,不存在该编号的商品");
		}
	}


	private static void changeFunction() {
		int index;
		if (cart.getItemList().size() == 0)
			return;
		outside: while (true) {
			System.out.println("输入你要修改商品的编号id,不修改输-1");
			int x = sc.nextInt();
			if (x == -1)
				return;
			System.out.println("这是该商品的信息:");
			System.out.println(warehouse.searchComByid(x));
			System.out.println("选择你要进行的操作,商品数量+1输“++”,-1输“--”,其他数字直接输入,退出输-1");
			String changes = sc.next();
			com = warehouse.searchComByid(x);
			switch (changes) {
			case "++":
				cart.add(com);
				break;
			case "--":
				cart.remove1(x);
				break;
			case "-1":
				break outside;
			default:
				cart.changenum(x, Integer.parseInt(changes));
				break;
			}
		}
	}

	private static void payingFunction() {
		System.out.println("请输入您要支付商品的id:(可连续输入多个商品(输入999全部选中),-1结束)");
		int[] comid = new int[50];
		int[] comqty = new int[50];
		int cnt = 0;
		int com = 0;
		double totalprice = 0.0;
		while (sc.hasNextInt()) {
			com = sc.nextInt();
			if(com==999)
			{
				cart.diplayAll();
				break;
			}
			if (com == -1)
			{
				System.out.println("您需支付" + totalprice + "元!");
				break;
			}
			comid[cnt++] = com;
			totalprice += cart.paying(com);

		}
		
		ShowImage showImage = new ShowImage();
		System.out.println("请扫码支付");
		Commodity com1;
		if(com==999)
			cart.settle(order1);
		for (int i = 0; i < cnt; i++)// 支付成功后,生成订单,删除库存
		{
			cart.settleItemList(comid[i], order1);
		}
	}
	private static void warehouseFunction()
	{
		int x=sc.nextInt();
		if(x==0) {
			while(true) {
				System.out.println("输入您要增减的商品id与数量(输入-1  -1结束)");
				int id=sc.nextInt();int num=sc.nextInt();
				warehouse.changeCom(id, num);
			}
		}else if(x==1) {
			System.out.println("输入要上新/下架的名称");
			String name=sc.next();
			warehouse.addOrdelete(name);
		}
		else
		{
			return;
		}
	}
}

功能:

  • 主菜单操作界面

 3.Cart

package ShoppingCart;

import java.util.ArrayList;
import java.util.List;

public class Cart {

	private List<ItemEntry> itemList;

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

	public void add(Commodity e) {// 将商品加入购物车
		if (e == null) {
			return;
		}
		int index = findById(e.getId());
		int num= e.getId();
		if (index == -1) {// 如果不包含该商品的条目
			itemList.add(new ItemEntry(e));
		} else {
			if (itemList.get(index).qty == itemList.get(index).item.getStock()) {
				System.out.println("不好意思,没有多余的库存了!");
				return;
			}
			itemList.get(index).increase();
		}
	}

	public List<ItemEntry> getItemList() {
		return itemList;
	}

	public void addv1(Commodity e) {// add的改进版。
		if (e == null) {
			return;
		}
		ItemEntry item = findItem(e);
		if (item == null) {
			itemList.add(new ItemEntry(e));
		} else {
			item.increase();
		}
	}

	/**
	 * 从购物车减少id对应的商品条目的数量。当条目中的数量<1则删除该条目。
	 * 
	 * @param id
	 * @return 如果id不存在,返回false;
	 */
	public boolean remove1(Integer id) {
		if (id == null)
			return false;
		int index = findById(id);
		if (index == -1) { // 未找到
			return false;
		} else {
			ItemEntry entry = itemList.get(index);
			if (entry.getQty() <= 1) {// 当条目数量<1则删除条目
				itemList.remove(index);
			} else {
				entry.decrease();
			}
		}
		return true;
	}

	public boolean changenum(Integer id, int num) {
		if (id == null)
			return false;
		int index = findById(id);
		if (index == -1) { // 未找到
			System.out.println("抱歉未能找到您输入的商品ID号");
			return false;
		} else {
			ItemEntry entry = itemList.get(index);
			if (num == 0) {
				entry.setQty(1);
				entry.decrease();
				return true;
			} else {
				if (num > entry.getItem().getStock()) {
					System.out.println("没有那么多库存啦!");
					return false;
				}
				entry.setQty(num);
				return true;
			}
		}
	}

	public void diplayAll() {
		double totalmoney = 0;
		if (itemList.size() == 0)
			System.out.println("客官这里空空如也!快去买东西吧!\n");
		System.out.println("为您展示购物车内容:");
		for (ItemEntry e : itemList) {
			totalmoney += e.getItem().getPrice() * e.qty;
			System.out.println("---" + e);
		}
		System.out.println("   totalmoney: " + totalmoney + " 元");
	}

	/**
	 * 通过id在购物车中查找是否已存在相关条目。 声明为private,因为只在本类中使用。
	 * 
	 * @param id
	 * @return 如果包含返回相关条目所在下标,否则返回-1
	 */
	private int findById(Integer id) {
		for (int i = 0; i < itemList.size(); i++) {
			if (itemList.get(i).getItem().getId().equals(id))
				return i;
		}
		return -1;
	}

	public double paying(int id) {
		int index = findById(id);
		if (index == -1) { // 未找到
			return 0.0;
		} else {
			ItemEntry entry = itemList.get(index);
			return entry.getQty() * entry.getItem().getPrice();
		}
	}

	public boolean decrease(int id) {
		int index = findById(id);
		if (index == -1) { // 未找到
			return false;
		} else {
			ItemEntry entry = itemList.get(index);
			entry.item.setStock(entry.item.getStock() - entry.qty);
		}
		return true;
	}

	/**
	 * 根据商品c返回购物车中对应的条目
	 * 
	 * @param c
	 * @return
	 */
	private ItemEntry findItem(Commodity c) {
		if (c == null)
			return null;
		for (int i = 0; i < itemList.size(); i++) {
			Commodity e = itemList.get(i).getItem();
			if (e.equals(c))
				return itemList.get(i);
		}
		return null;

	}

	public double settle(order ord)// 结算购物车里的东西
	{
		double toprice = 0.0;
		while (itemList.size() > 0) {
			ItemEntry e = itemList.get(0);
			ord.add(e.item, e.qty);
			itemList.remove(findById(e.item.getId()));
		}
		return toprice;
	}

	public void settleItemList(int id, order ord) {
		int index = findById(id);
		Commodity p = itemList.get(index).getItem();
		ord.add(p, itemList.get(index).qty);
		itemList.remove(findById(p.getId()));
	}

	private void clear()// 结算购物车里的东西
	{
		itemList.clear();
		System.out.println("购物车里的东西已清除完毕!");
	}

	/**
	 * 购物车条目类:代表购物车的某一个条目,包含商品与商品对应的数量。该类为内部类。
	 *
	 */
	private class ItemEntry {

		Commodity item;// 商品

		Integer qty;// 商品数量

		public ItemEntry(Commodity item) {
			this.item = item;
			qty = 1;
		}

		public void increase() {// 添加商品数量+1
			qty++;
		}

		public void decrease() {// 减少商品数量:-1
			qty--;
		}

		public Commodity getItem() {
			return item;
		}

		public Integer getQty() {
			return qty;
		}

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

		public void setQty(Integer qty) {
			this.qty = qty;
		}

		@Override
		public String toString() {
			return "ItemEntry [item=" + item + ", qty=" + qty + ", totalprice=" + item.getPrice() * qty + "]";
		}
	}
}

功能:

  • 将商品添加入购物车
  • 展示购物车数量
  • 更改商品数量
  • 按Id在购物车中寻找商品
  • 结算购物车

4.Commodity

package ShoppingCart;

/**
 * 商品类
 *
 */
public class Commodity {
	private Integer id;
	private String name;
	private Double price;
	private int stock;
	private static int idnum = 0;

	public Commodity() {
		this.id = idnum++;
	}

	public Commodity(String name, int stock) {
		this.id = idnum++;
		this.name = name;
		this.stock = stock;
	}

	public Commodity(String name) {
		this.id = idnum++;
		this.name = name;
	}

	public Commodity(String name, double price, int stock) {
		this.id = idnum++;
		this.name = name;
		this.price = price;
		this.stock = stock;
	}

	public Integer getId() {
		return id;
	}

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

	public int getStock() {
		return stock;
	}

	public void setStock(int stock) {
		this.stock = stock;
	}

	public void decrease(int less) {
		this.stock -= less;
	}

	public String getName() {
		return name;
	}

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

	public Double getPrice() {
		return price;
	}

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

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) { // 只比较id。id相同就说明两个商品相同。
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Commodity other = (Commodity) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Commodity [id=" + id + ", name=" + name + ", price=" + price + "]";
	}
}

功能:

  • 存储商品信息
  • 商品id比较

5.Functions

package ShoppingCart;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class Function {
	public static void clear() throws AWTException//清屏
    {
        Robot r = new Robot();
        r.mousePress(InputEvent.BUTTON3_MASK);       // 按下鼠标右键
        r.mouseRelease(InputEvent.BUTTON3_MASK);    // 释放鼠标右键
        r.keyPress(KeyEvent.VK_CONTROL);             // 按下Ctrl键
        r.keyPress(KeyEvent.VK_R);                    // 按下R键
        r.keyRelease(KeyEvent.VK_R);                  // 释放R键
        r.keyRelease(KeyEvent.VK_CONTROL);            // 释放Ctrl键
        r.delay(0);       
    }
	public static void menu() {
		System.out.println("********************************************");
		System.out.println("******            1. 浏览商城          ******");
		System.out.println("******            2. 搜索商品          ******");
		System.out.println("******            3. 查看购物车        ******");
		System.out.println("******            4. 查看订单          ******");
		System.out.println("******            5. 管理仓库(商家)   ******");
		System.out.println("******            6. 退出商城          ******");
		System.out.println("********************************************");
		System.out.println("*************  *输入您要进行的操作*  **********");
		System.out.print("请输入:");
	}
	public static void addToCartPage() {
		System.out.println();
		System.out.println("客官,里面可有您中意的物品呢?");
		System.out.println("若有输入其编号,将为您添加进购物车");
		System.out.print("请输入(可连续输入多个商品,退出请输-1):");
	}
	public static void addToCartPageTwo() {
		System.out.println();
		System.out.println("客官,是否是您中意的物品呢?");
		System.out.println("若是输入其编号,将为您添加进购物车");
		System.out.print("请输入(可连续输入多个商品,退出请输入返回):");
	}
	public static void cartFunctionPage() {
		System.out.println("输入您要进行的操作:");
		System.out.println("1.增删商品数量     2.选择商品进行支付  ");
	}
	public static void backpage() {
		System.out.println("您已成功下线!");
		System.out.println("欢迎下次光临小丸子商城!");
	}
	public static void cartpage() {
		System.out.println("输入您要进行的操作,调整购物车商品情况(0),paying(1):");
	}
	public static void warehousepage() {
		System.out.println("你已进入仓库!");
		System.out.println("输入你要进行的操作,库存增减(0),上新/下架(1),退出仓库(-1)");
		System.out.println("PS:下架时请输入完成商品名称");
	}
	public static void main(String[] args) {
		// TODO 自动生成的方法存根

	}
}

功能:

交互语言

6.Order

package ShoppingCart;

import java.util.ArrayList;
import java.util.List;

public class order {
	private List<Order> orderList;

	public order() {
		orderList = new ArrayList<>();
	}
	public void add(Commodity p,int num)
	{
		Order ord=new Order(p,num);
		orderList.add(ord);
	}
	public void displayall()
	{
		for (Order e : orderList) {
			System.out.println(e);
		}
	}
	
	private class Order {
		Commodity com;
		int num=0;
		String status = "未发货";

		@Override
		public String toString() {
			return "Order [com=" + com + ", name=" + status + "]";
		}

		public Order(Commodity item,int num) {
			this.com = item;
			this.num=num;
		}
	}
}

功能

显示&管理订单信息

四.实现思路

1、商城功能:包括所有上架商品展示、按id等类型查找商品,将商品加入购物车中

2、购物车功能:包括查看、清空购物车以及从购物车删除某商品,显示选定商品的总金额

3、订单功能:包括订单付款信息,发货信息

简易购物车的面向对象:

众所周知,JAVA是面向对象的编程语言,所以我们在设计类以及方法的时候首先要做的就是确定类,并创建对应的对象和方法,再以对象的方法去解决问题。同时也要考虑类封装,多态,继承。

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值