OOdesign

前期调查

系统功能结构图

UML图

系统功能描述

该购物车系统实现了增加、删除、修改商品和清空购物车功能,通过菜单选择进入不同的功能。

关键代码

class Product {
	private int id;//商品序号
	private String name;//商品名称
	private float price;//商品价格
	private int nums;//商品数量
	
	public static Product[] goodList = new Product[200];//存储商品的数组
	
	public Product() {
		super();
	}
	public Product(int num, String name, float price,int nums) {
		super();
		this.id = num;
		this.name = name;
		this.price = price;
		this.nums = nums;
	}
	public int getNums() {
		return nums;
	}
	public void setNums(int nums) {
		this.nums = nums;
	}
	public int getId() {
		return id;
	}
	public void setId(int num) {
		this.id = num;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	
}
class ShopCar {
	
	public static Product[] shopCars = new Product[200];//存储商品的数组
	
	public ShopCar() {
		super();
	}

}
class Shopping {
	

	public static void showShopping() { //超市界面

		while(true) {
			System.out.println("-------------------欢迎光临-------------------");
			
			System.out.println("[1].商品列表\n[2].选购商品\n[3].我的购物车\n[4].结算\n[5].退出\n");
			System.out.println("请选择:\n");
			Scanner input = new Scanner(System.in);		
			int choice = input.nextInt();
			if(choice == 1) {
				Goods.showGoods();
			}else if(choice == 2){
				int list;
				int nums;
				while(true) {
					System.out.println("请输入你要添加商品的序号");
					list = input.nextInt();
					if(list < Goods.addProduct) {
						break;
					}else {
						System.out.println("请输入正确的商品序号!");
					}
				}
				while(true) {
					System.out.println("请输入你要添加商品的数量");
					nums = input.nextInt();
					if(nums < Product.goodList[nums - 1].getNums()) {
						break;
					}else {
						System.out.println("商品库存不足,请重新输入!");
					}
				}
				UseShopCar.addGoodsCar(list,nums);
				
			}else if(choice ==3) {
				ShowShoppingCar.shoppingCar();
			}else if(choice ==4) {
				int allPrice = UseShopCar.settleAccounts();
				if(allPrice == 0) {
					System.out.println("请先购买商品!");
					return;
				}
				System.out.println("总价为:" + allPrice);
			}else if(choice ==5) {
				System.out.println("期待您的下次光临!");
				break;
			}else {
				System.out.println("请输入正确的数字");
			}
		}
		
	}
}
class ShowShoppingCar {
	private static Scanner input = new Scanner(System.in);
	//购物车界面
	public static void shoppingCar(){
		
		while(true) {
			UseShopCar.showShopCar();
			if(ShopCar.shopCars[0] == null) {
				return;
			}
			System.out.println("1.删除商品2.清空购物车3.返回上一层");
			int choice = input.nextInt();
			if(choice == 1) {
				System.out.println("请输入你要删除商品的序号");
				int deleteNum = input.nextInt();
				UseShopCar.deleteAccounts(deleteNum);
			}else if(choice == 2) {
				UseShopCar.clearShopCar();
			}else if(choice ==3) {
				break;
			}else {
				System.out.println("输入错误,请输入正确的数字!");
			}
			
		}
	}
}
//商品工具类
class Goods {
	
	private static int numsProduct = 0;//打印商品列表商品的序号
	public static int addProduct = 0;//添加商品的数组角标
	
	//初始化商品
	static {
		Product p1 = new Product(1001,"农夫山泉",2.0f,100);
		Product p2 = new Product(1002,"可口可乐",3.0f,100);
		Product p3 = new Product(1003,"果粒橙汁",4.5f,100);
		Product.goodList[addProduct++] = p1;
		Product.goodList[addProduct++] = p2;
		Product.goodList[addProduct++] = p3;//addProduct = 3
		
		
	}
	
	//显示商品列表
	public static void showGoods() {
		numsProduct = 1;
		System.out.println("序号\t" + "商品编号\t" + "商品名称\t" + "商品价格\t" + "商品数量\t");
		for(Product p :Product.goodList) {
			if(p != null) {
				System.out.print(numsProduct++ + "\t" + p.getId() + "\t" + p.getName() + "\t"  + p.getPrice() + "\t"  + p.getNums() + "\t" );
				System.out.println();
			}else {
				break;
			}
		}
	}
}
class UseShopCar {
	
	private static int nums = 0;
	public static int numsShopCarProduct = 0;
	//添加商品,将商品写入购物车
	public static void addGoodsCar(int id,int goodsNum) {
		Product addGoods = Product.goodList[id - 1];
		Product shopCar = new Product();
		shopCar.setId(addGoods.getId());
		shopCar.setName(addGoods.getName());
		shopCar.setPrice(addGoods.getPrice());
		shopCar.setNums(goodsNum);
		int goodsnums = addGoods.getNums();
		if((goodsnums - goodsNum) >= 0) {
			addGoods.setNums(goodsnums - goodsNum);
		}else {
			System.out.println("库存不足,请重新选择!");
			return;
		}
		ShopCar.shopCars[nums++] = shopCar;
	}
	
	//显示购物车列表
	public static void showShopCar() {
		
		if(ShopCar.shopCars[0] == null) {
			System.out.println("您的购物车是空的,请先添加商品后查看!");
			return;
		}
		
		numsShopCarProduct = 1;
		System.out.println("------------------你的购物车-------------------");
		System.out.println("序号\t" + "商品编号\t" + "商品名称\t" + "商品价格\t" + "商品数量\t");
		for(Product p :ShopCar.shopCars) {
			if(p != null) {
				System.out.print(numsShopCarProduct++ + "\t" + p.getId() + "\t" + p.getName() + "\t"  + p.getPrice() + "\t"  + p.getNums() + "\t" );
				System.out.println();
			}else {
				break;
			}
		}
	}
	
	//结算
	public static int settleAccounts() {
		int allPrice = 0;
		for(Product p :ShopCar.shopCars) {
			if(p != null) {
				allPrice += p.getPrice()*p.getNums();
			}else {
				break;
			}
		}
		return allPrice;
	}
	
	//删除购物车商品
	public static void deleteAccounts(int deletenum) {
		UseShopCar.returnNums(deletenum - 1);
		//覆盖删除
		for(int i = deletenum - 1;i < ShopCar.shopCars.length;i++ ) {
			if(ShopCar.shopCars[i + 1] == null) {
				ShopCar.shopCars[i] = ShopCar.shopCars[i + 1];
				break;
			}else {
				ShopCar.shopCars[i] = ShopCar.shopCars[i + 1];
			}
		}
		UseShopCar.nums--;
	}
	
	//清空购物车
	public static void clearShopCar() {
		for(int i = 0;i < ShopCar.shopCars.length;i++) {
			if(ShopCar.shopCars[i] != null) {
				UseShopCar.returnNums(i);
			}else {
				break;
			}
		}
		
		for(int i = 0;i < ShopCar.shopCars.length;i++) {
			if(ShopCar.shopCars[i] != null) {
				ShopCar.shopCars[i] = null;
			}else {
				break;
			}
		}
	}
	
	//将删除商品的数量返回至商品列表
	public static void returnNums(int deletenum) {
		
		Product delAccount = ShopCar.shopCars[deletenum];
		for(Product p :Product.goodList) {
			if(p.getId() == delAccount.getId()) {
				int nums = p.getNums() + delAccount.getNums();
				p.setNums(nums);
				break;
			}
		}
	}
}

效果展示

posted @ 2020-11-13 07:43  Vancciiii  阅读(161)  评论(0编辑  收藏  举报