【Java_Project】购物车

商品类
//商品类
public class Goods {
	// 私有属性:商品id、商品名称、商品单价
	private String id;
	private String name;
	private double price;

	public Goods() {
		super();
	}

	public Goods(String id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}

	public String getId() {
		return id;
	}

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

	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;
	}

}
商品项
package IWillPassYou_ST2;
//购物项(商品+购买数量)
public class ShopItem {

	//私有属性:商品、数量
	private Goods goods;
	private int amount;
	
	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public int getAmount() {
		return amount;
	}

	public void setAmount(int amount) {
		this.amount = amount;
	}
	
	public ShopItem(){
		super();
	}
	
	public ShopItem(Goods goods,int amount){
		super();
		this.goods=goods;
		this.amount=amount;
	}
	
	//单品小计
	public double singleSum() {
		return goods.getPrice()*amount;
	}
	
	@Override
	public String toString() {
		return "[ 购物项 | 商品编号 :"+goods.getId()+"| 商品名称:"+goods.getName()+"| 商品单价:"+goods.getPrice()
			+"| 购买数量:"+amount+"| 小计:"+singleSum()+" ]";
	}
}
购物车
package IWillPassYou_ST2;

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;

//购物车类
public class ShopCar {
	// 购物车:HashMap<商品Id,购物项>
	private HashMap<String, ShopItem> car = null;

	public ShopCar() {
		car = new HashMap<>();
	}

	// 添加购物项至购物车
	public void addItem(ShopItem si) {
		// 获取HashMap的Key_Set,获取键数据
		Set<String> keys = car.keySet();
		if (si != null) {
			for (String key : keys) {
				// 取出购物车中的购物项的商品id
				String cid = car.get(key).getGoods().getId();
				String id = si.getGoods().getId();
				// 购买购物项有两种情况
				// 1.如果购物车已有购物项,则更改其数量
				if (cid.equals(id)) {
					// 修改购物项的数量
					car.get(key).setAmount(car.get(key).getAmount() + si.getAmount());
					return;
				}
			}
			// 2.如果购物车没有购物项,则添加购物项
			car.put(si.getGoods().getId(), si);
		}
	}

	// 根据商品编号删除购物车中的指定购物项
	public boolean delItem(String id) {
		boolean flag = false;
		// 如果要删除的商品编号存在
		if (car.get(id) != null) {
			flag = true;
			car.remove(id);// 删除指定的购物项
		}
		return flag;
	}

	// 修改购物项中的商品数量
	public boolean updateItem(String id, int amount) {
		boolean flag = false;
		// 如果要修改的商品编号存在
		if (car.get(id) != null) {
			if (amount <= 0) {
				car.get(id).setAmount(0);
			} else {
				car.get(id).setAmount(amount);
				;// 修改数量
			}
			flag = true;
		}
		return flag;
	}

	// 结算商品
	public double getTotalPrice() {
		double total = 0;
		Collection<ShopItem> items = car.values();// 得到所有的的ShopItem
		for (ShopItem item : items) {
			total += item.singleSum();
		}
		return total;
	}

	// 查看购物车
	public void showCar() {
		Collection<ShopItem> items = car.values();
		// 把Collection容器中的元素转换成指定类型和长度的数组
		ShopItem[] citems = items.toArray(new ShopItem[items.size()]);
		Arrays.sort(citems, new Comparator<ShopItem>() {
			@Override
			public int compare(ShopItem o1, ShopItem o2) {
				if (o1.singleSum() > o2.singleSum()) {
					return 1;
				} else if (o1.singleSum() < o2.singleSum()) {
					return -1;
				} else {
					return 0;
				}
			}
		});
		for (int i = 0; i < citems.length; i++) {
			System.out.println(citems[i].toString());
		}
		System.out.println("\t\t\t\t\t总计:" + getTotalPrice());
	}
}

商铺
package IWillPassYou_ST2;

import java.util.Scanner;

public class TaoBao {
	private ShopCar shopCar = null;

	public TaoBao() {
		shopCar = new ShopCar();
	}

	public void mainMenu() {
		Scanner input = new Scanner(System.in);
		System.out.println("********浪矢杂货铺*********");
		System.out.println("1.添加商品");
		System.out.println("2.删除商品");
		System.out.println("3.修改商品");
		System.out.println("4.查看购物车");
		System.out.println("0.结束本次服务");
		System.out.println("请选择:");
		int choice = input.nextInt();
		switch (choice) {
		case 0:
			System.out.println("欢迎下次光临");
			System.exit(0);
			break;
		case 1:
			addProduct();// 添加商品
			mainMenu();
			break;
		case 2:
			delProduct();// 删除商品
			mainMenu();
			break;
		case 3:
			updateProduct();//修改商品
			mainMenu();
			break;
		case 4:
			showProduct(); //显示购物车
			mainMenu();
			break;
		default:
			System.out.println("输入错误!");
			System.exit(0);
			break;
		}
		input.close();
	}

	private void updateProduct() {
		System.out.println("*******更新购物车商品*******");
		shopCar.showCar();
		Scanner input = new Scanner(System.in);
		System.out.println("请选择要更新的商品编号");
		String id = input.next();
		System.out.println("请输入要更新的商品数量");
		int amount = input.nextInt();
		boolean flag = shopCar.updateItem(id, amount);
		if (flag) {
			System.out.println("更新商品成功");
			shopCar.showCar();
		} else {
			System.out.println("更新商品失败");
			shopCar.showCar();
		}
	}

	// 删除商品
	private void delProduct() {
		System.out.println("*******删除商品*******");
		shopCar.showCar();
		Scanner input = new Scanner(System.in);
		System.out.println("请选择要删除的商品编号");
		String id = input.next();
		boolean flag = shopCar.delItem(id);
		if (flag) {
			System.out.println("删除商品成功");
			shopCar.showCar();
		} else {
			System.out.println("删除商品失败");
			shopCar.showCar();
		}
		input.close();
	}

	// 显示购物车中的购物项
	private void showProduct() {
		System.out.println("*******购物车清单*******");
		shopCar.showCar();
	}

	// 购买商品
	private void addProduct() {
		Scanner input = new Scanner(System.in);
		System.out.println("**********浪矢杂货铺************");
		System.out.println("编号\t商品名字\t商品单价");
		System.out.println("1.\t香蕉\t¥10");
		System.out.println("2.\t苹果\t¥20");
		System.out.println("3.\t大鸭梨\t¥5");
		System.out.println("4.\t榴莲\t¥50");
		System.out.println("请输入要购买的商品编号");
		int id = input.nextInt();
		System.out.println("请输入要购买的商品数量");
		int amount = input.nextInt();
		ShopItem item = null;
		if (id == 1) {
			item = new ShopItem(new Goods(String.valueOf(id), "香蕉", 10), amount);
		} else if (id == 2) {
			item = new ShopItem(new Goods(String.valueOf(id), "苹果", 20), amount);
		} else if (id == 3) {
			item = new ShopItem(new Goods(String.valueOf(id), "大鸭梨",5), amount);
		} else if (id == 4) {
			item = new ShopItem(new Goods(String.valueOf(id), "榴莲", 50), amount);
		}
		shopCar.addItem(item);
		System.out.println("已添加至购物车");
	}
}

测试类
package IWillPassYou_ST2;

public class Test {
		public static void main(String[] args) {
			TaoBao b = new TaoBao();
			b.mainMenu();
		}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值