购物车

import java.util.*;


public class Main {


	static Scanner in = new Scanner(System.in);


	public static void main(String args[]) {


		Map<Integer, Goods> list = new TreeMap<Integer, Goods>();
		int n = in.nextInt();
		for (int i = 0; i < n; i++) {
			int id = in.nextInt();
			String name = in.next();
			String producer = in.next();
			double cost = in.nextDouble();
			int stock = in.nextInt();
			Goods goods = new Goods(id, name, producer, cost, stock);
			list.put(id, goods);
		}
		int t = in.nextInt();
		int[] a = new int[10000];
		for (int i = 0; i < a.length; i++) {
			a[i] = 0;
		}
		for (int i = 0; i < t; i++) {
			int id = in.nextInt();
			int count = in.nextInt();
			int symble = in.nextInt();


			if (symble == 1) {
				if (list.get(id).stock >= count) {
					list.get(id).stock -= count;
					a[id] += count;
				}
				else{
					a[id]+=list.get(id).stock;
					list.get(id).stock=0;
				}
			}
			if (symble == 2) {
				if(count>a[id]){
					list.get(id).stock+=a[id];
					a[id]=0;
				}else{
					a[id]-=count;
					list.get(id).stock+=count;
				}
			}
			if (symble == 3) {
				if (list.get(id).id == id) {
					list.get(id).stock += a[id];
					a[id] = 0;
				}
			}
		}


		int cnt = 0;
		for (Integer g : list.keySet()) {
			if (a[g] > 0) {
				cnt++;
			}
		}
		double sum = 0;
		System.out.println(cnt);
		for (Map.Entry<Integer,Goods> g : list.entrySet()) {
			if (a[g.getKey()] > 0) {
				System.out.print(g.getValue().id+"\t"+g.getValue().name+"\t"+g.getValue().producer+"\t");
				System.out.printf("%.2f", g.getValue().cost);
				System.out.println("\t"+g.getValue().stock+"\t"+a[g.getKey()]);
				sum += g.getValue().cost * a[g.getKey()];
			}
		}
		System.out.printf("%.2f", sum);
		System.out.println();
	}
}


class Goods {
	String name, producer;
	int id, stock;
	double cost;


	public Goods(int id, String name, String producer, double cost, int stock) {
		this.id = id;
		this.name = name;
		this.cost = cost;
		this.producer = producer;
		this.stock = stock;
	}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Main2 {
	public static void main(String[] args) {
		// 一、建立商品列表
		List<Product> productList = new ArrayList<Product>();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(); // 读入商品数量
		for (int i = 0; i < n; i++) {
			Product pro = new Product(sc.nextInt(), sc.next(), sc.next(), sc.nextDouble(), sc.nextInt());
			productList.add(pro);
		}

		// 二、对购物车进行m次操作
		int m = sc.nextInt(); // 读入操作次数
		Cart cart = new Cart(); // 建立购物车
		ProductService productService = new ProductService(); // 建立业务类
		// 对购物车的m次操作
		for (int i = 0; i < m; i++) {
			int id = sc.nextInt(); // 商品ID
			int number = sc.nextInt(); // 商品操作数量
			int op = sc.nextInt(); // 商品操作类型
			Product product = productService.getProductById(productList, id);
			// 根据商品ID得到商品对象
			if (op == 1) // 在购物车中增加商品
			{
				// 增加商品数量,以当前商品库存数量为上限
				number = (product.getCount() > number) ? number : product.getCount();
				// 加入购物车
				cart.addToCart(product, number);
			} else if (op == 2) // 退选购物车中的商品
			{
				// 得到购物车中的商品
				HashMap<Product, Integer> goods = cart.getGoods();
				// 如果购物车中含有该商品,则减少数量;否则,忽略此操作
				if (goods.keySet().contains(product)) {
					// 首先得到购物车中该商品的数量
					Integer count = goods.get(product);
					// 操作数量以购物车中的数量为上限
					number = number > count ? count : number;
					// 在购物车中退选number件商品
					cart.addToCart(product, number * (-1));
				}
			} else if (op == 3) // 删除购物车中的商品
			{
				// 得到购物车中的商品
				HashMap<Product, Integer> goods = cart.getGoods();
				// 如果购物车中有该商品,则删除它;否则,忽略此操作
				if (goods.keySet().contains(product)) {
					cart.removeFromCart(product);
				}
			}
		}
		// 显示购物车商品信息
		cart.showCart();
	}
}

/**
 * 购物车类——核心类
 */
class Cart {
	//存放购物车中的商品集合
	private HashMap<Product, Integer> goods = new HashMap<Product, Integer>();
	//购物车中的商品总价格
	private double totalPrice;
	
	//得到购物车中的商品
	public HashMap<Product, Integer> getGoods() {
		return goods;
	}
	//得到购物车中商品总价
	public double getTotalPrice() {
		return totalPrice;
	}

	/**
	 * 商品加入购物车
	 * @param product——在购物车中待操作的商品
	 * @param number——在购物车中待操作商品的数量(它在上层已经过合法检验了)
	 */
	public void addToCart(Product product, int number) {
		//1、若购物车中含该商品,则更新其数量(可能增加,也可能减少);
		//   否则,在其中新增一件商品
		if (goods.containsKey(product)) {
			goods.put(product, goods.get(product) + number);
		} else {
			goods.put(product, number);
		}
		//2、若更新后的购物车该商品数量为0,则在购物车集合中删除该条目
		if (goods.get(product) == 0) {
			removeFromCart(product);
		}
		//3、维护该商品的库存数量
		product.setCount(product.getCount() - number);
		//4、计算总价格
		calTotalPrice();
	}

	/**
	 * 删除商品
	 * @param product
	 */
	public void removeFromCart(Product product) {
		//1、设置待删除商品的库存
		product.setCount(product.getCount() + goods.get(product));
		//2、删除商品
		goods.remove(product);
		//3、重新计算总价格
		calTotalPrice();
	}

	/**
	 * 计算总价格
	 */
	public void calTotalPrice() {
		double sum = 0;
		for (Product product : goods.keySet()) {
			sum += product.getPrice() * goods.get(product);
		}
		totalPrice = sum;
	}

	/**
	 * 显示购物车信息
	 */
	public void showCart() {
		//1、将HashMap中的数据根据KEY排序
		List<Product> list = new ArrayList<Product>(goods.keySet());
		Collections.sort(list, new Comparator<Product>() {
			@Override
			public int compare(Product o1, Product o2) {
				return o1.getId() - o2.getId();
			}
		});
		
		//2、输出购物车中的商品数量
		System.out.println(goods.size());
		
		//3、输出购物车中的商品详情(商品信息)和数量
		for (Product product : list) {
			System.out.println(product.toString() + " " + goods.get(product));
		}
		
		//4、输出购物车商品的总价格
		System.out.println(String.format("%.2f", totalPrice));
	}
}

/**
 * 商品类
 */
class Product {
	//属性
	private int id;
	private String name;
	private String producer;
	private double price;
	private int count;
    //构造方法1
	public Product() {
		super();
	}
	//构造方法2
	public Product(int id, String name, String producer, double price, int count) {
		super();
		this.id = id;
		this.name = name;
		this.producer = producer;
		this.price = price;
		this.count = count;
	}
	//SET、GET方法
	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 String getProducer() {
		return producer;
	}

	public void setProducer(String producer) {
		this.producer = producer;
	}

	public double getPrice() {
		return price;
	}

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

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	//注意:字符串形式与题目要求相符
	@Override
	public String toString() {
		return id + " " + name + " " + producer + " " + String.format("%.2f", price) + " " + count;
	}

}

/**
 * 商品业务类
 */
class ProductService {
	//根据商品ID得到商品对象
	public Product getProductById(List<Product> productList, int id) {
		Product product = null;
		for (Product pro : productList) {
			if (id == pro.getId()) {
				product = pro;
				break;
			}
		}
		return product;
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值